Fix: Crashbug that was caused by an import of an
[osm/vim-emu.git] / src / emuvim / examples / osm_default_daemon_topology_2_pop.py
1 """
2 Copyright (c) 2017 SONATA-NFV and Paderborn University
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, Paderborn University
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 import time
30 import signal
31 from mininet.log import setLogLevel
32 from emuvim.dcemulator.net import DCNetwork
33 from emuvim.api.rest.rest_api_endpoint import RestApiEndpoint
34 from emuvim.api.openstack.openstack_api_endpoint import OpenstackApiEndpoint
35 from mininet.link import TCLink
36
37 logging.basicConfig(level=logging.INFO)
38 setLogLevel('info') # set Mininet loglevel
39 logging.getLogger('werkzeug').setLevel(logging.DEBUG)
40 logging.getLogger('api.openstack.base').setLevel(logging.DEBUG)
41 logging.getLogger('api.openstack.compute').setLevel(logging.DEBUG)
42 logging.getLogger('api.openstack.keystone').setLevel(logging.DEBUG)
43 logging.getLogger('api.openstack.nova').setLevel(logging.DEBUG)
44 logging.getLogger('api.openstack.neutron').setLevel(logging.DEBUG)
45 logging.getLogger('api.openstack.heat').setLevel(logging.DEBUG)
46 logging.getLogger('api.openstack.heat.parser').setLevel(logging.DEBUG)
47 logging.getLogger('api.openstack.glance').setLevel(logging.DEBUG)
48 logging.getLogger('api.openstack.helper').setLevel(logging.DEBUG)
49
50 class DaemonTopology(object):
51 """
52 Topology with two datacenters:
53
54 dc1 <-- 50ms --> dc2
55 """
56
57 def __init__(self):
58 self.running = True
59 signal.signal(signal.SIGINT, self._stop_by_signal)
60 signal.signal(signal.SIGTERM, self._stop_by_signal)
61 # create and start topology
62 self.create_topology()
63 self.start_topology()
64 self.daemonize()
65 self.stop_topology()
66
67 def create_topology(self):
68 self.net = DCNetwork(monitor=False, enable_learning=True)
69 self.dc1 = self.net.addDatacenter("dc1")
70 self.dc2 = self.net.addDatacenter("dc2")
71 self.net.addLink(self.dc1, self.dc2, cls=TCLink, delay="50ms")
72 # add OpenStack-like APIs to the emulated DC
73 self.api1 = OpenstackApiEndpoint("0.0.0.0", 6001)
74 self.api1.connect_datacenter(self.dc1)
75 self.api1.connect_dc_network(self.net)
76 self.api2 = OpenstackApiEndpoint("0.0.0.0", 6002)
77 self.api2.connect_datacenter(self.dc2)
78 self.api2.connect_dc_network(self.net)
79 # add the command line interface endpoint to the emulated DC (REST API)
80 self.rapi1 = RestApiEndpoint("0.0.0.0", 5001)
81 self.rapi1.connectDCNetwork(self.net)
82 self.rapi1.connectDatacenter(self.dc1)
83 self.rapi1.connectDatacenter(self.dc2)
84
85 def start_topology(self):
86 self.api1.start()
87 self.api2.start()
88 self.rapi1.start()
89 self.net.start()
90
91 def daemonize(self):
92 print("Daemonizing vim-emu. Send SIGTERM or SIGKILL to stop.")
93 while self.running:
94 time.sleep(1)
95
96 def _stop_by_signal(self, signum, frame):
97 print("Received SIGNAL {}. Stopping.".format(signum))
98 self.running = False
99
100 def stop_topology(self):
101 self.api1.stop()
102 self.api2.stop()
103 self.rapi1.stop()
104 self.net.stop()
105
106
107 def main():
108 t = DaemonTopology()
109
110
111 if __name__ == '__main__':
112 main()