blob: d8ac91f9597a25d23ba64b0ab14f916e6cf6fc0e [file] [log] [blame]
tiernoc0e42e22018-05-11 11:36:10 +02001#!/usr/bin/python3
2# -*- coding: utf-8 -*-
3
tierno2e215512018-11-28 09:37:52 +00004##
5# Copyright 2018 Telefonica S.A.
6#
7# Licensed under the Apache License, Version 2.0 (the "License"); you may
8# not use this file except in compliance with the License. You may obtain
9# a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16# License for the specific language governing permissions and limitations
17# under the License.
18##
19
quilesj7e13aeb2019-10-08 13:34:55 +020020
21# DEBUG WITH PDB
22import os
23import pdb
24
tiernoc0e42e22018-05-11 11:36:10 +020025import asyncio
26import yaml
tierno275411e2018-05-16 14:33:32 +020027import logging
28import logging.handlers
29import getopt
tierno275411e2018-05-16 14:33:32 +020030import sys
tierno59d22d22018-09-25 18:10:19 +020031
quilesj7e13aeb2019-10-08 13:34:55 +020032from osm_lcm import ns
33from osm_lcm import vim_sdn
34from osm_lcm import netslice
35from osm_lcm import ROclient
36
tierno8069ce52019-08-28 15:34:33 +000037from time import time, sleep
38from osm_lcm.lcm_utils import versiontuple, LcmException, TaskRegistry, LcmExceptionExit
39from osm_lcm import version as lcm_version, version_date as lcm_version_date
40
tierno98768132018-09-11 12:07:21 +020041from osm_common import dbmemory, dbmongo, fslocal, msglocal, msgkafka
42from osm_common import version as common_version
tierno59d22d22018-09-25 18:10:19 +020043from osm_common.dbbase import DbException
tiernoc0e42e22018-05-11 11:36:10 +020044from osm_common.fsbase import FsException
45from osm_common.msgbase import MsgException
tierno275411e2018-05-16 14:33:32 +020046from os import environ, path
tierno16427352019-04-22 11:37:36 +000047from random import choice as random_choice
tierno59d22d22018-09-25 18:10:19 +020048from n2vc import version as n2vc_version
tiernoc0e42e22018-05-11 11:36:10 +020049
quilesj7e13aeb2019-10-08 13:34:55 +020050if os.getenv('OSMLCM_PDB_DEBUG', None) is not None:
51 pdb.set_trace()
52
tiernoc0e42e22018-05-11 11:36:10 +020053
tierno275411e2018-05-16 14:33:32 +020054__author__ = "Alfonso Tierno"
tiernoe64f7fb2019-09-11 08:55:52 +000055min_RO_version = "6.0.2"
tierno6e9d2eb2018-09-12 17:47:18 +020056min_n2vc_version = "0.0.2"
quilesj7e13aeb2019-10-08 13:34:55 +020057
tierno16427352019-04-22 11:37:36 +000058min_common_version = "0.1.19"
tierno86aa62f2018-08-20 11:57:04 +000059# uncomment if LCM is installed as library and installed, and get them from __init__.py
tierno8069ce52019-08-28 15:34:33 +000060# lcm_version = '0.1.41'
61# lcm_version_date = '2019-06-19'
tierno3e359b12019-02-03 02:29:13 +010062health_check_file = path.expanduser("~") + "/time_last_ping" # TODO find better location for this file
tierno275411e2018-05-16 14:33:32 +020063
64
tiernoc0e42e22018-05-11 11:36:10 +020065class Lcm:
66
tiernoa9843d82018-10-24 10:44:20 +020067 ping_interval_pace = 120 # how many time ping is send once is confirmed all is running
tiernof578e552018-11-08 19:07:20 +010068 ping_interval_boot = 5 # how many time ping is sent when booting
tiernoa9843d82018-10-24 10:44:20 +020069
tierno59d22d22018-09-25 18:10:19 +020070 def __init__(self, config_file, loop=None):
tiernoc0e42e22018-05-11 11:36:10 +020071 """
72 Init, Connect to database, filesystem storage, and messaging
73 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
74 :return: None
75 """
76
77 self.db = None
78 self.msg = None
tierno16427352019-04-22 11:37:36 +000079 self.msg_admin = None
tiernoc0e42e22018-05-11 11:36:10 +020080 self.fs = None
81 self.pings_not_received = 1
tiernoc2564fe2019-01-28 16:18:56 +000082 self.consecutive_errors = 0
83 self.first_start = False
tiernoc0e42e22018-05-11 11:36:10 +020084
tiernoc0e42e22018-05-11 11:36:10 +020085 # logging
86 self.logger = logging.getLogger('lcm')
tierno16427352019-04-22 11:37:36 +000087 # get id
88 self.worker_id = self.get_process_id()
tiernoc0e42e22018-05-11 11:36:10 +020089 # load configuration
90 config = self.read_config_file(config_file)
91 self.config = config
tierno750b2452018-05-17 16:39:29 +020092 self.ro_config = {
tiernoc0e42e22018-05-11 11:36:10 +020093 "endpoint_url": "http://{}:{}/openmano".format(config["RO"]["host"], config["RO"]["port"]),
tierno750b2452018-05-17 16:39:29 +020094 "tenant": config.get("tenant", "osm"),
tiernoc0e42e22018-05-11 11:36:10 +020095 "logger_name": "lcm.ROclient",
96 "loglevel": "ERROR",
97 }
98
tierno59d22d22018-09-25 18:10:19 +020099 self.vca_config = config["VCA"]
100
101 self.loop = loop or asyncio.get_event_loop()
tiernoc0e42e22018-05-11 11:36:10 +0200102
103 # logging
104 log_format_simple = "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
105 log_formatter_simple = logging.Formatter(log_format_simple, datefmt='%Y-%m-%dT%H:%M:%S')
106 config["database"]["logger_name"] = "lcm.db"
107 config["storage"]["logger_name"] = "lcm.fs"
108 config["message"]["logger_name"] = "lcm.msg"
tierno86aa62f2018-08-20 11:57:04 +0000109 if config["global"].get("logfile"):
tiernoc0e42e22018-05-11 11:36:10 +0200110 file_handler = logging.handlers.RotatingFileHandler(config["global"]["logfile"],
111 maxBytes=100e6, backupCount=9, delay=0)
112 file_handler.setFormatter(log_formatter_simple)
113 self.logger.addHandler(file_handler)
tierno86aa62f2018-08-20 11:57:04 +0000114 if not config["global"].get("nologging"):
tiernoc0e42e22018-05-11 11:36:10 +0200115 str_handler = logging.StreamHandler()
116 str_handler.setFormatter(log_formatter_simple)
117 self.logger.addHandler(str_handler)
118
119 if config["global"].get("loglevel"):
120 self.logger.setLevel(config["global"]["loglevel"])
121
122 # logging other modules
123 for k1, logname in {"message": "lcm.msg", "database": "lcm.db", "storage": "lcm.fs"}.items():
124 config[k1]["logger_name"] = logname
125 logger_module = logging.getLogger(logname)
tierno86aa62f2018-08-20 11:57:04 +0000126 if config[k1].get("logfile"):
tiernoc0e42e22018-05-11 11:36:10 +0200127 file_handler = logging.handlers.RotatingFileHandler(config[k1]["logfile"],
128 maxBytes=100e6, backupCount=9, delay=0)
129 file_handler.setFormatter(log_formatter_simple)
130 logger_module.addHandler(file_handler)
tierno86aa62f2018-08-20 11:57:04 +0000131 if config[k1].get("loglevel"):
tiernoc0e42e22018-05-11 11:36:10 +0200132 logger_module.setLevel(config[k1]["loglevel"])
tierno86aa62f2018-08-20 11:57:04 +0000133 self.logger.critical("starting osm/lcm version {} {}".format(lcm_version, lcm_version_date))
tierno59d22d22018-09-25 18:10:19 +0200134
tiernoc0e42e22018-05-11 11:36:10 +0200135 # check version of N2VC
136 # TODO enhance with int conversion or from distutils.version import LooseVersion
137 # or with list(map(int, version.split(".")))
tierno59d22d22018-09-25 18:10:19 +0200138 if versiontuple(n2vc_version) < versiontuple(min_n2vc_version):
tierno6e9d2eb2018-09-12 17:47:18 +0200139 raise LcmException("Not compatible osm/N2VC version '{}'. Needed '{}' or higher".format(
tierno59d22d22018-09-25 18:10:19 +0200140 n2vc_version, min_n2vc_version))
141 # check version of common
tierno27246d82018-09-27 15:59:09 +0200142 if versiontuple(common_version) < versiontuple(min_common_version):
tierno6e9d2eb2018-09-12 17:47:18 +0200143 raise LcmException("Not compatible osm/common version '{}'. Needed '{}' or higher".format(
144 common_version, min_common_version))
tierno22f4f9c2018-06-11 18:53:39 +0200145
tiernoc0e42e22018-05-11 11:36:10 +0200146 try:
tierno22f4f9c2018-06-11 18:53:39 +0200147 # TODO check database version
tiernoc0e42e22018-05-11 11:36:10 +0200148 if config["database"]["driver"] == "mongo":
149 self.db = dbmongo.DbMongo()
150 self.db.db_connect(config["database"])
151 elif config["database"]["driver"] == "memory":
152 self.db = dbmemory.DbMemory()
153 self.db.db_connect(config["database"])
154 else:
155 raise LcmException("Invalid configuration param '{}' at '[database]':'driver'".format(
156 config["database"]["driver"]))
157
158 if config["storage"]["driver"] == "local":
159 self.fs = fslocal.FsLocal()
160 self.fs.fs_connect(config["storage"])
161 else:
162 raise LcmException("Invalid configuration param '{}' at '[storage]':'driver'".format(
163 config["storage"]["driver"]))
164
quilesj7e13aeb2019-10-08 13:34:55 +0200165 # copy message configuration in order to remove 'group_id' for msg_admin
tiernoc2564fe2019-01-28 16:18:56 +0000166 config_message = config["message"].copy()
167 config_message["loop"] = self.loop
168 if config_message["driver"] == "local":
tiernoc0e42e22018-05-11 11:36:10 +0200169 self.msg = msglocal.MsgLocal()
tiernoc2564fe2019-01-28 16:18:56 +0000170 self.msg.connect(config_message)
tierno16427352019-04-22 11:37:36 +0000171 self.msg_admin = msglocal.MsgLocal()
172 config_message.pop("group_id", None)
173 self.msg_admin.connect(config_message)
tiernoc2564fe2019-01-28 16:18:56 +0000174 elif config_message["driver"] == "kafka":
tiernoc0e42e22018-05-11 11:36:10 +0200175 self.msg = msgkafka.MsgKafka()
tiernoc2564fe2019-01-28 16:18:56 +0000176 self.msg.connect(config_message)
tierno16427352019-04-22 11:37:36 +0000177 self.msg_admin = msgkafka.MsgKafka()
178 config_message.pop("group_id", None)
179 self.msg_admin.connect(config_message)
tiernoc0e42e22018-05-11 11:36:10 +0200180 else:
181 raise LcmException("Invalid configuration param '{}' at '[message]':'driver'".format(
tiernoc2564fe2019-01-28 16:18:56 +0000182 config["message"]["driver"]))
tiernoc0e42e22018-05-11 11:36:10 +0200183 except (DbException, FsException, MsgException) as e:
184 self.logger.critical(str(e), exc_info=True)
185 raise LcmException(str(e))
186
kuused124bfe2019-06-18 12:09:24 +0200187 # contains created tasks/futures to be able to cancel
188 self.lcm_tasks = TaskRegistry(self.worker_id, self.db, self.logger)
189
tierno59d22d22018-09-25 18:10:19 +0200190 self.ns = ns.NsLcm(self.db, self.msg, self.fs, self.lcm_tasks, self.ro_config, self.vca_config, self.loop)
Felipe Vicensc2033f22018-11-15 15:09:58 +0100191 self.netslice = netslice.NetsliceLcm(self.db, self.msg, self.fs, self.lcm_tasks, self.ro_config,
192 self.vca_config, self.loop)
tierno59d22d22018-09-25 18:10:19 +0200193 self.vim = vim_sdn.VimLcm(self.db, self.msg, self.fs, self.lcm_tasks, self.ro_config, self.loop)
tiernoe37b57d2018-12-11 17:22:51 +0000194 self.wim = vim_sdn.WimLcm(self.db, self.msg, self.fs, self.lcm_tasks, self.ro_config, self.loop)
tierno59d22d22018-09-25 18:10:19 +0200195 self.sdn = vim_sdn.SdnLcm(self.db, self.msg, self.fs, self.lcm_tasks, self.ro_config, self.loop)
calvinosanch9f9c6f22019-11-04 13:37:39 +0100196 self.k8scluster = vim_sdn.K8sClusterLcm(self.db, self.msg, self.fs, self.lcm_tasks, self.vca_config, self.loop)
197 self.k8srepo = vim_sdn.K8sRepoLcm(self.db, self.msg, self.fs, self.lcm_tasks, self.vca_config, self.loop)
tierno59d22d22018-09-25 18:10:19 +0200198
tierno22f4f9c2018-06-11 18:53:39 +0200199 async def check_RO_version(self):
tiernoe64f7fb2019-09-11 08:55:52 +0000200 tries = 14
201 last_error = None
202 while True:
203 try:
204 ro_server = ROclient.ROClient(self.loop, **self.ro_config)
205 ro_version = await ro_server.get_version()
206 if versiontuple(ro_version) < versiontuple(min_RO_version):
207 raise LcmException("Not compatible osm/RO version '{}'. Needed '{}' or higher".format(
208 ro_version, min_RO_version))
209 self.logger.info("Connected to RO version {}".format(ro_version))
210 return
211 except ROclient.ROClientException as e:
212 tries -= 1
213 error_text = "Error while connecting to RO on {}: {}".format(self.ro_config["endpoint_url"], e)
214 if tries <= 0:
215 self.logger.critical(error_text)
216 raise LcmException(error_text)
217 if last_error != error_text:
218 last_error = error_text
219 self.logger.error(error_text + ". Waiting until {} seconds".format(5*tries))
220 await asyncio.sleep(5)
tierno22f4f9c2018-06-11 18:53:39 +0200221
tiernoc0e42e22018-05-11 11:36:10 +0200222 async def test(self, param=None):
223 self.logger.debug("Starting/Ending test task: {}".format(param))
224
tiernoc0e42e22018-05-11 11:36:10 +0200225 async def kafka_ping(self):
226 self.logger.debug("Task kafka_ping Enter")
227 consecutive_errors = 0
228 first_start = True
229 kafka_has_received = False
230 self.pings_not_received = 1
231 while True:
232 try:
tierno16427352019-04-22 11:37:36 +0000233 await self.msg_admin.aiowrite(
234 "admin", "ping",
235 {"from": "lcm", "to": "lcm", "worker_id": self.worker_id, "version": lcm_version},
236 self.loop)
tiernoc0e42e22018-05-11 11:36:10 +0200237 # time between pings are low when it is not received and at starting
tiernoa9843d82018-10-24 10:44:20 +0200238 wait_time = self.ping_interval_boot if not kafka_has_received else self.ping_interval_pace
tiernoc0e42e22018-05-11 11:36:10 +0200239 if not self.pings_not_received:
240 kafka_has_received = True
241 self.pings_not_received += 1
242 await asyncio.sleep(wait_time, loop=self.loop)
243 if self.pings_not_received > 10:
244 raise LcmException("It is not receiving pings from Kafka bus")
245 consecutive_errors = 0
246 first_start = False
247 except LcmException:
248 raise
249 except Exception as e:
250 # if not first_start is the first time after starting. So leave more time and wait
251 # to allow kafka starts
252 if consecutive_errors == 8 if not first_start else 30:
253 self.logger.error("Task kafka_read task exit error too many errors. Exception: {}".format(e))
254 raise
255 consecutive_errors += 1
256 self.logger.error("Task kafka_read retrying after Exception {}".format(e))
tierno16427352019-04-22 11:37:36 +0000257 wait_time = 2 if not first_start else 5
tiernoc0e42e22018-05-11 11:36:10 +0200258 await asyncio.sleep(wait_time, loop=self.loop)
259
gcalvinoed7f6d42018-12-14 14:44:56 +0100260 def kafka_read_callback(self, topic, command, params):
261 order_id = 1
262
263 if topic != "admin" and command != "ping":
264 self.logger.debug("Task kafka_read receives {} {}: {}".format(topic, command, params))
265 self.consecutive_errors = 0
266 self.first_start = False
267 order_id += 1
268 if command == "exit":
269 raise LcmExceptionExit
270 elif command.startswith("#"):
271 return
272 elif command == "echo":
273 # just for test
274 print(params)
275 sys.stdout.flush()
276 return
277 elif command == "test":
278 asyncio.Task(self.test(params), loop=self.loop)
279 return
280
281 if topic == "admin":
282 if command == "ping" and params["to"] == "lcm" and params["from"] == "lcm":
tierno16427352019-04-22 11:37:36 +0000283 if params.get("worker_id") != self.worker_id:
284 return
gcalvinoed7f6d42018-12-14 14:44:56 +0100285 self.pings_not_received = 0
tierno3e359b12019-02-03 02:29:13 +0100286 try:
287 with open(health_check_file, "w") as f:
288 f.write(str(time()))
289 except Exception as e:
290 self.logger.error("Cannot write into '{}' for healthcheck: {}".format(health_check_file, e))
gcalvinoed7f6d42018-12-14 14:44:56 +0100291 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100292 elif topic == "k8scluster":
293 if command == "create" or command == "created":
294 k8scluster_id = params.get("_id")
295 task = asyncio.ensure_future(self.k8scluster.create(params, order_id))
296 self.lcm_tasks.register("k8scluster", k8scluster_id, order_id, "k8scluster_create", task)
297 return
298 elif command == "delete" or command == "deleted":
299 k8scluster_id = params.get("_id")
300 task = asyncio.ensure_future(self.k8scluster.delete(params, order_id))
301 self.lcm_tasks.register("k8scluster", k8scluster_id, order_id, "k8scluster_delete", task)
302 return
303 elif topic == "k8srepo":
304 if command == "create" or command == "created":
305 k8srepo_id = params.get("_id")
306 self.logger.debug("k8srepo_id = {}".format(k8srepo_id))
307 task = asyncio.ensure_future(self.k8srepo.create(params, order_id))
308 self.lcm_tasks.register("k8srepo", k8srepo_id, order_id, "k8srepo_create", task)
309 return
310 elif command == "delete" or command == "deleted":
311 k8srepo_id = params.get("_id")
312 task = asyncio.ensure_future(self.k8srepo.delete(params, order_id))
313 self.lcm_tasks.register("k8srepo", k8srepo_id, order_id, "k8srepo_delete", task)
314 return
gcalvinoed7f6d42018-12-14 14:44:56 +0100315 elif topic == "ns":
calvinosanch9f9c6f22019-11-04 13:37:39 +0100316 if command == "instantiate" or command == "instantiated":
gcalvinoed7f6d42018-12-14 14:44:56 +0100317 # self.logger.debug("Deploying NS {}".format(nsr_id))
318 nslcmop = params
319 nslcmop_id = nslcmop["_id"]
320 nsr_id = nslcmop["nsInstanceId"]
321 task = asyncio.ensure_future(self.ns.instantiate(nsr_id, nslcmop_id))
322 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_instantiate", task)
323 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100324 elif command == "terminate" or command == "terminated":
gcalvinoed7f6d42018-12-14 14:44:56 +0100325 # self.logger.debug("Deleting NS {}".format(nsr_id))
326 nslcmop = params
327 nslcmop_id = nslcmop["_id"]
328 nsr_id = nslcmop["nsInstanceId"]
329 self.lcm_tasks.cancel(topic, nsr_id)
330 task = asyncio.ensure_future(self.ns.terminate(nsr_id, nslcmop_id))
331 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_terminate", task)
332 return
333 elif command == "action":
334 # self.logger.debug("Update NS {}".format(nsr_id))
335 nslcmop = params
336 nslcmop_id = nslcmop["_id"]
337 nsr_id = nslcmop["nsInstanceId"]
338 task = asyncio.ensure_future(self.ns.action(nsr_id, nslcmop_id))
339 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_action", task)
340 return
341 elif command == "scale":
342 # self.logger.debug("Update NS {}".format(nsr_id))
343 nslcmop = params
344 nslcmop_id = nslcmop["_id"]
345 nsr_id = nslcmop["nsInstanceId"]
346 task = asyncio.ensure_future(self.ns.scale(nsr_id, nslcmop_id))
347 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_scale", task)
348 return
349 elif command == "show":
tiernoc2564fe2019-01-28 16:18:56 +0000350 nsr_id = params
gcalvinoed7f6d42018-12-14 14:44:56 +0100351 try:
352 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
353 print("nsr:\n _id={}\n operational-status: {}\n config-status: {}"
354 "\n detailed-status: {}\n deploy: {}\n tasks: {}"
355 "".format(nsr_id, db_nsr["operational-status"], db_nsr["config-status"],
356 db_nsr["detailed-status"],
357 db_nsr["_admin"]["deployed"], self.lcm_ns_tasks.get(nsr_id)))
358 except Exception as e:
359 print("nsr {} not found: {}".format(nsr_id, e))
360 sys.stdout.flush()
361 return
362 elif command == "deleted":
363 return # TODO cleaning of task just in case should be done
364 elif command in ("terminated", "instantiated", "scaled", "actioned"): # "scaled-cooldown-time"
365 return
366 elif topic == "nsi": # netslice LCM processes (instantiate, terminate, etc)
calvinosanch9f9c6f22019-11-04 13:37:39 +0100367 if command == "instantiate" or command == "instantiated":
gcalvinoed7f6d42018-12-14 14:44:56 +0100368 # self.logger.debug("Instantiating Network Slice {}".format(nsilcmop["netsliceInstanceId"]))
369 nsilcmop = params
370 nsilcmop_id = nsilcmop["_id"] # slice operation id
371 nsir_id = nsilcmop["netsliceInstanceId"] # slice record id
372 task = asyncio.ensure_future(self.netslice.instantiate(nsir_id, nsilcmop_id))
373 self.lcm_tasks.register("nsi", nsir_id, nsilcmop_id, "nsi_instantiate", task)
374 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100375 elif command == "terminate" or command == "terminated":
gcalvinoed7f6d42018-12-14 14:44:56 +0100376 # self.logger.debug("Terminating Network Slice NS {}".format(nsilcmop["netsliceInstanceId"]))
377 nsilcmop = params
378 nsilcmop_id = nsilcmop["_id"] # slice operation id
379 nsir_id = nsilcmop["netsliceInstanceId"] # slice record id
380 self.lcm_tasks.cancel(topic, nsir_id)
381 task = asyncio.ensure_future(self.netslice.terminate(nsir_id, nsilcmop_id))
382 self.lcm_tasks.register("nsi", nsir_id, nsilcmop_id, "nsi_terminate", task)
383 return
384 elif command == "show":
tiernoc2564fe2019-01-28 16:18:56 +0000385 nsir_id = params
gcalvinoed7f6d42018-12-14 14:44:56 +0100386 try:
387 db_nsir = self.db.get_one("nsirs", {"_id": nsir_id})
388 print("nsir:\n _id={}\n operational-status: {}\n config-status: {}"
389 "\n detailed-status: {}\n deploy: {}\n tasks: {}"
390 "".format(nsir_id, db_nsir["operational-status"], db_nsir["config-status"],
391 db_nsir["detailed-status"],
392 db_nsir["_admin"]["deployed"], self.lcm_netslice_tasks.get(nsir_id)))
393 except Exception as e:
394 print("nsir {} not found: {}".format(nsir_id, e))
395 sys.stdout.flush()
396 return
397 elif command == "deleted":
398 return # TODO cleaning of task just in case should be done
399 elif command in ("terminated", "instantiated", "scaled", "actioned"): # "scaled-cooldown-time"
400 return
401 elif topic == "vim_account":
402 vim_id = params["_id"]
calvinosanch9f9c6f22019-11-04 13:37:39 +0100403 if command == "create" or command == "created":
gcalvinoed7f6d42018-12-14 14:44:56 +0100404 task = asyncio.ensure_future(self.vim.create(params, order_id))
405 self.lcm_tasks.register("vim_account", vim_id, order_id, "vim_create", task)
406 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100407 elif command == "delete" or command == "deleted":
gcalvinoed7f6d42018-12-14 14:44:56 +0100408 self.lcm_tasks.cancel(topic, vim_id)
kuuse6a470c62019-07-10 13:52:45 +0200409 task = asyncio.ensure_future(self.vim.delete(params, order_id))
gcalvinoed7f6d42018-12-14 14:44:56 +0100410 self.lcm_tasks.register("vim_account", vim_id, order_id, "vim_delete", task)
411 return
412 elif command == "show":
413 print("not implemented show with vim_account")
414 sys.stdout.flush()
415 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100416 elif command == "edit" or command == "edited":
gcalvinoed7f6d42018-12-14 14:44:56 +0100417 task = asyncio.ensure_future(self.vim.edit(params, order_id))
418 self.lcm_tasks.register("vim_account", vim_id, order_id, "vim_edit", task)
419 return
420 elif topic == "wim_account":
421 wim_id = params["_id"]
calvinosanch9f9c6f22019-11-04 13:37:39 +0100422 if command == "create" or command == "created":
gcalvinoed7f6d42018-12-14 14:44:56 +0100423 task = asyncio.ensure_future(self.wim.create(params, order_id))
424 self.lcm_tasks.register("wim_account", wim_id, order_id, "wim_create", task)
425 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100426 elif command == "delete" or command == "deleted":
gcalvinoed7f6d42018-12-14 14:44:56 +0100427 self.lcm_tasks.cancel(topic, wim_id)
kuuse6a470c62019-07-10 13:52:45 +0200428 task = asyncio.ensure_future(self.wim.delete(params, order_id))
gcalvinoed7f6d42018-12-14 14:44:56 +0100429 self.lcm_tasks.register("wim_account", wim_id, order_id, "wim_delete", task)
430 return
431 elif command == "show":
432 print("not implemented show with wim_account")
433 sys.stdout.flush()
434 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100435 elif command == "edit" or command == "edited":
gcalvinoed7f6d42018-12-14 14:44:56 +0100436 task = asyncio.ensure_future(self.wim.edit(params, order_id))
437 self.lcm_tasks.register("wim_account", wim_id, order_id, "wim_edit", task)
438 return
439 elif topic == "sdn":
440 _sdn_id = params["_id"]
calvinosanch9f9c6f22019-11-04 13:37:39 +0100441 if command == "create" or command == "created":
gcalvinoed7f6d42018-12-14 14:44:56 +0100442 task = asyncio.ensure_future(self.sdn.create(params, order_id))
443 self.lcm_tasks.register("sdn", _sdn_id, order_id, "sdn_create", task)
444 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100445 elif command == "delete" or command == "deleted":
gcalvinoed7f6d42018-12-14 14:44:56 +0100446 self.lcm_tasks.cancel(topic, _sdn_id)
kuuse6a470c62019-07-10 13:52:45 +0200447 task = asyncio.ensure_future(self.sdn.delete(params, order_id))
gcalvinoed7f6d42018-12-14 14:44:56 +0100448 self.lcm_tasks.register("sdn", _sdn_id, order_id, "sdn_delete", task)
449 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100450 elif command == "edit" or command == "edited":
gcalvinoed7f6d42018-12-14 14:44:56 +0100451 task = asyncio.ensure_future(self.sdn.edit(params, order_id))
452 self.lcm_tasks.register("sdn", _sdn_id, order_id, "sdn_edit", task)
453 return
454 self.logger.critical("unknown topic {} and command '{}'".format(topic, command))
455
tiernoc0e42e22018-05-11 11:36:10 +0200456 async def kafka_read(self):
tierno16427352019-04-22 11:37:36 +0000457 self.logger.debug("Task kafka_read Enter with worker_id={}".format(self.worker_id))
tiernoc0e42e22018-05-11 11:36:10 +0200458 # future = asyncio.Future()
gcalvinoed7f6d42018-12-14 14:44:56 +0100459 self.consecutive_errors = 0
460 self.first_start = True
461 while self.consecutive_errors < 10:
tiernoc0e42e22018-05-11 11:36:10 +0200462 try:
calvinosanch9f9c6f22019-11-04 13:37:39 +0100463 topics = ("ns", "vim_account", "wim_account", "sdn", "nsi", "k8scluster", "k8srepo")
tierno16427352019-04-22 11:37:36 +0000464 topics_admin = ("admin", )
465 await asyncio.gather(
466 self.msg.aioread(topics, self.loop, self.kafka_read_callback),
467 self.msg_admin.aioread(topics_admin, self.loop, self.kafka_read_callback, group_id=False)
468 )
tiernoc0e42e22018-05-11 11:36:10 +0200469
gcalvinoed7f6d42018-12-14 14:44:56 +0100470 except LcmExceptionExit:
471 self.logger.debug("Bye!")
472 break
tiernoc0e42e22018-05-11 11:36:10 +0200473 except Exception as e:
474 # if not first_start is the first time after starting. So leave more time and wait
475 # to allow kafka starts
gcalvinoed7f6d42018-12-14 14:44:56 +0100476 if self.consecutive_errors == 8 if not self.first_start else 30:
tiernoc0e42e22018-05-11 11:36:10 +0200477 self.logger.error("Task kafka_read task exit error too many errors. Exception: {}".format(e))
478 raise
gcalvinoed7f6d42018-12-14 14:44:56 +0100479 self.consecutive_errors += 1
tiernoc0e42e22018-05-11 11:36:10 +0200480 self.logger.error("Task kafka_read retrying after Exception {}".format(e))
gcalvinoed7f6d42018-12-14 14:44:56 +0100481 wait_time = 2 if not self.first_start else 5
tiernoc0e42e22018-05-11 11:36:10 +0200482 await asyncio.sleep(wait_time, loop=self.loop)
483
484 # self.logger.debug("Task kafka_read terminating")
485 self.logger.debug("Task kafka_read exit")
486
487 def start(self):
tierno22f4f9c2018-06-11 18:53:39 +0200488
489 # check RO version
490 self.loop.run_until_complete(self.check_RO_version())
491
tiernoc0e42e22018-05-11 11:36:10 +0200492 self.loop.run_until_complete(asyncio.gather(
493 self.kafka_read(),
494 self.kafka_ping()
495 ))
496 # TODO
497 # self.logger.debug("Terminating cancelling creation tasks")
tiernoca2e16a2018-06-29 15:25:24 +0200498 # self.lcm_tasks.cancel("ALL", "create")
tiernoc0e42e22018-05-11 11:36:10 +0200499 # timeout = 200
500 # while self.is_pending_tasks():
501 # self.logger.debug("Task kafka_read terminating. Waiting for tasks termination")
502 # await asyncio.sleep(2, loop=self.loop)
503 # timeout -= 2
504 # if not timeout:
tiernoca2e16a2018-06-29 15:25:24 +0200505 # self.lcm_tasks.cancel("ALL", "ALL")
tiernoc0e42e22018-05-11 11:36:10 +0200506 self.loop.close()
507 self.loop = None
508 if self.db:
509 self.db.db_disconnect()
510 if self.msg:
511 self.msg.disconnect()
tierno16427352019-04-22 11:37:36 +0000512 if self.msg_admin:
513 self.msg_admin.disconnect()
tiernoc0e42e22018-05-11 11:36:10 +0200514 if self.fs:
515 self.fs.fs_disconnect()
516
tiernoc0e42e22018-05-11 11:36:10 +0200517 def read_config_file(self, config_file):
518 # TODO make a [ini] + yaml inside parser
519 # the configparser library is not suitable, because it does not admit comments at the end of line,
520 # and not parse integer or boolean
521 try:
522 with open(config_file) as f:
523 conf = yaml.load(f)
524 for k, v in environ.items():
525 if not k.startswith("OSMLCM_"):
526 continue
527 k_items = k.lower().split("_")
tierno17a612f2018-10-23 11:30:42 +0200528 if len(k_items) < 3:
529 continue
530 if k_items[1] in ("ro", "vca"):
531 # put in capital letter
532 k_items[1] = k_items[1].upper()
tiernoc0e42e22018-05-11 11:36:10 +0200533 c = conf
534 try:
535 for k_item in k_items[1:-1]:
tiernoc0e42e22018-05-11 11:36:10 +0200536 c = c[k_item]
537 if k_items[-1] == "port":
538 c[k_items[-1]] = int(v)
539 else:
540 c[k_items[-1]] = v
541 except Exception as e:
542 self.logger.warn("skipping environ '{}' on exception '{}'".format(k, e))
543
544 return conf
545 except Exception as e:
546 self.logger.critical("At config file '{}': {}".format(config_file, e))
547 exit(1)
548
tierno16427352019-04-22 11:37:36 +0000549 @staticmethod
550 def get_process_id():
551 """
552 Obtain a unique ID for this process. If running from inside docker, it will get docker ID. If not it
553 will provide a random one
554 :return: Obtained ID
555 """
556 # Try getting docker id. If fails, get pid
557 try:
558 with open("/proc/self/cgroup", "r") as f:
559 text_id_ = f.readline()
560 _, _, text_id = text_id_.rpartition("/")
561 text_id = text_id.replace('\n', '')[:12]
562 if text_id:
563 return text_id
564 except Exception:
565 pass
566 # Return a random id
567 return ''.join(random_choice("0123456789abcdef") for _ in range(12))
568
tiernoc0e42e22018-05-11 11:36:10 +0200569
tierno275411e2018-05-16 14:33:32 +0200570def usage():
571 print("""Usage: {} [options]
quilesj7e13aeb2019-10-08 13:34:55 +0200572 -c|--config [configuration_file]: loads the configuration file (default: ./lcm.cfg)
tiernoa9843d82018-10-24 10:44:20 +0200573 --health-check: do not run lcm, but inspect kafka bus to determine if lcm is healthy
tierno275411e2018-05-16 14:33:32 +0200574 -h|--help: shows this help
575 """.format(sys.argv[0]))
tierno750b2452018-05-17 16:39:29 +0200576 # --log-socket-host HOST: send logs to this host")
577 # --log-socket-port PORT: send logs using this port (default: 9022)")
tierno275411e2018-05-16 14:33:32 +0200578
579
tierno3e359b12019-02-03 02:29:13 +0100580def health_check():
581 retry = 2
582 while retry:
583 retry -= 1
584 try:
585 with open(health_check_file, "r") as f:
586 last_received_ping = f.read()
587
588 if time() - float(last_received_ping) < Lcm.ping_interval_pace + 10:
589 exit(0)
590 except Exception:
591 pass
592 if retry:
593 sleep(6)
594 exit(1)
595
596
tiernoc0e42e22018-05-11 11:36:10 +0200597if __name__ == '__main__':
quilesj7e13aeb2019-10-08 13:34:55 +0200598
tierno275411e2018-05-16 14:33:32 +0200599 try:
600 # load parameters and configuration
quilesj7e13aeb2019-10-08 13:34:55 +0200601 # -h
602 # -c value
603 # --config value
604 # --help
605 # --health-check
tiernoa9843d82018-10-24 10:44:20 +0200606 opts, args = getopt.getopt(sys.argv[1:], "hc:", ["config=", "help", "health-check"])
tierno275411e2018-05-16 14:33:32 +0200607 # TODO add "log-socket-host=", "log-socket-port=", "log-file="
608 config_file = None
609 for o, a in opts:
610 if o in ("-h", "--help"):
611 usage()
612 sys.exit()
613 elif o in ("-c", "--config"):
614 config_file = a
tiernoa9843d82018-10-24 10:44:20 +0200615 elif o == "--health-check":
tierno3e359b12019-02-03 02:29:13 +0100616 health_check()
tierno275411e2018-05-16 14:33:32 +0200617 # elif o == "--log-socket-port":
618 # log_socket_port = a
619 # elif o == "--log-socket-host":
620 # log_socket_host = a
621 # elif o == "--log-file":
622 # log_file = a
623 else:
624 assert False, "Unhandled option"
quilesj7e13aeb2019-10-08 13:34:55 +0200625
tierno275411e2018-05-16 14:33:32 +0200626 if config_file:
627 if not path.isfile(config_file):
quilesj7e13aeb2019-10-08 13:34:55 +0200628 print("configuration file '{}' does not exist".format(config_file), file=sys.stderr)
tierno275411e2018-05-16 14:33:32 +0200629 exit(1)
630 else:
631 for config_file in (__file__[:__file__.rfind(".")] + ".cfg", "./lcm.cfg", "/etc/osm/lcm.cfg"):
632 if path.isfile(config_file):
633 break
634 else:
tierno17a612f2018-10-23 11:30:42 +0200635 print("No configuration file 'lcm.cfg' found neither at local folder nor at /etc/osm/", file=sys.stderr)
tierno275411e2018-05-16 14:33:32 +0200636 exit(1)
637 lcm = Lcm(config_file)
tierno3e359b12019-02-03 02:29:13 +0100638 lcm.start()
tierno22f4f9c2018-06-11 18:53:39 +0200639 except (LcmException, getopt.GetoptError) as e:
tierno275411e2018-05-16 14:33:32 +0200640 print(str(e), file=sys.stderr)
641 # usage()
642 exit(1)