cleanup and license header for examples and tests
[osm/vim-emu.git] / src / emuvim / examples / monitoring_demo_topology.py
1 """
2 Copyright (c) 2015 SONATA-NFV
3 ALL RIGHTS RESERVED.
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16
17 Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18 nor the names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior written
20 permission.
21
22 This work has been performed in the framework of the SONATA project,
23 funded by the European Commission under Grant number 671517 through
24 the Horizon 2020 and 5G-PPP programmes. The authors would like to
25 acknowledge the contributions of their colleagues of the SONATA
26 partner consortium (www.sonata-nfv.eu).
27 """
28 import logging
29 from mininet.log import setLogLevel
30 from emuvim.dcemulator.net import DCNetwork
31 from emuvim.api.zerorpc.compute import ZeroRpcApiEndpoint
32 from emuvim.api.zerorpc.network import ZeroRpcApiEndpointDCNetwork
33
34 logging.basicConfig(level=logging.INFO)
35
36
37 def create_topology1():
38 """
39 1. Create a data center network object (DCNetwork) with monitoring enabled
40 """
41 net = DCNetwork(monitor=True, enable_learning=False)
42
43 """
44 1b. Add endpoint APIs for the whole DCNetwork,
45 to access and control the networking from outside.
46 e.g., to setup forwarding paths between compute
47 instances aka. VNFs (represented by Docker containers), passing through
48 different switches and datacenters of the emulated topology
49 """
50 mon_api = ZeroRpcApiEndpointDCNetwork("0.0.0.0", 5151)
51 mon_api.connectDCNetwork(net)
52 mon_api.start()
53
54 """
55 2. Add (logical) data centers to the topology
56 (each data center is one "bigswitch" in our simplified
57 first prototype)
58 """
59 dc1 = net.addDatacenter("datacenter1")
60 dc2 = net.addDatacenter("datacenter2")
61 #dc3 = net.addDatacenter("long_data_center_name3")
62 #dc4 = net.addDatacenter(
63 # "datacenter4",
64 # metadata={"mydata": "we can also add arbitrary metadata to each DC"})
65
66 """
67 3. You can add additional SDN switches for data center
68 interconnections to the network.
69 """
70 s1 = net.addSwitch("s1")
71
72 """
73 4. Add links between your data centers and additional switches
74 to define you topology.
75 These links can use Mininet's features to limit bw, add delay or jitter.
76 """
77 #net.addLink(dc1, dc2, delay="10ms")
78 #net.addLink(dc1, dc2)
79 net.addLink(dc1, s1)
80 net.addLink(s1, dc2)
81 #net.addLink("datacenter1", s1, delay="20ms")
82 #net.addLink(s1, dc3)
83 #net.addLink(s1, "datacenter4")
84
85
86 """
87 5. We want to access and control our data centers from the outside,
88 e.g., we want to connect an orchestrator to start/stop compute
89 resources aka. VNFs (represented by Docker containers in the emulated)
90
91 So we need to instantiate API endpoints (e.g. a zerorpc or REST
92 interface). Depending on the endpoint implementations, we can connect
93 one or more data centers to it, which can then be controlled through
94 this API, e.g., start/stop/list compute instances.
95 """
96 # create a new instance of a endpoint implementation
97 zapi1 = ZeroRpcApiEndpoint("0.0.0.0", 4242)
98 # connect data centers to this endpoint
99 zapi1.connectDatacenter(dc1)
100 zapi1.connectDatacenter(dc2)
101 #zapi1.connectDatacenter(dc3)
102 #zapi1.connectDatacenter(dc4)
103 # run API endpoint server (in another thread, don't block)
104 zapi1.start()
105
106 """
107 5.1. For our example, we create a second endpoint to illustrate that
108 this is supported by our design. This feature allows us to have
109 one API endpoint for each data center. This makes the emulation
110 environment more realistic because you can easily create one
111 OpenStack-like REST API endpoint for *each* data center.
112 This will look like a real-world multi PoP/data center deployment
113 from the perspective of an orchestrator.
114 """
115 #zapi2 = ZeroRpcApiEndpoint("0.0.0.0", 4343)
116 #zapi2.connectDatacenter(dc3)
117 #zapi2.connectDatacenter(dc4)
118 #zapi2.start()
119
120 """
121 6. Finally we are done and can start our network (the emulator).
122 We can also enter the Mininet CLI to interactively interact
123 with our compute resources (just like in default Mininet).
124 But we can also implement fully automated experiments that
125 can be executed again and again.
126 """
127 net.start()
128 net.CLI()
129 # when the user types exit in the CLI, we stop the emulator
130 net.stop()
131
132
133 def main():
134 setLogLevel('info') # set Mininet loglevel
135 create_topology1()
136
137
138 if __name__ == '__main__':
139 main()