1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
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
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
40 class OpenstackApiEndpoint():
42 Base class for an OpenStack datacenter.
43 It holds information about all connected endpoints.
47 def __init__(self
, listenip
, port
):
50 self
.compute
= compute
.OpenstackCompute()
51 self
.openstack_endpoints
= dict()
52 self
.openstack_endpoints
['keystone'] = KeystoneDummyApi(
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
)
63 self
.rest_threads
= list()
64 self
.manage
= OpenstackManage()
65 self
.manage
.add_endpoint(self
)
66 OpenstackApiEndpoint
.dc_apis
.append(self
)
68 def connect_datacenter(self
, dc
):
70 Connect a datacenter to this endpoint.
71 An endpoint can only be connected to a single datacenter.
73 :param dc: Datacenter object
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
))
82 def connect_dc_network(self
, dc_network
):
84 Connect the datacenter network to the endpoint.
86 :param dc_network: Datacenter network reference
87 :type dc_network: :class:`.net`
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
))
94 def start(self
, wait_for_port
=False):
96 Start all connected OpenStack endpoints that are connected to this API endpoint.
98 for c
in self
.openstack_endpoints
.values():
99 c
.compute
= self
.compute
100 c
.manage
= self
.manage
103 self
._wait
_for
_port
(c
.ip
, c
.port
)
107 Stop all connected OpenStack endpoints that are connected to this API endpoint.
109 for c
in self
.openstack_endpoints
.values():
111 for c
in self
.openstack_endpoints
.values():
113 c
.server_thread
.join()
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
))
122 break # port is open proceed
125 "Waiting for {}:{} ... ({}/10)".format(ip
, port
, i
+ 1))