44f2b2ed0c757553d47a8e21b043678ab7a6a0d1
[osm/LCM.git] / osm_lcm / lcm.py
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 ##
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
20 import asyncio
21 import yaml
22 import logging
23 import logging.handlers
24 import getopt
25 import sys
26 import ROclient
27 import ns
28 import vim_sdn
29 import netslice
30 from lcm_utils import versiontuple, LcmException, TaskRegistry, LcmExceptionExit
31
32 # from osm_lcm import version as lcm_version, version_date as lcm_version_date, ROclient
33 from osm_common import dbmemory, dbmongo, fslocal, msglocal, msgkafka
34 from osm_common import version as common_version
35 from osm_common.dbbase import DbException
36 from osm_common.fsbase import FsException
37 from osm_common.msgbase import MsgException
38 from os import environ, path
39 from n2vc import version as n2vc_version
40
41
42 __author__ = "Alfonso Tierno"
43 min_RO_version = [0, 6, 3]
44 min_n2vc_version = "0.0.2"
45 min_common_version = "0.1.11"
46 # uncomment if LCM is installed as library and installed, and get them from __init__.py
47 lcm_version = '0.1.32'
48 lcm_version_date = '2019-01-28'
49
50
51 class Lcm:
52
53 ping_interval_pace = 120 # how many time ping is send once is confirmed all is running
54 ping_interval_boot = 5 # how many time ping is sent when booting
55
56 def __init__(self, config_file, loop=None):
57 """
58 Init, Connect to database, filesystem storage, and messaging
59 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
60 :return: None
61 """
62
63 self.db = None
64 self.msg = None
65 self.fs = None
66 self.pings_not_received = 1
67 self.consecutive_errors = 0
68 self.first_start = False
69
70 # contains created tasks/futures to be able to cancel
71 self.lcm_tasks = TaskRegistry()
72 # logging
73 self.logger = logging.getLogger('lcm')
74 # load configuration
75 config = self.read_config_file(config_file)
76 self.config = config
77 self.ro_config = {
78 "endpoint_url": "http://{}:{}/openmano".format(config["RO"]["host"], config["RO"]["port"]),
79 "tenant": config.get("tenant", "osm"),
80 "logger_name": "lcm.ROclient",
81 "loglevel": "ERROR",
82 }
83
84 self.vca_config = config["VCA"]
85
86 self.loop = loop or asyncio.get_event_loop()
87
88 # logging
89 log_format_simple = "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
90 log_formatter_simple = logging.Formatter(log_format_simple, datefmt='%Y-%m-%dT%H:%M:%S')
91 config["database"]["logger_name"] = "lcm.db"
92 config["storage"]["logger_name"] = "lcm.fs"
93 config["message"]["logger_name"] = "lcm.msg"
94 if config["global"].get("logfile"):
95 file_handler = logging.handlers.RotatingFileHandler(config["global"]["logfile"],
96 maxBytes=100e6, backupCount=9, delay=0)
97 file_handler.setFormatter(log_formatter_simple)
98 self.logger.addHandler(file_handler)
99 if not config["global"].get("nologging"):
100 str_handler = logging.StreamHandler()
101 str_handler.setFormatter(log_formatter_simple)
102 self.logger.addHandler(str_handler)
103
104 if config["global"].get("loglevel"):
105 self.logger.setLevel(config["global"]["loglevel"])
106
107 # logging other modules
108 for k1, logname in {"message": "lcm.msg", "database": "lcm.db", "storage": "lcm.fs"}.items():
109 config[k1]["logger_name"] = logname
110 logger_module = logging.getLogger(logname)
111 if config[k1].get("logfile"):
112 file_handler = logging.handlers.RotatingFileHandler(config[k1]["logfile"],
113 maxBytes=100e6, backupCount=9, delay=0)
114 file_handler.setFormatter(log_formatter_simple)
115 logger_module.addHandler(file_handler)
116 if config[k1].get("loglevel"):
117 logger_module.setLevel(config[k1]["loglevel"])
118 self.logger.critical("starting osm/lcm version {} {}".format(lcm_version, lcm_version_date))
119
120 # check version of N2VC
121 # TODO enhance with int conversion or from distutils.version import LooseVersion
122 # or with list(map(int, version.split(".")))
123 if versiontuple(n2vc_version) < versiontuple(min_n2vc_version):
124 raise LcmException("Not compatible osm/N2VC version '{}'. Needed '{}' or higher".format(
125 n2vc_version, min_n2vc_version))
126 # check version of common
127 if versiontuple(common_version) < versiontuple(min_common_version):
128 raise LcmException("Not compatible osm/common version '{}'. Needed '{}' or higher".format(
129 common_version, min_common_version))
130
131 try:
132 # TODO check database version
133 if config["database"]["driver"] == "mongo":
134 self.db = dbmongo.DbMongo()
135 self.db.db_connect(config["database"])
136 elif config["database"]["driver"] == "memory":
137 self.db = dbmemory.DbMemory()
138 self.db.db_connect(config["database"])
139 else:
140 raise LcmException("Invalid configuration param '{}' at '[database]':'driver'".format(
141 config["database"]["driver"]))
142
143 if config["storage"]["driver"] == "local":
144 self.fs = fslocal.FsLocal()
145 self.fs.fs_connect(config["storage"])
146 else:
147 raise LcmException("Invalid configuration param '{}' at '[storage]':'driver'".format(
148 config["storage"]["driver"]))
149
150 config_message = config["message"].copy()
151 config_message["loop"] = self.loop
152 if config_message["driver"] == "local":
153 self.msg = msglocal.MsgLocal()
154 self.msg.connect(config_message)
155 elif config_message["driver"] == "kafka":
156 self.msg = msgkafka.MsgKafka()
157 self.msg.connect(config_message)
158 else:
159 raise LcmException("Invalid configuration param '{}' at '[message]':'driver'".format(
160 config["message"]["driver"]))
161 except (DbException, FsException, MsgException) as e:
162 self.logger.critical(str(e), exc_info=True)
163 raise LcmException(str(e))
164
165 self.ns = ns.NsLcm(self.db, self.msg, self.fs, self.lcm_tasks, self.ro_config, self.vca_config, self.loop)
166 self.netslice = netslice.NetsliceLcm(self.db, self.msg, self.fs, self.lcm_tasks, self.ro_config,
167 self.vca_config, self.loop)
168 self.vim = vim_sdn.VimLcm(self.db, self.msg, self.fs, self.lcm_tasks, self.ro_config, self.loop)
169 self.wim = vim_sdn.WimLcm(self.db, self.msg, self.fs, self.lcm_tasks, self.ro_config, self.loop)
170 self.sdn = vim_sdn.SdnLcm(self.db, self.msg, self.fs, self.lcm_tasks, self.ro_config, self.loop)
171
172 async def check_RO_version(self):
173 try:
174 RO = ROclient.ROClient(self.loop, **self.ro_config)
175 RO_version = await RO.get_version()
176 if RO_version < min_RO_version:
177 raise LcmException("Not compatible osm/RO version '{}.{}.{}'. Needed '{}.{}.{}' or higher".format(
178 *RO_version, *min_RO_version
179 ))
180 except ROclient.ROClientException as e:
181 error_text = "Error while conneting to osm/RO " + str(e)
182 self.logger.critical(error_text, exc_info=True)
183 raise LcmException(error_text)
184
185 async def test(self, param=None):
186 self.logger.debug("Starting/Ending test task: {}".format(param))
187
188 async def kafka_ping(self):
189 self.logger.debug("Task kafka_ping Enter")
190 consecutive_errors = 0
191 first_start = True
192 kafka_has_received = False
193 self.pings_not_received = 1
194 while True:
195 try:
196 await self.msg.aiowrite("admin", "ping", {"from": "lcm", "to": "lcm"}, self.loop)
197 # time between pings are low when it is not received and at starting
198 wait_time = self.ping_interval_boot if not kafka_has_received else self.ping_interval_pace
199 if not self.pings_not_received:
200 kafka_has_received = True
201 self.pings_not_received += 1
202 await asyncio.sleep(wait_time, loop=self.loop)
203 if self.pings_not_received > 10:
204 raise LcmException("It is not receiving pings from Kafka bus")
205 consecutive_errors = 0
206 first_start = False
207 except LcmException:
208 raise
209 except Exception as e:
210 # if not first_start is the first time after starting. So leave more time and wait
211 # to allow kafka starts
212 if consecutive_errors == 8 if not first_start else 30:
213 self.logger.error("Task kafka_read task exit error too many errors. Exception: {}".format(e))
214 raise
215 consecutive_errors += 1
216 self.logger.error("Task kafka_read retrying after Exception {}".format(e))
217 wait_time = 1 if not first_start else 5
218 await asyncio.sleep(wait_time, loop=self.loop)
219
220 def kafka_read_callback(self, topic, command, params):
221 order_id = 1
222
223 if topic != "admin" and command != "ping":
224 self.logger.debug("Task kafka_read receives {} {}: {}".format(topic, command, params))
225 self.consecutive_errors = 0
226 self.first_start = False
227 order_id += 1
228 if command == "exit":
229 raise LcmExceptionExit
230 elif command.startswith("#"):
231 return
232 elif command == "echo":
233 # just for test
234 print(params)
235 sys.stdout.flush()
236 return
237 elif command == "test":
238 asyncio.Task(self.test(params), loop=self.loop)
239 return
240
241 if topic == "admin":
242 if command == "ping" and params["to"] == "lcm" and params["from"] == "lcm":
243 self.pings_not_received = 0
244 return
245 elif topic == "ns":
246 if command == "instantiate":
247 # self.logger.debug("Deploying NS {}".format(nsr_id))
248 nslcmop = params
249 nslcmop_id = nslcmop["_id"]
250 nsr_id = nslcmop["nsInstanceId"]
251 task = asyncio.ensure_future(self.ns.instantiate(nsr_id, nslcmop_id))
252 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_instantiate", task)
253 return
254 elif command == "terminate":
255 # self.logger.debug("Deleting NS {}".format(nsr_id))
256 nslcmop = params
257 nslcmop_id = nslcmop["_id"]
258 nsr_id = nslcmop["nsInstanceId"]
259 self.lcm_tasks.cancel(topic, nsr_id)
260 task = asyncio.ensure_future(self.ns.terminate(nsr_id, nslcmop_id))
261 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_terminate", task)
262 return
263 elif command == "action":
264 # self.logger.debug("Update NS {}".format(nsr_id))
265 nslcmop = params
266 nslcmop_id = nslcmop["_id"]
267 nsr_id = nslcmop["nsInstanceId"]
268 task = asyncio.ensure_future(self.ns.action(nsr_id, nslcmop_id))
269 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_action", task)
270 return
271 elif command == "scale":
272 # self.logger.debug("Update NS {}".format(nsr_id))
273 nslcmop = params
274 nslcmop_id = nslcmop["_id"]
275 nsr_id = nslcmop["nsInstanceId"]
276 task = asyncio.ensure_future(self.ns.scale(nsr_id, nslcmop_id))
277 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_scale", task)
278 return
279 elif command == "show":
280 nsr_id = params
281 try:
282 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
283 print("nsr:\n _id={}\n operational-status: {}\n config-status: {}"
284 "\n detailed-status: {}\n deploy: {}\n tasks: {}"
285 "".format(nsr_id, db_nsr["operational-status"], db_nsr["config-status"],
286 db_nsr["detailed-status"],
287 db_nsr["_admin"]["deployed"], self.lcm_ns_tasks.get(nsr_id)))
288 except Exception as e:
289 print("nsr {} not found: {}".format(nsr_id, e))
290 sys.stdout.flush()
291 return
292 elif command == "deleted":
293 return # TODO cleaning of task just in case should be done
294 elif command in ("terminated", "instantiated", "scaled", "actioned"): # "scaled-cooldown-time"
295 return
296 elif topic == "nsi": # netslice LCM processes (instantiate, terminate, etc)
297 if command == "instantiate":
298 # self.logger.debug("Instantiating Network Slice {}".format(nsilcmop["netsliceInstanceId"]))
299 nsilcmop = params
300 nsilcmop_id = nsilcmop["_id"] # slice operation id
301 nsir_id = nsilcmop["netsliceInstanceId"] # slice record id
302 task = asyncio.ensure_future(self.netslice.instantiate(nsir_id, nsilcmop_id))
303 self.lcm_tasks.register("nsi", nsir_id, nsilcmop_id, "nsi_instantiate", task)
304 return
305 elif command == "terminate":
306 # self.logger.debug("Terminating Network Slice NS {}".format(nsilcmop["netsliceInstanceId"]))
307 nsilcmop = params
308 nsilcmop_id = nsilcmop["_id"] # slice operation id
309 nsir_id = nsilcmop["netsliceInstanceId"] # slice record id
310 self.lcm_tasks.cancel(topic, nsir_id)
311 task = asyncio.ensure_future(self.netslice.terminate(nsir_id, nsilcmop_id))
312 self.lcm_tasks.register("nsi", nsir_id, nsilcmop_id, "nsi_terminate", task)
313 return
314 elif command == "show":
315 nsir_id = params
316 try:
317 db_nsir = self.db.get_one("nsirs", {"_id": nsir_id})
318 print("nsir:\n _id={}\n operational-status: {}\n config-status: {}"
319 "\n detailed-status: {}\n deploy: {}\n tasks: {}"
320 "".format(nsir_id, db_nsir["operational-status"], db_nsir["config-status"],
321 db_nsir["detailed-status"],
322 db_nsir["_admin"]["deployed"], self.lcm_netslice_tasks.get(nsir_id)))
323 except Exception as e:
324 print("nsir {} not found: {}".format(nsir_id, e))
325 sys.stdout.flush()
326 return
327 elif command == "deleted":
328 return # TODO cleaning of task just in case should be done
329 elif command in ("terminated", "instantiated", "scaled", "actioned"): # "scaled-cooldown-time"
330 return
331 elif topic == "vim_account":
332 vim_id = params["_id"]
333 if command == "create":
334 task = asyncio.ensure_future(self.vim.create(params, order_id))
335 self.lcm_tasks.register("vim_account", vim_id, order_id, "vim_create", task)
336 return
337 elif command == "delete":
338 self.lcm_tasks.cancel(topic, vim_id)
339 task = asyncio.ensure_future(self.vim.delete(vim_id, order_id))
340 self.lcm_tasks.register("vim_account", vim_id, order_id, "vim_delete", task)
341 return
342 elif command == "show":
343 print("not implemented show with vim_account")
344 sys.stdout.flush()
345 return
346 elif command == "edit":
347 task = asyncio.ensure_future(self.vim.edit(params, order_id))
348 self.lcm_tasks.register("vim_account", vim_id, order_id, "vim_edit", task)
349 return
350 elif topic == "wim_account":
351 wim_id = params["_id"]
352 if command == "create":
353 task = asyncio.ensure_future(self.wim.create(params, order_id))
354 self.lcm_tasks.register("wim_account", wim_id, order_id, "wim_create", task)
355 return
356 elif command == "delete":
357 self.lcm_tasks.cancel(topic, wim_id)
358 task = asyncio.ensure_future(self.wim.delete(wim_id, order_id))
359 self.lcm_tasks.register("wim_account", wim_id, order_id, "wim_delete", task)
360 return
361 elif command == "show":
362 print("not implemented show with wim_account")
363 sys.stdout.flush()
364 return
365 elif command == "edit":
366 task = asyncio.ensure_future(self.wim.edit(params, order_id))
367 self.lcm_tasks.register("wim_account", wim_id, order_id, "wim_edit", task)
368 return
369 elif topic == "sdn":
370 _sdn_id = params["_id"]
371 if command == "create":
372 task = asyncio.ensure_future(self.sdn.create(params, order_id))
373 self.lcm_tasks.register("sdn", _sdn_id, order_id, "sdn_create", task)
374 return
375 elif command == "delete":
376 self.lcm_tasks.cancel(topic, _sdn_id)
377 task = asyncio.ensure_future(self.sdn.delete(_sdn_id, order_id))
378 self.lcm_tasks.register("sdn", _sdn_id, order_id, "sdn_delete", task)
379 return
380 elif command == "edit":
381 task = asyncio.ensure_future(self.sdn.edit(params, order_id))
382 self.lcm_tasks.register("sdn", _sdn_id, order_id, "sdn_edit", task)
383 return
384 self.logger.critical("unknown topic {} and command '{}'".format(topic, command))
385
386 async def kafka_read(self):
387 self.logger.debug("Task kafka_read Enter")
388 # future = asyncio.Future()
389 self.consecutive_errors = 0
390 self.first_start = True
391 while self.consecutive_errors < 10:
392 try:
393 topics = ("admin", "ns", "vim_account", "wim_account", "sdn", "nsi")
394 await self.msg.aioread(topics, self.loop, self.kafka_read_callback)
395
396 except LcmExceptionExit:
397 self.logger.debug("Bye!")
398 break
399 except Exception as e:
400 # if not first_start is the first time after starting. So leave more time and wait
401 # to allow kafka starts
402 if self.consecutive_errors == 8 if not self.first_start else 30:
403 self.logger.error("Task kafka_read task exit error too many errors. Exception: {}".format(e))
404 raise
405 self.consecutive_errors += 1
406 self.logger.error("Task kafka_read retrying after Exception {}".format(e))
407 wait_time = 2 if not self.first_start else 5
408 await asyncio.sleep(wait_time, loop=self.loop)
409
410 # self.logger.debug("Task kafka_read terminating")
411 self.logger.debug("Task kafka_read exit")
412
413 def health_check(self):
414
415 global exit_code
416 task = None
417 exit_code = 1
418
419 def health_check_callback(topic, command, params):
420 global exit_code
421 print("receiving callback {} {} {}".format(topic, command, params))
422 if topic == "admin" and command == "ping" and params["to"] == "lcm" and params["from"] == "lcm":
423 # print("received LCM ping")
424 exit_code = 0
425 task.cancel()
426
427 try:
428 task = asyncio.ensure_future(self.msg.aioread(("admin",), self.loop, health_check_callback))
429 self.loop.run_until_complete(task)
430 except Exception:
431 pass
432 exit(exit_code)
433
434 def start(self):
435
436 # check RO version
437 self.loop.run_until_complete(self.check_RO_version())
438
439 self.loop.run_until_complete(asyncio.gather(
440 self.kafka_read(),
441 self.kafka_ping()
442 ))
443 # TODO
444 # self.logger.debug("Terminating cancelling creation tasks")
445 # self.lcm_tasks.cancel("ALL", "create")
446 # timeout = 200
447 # while self.is_pending_tasks():
448 # self.logger.debug("Task kafka_read terminating. Waiting for tasks termination")
449 # await asyncio.sleep(2, loop=self.loop)
450 # timeout -= 2
451 # if not timeout:
452 # self.lcm_tasks.cancel("ALL", "ALL")
453 self.loop.close()
454 self.loop = None
455 if self.db:
456 self.db.db_disconnect()
457 if self.msg:
458 self.msg.disconnect()
459 if self.fs:
460 self.fs.fs_disconnect()
461
462 def read_config_file(self, config_file):
463 # TODO make a [ini] + yaml inside parser
464 # the configparser library is not suitable, because it does not admit comments at the end of line,
465 # and not parse integer or boolean
466 try:
467 with open(config_file) as f:
468 conf = yaml.load(f)
469 for k, v in environ.items():
470 if not k.startswith("OSMLCM_"):
471 continue
472 k_items = k.lower().split("_")
473 if len(k_items) < 3:
474 continue
475 if k_items[1] in ("ro", "vca"):
476 # put in capital letter
477 k_items[1] = k_items[1].upper()
478 c = conf
479 try:
480 for k_item in k_items[1:-1]:
481 c = c[k_item]
482 if k_items[-1] == "port":
483 c[k_items[-1]] = int(v)
484 else:
485 c[k_items[-1]] = v
486 except Exception as e:
487 self.logger.warn("skipping environ '{}' on exception '{}'".format(k, e))
488
489 return conf
490 except Exception as e:
491 self.logger.critical("At config file '{}': {}".format(config_file, e))
492 exit(1)
493
494
495 def usage():
496 print("""Usage: {} [options]
497 -c|--config [configuration_file]: loads the configuration file (default: ./nbi.cfg)
498 --health-check: do not run lcm, but inspect kafka bus to determine if lcm is healthy
499 -h|--help: shows this help
500 """.format(sys.argv[0]))
501 # --log-socket-host HOST: send logs to this host")
502 # --log-socket-port PORT: send logs using this port (default: 9022)")
503
504
505 if __name__ == '__main__':
506 try:
507 # load parameters and configuration
508 opts, args = getopt.getopt(sys.argv[1:], "hc:", ["config=", "help", "health-check"])
509 # TODO add "log-socket-host=", "log-socket-port=", "log-file="
510 config_file = None
511 health_check = None
512 for o, a in opts:
513 if o in ("-h", "--help"):
514 usage()
515 sys.exit()
516 elif o in ("-c", "--config"):
517 config_file = a
518 elif o == "--health-check":
519 health_check = True
520 # elif o == "--log-socket-port":
521 # log_socket_port = a
522 # elif o == "--log-socket-host":
523 # log_socket_host = a
524 # elif o == "--log-file":
525 # log_file = a
526 else:
527 assert False, "Unhandled option"
528 if config_file:
529 if not path.isfile(config_file):
530 print("configuration file '{}' not exist".format(config_file), file=sys.stderr)
531 exit(1)
532 else:
533 for config_file in (__file__[:__file__.rfind(".")] + ".cfg", "./lcm.cfg", "/etc/osm/lcm.cfg"):
534 if path.isfile(config_file):
535 break
536 else:
537 print("No configuration file 'lcm.cfg' found neither at local folder nor at /etc/osm/", file=sys.stderr)
538 exit(1)
539 lcm = Lcm(config_file)
540 if health_check:
541 lcm.health_check()
542 else:
543 lcm.start()
544 except (LcmException, getopt.GetoptError) as e:
545 print(str(e), file=sys.stderr)
546 # usage()
547 exit(1)