migration to python3 (#1)
[osm/vim-emu.git] / src / emuvim / api / tango / __init__.py
1 # Copyright (c) 2018 SONATA-NFV, 5GTANGO 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, 5GTANGO, 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 #
27 # This work has also been performed in the framework of the 5GTANGO project,
28 # funded by the European Commission under Grant number 761493 through
29 # the Horizon 2020 and 5G-PPP programmes. The authors would like to
30 # acknowledge the contributions of their colleagues of the 5GTANGO
31 # partner consortium (www.5gtango.eu).
32 import logging
33 import threading
34 from emuvim.api.tango import llcm
35
36
37 LOG = logging.getLogger("5gtango.llcm")
38
39
40 class TangoLLCMEndpoint(object):
41 """
42 Creates and starts the 5GTANGO lightweight life cycle manager to simply
43 deploy 5GTANGO service packages on the emulator.
44 The code is based on SONATA's dummygatekeeper.
45 """
46
47 def __init__(self, listenip, port, deploy_sap=False, docker_management=False,
48 auto_deploy=False, auto_delete=False, sap_vnfd_path=None,
49 placement_algorithm_obj=None, env_conf_folder=None):
50 self.dcs = {}
51 self.ip = listenip
52 self.port = port
53 llcm.DEPLOY_SAP = deploy_sap
54 llcm.USE_DOCKER_MGMT = docker_management
55 llcm.AUTO_DEPLOY = auto_deploy
56 llcm.AUTO_DELETE = auto_delete
57 llcm.SAP_VNFD = sap_vnfd_path
58 if placement_algorithm_obj is None:
59 # Default placement is RR placement
60 placement_algorithm_obj = llcm.RoundRobinDcPlacement()
61 llcm.PLACEMENT_ALGORITHM_OBJ = placement_algorithm_obj
62 llcm.PER_INSTANCE_ENV_CONFIGURATION_FOLDER = env_conf_folder
63 LOG.info("Created 5GTANGO LLCM API endpoint %s" % self)
64 LOG.info("Using placement algorithm: {}".format(
65 placement_algorithm_obj))
66
67 def __repr__(self):
68 return "%s(%s:%d)" % (self.__class__.__name__, self.ip, self.port)
69
70 def connectDatacenter(self, dc):
71 self.dcs[dc.label] = dc
72 LOG.debug("Connected DC(%s) to API endpoint %s" % (
73 dc, self))
74
75 def start(self):
76 thread = threading.Thread(target=self._api_server_thread, args=())
77 thread.daemon = True
78 thread.start()
79 LOG.info("Started API endpoint %s" % self)
80
81 def _api_server_thread(self):
82 llcm.start_rest_api(self.ip, self.port, self.dcs)
83
84 def stop(self):
85 llcm.stop_rest_api()