Fixes of the REST interface dashboard.
[osm/vim-emu.git] / examples / osm_default_daemon_topology_2_pop.py
1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
2 # ALL RIGHTS RESERVED.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # Neither the name of the SONATA-NFV, Paderborn University
17 # nor the names of its contributors may be used to endorse or promote
18 # products derived from this software without specific prior written
19 # permission.
20 #
21 # This work has been performed in the framework of the SONATA project,
22 # funded by the European Commission under Grant number 671517 through
23 # the Horizon 2020 and 5G-PPP programmes. The authors would like to
24 # acknowledge the contributions of their colleagues of the SONATA
25 # partner consortium (www.sonata-nfv.eu).
26 import logging
27 import time
28 import signal
29 from mininet.log import setLogLevel
30 from emuvim.dcemulator.net import DCNetwork
31 from emuvim.api.rest.rest_api_endpoint import RestApiEndpoint
32 from emuvim.api.openstack.openstack_api_endpoint import OpenstackApiEndpoint
33 from mininet.link import TCLink
34
35 logging.basicConfig(level=logging.INFO)
36 setLogLevel('info') # set Mininet loglevel
37 logging.getLogger('werkzeug').setLevel(logging.DEBUG)
38 logging.getLogger('api.openstack.base').setLevel(logging.DEBUG)
39 logging.getLogger('api.openstack.compute').setLevel(logging.DEBUG)
40 logging.getLogger('api.openstack.keystone').setLevel(logging.DEBUG)
41 logging.getLogger('api.openstack.nova').setLevel(logging.DEBUG)
42 logging.getLogger('api.openstack.neutron').setLevel(logging.DEBUG)
43 logging.getLogger('api.openstack.heat').setLevel(logging.DEBUG)
44 logging.getLogger('api.openstack.heat.parser').setLevel(logging.DEBUG)
45 logging.getLogger('api.openstack.glance').setLevel(logging.DEBUG)
46 logging.getLogger('api.openstack.helper').setLevel(logging.DEBUG)
47
48
49 class DaemonTopology(object):
50 """
51 Topology with two datacenters:
52
53 dc1 <-- 50ms --> dc2
54 """
55
56 def __init__(self):
57 self.running = True
58 signal.signal(signal.SIGINT, self._stop_by_signal)
59 signal.signal(signal.SIGTERM, self._stop_by_signal)
60 # create and start topology
61 self.create_topology()
62 self.start_topology()
63 self.daemonize()
64 self.stop_topology()
65
66 def create_topology(self):
67 self.net = DCNetwork(monitor=False, enable_learning=True)
68 self.dc1 = self.net.addDatacenter("dc1")
69 self.dc2 = self.net.addDatacenter("dc2")
70 self.net.addLink(self.dc1, self.dc2, cls=TCLink, delay="50ms")
71 # add OpenStack-like APIs to the emulated DC
72 self.api1 = OpenstackApiEndpoint("0.0.0.0", 6001)
73 self.api1.connect_datacenter(self.dc1)
74 self.api1.connect_dc_network(self.net)
75 self.api2 = OpenstackApiEndpoint("0.0.0.0", 6002)
76 self.api2.connect_datacenter(self.dc2)
77 self.api2.connect_dc_network(self.net)
78 # add the command line interface endpoint to the emulated DC (REST API)
79 self.rapi1 = RestApiEndpoint("0.0.0.0", 5001)
80 self.rapi1.connectDCNetwork(self.net)
81 self.rapi1.connectDatacenter(self.dc1)
82 self.rapi1.connectDatacenter(self.dc2)
83
84 def start_topology(self):
85 self.api1.start()
86 self.api2.start()
87 self.rapi1.start()
88 self.net.start()
89
90 def daemonize(self):
91 print("Daemonizing vim-emu. Send SIGTERM or SIGKILL to stop.")
92 while self.running:
93 time.sleep(1)
94
95 def _stop_by_signal(self, signum, frame):
96 print("Received SIGNAL {}. Stopping.".format(signum))
97 self.running = False
98
99 def stop_topology(self):
100 self.api1.stop()
101 self.api2.stop()
102 self.rapi1.stop()
103 self.net.stop()
104
105
106 def main():
107 DaemonTopology()
108
109
110 if __name__ == '__main__':
111 main()