blob: 48c07eb3961f1761668975789b9f9ad149168a3c [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"]
364 if command == "create":
365 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
377 elif command == "edit":
378 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
381 elif topic == "wim_account":
382 wim_id = params["_id"]
383 if command == "create":
384 task = asyncio.ensure_future(self.wim.create(params, order_id))
385 self.lcm_tasks.register("wim_account", wim_id, order_id, "wim_create", task)
386 return
387 elif command == "delete":
388 self.lcm_tasks.cancel(topic, wim_id)
kuuse6a470c62019-07-10 13:52:45 +0200389 task = asyncio.ensure_future(self.wim.delete(params, order_id))
gcalvinoed7f6d42018-12-14 14:44:56 +0100390 self.lcm_tasks.register("wim_account", wim_id, order_id, "wim_delete", task)
391 return
392 elif command == "show":
393 print("not implemented show with wim_account")
394 sys.stdout.flush()
395 return
396 elif command == "edit":
397 task = asyncio.ensure_future(self.wim.edit(params, order_id))
398 self.lcm_tasks.register("wim_account", wim_id, order_id, "wim_edit", task)
399 return
400 elif topic == "sdn":
401 _sdn_id = params["_id"]
402 if command == "create":
403 task = asyncio.ensure_future(self.sdn.create(params, order_id))
404 self.lcm_tasks.register("sdn", _sdn_id, order_id, "sdn_create", task)
405 return
406 elif command == "delete":
407 self.lcm_tasks.cancel(topic, _sdn_id)
kuuse6a470c62019-07-10 13:52:45 +0200408 task = asyncio.ensure_future(self.sdn.delete(params, order_id))
gcalvinoed7f6d42018-12-14 14:44:56 +0100409 self.lcm_tasks.register("sdn", _sdn_id, order_id, "sdn_delete", task)
410 return
411 elif command == "edit":
412 task = asyncio.ensure_future(self.sdn.edit(params, order_id))
413 self.lcm_tasks.register("sdn", _sdn_id, order_id, "sdn_edit", task)
414 return
415 self.logger.critical("unknown topic {} and command '{}'".format(topic, command))
416
tiernoc0e42e22018-05-11 11:36:10 +0200417 async def kafka_read(self):
tierno16427352019-04-22 11:37:36 +0000418 self.logger.debug("Task kafka_read Enter with worker_id={}".format(self.worker_id))
tiernoc0e42e22018-05-11 11:36:10 +0200419 # future = asyncio.Future()
gcalvinoed7f6d42018-12-14 14:44:56 +0100420 self.consecutive_errors = 0
421 self.first_start = True
422 while self.consecutive_errors < 10:
tiernoc0e42e22018-05-11 11:36:10 +0200423 try:
tierno16427352019-04-22 11:37:36 +0000424 topics = ("ns", "vim_account", "wim_account", "sdn", "nsi")
425 topics_admin = ("admin", )
426 await asyncio.gather(
427 self.msg.aioread(topics, self.loop, self.kafka_read_callback),
428 self.msg_admin.aioread(topics_admin, self.loop, self.kafka_read_callback, group_id=False)
429 )
tiernoc0e42e22018-05-11 11:36:10 +0200430
gcalvinoed7f6d42018-12-14 14:44:56 +0100431 except LcmExceptionExit:
432 self.logger.debug("Bye!")
433 break
tiernoc0e42e22018-05-11 11:36:10 +0200434 except Exception as e:
435 # if not first_start is the first time after starting. So leave more time and wait
436 # to allow kafka starts
gcalvinoed7f6d42018-12-14 14:44:56 +0100437 if self.consecutive_errors == 8 if not self.first_start else 30:
tiernoc0e42e22018-05-11 11:36:10 +0200438 self.logger.error("Task kafka_read task exit error too many errors. Exception: {}".format(e))
439 raise
gcalvinoed7f6d42018-12-14 14:44:56 +0100440 self.consecutive_errors += 1
tiernoc0e42e22018-05-11 11:36:10 +0200441 self.logger.error("Task kafka_read retrying after Exception {}".format(e))
gcalvinoed7f6d42018-12-14 14:44:56 +0100442 wait_time = 2 if not self.first_start else 5
tiernoc0e42e22018-05-11 11:36:10 +0200443 await asyncio.sleep(wait_time, loop=self.loop)
444
445 # self.logger.debug("Task kafka_read terminating")
446 self.logger.debug("Task kafka_read exit")
447
448 def start(self):
tierno22f4f9c2018-06-11 18:53:39 +0200449
450 # check RO version
451 self.loop.run_until_complete(self.check_RO_version())
452
tiernoc0e42e22018-05-11 11:36:10 +0200453 self.loop.run_until_complete(asyncio.gather(
454 self.kafka_read(),
455 self.kafka_ping()
456 ))
457 # TODO
458 # self.logger.debug("Terminating cancelling creation tasks")
tiernoca2e16a2018-06-29 15:25:24 +0200459 # self.lcm_tasks.cancel("ALL", "create")
tiernoc0e42e22018-05-11 11:36:10 +0200460 # timeout = 200
461 # while self.is_pending_tasks():
462 # self.logger.debug("Task kafka_read terminating. Waiting for tasks termination")
463 # await asyncio.sleep(2, loop=self.loop)
464 # timeout -= 2
465 # if not timeout:
tiernoca2e16a2018-06-29 15:25:24 +0200466 # self.lcm_tasks.cancel("ALL", "ALL")
tiernoc0e42e22018-05-11 11:36:10 +0200467 self.loop.close()
468 self.loop = None
469 if self.db:
470 self.db.db_disconnect()
471 if self.msg:
472 self.msg.disconnect()
tierno16427352019-04-22 11:37:36 +0000473 if self.msg_admin:
474 self.msg_admin.disconnect()
tiernoc0e42e22018-05-11 11:36:10 +0200475 if self.fs:
476 self.fs.fs_disconnect()
477
tiernoc0e42e22018-05-11 11:36:10 +0200478 def read_config_file(self, config_file):
479 # TODO make a [ini] + yaml inside parser
480 # the configparser library is not suitable, because it does not admit comments at the end of line,
481 # and not parse integer or boolean
482 try:
483 with open(config_file) as f:
484 conf = yaml.load(f)
485 for k, v in environ.items():
486 if not k.startswith("OSMLCM_"):
487 continue
488 k_items = k.lower().split("_")
tierno17a612f2018-10-23 11:30:42 +0200489 if len(k_items) < 3:
490 continue
491 if k_items[1] in ("ro", "vca"):
492 # put in capital letter
493 k_items[1] = k_items[1].upper()
tiernoc0e42e22018-05-11 11:36:10 +0200494 c = conf
495 try:
496 for k_item in k_items[1:-1]:
tiernoc0e42e22018-05-11 11:36:10 +0200497 c = c[k_item]
498 if k_items[-1] == "port":
499 c[k_items[-1]] = int(v)
500 else:
501 c[k_items[-1]] = v
502 except Exception as e:
503 self.logger.warn("skipping environ '{}' on exception '{}'".format(k, e))
504
505 return conf
506 except Exception as e:
507 self.logger.critical("At config file '{}': {}".format(config_file, e))
508 exit(1)
509
tierno16427352019-04-22 11:37:36 +0000510 @staticmethod
511 def get_process_id():
512 """
513 Obtain a unique ID for this process. If running from inside docker, it will get docker ID. If not it
514 will provide a random one
515 :return: Obtained ID
516 """
517 # Try getting docker id. If fails, get pid
518 try:
519 with open("/proc/self/cgroup", "r") as f:
520 text_id_ = f.readline()
521 _, _, text_id = text_id_.rpartition("/")
522 text_id = text_id.replace('\n', '')[:12]
523 if text_id:
524 return text_id
525 except Exception:
526 pass
527 # Return a random id
528 return ''.join(random_choice("0123456789abcdef") for _ in range(12))
529
tiernoc0e42e22018-05-11 11:36:10 +0200530
tierno275411e2018-05-16 14:33:32 +0200531def usage():
532 print("""Usage: {} [options]
533 -c|--config [configuration_file]: loads the configuration file (default: ./nbi.cfg)
tiernoa9843d82018-10-24 10:44:20 +0200534 --health-check: do not run lcm, but inspect kafka bus to determine if lcm is healthy
tierno275411e2018-05-16 14:33:32 +0200535 -h|--help: shows this help
536 """.format(sys.argv[0]))
tierno750b2452018-05-17 16:39:29 +0200537 # --log-socket-host HOST: send logs to this host")
538 # --log-socket-port PORT: send logs using this port (default: 9022)")
tierno275411e2018-05-16 14:33:32 +0200539
540
tierno3e359b12019-02-03 02:29:13 +0100541def health_check():
542 retry = 2
543 while retry:
544 retry -= 1
545 try:
546 with open(health_check_file, "r") as f:
547 last_received_ping = f.read()
548
549 if time() - float(last_received_ping) < Lcm.ping_interval_pace + 10:
550 exit(0)
551 except Exception:
552 pass
553 if retry:
554 sleep(6)
555 exit(1)
556
557
tiernoc0e42e22018-05-11 11:36:10 +0200558if __name__ == '__main__':
tierno275411e2018-05-16 14:33:32 +0200559 try:
560 # load parameters and configuration
tiernoa9843d82018-10-24 10:44:20 +0200561 opts, args = getopt.getopt(sys.argv[1:], "hc:", ["config=", "help", "health-check"])
tierno275411e2018-05-16 14:33:32 +0200562 # TODO add "log-socket-host=", "log-socket-port=", "log-file="
563 config_file = None
564 for o, a in opts:
565 if o in ("-h", "--help"):
566 usage()
567 sys.exit()
568 elif o in ("-c", "--config"):
569 config_file = a
tiernoa9843d82018-10-24 10:44:20 +0200570 elif o == "--health-check":
tierno3e359b12019-02-03 02:29:13 +0100571 health_check()
tierno275411e2018-05-16 14:33:32 +0200572 # elif o == "--log-socket-port":
573 # log_socket_port = a
574 # elif o == "--log-socket-host":
575 # log_socket_host = a
576 # elif o == "--log-file":
577 # log_file = a
578 else:
579 assert False, "Unhandled option"
580 if config_file:
581 if not path.isfile(config_file):
tierno17a612f2018-10-23 11:30:42 +0200582 print("configuration file '{}' not exist".format(config_file), file=sys.stderr)
tierno275411e2018-05-16 14:33:32 +0200583 exit(1)
584 else:
585 for config_file in (__file__[:__file__.rfind(".")] + ".cfg", "./lcm.cfg", "/etc/osm/lcm.cfg"):
586 if path.isfile(config_file):
587 break
588 else:
tierno17a612f2018-10-23 11:30:42 +0200589 print("No configuration file 'lcm.cfg' found neither at local folder nor at /etc/osm/", file=sys.stderr)
tierno275411e2018-05-16 14:33:32 +0200590 exit(1)
591 lcm = Lcm(config_file)
tierno3e359b12019-02-03 02:29:13 +0100592 lcm.start()
tierno22f4f9c2018-06-11 18:53:39 +0200593 except (LcmException, getopt.GetoptError) as e:
tierno275411e2018-05-16 14:33:32 +0200594 print(str(e), file=sys.stderr)
595 # usage()
596 exit(1)