Ensure timely termination of all flask servers
[osm/vim-emu.git] / src / emuvim / api / openstack / openstack_api_endpoint.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 from manage import OpenstackManage
27
28 from openstack_dummies.glance_dummy_api import GlanceDummyApi
29 from openstack_dummies.heat_dummy_api import HeatDummyApi
30 from openstack_dummies.keystone_dummy_api import KeystoneDummyApi
31 from openstack_dummies.neutron_dummy_api import NeutronDummyApi
32 from openstack_dummies.nova_dummy_api import NovaDummyApi
33
34 import logging
35 import compute
36 import socket
37 import time
38
39
40 class OpenstackApiEndpoint():
41 """
42 Base class for an OpenStack datacenter.
43 It holds information about all connected endpoints.
44 """
45 dc_apis = []
46
47 def __init__(self, listenip, port):
48 self.ip = listenip
49 self.port = port
50 self.compute = compute.OpenstackCompute()
51 self.openstack_endpoints = dict()
52 self.openstack_endpoints['keystone'] = KeystoneDummyApi(
53 self.ip, self.port)
54 self.openstack_endpoints['neutron'] = NeutronDummyApi(
55 self.ip, self.port + 4696, self.compute)
56 self.openstack_endpoints['nova'] = NovaDummyApi(
57 self.ip, self.port + 3774, self.compute)
58 self.openstack_endpoints['heat'] = HeatDummyApi(
59 self.ip, self.port + 3004, self.compute)
60 self.openstack_endpoints['glance'] = GlanceDummyApi(
61 self.ip, self.port + 4242, self.compute)
62
63 self.rest_threads = list()
64 self.manage = OpenstackManage()
65 self.manage.add_endpoint(self)
66 OpenstackApiEndpoint.dc_apis.append(self)
67
68 def connect_datacenter(self, dc):
69 """
70 Connect a datacenter to this endpoint.
71 An endpoint can only be connected to a single datacenter.
72
73 :param dc: Datacenter object
74 :type dc: :class:`dc`
75 """
76 self.compute.dc = dc
77 for ep in self.openstack_endpoints.values():
78 ep.manage = self.manage
79 logging.info("Connected DC(%s) to API endpoint %s(%s:%d)" %
80 (dc.label, self.__class__.__name__, self.ip, self.port))
81
82 def connect_dc_network(self, dc_network):
83 """
84 Connect the datacenter network to the endpoint.
85
86 :param dc_network: Datacenter network reference
87 :type dc_network: :class:`.net`
88 """
89 self.manage.net = dc_network
90 self.compute.nets[self.manage.floating_network.id] = self.manage.floating_network
91 logging.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
92 self.__class__.__name__, self.ip, self.port))
93
94 def start(self, wait_for_port=False):
95 """
96 Start all connected OpenStack endpoints that are connected to this API endpoint.
97 """
98 for c in self.openstack_endpoints.values():
99 c.compute = self.compute
100 c.manage = self.manage
101 c.start()
102 if wait_for_port:
103 self._wait_for_port(c.ip, c.port)
104
105 def stop(self):
106 """
107 Stop all connected OpenStack endpoints that are connected to this API endpoint.
108 """
109 for c in self.openstack_endpoints.values():
110 c.stop()
111 for c in self.openstack_endpoints.values():
112 if c.server_thread:
113 c.server_thread.join()
114 self.manage.stop()
115
116 def _wait_for_port(self, ip, port):
117 for i in range(0, 10):
118 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
119 s.settimeout(1) # 1 Second Timeout
120 r = s.connect_ex((ip, port))
121 if r == 0:
122 break # port is open proceed
123 else:
124 logging.warning(
125 "Waiting for {}:{} ... ({}/10)".format(ip, port, i + 1))
126 time.sleep(1)