fcdf9f013a0c5f5ec9533f2e402d314ab0d7b0fb
[osm/vim-emu.git] / src / emuvim / api / openstack / openstack_api_endpoint.py
1 """
2 Copyright (c) 2017 SONATA-NFV and Paderborn University
3 ALL RIGHTS RESERVED.
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16
17 Neither the name of the SONATA-NFV, Paderborn University
18 nor the names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior written
20 permission.
21
22 This work has been performed in the framework of the SONATA project,
23 funded by the European Commission under Grant number 671517 through
24 the Horizon 2020 and 5G-PPP programmes. The authors would like to
25 acknowledge the contributions of their colleagues of the SONATA
26 partner consortium (www.sonata-nfv.eu).
27 """
28 from manage import OpenstackManage
29 from openstack_dummies import *
30 import logging
31 import threading
32 import compute
33 import requests
34 import socket
35 import time
36
37
38 class OpenstackApiEndpoint():
39 """
40 Base class for an OpenStack datacenter.
41 It holds information about all connected endpoints.
42 """
43 dc_apis = []
44
45 def __init__(self, listenip, port):
46 self.ip = listenip
47 self.port = port
48 self.compute = compute.OpenstackCompute()
49 self.openstack_endpoints = dict()
50 self.openstack_endpoints['keystone'] = KeystoneDummyApi(self.ip, self.port)
51 self.openstack_endpoints['neutron'] = NeutronDummyApi(self.ip, self.port + 4696, self.compute)
52 self.openstack_endpoints['nova'] = NovaDummyApi(self.ip, self.port + 3774, self.compute)
53 self.openstack_endpoints['heat'] = HeatDummyApi(self.ip, self.port + 3004, self.compute)
54 self.openstack_endpoints['glance'] = GlanceDummyApi(self.ip, self.port + 4242, self.compute)
55
56 self.rest_threads = list()
57 self.manage = OpenstackManage()
58 self.manage.add_endpoint(self)
59 OpenstackApiEndpoint.dc_apis.append(self)
60
61 def connect_datacenter(self, dc):
62 """
63 Connect a datacenter to this endpoint.
64 An endpoint can only be connected to a single datacenter.
65
66 :param dc: Datacenter object
67 :type dc: :class:`dc`
68 """
69 self.compute.dc = dc
70 for ep in self.openstack_endpoints.values():
71 ep.manage = self.manage
72 logging.info \
73 ("Connected DC(%s) to API endpoint %s(%s:%d)" % (dc.label, self.__class__.__name__, self.ip, self.port))
74
75 def connect_dc_network(self, dc_network):
76 """
77 Connect the datacenter network to the endpoint.
78
79 :param dc_network: Datacenter network reference
80 :type dc_network: :class:`.net`
81 """
82 self.manage.net = dc_network
83 self.compute.nets[self.manage.floating_network.id] = self.manage.floating_network
84 logging.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
85 self.__class__.__name__, self.ip, self.port))
86
87 def start(self, wait_for_port=False):
88 """
89 Start all connected OpenStack endpoints that are connected to this API endpoint.
90 """
91 for component in self.openstack_endpoints.values():
92 component.compute = self.compute
93 component.manage = self.manage
94 thread = threading.Thread(target=component._start_flask, args=())
95 thread.daemon = True
96 thread.name = component.__class__
97 thread.start()
98 if wait_for_port:
99 self._wait_for_port(component.ip, component.port)
100
101
102 def stop(self):
103 """
104 Stop all connected OpenStack endpoints that are connected to this API endpoint.
105 """
106 for component in self.openstack_endpoints.values():
107 url = "http://" + component.ip + ":" + str(component.port) + "/shutdown"
108 try:
109 requests.get(url)
110 except:
111 # seems to be stopped
112 pass
113
114 def _wait_for_port(self, ip, port):
115 for i in range(0, 10):
116 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
117 s.settimeout(1) # 1 Second Timeout
118 r = s.connect_ex((ip, port))
119 if r == 0:
120 break # port is open proceed
121 else:
122 logging.warning("Waiting for {}:{} ... ({}/10)".format(ip, port, i + 1))
123 time.sleep(1)