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