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 emuvim
.api
.openstack
.manage
import OpenstackManage
28 from emuvim
.api
.openstack
.openstack_dummies
.glance_dummy_api
import \
30 from emuvim
.api
.openstack
.openstack_dummies
.heat_dummy_api
import \
32 from emuvim
.api
.openstack
.openstack_dummies
.keystone_dummy_api
import \
34 from emuvim
.api
.openstack
.openstack_dummies
.neutron_dummy_api
import \
36 from emuvim
.api
.openstack
.openstack_dummies
.nova_dummy_api
import \
40 import emuvim
.api
.openstack
.compute
as compute
45 class OpenstackApiEndpoint():
47 Base class for an OpenStack datacenter.
48 It holds information about all connected endpoints.
52 def __init__(self
, listenip
, port
):
55 self
.compute
= compute
.OpenstackCompute()
56 self
.openstack_endpoints
= dict()
57 self
.openstack_endpoints
['keystone'] = KeystoneDummyApi(
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
)
68 self
.rest_threads
= list()
69 self
.manage
= OpenstackManage()
70 self
.manage
.add_endpoint(self
)
71 OpenstackApiEndpoint
.dc_apis
.append(self
)
73 def connect_datacenter(self
, dc
):
75 Connect a datacenter to this endpoint.
76 An endpoint can only be connected to a single datacenter.
78 :param dc: Datacenter object
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
))
87 def connect_dc_network(self
, dc_network
):
89 Connect the datacenter network to the endpoint.
91 :param dc_network: Datacenter network reference
92 :type dc_network: :class:`.net`
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
))
99 def start(self
, wait_for_port
=False):
101 Start all connected OpenStack endpoints that are connected to this API endpoint.
103 for c
in self
.openstack_endpoints
.values():
104 c
.compute
= self
.compute
105 c
.manage
= self
.manage
108 self
._wait
_for
_port
(c
.ip
, c
.port
)
112 Stop all connected OpenStack endpoints that are connected to this API endpoint.
114 for c
in self
.openstack_endpoints
.values():
116 for c
in self
.openstack_endpoints
.values():
118 c
.server_thread
.join()
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
))
127 break # port is open proceed
130 "Waiting for {}:{} ... ({}/10)".format(ip
, port
, i
+ 1))