blob: 274154b16834e89b08c815d02264adfe8b022713 [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
tiernoc0e42e22018-05-11 11:36:10 +020020import asyncio
21import yaml
tierno275411e2018-05-16 14:33:32 +020022import logging
23import logging.handlers
24import getopt
tierno275411e2018-05-16 14:33:32 +020025import sys
tierno59d22d22018-09-25 18:10:19 +020026
tierno8069ce52019-08-28 15:34:33 +000027from osm_lcm import ROclient, ns, vim_sdn, netslice
28from time import time, sleep
29from osm_lcm.lcm_utils import versiontuple, LcmException, TaskRegistry, LcmExceptionExit
30from osm_lcm import version as lcm_version, version_date as lcm_version_date
31
tierno98768132018-09-11 12:07:21 +020032from osm_common import dbmemory, dbmongo, fslocal, msglocal, msgkafka
33from osm_common import version as common_version
tierno59d22d22018-09-25 18:10:19 +020034from osm_common.dbbase import DbException
tiernoc0e42e22018-05-11 11:36:10 +020035from osm_common.fsbase import FsException
36from osm_common.msgbase import MsgException
tierno275411e2018-05-16 14:33:32 +020037from os import environ, path
tierno16427352019-04-22 11:37:36 +000038from random import choice as random_choice
tierno59d22d22018-09-25 18:10:19 +020039from n2vc import version as n2vc_version
tiernoc0e42e22018-05-11 11:36:10 +020040
41
tierno275411e2018-05-16 14:33:32 +020042__author__ = "Alfonso Tierno"
tiernoe64f7fb2019-09-11 08:55:52 +000043min_RO_version = "6.0.2"
tierno6e9d2eb2018-09-12 17:47:18 +020044min_n2vc_version = "0.0.2"
tierno16427352019-04-22 11:37:36 +000045min_common_version = "0.1.19"
tierno86aa62f2018-08-20 11:57:04 +000046# uncomment if LCM is installed as library and installed, and get them from __init__.py
tierno8069ce52019-08-28 15:34:33 +000047# lcm_version = '0.1.41'
48# lcm_version_date = '2019-06-19'
tierno3e359b12019-02-03 02:29:13 +010049health_check_file = path.expanduser("~") + "/time_last_ping" # TODO find better location for this file
tierno275411e2018-05-16 14:33:32 +020050
51
tiernoc0e42e22018-05-11 11:36:10 +020052class Lcm:
53
tiernoa9843d82018-10-24 10:44:20 +020054 ping_interval_pace = 120 # how many time ping is send once is confirmed all is running
tiernof578e552018-11-08 19:07:20 +010055 ping_interval_boot = 5 # how many time ping is sent when booting
tiernoa9843d82018-10-24 10:44:20 +020056
tierno59d22d22018-09-25 18:10:19 +020057 def __init__(self, config_file, loop=None):
tiernoc0e42e22018-05-11 11:36:10 +020058 """
59 Init, Connect to database, filesystem storage, and messaging
60 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
61 :return: None
62 """
63
64 self.db = None
65 self.msg = None
tierno16427352019-04-22 11:37:36 +000066 self.msg_admin = None
tiernoc0e42e22018-05-11 11:36:10 +020067 self.fs = None
68 self.pings_not_received = 1
tiernoc2564fe2019-01-28 16:18:56 +000069 self.consecutive_errors = 0
70 self.first_start = False
tiernoc0e42e22018-05-11 11:36:10 +020071
tiernoc0e42e22018-05-11 11:36:10 +020072 # logging
73 self.logger = logging.getLogger('lcm')
tierno16427352019-04-22 11:37:36 +000074 # get id
75 self.worker_id = self.get_process_id()
tiernoc0e42e22018-05-11 11:36:10 +020076 # load configuration
77 config = self.read_config_file(config_file)
78 self.config = config
tierno750b2452018-05-17 16:39:29 +020079 self.ro_config = {
tiernoc0e42e22018-05-11 11:36:10 +020080 "endpoint_url": "http://{}:{}/openmano".format(config["RO"]["host"], config["RO"]["port"]),
tierno750b2452018-05-17 16:39:29 +020081 "tenant": config.get("tenant", "osm"),
tiernoc0e42e22018-05-11 11:36:10 +020082 "logger_name": "lcm.ROclient",
83 "loglevel": "ERROR",
84 }
85
tierno59d22d22018-09-25 18:10:19 +020086 self.vca_config = config["VCA"]
87
88 self.loop = loop or asyncio.get_event_loop()
tiernoc0e42e22018-05-11 11:36:10 +020089
90 # logging
91 log_format_simple = "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
92 log_formatter_simple = logging.Formatter(log_format_simple, datefmt='%Y-%m-%dT%H:%M:%S')
93 config["database"]["logger_name"] = "lcm.db"
94 config["storage"]["logger_name"] = "lcm.fs"
95 config["message"]["logger_name"] = "lcm.msg"
tierno86aa62f2018-08-20 11:57:04 +000096 if config["global"].get("logfile"):
tiernoc0e42e22018-05-11 11:36:10 +020097 file_handler = logging.handlers.RotatingFileHandler(config["global"]["logfile"],
98 maxBytes=100e6, backupCount=9, delay=0)
99 file_handler.setFormatter(log_formatter_simple)
100 self.logger.addHandler(file_handler)
tierno86aa62f2018-08-20 11:57:04 +0000101 if not config["global"].get("nologging"):
tiernoc0e42e22018-05-11 11:36:10 +0200102 str_handler = logging.StreamHandler()
103 str_handler.setFormatter(log_formatter_simple)
104 self.logger.addHandler(str_handler)
105
106 if config["global"].get("loglevel"):
107 self.logger.setLevel(config["global"]["loglevel"])
108
109 # logging other modules
110 for k1, logname in {"message": "lcm.msg", "database": "lcm.db", "storage": "lcm.fs"}.items():
111 config[k1]["logger_name"] = logname
112 logger_module = logging.getLogger(logname)
tierno86aa62f2018-08-20 11:57:04 +0000113 if config[k1].get("logfile"):
tiernoc0e42e22018-05-11 11:36:10 +0200114 file_handler = logging.handlers.RotatingFileHandler(config[k1]["logfile"],
115 maxBytes=100e6, backupCount=9, delay=0)
116 file_handler.setFormatter(log_formatter_simple)
117 logger_module.addHandler(file_handler)
tierno86aa62f2018-08-20 11:57:04 +0000118 if config[k1].get("loglevel"):
tiernoc0e42e22018-05-11 11:36:10 +0200119 logger_module.setLevel(config[k1]["loglevel"])
tierno86aa62f2018-08-20 11:57:04 +0000120 self.logger.critical("starting osm/lcm version {} {}".format(lcm_version, lcm_version_date))
tierno59d22d22018-09-25 18:10:19 +0200121
tiernoc0e42e22018-05-11 11:36:10 +0200122 # check version of N2VC
123 # TODO enhance with int conversion or from distutils.version import LooseVersion
124 # or with list(map(int, version.split(".")))
tierno59d22d22018-09-25 18:10:19 +0200125 if versiontuple(n2vc_version) < versiontuple(min_n2vc_version):
tierno6e9d2eb2018-09-12 17:47:18 +0200126 raise LcmException("Not compatible osm/N2VC version '{}'. Needed '{}' or higher".format(
tierno59d22d22018-09-25 18:10:19 +0200127 n2vc_version, min_n2vc_version))
128 # check version of common
tierno27246d82018-09-27 15:59:09 +0200129 if versiontuple(common_version) < versiontuple(min_common_version):
tierno6e9d2eb2018-09-12 17:47:18 +0200130 raise LcmException("Not compatible osm/common version '{}'. Needed '{}' or higher".format(
131 common_version, min_common_version))
tierno22f4f9c2018-06-11 18:53:39 +0200132
tiernoc0e42e22018-05-11 11:36:10 +0200133 try:
tierno22f4f9c2018-06-11 18:53:39 +0200134 # TODO check database version
tiernoc0e42e22018-05-11 11:36:10 +0200135 if config["database"]["driver"] == "mongo":
136 self.db = dbmongo.DbMongo()
137 self.db.db_connect(config["database"])
138 elif config["database"]["driver"] == "memory":
139 self.db = dbmemory.DbMemory()
140 self.db.db_connect(config["database"])
141 else:
142 raise LcmException("Invalid configuration param '{}' at '[database]':'driver'".format(
143 config["database"]["driver"]))
144
145 if config["storage"]["driver"] == "local":
146 self.fs = fslocal.FsLocal()
147 self.fs.fs_connect(config["storage"])
148 else:
149 raise LcmException("Invalid configuration param '{}' at '[storage]':'driver'".format(
150 config["storage"]["driver"]))
151
tiernoc2564fe2019-01-28 16:18:56 +0000152 config_message = config["message"].copy()
153 config_message["loop"] = self.loop
154 if config_message["driver"] == "local":
tiernoc0e42e22018-05-11 11:36:10 +0200155 self.msg = msglocal.MsgLocal()
tiernoc2564fe2019-01-28 16:18:56 +0000156 self.msg.connect(config_message)
tierno16427352019-04-22 11:37:36 +0000157 self.msg_admin = msglocal.MsgLocal()
158 config_message.pop("group_id", None)
159 self.msg_admin.connect(config_message)
tiernoc2564fe2019-01-28 16:18:56 +0000160 elif config_message["driver"] == "kafka":
tiernoc0e42e22018-05-11 11:36:10 +0200161 self.msg = msgkafka.MsgKafka()
tiernoc2564fe2019-01-28 16:18:56 +0000162 self.msg.connect(config_message)
tierno16427352019-04-22 11:37:36 +0000163 self.msg_admin = msgkafka.MsgKafka()
164 config_message.pop("group_id", None)
165 self.msg_admin.connect(config_message)
tiernoc0e42e22018-05-11 11:36:10 +0200166 else:
167 raise LcmException("Invalid configuration param '{}' at '[message]':'driver'".format(
tiernoc2564fe2019-01-28 16:18:56 +0000168 config["message"]["driver"]))
tiernoc0e42e22018-05-11 11:36:10 +0200169 except (DbException, FsException, MsgException) as e:
170 self.logger.critical(str(e), exc_info=True)
171 raise LcmException(str(e))
172
kuused124bfe2019-06-18 12:09:24 +0200173 # contains created tasks/futures to be able to cancel
174 self.lcm_tasks = TaskRegistry(self.worker_id, self.db, self.logger)
175
tierno59d22d22018-09-25 18:10:19 +0200176 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 +0100177 self.netslice = netslice.NetsliceLcm(self.db, self.msg, self.fs, self.lcm_tasks, self.ro_config,
178 self.vca_config, self.loop)
tierno59d22d22018-09-25 18:10:19 +0200179 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 +0000180 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 +0200181 self.sdn = vim_sdn.SdnLcm(self.db, self.msg, self.fs, self.lcm_tasks, self.ro_config, self.loop)
182
tierno22f4f9c2018-06-11 18:53:39 +0200183 async def check_RO_version(self):
tiernoe64f7fb2019-09-11 08:55:52 +0000184 tries = 14
185 last_error = None
186 while True:
187 try:
188 ro_server = ROclient.ROClient(self.loop, **self.ro_config)
189 ro_version = await ro_server.get_version()
190 if versiontuple(ro_version) < versiontuple(min_RO_version):
191 raise LcmException("Not compatible osm/RO version '{}'. Needed '{}' or higher".format(
192 ro_version, min_RO_version))
193 self.logger.info("Connected to RO version {}".format(ro_version))
194 return
195 except ROclient.ROClientException as e:
196 tries -= 1
197 error_text = "Error while connecting to RO on {}: {}".format(self.ro_config["endpoint_url"], e)
198 if tries <= 0:
199 self.logger.critical(error_text)
200 raise LcmException(error_text)
201 if last_error != error_text:
202 last_error = error_text
203 self.logger.error(error_text + ". Waiting until {} seconds".format(5*tries))
204 await asyncio.sleep(5)
tierno22f4f9c2018-06-11 18:53:39 +0200205
tiernoc0e42e22018-05-11 11:36:10 +0200206 async def test(self, param=None):
207 self.logger.debug("Starting/Ending test task: {}".format(param))
208
tiernoc0e42e22018-05-11 11:36:10 +0200209 async def kafka_ping(self):
210 self.logger.debug("Task kafka_ping Enter")
211 consecutive_errors = 0
212 first_start = True
213 kafka_has_received = False
214 self.pings_not_received = 1
215 while True:
216 try:
tierno16427352019-04-22 11:37:36 +0000217 await self.msg_admin.aiowrite(
218 "admin", "ping",
219 {"from": "lcm", "to": "lcm", "worker_id": self.worker_id, "version": lcm_version},
220 self.loop)
tiernoc0e42e22018-05-11 11:36:10 +0200221 # time between pings are low when it is not received and at starting
tiernoa9843d82018-10-24 10:44:20 +0200222 wait_time = self.ping_interval_boot if not kafka_has_received else self.ping_interval_pace
tiernoc0e42e22018-05-11 11:36:10 +0200223 if not self.pings_not_received:
224 kafka_has_received = True
225 self.pings_not_received += 1
226 await asyncio.sleep(wait_time, loop=self.loop)
227 if self.pings_not_received > 10:
228 raise LcmException("It is not receiving pings from Kafka bus")
229 consecutive_errors = 0
230 first_start = False
231 except LcmException:
232 raise
233 except Exception as e:
234 # if not first_start is the first time after starting. So leave more time and wait
235 # to allow kafka starts
236 if consecutive_errors == 8 if not first_start else 30:
237 self.logger.error("Task kafka_read task exit error too many errors. Exception: {}".format(e))
238 raise
239 consecutive_errors += 1
240 self.logger.error("Task kafka_read retrying after Exception {}".format(e))
tierno16427352019-04-22 11:37:36 +0000241 wait_time = 2 if not first_start else 5
tiernoc0e42e22018-05-11 11:36:10 +0200242 await asyncio.sleep(wait_time, loop=self.loop)
243
gcalvinoed7f6d42018-12-14 14:44:56 +0100244 def kafka_read_callback(self, topic, command, params):
245 order_id = 1
246
247 if topic != "admin" and command != "ping":
248 self.logger.debug("Task kafka_read receives {} {}: {}".format(topic, command, params))
249 self.consecutive_errors = 0
250 self.first_start = False
251 order_id += 1
252 if command == "exit":
253 raise LcmExceptionExit
254 elif command.startswith("#"):
255 return
256 elif command == "echo":
257 # just for test
258 print(params)
259 sys.stdout.flush()
260 return
261 elif command == "test":
262 asyncio.Task(self.test(params), loop=self.loop)
263 return
264
265 if topic == "admin":
266 if command == "ping" and params["to"] == "lcm" and params["from"] == "lcm":
tierno16427352019-04-22 11:37:36 +0000267 if params.get("worker_id") != self.worker_id:
268 return
gcalvinoed7f6d42018-12-14 14:44:56 +0100269 self.pings_not_received = 0
tierno3e359b12019-02-03 02:29:13 +0100270 try:
271 with open(health_check_file, "w") as f:
272 f.write(str(time()))
273 except Exception as e:
274 self.logger.error("Cannot write into '{}' for healthcheck: {}".format(health_check_file, e))
gcalvinoed7f6d42018-12-14 14:44:56 +0100275 return
276 elif topic == "ns":
277 if command == "instantiate":
278 # self.logger.debug("Deploying NS {}".format(nsr_id))
279 nslcmop = params
280 nslcmop_id = nslcmop["_id"]
281 nsr_id = nslcmop["nsInstanceId"]
282 task = asyncio.ensure_future(self.ns.instantiate(nsr_id, nslcmop_id))
283 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_instantiate", task)
284 return
285 elif command == "terminate":
286 # self.logger.debug("Deleting NS {}".format(nsr_id))
287 nslcmop = params
288 nslcmop_id = nslcmop["_id"]
289 nsr_id = nslcmop["nsInstanceId"]
290 self.lcm_tasks.cancel(topic, nsr_id)
291 task = asyncio.ensure_future(self.ns.terminate(nsr_id, nslcmop_id))
292 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_terminate", task)
293 return
294 elif command == "action":
295 # self.logger.debug("Update NS {}".format(nsr_id))
296 nslcmop = params
297 nslcmop_id = nslcmop["_id"]
298 nsr_id = nslcmop["nsInstanceId"]
299 task = asyncio.ensure_future(self.ns.action(nsr_id, nslcmop_id))
300 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_action", task)
301 return
302 elif command == "scale":
303 # self.logger.debug("Update NS {}".format(nsr_id))
304 nslcmop = params
305 nslcmop_id = nslcmop["_id"]
306 nsr_id = nslcmop["nsInstanceId"]
307 task = asyncio.ensure_future(self.ns.scale(nsr_id, nslcmop_id))
308 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_scale", task)
309 return
310 elif command == "show":
tiernoc2564fe2019-01-28 16:18:56 +0000311 nsr_id = params
gcalvinoed7f6d42018-12-14 14:44:56 +0100312 try:
313 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
314 print("nsr:\n _id={}\n operational-status: {}\n config-status: {}"
315 "\n detailed-status: {}\n deploy: {}\n tasks: {}"
316 "".format(nsr_id, db_nsr["operational-status"], db_nsr["config-status"],
317 db_nsr["detailed-status"],
318 db_nsr["_admin"]["deployed"], self.lcm_ns_tasks.get(nsr_id)))
319 except Exception as e:
320 print("nsr {} not found: {}".format(nsr_id, e))
321 sys.stdout.flush()
322 return
323 elif command == "deleted":
324 return # TODO cleaning of task just in case should be done
325 elif command in ("terminated", "instantiated", "scaled", "actioned"): # "scaled-cooldown-time"
326 return
327 elif topic == "nsi": # netslice LCM processes (instantiate, terminate, etc)
328 if command == "instantiate":
329 # self.logger.debug("Instantiating Network Slice {}".format(nsilcmop["netsliceInstanceId"]))
330 nsilcmop = params
331 nsilcmop_id = nsilcmop["_id"] # slice operation id
332 nsir_id = nsilcmop["netsliceInstanceId"] # slice record id
333 task = asyncio.ensure_future(self.netslice.instantiate(nsir_id, nsilcmop_id))
334 self.lcm_tasks.register("nsi", nsir_id, nsilcmop_id, "nsi_instantiate", task)
335 return
336 elif command == "terminate":
337 # self.logger.debug("Terminating Network Slice NS {}".format(nsilcmop["netsliceInstanceId"]))
338 nsilcmop = params
339 nsilcmop_id = nsilcmop["_id"] # slice operation id
340 nsir_id = nsilcmop["netsliceInstanceId"] # slice record id
341 self.lcm_tasks.cancel(topic, nsir_id)
342 task = asyncio.ensure_future(self.netslice.terminate(nsir_id, nsilcmop_id))
343 self.lcm_tasks.register("nsi", nsir_id, nsilcmop_id, "nsi_terminate", task)
344 return
345 elif command == "show":
tiernoc2564fe2019-01-28 16:18:56 +0000346 nsir_id = params
gcalvinoed7f6d42018-12-14 14:44:56 +0100347 try:
348 db_nsir = self.db.get_one("nsirs", {"_id": nsir_id})
349 print("nsir:\n _id={}\n operational-status: {}\n config-status: {}"
350 "\n detailed-status: {}\n deploy: {}\n tasks: {}"
351 "".format(nsir_id, db_nsir["operational-status"], db_nsir["config-status"],
352 db_nsir["detailed-status"],
353 db_nsir["_admin"]["deployed"], self.lcm_netslice_tasks.get(nsir_id)))
354 except Exception as e:
355 print("nsir {} not found: {}".format(nsir_id, e))
356 sys.stdout.flush()
357 return
358 elif command == "deleted":
359 return # TODO cleaning of task just in case should be done
360 elif command in ("terminated", "instantiated", "scaled", "actioned"): # "scaled-cooldown-time"
361 return
362 elif topic == "vim_account":
363 vim_id = params["_id"]
tiernof210c1c2019-10-16 09:09:58 +0000364 if command in ("create", "created"):
gcalvinoed7f6d42018-12-14 14:44:56 +0100365 task = asyncio.ensure_future(self.vim.create(params, order_id))
366 self.lcm_tasks.register("vim_account", vim_id, order_id, "vim_create", task)
367 return
368 elif command == "delete":
369 self.lcm_tasks.cancel(topic, vim_id)
kuuse6a470c62019-07-10 13:52:45 +0200370 task = asyncio.ensure_future(self.vim.delete(params, order_id))
gcalvinoed7f6d42018-12-14 14:44:56 +0100371 self.lcm_tasks.register("vim_account", vim_id, order_id, "vim_delete", task)
372 return
373 elif command == "show":
374 print("not implemented show with vim_account")
375 sys.stdout.flush()
376 return
tiernof210c1c2019-10-16 09:09:58 +0000377 elif command in ("edit", "edited"):
gcalvinoed7f6d42018-12-14 14:44:56 +0100378 task = asyncio.ensure_future(self.vim.edit(params, order_id))
379 self.lcm_tasks.register("vim_account", vim_id, order_id, "vim_edit", task)
380 return
tiernof210c1c2019-10-16 09:09:58 +0000381 elif command == "deleted":
382 return # TODO cleaning of task just in case should be done
gcalvinoed7f6d42018-12-14 14:44:56 +0100383 elif topic == "wim_account":
384 wim_id = params["_id"]
tiernof210c1c2019-10-16 09:09:58 +0000385 if command in ("create", "created"):
gcalvinoed7f6d42018-12-14 14:44:56 +0100386 task = asyncio.ensure_future(self.wim.create(params, order_id))
387 self.lcm_tasks.register("wim_account", wim_id, order_id, "wim_create", task)
388 return
389 elif command == "delete":
390 self.lcm_tasks.cancel(topic, wim_id)
kuuse6a470c62019-07-10 13:52:45 +0200391 task = asyncio.ensure_future(self.wim.delete(params, order_id))
gcalvinoed7f6d42018-12-14 14:44:56 +0100392 self.lcm_tasks.register("wim_account", wim_id, order_id, "wim_delete", task)
393 return
394 elif command == "show":
395 print("not implemented show with wim_account")
396 sys.stdout.flush()
397 return
tiernof210c1c2019-10-16 09:09:58 +0000398 elif command in ("edit", "edited"):
gcalvinoed7f6d42018-12-14 14:44:56 +0100399 task = asyncio.ensure_future(self.wim.edit(params, order_id))
400 self.lcm_tasks.register("wim_account", wim_id, order_id, "wim_edit", task)
401 return
tiernof210c1c2019-10-16 09:09:58 +0000402 elif command == "deleted":
403 return # TODO cleaning of task just in case should be done
gcalvinoed7f6d42018-12-14 14:44:56 +0100404 elif topic == "sdn":
405 _sdn_id = params["_id"]
tiernof210c1c2019-10-16 09:09:58 +0000406 if command in ("create", "created"):
gcalvinoed7f6d42018-12-14 14:44:56 +0100407 task = asyncio.ensure_future(self.sdn.create(params, order_id))
408 self.lcm_tasks.register("sdn", _sdn_id, order_id, "sdn_create", task)
409 return
410 elif command == "delete":
411 self.lcm_tasks.cancel(topic, _sdn_id)
kuuse6a470c62019-07-10 13:52:45 +0200412 task = asyncio.ensure_future(self.sdn.delete(params, order_id))
gcalvinoed7f6d42018-12-14 14:44:56 +0100413 self.lcm_tasks.register("sdn", _sdn_id, order_id, "sdn_delete", task)
414 return
tiernof210c1c2019-10-16 09:09:58 +0000415 elif command in ("edit", "edited"):
gcalvinoed7f6d42018-12-14 14:44:56 +0100416 task = asyncio.ensure_future(self.sdn.edit(params, order_id))
417 self.lcm_tasks.register("sdn", _sdn_id, order_id, "sdn_edit", task)
418 return
tiernof210c1c2019-10-16 09:09:58 +0000419 elif command == "deleted":
420 return # TODO cleaning of task just in case should be done
gcalvinoed7f6d42018-12-14 14:44:56 +0100421 self.logger.critical("unknown topic {} and command '{}'".format(topic, command))
422
tiernoc0e42e22018-05-11 11:36:10 +0200423 async def kafka_read(self):
tierno16427352019-04-22 11:37:36 +0000424 self.logger.debug("Task kafka_read Enter with worker_id={}".format(self.worker_id))
tiernoc0e42e22018-05-11 11:36:10 +0200425 # future = asyncio.Future()
gcalvinoed7f6d42018-12-14 14:44:56 +0100426 self.consecutive_errors = 0
427 self.first_start = True
428 while self.consecutive_errors < 10:
tiernoc0e42e22018-05-11 11:36:10 +0200429 try:
tierno16427352019-04-22 11:37:36 +0000430 topics = ("ns", "vim_account", "wim_account", "sdn", "nsi")
431 topics_admin = ("admin", )
432 await asyncio.gather(
433 self.msg.aioread(topics, self.loop, self.kafka_read_callback),
434 self.msg_admin.aioread(topics_admin, self.loop, self.kafka_read_callback, group_id=False)
435 )
tiernoc0e42e22018-05-11 11:36:10 +0200436
gcalvinoed7f6d42018-12-14 14:44:56 +0100437 except LcmExceptionExit:
438 self.logger.debug("Bye!")
439 break
tiernoc0e42e22018-05-11 11:36:10 +0200440 except Exception as e:
441 # if not first_start is the first time after starting. So leave more time and wait
442 # to allow kafka starts
gcalvinoed7f6d42018-12-14 14:44:56 +0100443 if self.consecutive_errors == 8 if not self.first_start else 30:
tiernoc0e42e22018-05-11 11:36:10 +0200444 self.logger.error("Task kafka_read task exit error too many errors. Exception: {}".format(e))
445 raise
gcalvinoed7f6d42018-12-14 14:44:56 +0100446 self.consecutive_errors += 1
tiernoc0e42e22018-05-11 11:36:10 +0200447 self.logger.error("Task kafka_read retrying after Exception {}".format(e))
gcalvinoed7f6d42018-12-14 14:44:56 +0100448 wait_time = 2 if not self.first_start else 5
tiernoc0e42e22018-05-11 11:36:10 +0200449 await asyncio.sleep(wait_time, loop=self.loop)
450
451 # self.logger.debug("Task kafka_read terminating")
452 self.logger.debug("Task kafka_read exit")
453
454 def start(self):
tierno22f4f9c2018-06-11 18:53:39 +0200455
456 # check RO version
457 self.loop.run_until_complete(self.check_RO_version())
458
tiernoc0e42e22018-05-11 11:36:10 +0200459 self.loop.run_until_complete(asyncio.gather(
460 self.kafka_read(),
461 self.kafka_ping()
462 ))
463 # TODO
464 # self.logger.debug("Terminating cancelling creation tasks")
tiernoca2e16a2018-06-29 15:25:24 +0200465 # self.lcm_tasks.cancel("ALL", "create")
tiernoc0e42e22018-05-11 11:36:10 +0200466 # timeout = 200
467 # while self.is_pending_tasks():
468 # self.logger.debug("Task kafka_read terminating. Waiting for tasks termination")
469 # await asyncio.sleep(2, loop=self.loop)
470 # timeout -= 2
471 # if not timeout:
tiernoca2e16a2018-06-29 15:25:24 +0200472 # self.lcm_tasks.cancel("ALL", "ALL")
tiernoc0e42e22018-05-11 11:36:10 +0200473 self.loop.close()
474 self.loop = None
475 if self.db:
476 self.db.db_disconnect()
477 if self.msg:
478 self.msg.disconnect()
tierno16427352019-04-22 11:37:36 +0000479 if self.msg_admin:
480 self.msg_admin.disconnect()
tiernoc0e42e22018-05-11 11:36:10 +0200481 if self.fs:
482 self.fs.fs_disconnect()
483
tiernoc0e42e22018-05-11 11:36:10 +0200484 def read_config_file(self, config_file):
485 # TODO make a [ini] + yaml inside parser
486 # the configparser library is not suitable, because it does not admit comments at the end of line,
487 # and not parse integer or boolean
488 try:
489 with open(config_file) as f:
490 conf = yaml.load(f)
491 for k, v in environ.items():
492 if not k.startswith("OSMLCM_"):
493 continue
494 k_items = k.lower().split("_")
tierno17a612f2018-10-23 11:30:42 +0200495 if len(k_items) < 3:
496 continue
497 if k_items[1] in ("ro", "vca"):
498 # put in capital letter
499 k_items[1] = k_items[1].upper()
tiernoc0e42e22018-05-11 11:36:10 +0200500 c = conf
501 try:
502 for k_item in k_items[1:-1]:
tiernoc0e42e22018-05-11 11:36:10 +0200503 c = c[k_item]
504 if k_items[-1] == "port":
505 c[k_items[-1]] = int(v)
506 else:
507 c[k_items[-1]] = v
508 except Exception as e:
509 self.logger.warn("skipping environ '{}' on exception '{}'".format(k, e))
510
511 return conf
512 except Exception as e:
513 self.logger.critical("At config file '{}': {}".format(config_file, e))
514 exit(1)
515
tierno16427352019-04-22 11:37:36 +0000516 @staticmethod
517 def get_process_id():
518 """
519 Obtain a unique ID for this process. If running from inside docker, it will get docker ID. If not it
520 will provide a random one
521 :return: Obtained ID
522 """
523 # Try getting docker id. If fails, get pid
524 try:
525 with open("/proc/self/cgroup", "r") as f:
526 text_id_ = f.readline()
527 _, _, text_id = text_id_.rpartition("/")
528 text_id = text_id.replace('\n', '')[:12]
529 if text_id:
530 return text_id
531 except Exception:
532 pass
533 # Return a random id
534 return ''.join(random_choice("0123456789abcdef") for _ in range(12))
535
tiernoc0e42e22018-05-11 11:36:10 +0200536
tierno275411e2018-05-16 14:33:32 +0200537def usage():
538 print("""Usage: {} [options]
539 -c|--config [configuration_file]: loads the configuration file (default: ./nbi.cfg)
tiernoa9843d82018-10-24 10:44:20 +0200540 --health-check: do not run lcm, but inspect kafka bus to determine if lcm is healthy
tierno275411e2018-05-16 14:33:32 +0200541 -h|--help: shows this help
542 """.format(sys.argv[0]))
tierno750b2452018-05-17 16:39:29 +0200543 # --log-socket-host HOST: send logs to this host")
544 # --log-socket-port PORT: send logs using this port (default: 9022)")
tierno275411e2018-05-16 14:33:32 +0200545
546
tierno3e359b12019-02-03 02:29:13 +0100547def health_check():
548 retry = 2
549 while retry:
550 retry -= 1
551 try:
552 with open(health_check_file, "r") as f:
553 last_received_ping = f.read()
554
555 if time() - float(last_received_ping) < Lcm.ping_interval_pace + 10:
556 exit(0)
557 except Exception:
558 pass
559 if retry:
560 sleep(6)
561 exit(1)
562
563
tiernoc0e42e22018-05-11 11:36:10 +0200564if __name__ == '__main__':
tierno275411e2018-05-16 14:33:32 +0200565 try:
566 # load parameters and configuration
tiernoa9843d82018-10-24 10:44:20 +0200567 opts, args = getopt.getopt(sys.argv[1:], "hc:", ["config=", "help", "health-check"])
tierno275411e2018-05-16 14:33:32 +0200568 # TODO add "log-socket-host=", "log-socket-port=", "log-file="
569 config_file = None
570 for o, a in opts:
571 if o in ("-h", "--help"):
572 usage()
573 sys.exit()
574 elif o in ("-c", "--config"):
575 config_file = a
tiernoa9843d82018-10-24 10:44:20 +0200576 elif o == "--health-check":
tierno3e359b12019-02-03 02:29:13 +0100577 health_check()
tierno275411e2018-05-16 14:33:32 +0200578 # elif o == "--log-socket-port":
579 # log_socket_port = a
580 # elif o == "--log-socket-host":
581 # log_socket_host = a
582 # elif o == "--log-file":
583 # log_file = a
584 else:
585 assert False, "Unhandled option"
586 if config_file:
587 if not path.isfile(config_file):
tierno17a612f2018-10-23 11:30:42 +0200588 print("configuration file '{}' not exist".format(config_file), file=sys.stderr)
tierno275411e2018-05-16 14:33:32 +0200589 exit(1)
590 else:
591 for config_file in (__file__[:__file__.rfind(".")] + ".cfg", "./lcm.cfg", "/etc/osm/lcm.cfg"):
592 if path.isfile(config_file):
593 break
594 else:
tierno17a612f2018-10-23 11:30:42 +0200595 print("No configuration file 'lcm.cfg' found neither at local folder nor at /etc/osm/", file=sys.stderr)
tierno275411e2018-05-16 14:33:32 +0200596 exit(1)
597 lcm = Lcm(config_file)
tierno3e359b12019-02-03 02:29:13 +0100598 lcm.start()
tierno22f4f9c2018-06-11 18:53:39 +0200599 except (LcmException, getopt.GetoptError) as e:
tierno275411e2018-05-16 14:33:32 +0200600 print(str(e), file=sys.stderr)
601 # usage()
602 exit(1)