blob: 89d877b6af49af79bd13613318b31b0b3bf23748 [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
quilesj7e13aeb2019-10-08 13:34:55 +020022import pdb
23
k4.rahul44306072023-05-05 14:24:31 +053024import os
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
Gabriel Cuba4c0e6802023-10-09 13:22:38 -050031from random import SystemRandom
tierno59d22d22018-09-25 18:10:19 +020032
rshri932105f2024-07-05 15:11:55 +000033from osm_lcm import ns, vim_sdn, netslice, k8s
tierno69f0d382020-05-07 13:08:09 +000034from osm_lcm.ng_ro import NgRoException, NgRoClient
35from osm_lcm.ROclient import ROClient, ROClientException
quilesj7e13aeb2019-10-08 13:34:55 +020036
tierno94f06112020-02-11 12:38:19 +000037from time import time
tierno8069ce52019-08-28 15:34:33 +000038from osm_lcm.lcm_utils import versiontuple, LcmException, TaskRegistry, LcmExceptionExit
tiernoa4dea5a2020-01-05 16:29:30 +000039from osm_lcm import version as lcm_version, version_date as lcm_version_date
tierno8069ce52019-08-28 15:34:33 +000040
bravof922c4172020-11-24 21:21:43 -030041from osm_common import msglocal, msgkafka
tierno98768132018-09-11 12:07:21 +020042from 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
bravof922c4172020-11-24 21:21:43 -030046from osm_lcm.data_utils.database.database import Database
47from osm_lcm.data_utils.filesystem.filesystem import Filesystem
Luis Vegaa27dc532022-11-11 20:10:49 +000048from osm_lcm.data_utils.lcm_config import LcmCfg
garciadeblas96b94f52024-07-08 16:18:21 +020049from osm_lcm.data_utils.list_utils import find_in_list
aticig56b86c22022-06-29 10:43:05 +030050from osm_lcm.lcm_hc import get_health_check_file
Gabriel Cuba4c0e6802023-10-09 13:22:38 -050051from os import path, getenv
tierno59d22d22018-09-25 18:10:19 +020052from n2vc import version as n2vc_version
bravof922c4172020-11-24 21:21:43 -030053import traceback
tiernoc0e42e22018-05-11 11:36:10 +020054
Gabriel Cuba4c0e6802023-10-09 13:22:38 -050055if getenv("OSMLCM_PDB_DEBUG", None) is not None:
quilesj7e13aeb2019-10-08 13:34:55 +020056 pdb.set_trace()
57
tiernoc0e42e22018-05-11 11:36:10 +020058
tierno275411e2018-05-16 14:33:32 +020059__author__ = "Alfonso Tierno"
tiernoe64f7fb2019-09-11 08:55:52 +000060min_RO_version = "6.0.2"
tierno6e9d2eb2018-09-12 17:47:18 +020061min_n2vc_version = "0.0.2"
quilesj7e13aeb2019-10-08 13:34:55 +020062
tierno16427352019-04-22 11:37:36 +000063min_common_version = "0.1.19"
tierno275411e2018-05-16 14:33:32 +020064
65
tiernoc0e42e22018-05-11 11:36:10 +020066class Lcm:
garciadeblas96b94f52024-07-08 16:18:21 +020067 profile_collection_mapping = {
68 "infra_controller_profiles": "k8sinfra_controller",
69 "infra_config_profiles": "k8sinfra_config",
70 "resource_profiles": "k8sresource",
71 "app_profiles": "k8sapp",
72 }
73
garciadeblas5697b8b2021-03-24 09:17:02 +010074 ping_interval_pace = (
75 120 # how many time ping is send once is confirmed all is running
76 )
77 ping_interval_boot = 5 # how many time ping is sent when booting
Luis Vegaa27dc532022-11-11 20:10:49 +000078
79 main_config = LcmCfg()
tiernoa9843d82018-10-24 10:44:20 +020080
Gabriel Cubae7898982023-05-11 01:57:21 -050081 def __init__(self, config_file):
tiernoc0e42e22018-05-11 11:36:10 +020082 """
83 Init, Connect to database, filesystem storage, and messaging
84 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
85 :return: None
86 """
tiernoc0e42e22018-05-11 11:36:10 +020087 self.db = None
88 self.msg = None
tierno16427352019-04-22 11:37:36 +000089 self.msg_admin = None
tiernoc0e42e22018-05-11 11:36:10 +020090 self.fs = None
91 self.pings_not_received = 1
tiernoc2564fe2019-01-28 16:18:56 +000092 self.consecutive_errors = 0
93 self.first_start = False
tiernoc0e42e22018-05-11 11:36:10 +020094
tiernoc0e42e22018-05-11 11:36:10 +020095 # logging
garciadeblas5697b8b2021-03-24 09:17:02 +010096 self.logger = logging.getLogger("lcm")
tierno16427352019-04-22 11:37:36 +000097 # get id
98 self.worker_id = self.get_process_id()
tiernoc0e42e22018-05-11 11:36:10 +020099 # load configuration
100 config = self.read_config_file(config_file)
Luis Vegaa27dc532022-11-11 20:10:49 +0000101 self.main_config.set_from_dict(config)
102 self.main_config.transform()
103 self.main_config.load_from_env()
104 self.logger.critical("Loaded configuration:" + str(self.main_config.to_dict()))
105 # TODO: check if lcm_hc.py is necessary
106 self.health_check_file = get_health_check_file(self.main_config.to_dict())
garciadeblas5697b8b2021-03-24 09:17:02 +0100107 self.ns = (
108 self.netslice
109 ) = (
110 self.vim
rshri932105f2024-07-05 15:11:55 +0000111 ) = (
112 self.wim
113 ) = (
114 self.sdn
115 ) = (
116 self.k8scluster
117 ) = (
118 self.vca
119 ) = (
120 self.k8srepo
121 ) = (
122 self.cluster
123 ) = (
124 self.k8s_app
125 ) = self.k8s_resource = self.k8s_infra_controller = self.k8s_infra_config = None
tiernoc0e42e22018-05-11 11:36:10 +0200126
127 # logging
garciadeblas5697b8b2021-03-24 09:17:02 +0100128 log_format_simple = (
129 "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
130 )
131 log_formatter_simple = logging.Formatter(
132 log_format_simple, datefmt="%Y-%m-%dT%H:%M:%S"
133 )
Luis Vegaa27dc532022-11-11 20:10:49 +0000134 if self.main_config.globalConfig.logfile:
garciadeblas5697b8b2021-03-24 09:17:02 +0100135 file_handler = logging.handlers.RotatingFileHandler(
Luis Vegaa27dc532022-11-11 20:10:49 +0000136 self.main_config.globalConfig.logfile,
137 maxBytes=100e6,
138 backupCount=9,
139 delay=0,
garciadeblas5697b8b2021-03-24 09:17:02 +0100140 )
tiernoc0e42e22018-05-11 11:36:10 +0200141 file_handler.setFormatter(log_formatter_simple)
142 self.logger.addHandler(file_handler)
Luis Vegaa27dc532022-11-11 20:10:49 +0000143 if not self.main_config.globalConfig.to_dict()["nologging"]:
tiernoc0e42e22018-05-11 11:36:10 +0200144 str_handler = logging.StreamHandler()
145 str_handler.setFormatter(log_formatter_simple)
146 self.logger.addHandler(str_handler)
147
Luis Vegaa27dc532022-11-11 20:10:49 +0000148 if self.main_config.globalConfig.to_dict()["loglevel"]:
149 self.logger.setLevel(self.main_config.globalConfig.loglevel)
tiernoc0e42e22018-05-11 11:36:10 +0200150
151 # logging other modules
garciadeblas4623e982024-09-11 14:28:38 +0200152 for logger in ("message", "database", "storage", "tsdb", "gitops"):
Luis Vegaa27dc532022-11-11 20:10:49 +0000153 logger_config = self.main_config.to_dict()[logger]
154 logger_module = logging.getLogger(logger_config["logger_name"])
155 if logger_config["logfile"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100156 file_handler = logging.handlers.RotatingFileHandler(
Luis Vegaa27dc532022-11-11 20:10:49 +0000157 logger_config["logfile"], maxBytes=100e6, backupCount=9, delay=0
garciadeblas5697b8b2021-03-24 09:17:02 +0100158 )
tiernoc0e42e22018-05-11 11:36:10 +0200159 file_handler.setFormatter(log_formatter_simple)
160 logger_module.addHandler(file_handler)
Luis Vegaa27dc532022-11-11 20:10:49 +0000161 if logger_config["loglevel"]:
162 logger_module.setLevel(logger_config["loglevel"])
garciadeblas5697b8b2021-03-24 09:17:02 +0100163 self.logger.critical(
164 "starting osm/lcm version {} {}".format(lcm_version, lcm_version_date)
165 )
tierno59d22d22018-09-25 18:10:19 +0200166
tiernoc0e42e22018-05-11 11:36:10 +0200167 # check version of N2VC
168 # TODO enhance with int conversion or from distutils.version import LooseVersion
169 # or with list(map(int, version.split(".")))
tierno59d22d22018-09-25 18:10:19 +0200170 if versiontuple(n2vc_version) < versiontuple(min_n2vc_version):
garciadeblas5697b8b2021-03-24 09:17:02 +0100171 raise LcmException(
172 "Not compatible osm/N2VC version '{}'. Needed '{}' or higher".format(
173 n2vc_version, min_n2vc_version
174 )
175 )
tierno59d22d22018-09-25 18:10:19 +0200176 # check version of common
tierno27246d82018-09-27 15:59:09 +0200177 if versiontuple(common_version) < versiontuple(min_common_version):
garciadeblas5697b8b2021-03-24 09:17:02 +0100178 raise LcmException(
179 "Not compatible osm/common version '{}'. Needed '{}' or higher".format(
180 common_version, min_common_version
181 )
182 )
tierno22f4f9c2018-06-11 18:53:39 +0200183
tiernoc0e42e22018-05-11 11:36:10 +0200184 try:
Luis Vegaa27dc532022-11-11 20:10:49 +0000185 self.db = Database(self.main_config.to_dict()).instance.db
tiernoc0e42e22018-05-11 11:36:10 +0200186
Luis Vegaa27dc532022-11-11 20:10:49 +0000187 self.fs = Filesystem(self.main_config.to_dict()).instance.fs
sousaedu40365e82021-07-26 15:24:21 +0200188 self.fs.sync()
tiernoc0e42e22018-05-11 11:36:10 +0200189
quilesj7e13aeb2019-10-08 13:34:55 +0200190 # copy message configuration in order to remove 'group_id' for msg_admin
Luis Vegaa27dc532022-11-11 20:10:49 +0000191 config_message = self.main_config.message.to_dict()
Gabriel Cubae7898982023-05-11 01:57:21 -0500192 config_message["loop"] = asyncio.get_event_loop()
tiernoc2564fe2019-01-28 16:18:56 +0000193 if config_message["driver"] == "local":
tiernoc0e42e22018-05-11 11:36:10 +0200194 self.msg = msglocal.MsgLocal()
tiernoc2564fe2019-01-28 16:18:56 +0000195 self.msg.connect(config_message)
tierno16427352019-04-22 11:37:36 +0000196 self.msg_admin = msglocal.MsgLocal()
197 config_message.pop("group_id", None)
198 self.msg_admin.connect(config_message)
tiernoc2564fe2019-01-28 16:18:56 +0000199 elif config_message["driver"] == "kafka":
tiernoc0e42e22018-05-11 11:36:10 +0200200 self.msg = msgkafka.MsgKafka()
tiernoc2564fe2019-01-28 16:18:56 +0000201 self.msg.connect(config_message)
tierno16427352019-04-22 11:37:36 +0000202 self.msg_admin = msgkafka.MsgKafka()
203 config_message.pop("group_id", None)
204 self.msg_admin.connect(config_message)
tiernoc0e42e22018-05-11 11:36:10 +0200205 else:
garciadeblas5697b8b2021-03-24 09:17:02 +0100206 raise LcmException(
207 "Invalid configuration param '{}' at '[message]':'driver'".format(
Luis Vegaa27dc532022-11-11 20:10:49 +0000208 self.main_config.message.driver
garciadeblas5697b8b2021-03-24 09:17:02 +0100209 )
210 )
tiernoc0e42e22018-05-11 11:36:10 +0200211 except (DbException, FsException, MsgException) as e:
212 self.logger.critical(str(e), exc_info=True)
213 raise LcmException(str(e))
214
kuused124bfe2019-06-18 12:09:24 +0200215 # contains created tasks/futures to be able to cancel
bravof922c4172020-11-24 21:21:43 -0300216 self.lcm_tasks = TaskRegistry(self.worker_id, self.logger)
kuused124bfe2019-06-18 12:09:24 +0200217
rshri932105f2024-07-05 15:11:55 +0000218 self.logger.info(
219 "Worker_id: {} main_config: {} lcm tasks: {}".format(
220 self.worker_id, self.main_config, self.lcm_tasks
221 )
222 )
223
tierno22f4f9c2018-06-11 18:53:39 +0200224 async def check_RO_version(self):
tiernoe64f7fb2019-09-11 08:55:52 +0000225 tries = 14
226 last_error = None
227 while True:
Luis Vegaa27dc532022-11-11 20:10:49 +0000228 ro_uri = self.main_config.RO.uri
229 if not ro_uri:
230 ro_uri = ""
tiernoe64f7fb2019-09-11 08:55:52 +0000231 try:
tierno2357f4e2020-10-19 16:38:59 +0000232 # try new RO, if fail old RO
233 try:
Luis Vegaa27dc532022-11-11 20:10:49 +0000234 self.main_config.RO.uri = ro_uri + "ro"
Gabriel Cubae7898982023-05-11 01:57:21 -0500235 ro_server = NgRoClient(**self.main_config.RO.to_dict())
tierno2357f4e2020-10-19 16:38:59 +0000236 ro_version = await ro_server.get_version()
Luis Vegaa27dc532022-11-11 20:10:49 +0000237 self.main_config.RO.ng = True
tierno2357f4e2020-10-19 16:38:59 +0000238 except Exception:
Luis Vegaa27dc532022-11-11 20:10:49 +0000239 self.main_config.RO.uri = ro_uri + "openmano"
Gabriel Cubae7898982023-05-11 01:57:21 -0500240 ro_server = ROClient(**self.main_config.RO.to_dict())
tierno2357f4e2020-10-19 16:38:59 +0000241 ro_version = await ro_server.get_version()
Luis Vegaa27dc532022-11-11 20:10:49 +0000242 self.main_config.RO.ng = False
tiernoe64f7fb2019-09-11 08:55:52 +0000243 if versiontuple(ro_version) < versiontuple(min_RO_version):
garciadeblas5697b8b2021-03-24 09:17:02 +0100244 raise LcmException(
245 "Not compatible osm/RO version '{}'. Needed '{}' or higher".format(
246 ro_version, min_RO_version
247 )
248 )
249 self.logger.info(
250 "Connected to RO version {} new-generation version {}".format(
Luis Vegaa27dc532022-11-11 20:10:49 +0000251 ro_version, self.main_config.RO.ng
garciadeblas5697b8b2021-03-24 09:17:02 +0100252 )
253 )
tiernoe64f7fb2019-09-11 08:55:52 +0000254 return
tierno69f0d382020-05-07 13:08:09 +0000255 except (ROClientException, NgRoException) as e:
Luis Vegaa27dc532022-11-11 20:10:49 +0000256 self.main_config.RO.uri = ro_uri
tiernoe64f7fb2019-09-11 08:55:52 +0000257 tries -= 1
bravof922c4172020-11-24 21:21:43 -0300258 traceback.print_tb(e.__traceback__)
garciadeblas5697b8b2021-03-24 09:17:02 +0100259 error_text = "Error while connecting to RO on {}: {}".format(
Luis Vegaa27dc532022-11-11 20:10:49 +0000260 self.main_config.RO.uri, e
garciadeblas5697b8b2021-03-24 09:17:02 +0100261 )
tiernoe64f7fb2019-09-11 08:55:52 +0000262 if tries <= 0:
263 self.logger.critical(error_text)
264 raise LcmException(error_text)
265 if last_error != error_text:
266 last_error = error_text
garciadeblas5697b8b2021-03-24 09:17:02 +0100267 self.logger.error(
268 error_text + ". Waiting until {} seconds".format(5 * tries)
269 )
tiernoe64f7fb2019-09-11 08:55:52 +0000270 await asyncio.sleep(5)
tierno22f4f9c2018-06-11 18:53:39 +0200271
tiernoc0e42e22018-05-11 11:36:10 +0200272 async def test(self, param=None):
273 self.logger.debug("Starting/Ending test task: {}".format(param))
274
tiernoc0e42e22018-05-11 11:36:10 +0200275 async def kafka_ping(self):
276 self.logger.debug("Task kafka_ping Enter")
277 consecutive_errors = 0
278 first_start = True
279 kafka_has_received = False
280 self.pings_not_received = 1
281 while True:
282 try:
tierno16427352019-04-22 11:37:36 +0000283 await self.msg_admin.aiowrite(
garciadeblas5697b8b2021-03-24 09:17:02 +0100284 "admin",
285 "ping",
286 {
287 "from": "lcm",
288 "to": "lcm",
289 "worker_id": self.worker_id,
290 "version": lcm_version,
291 },
garciadeblas5697b8b2021-03-24 09:17:02 +0100292 )
tiernoc0e42e22018-05-11 11:36:10 +0200293 # time between pings are low when it is not received and at starting
garciadeblas5697b8b2021-03-24 09:17:02 +0100294 wait_time = (
295 self.ping_interval_boot
296 if not kafka_has_received
297 else self.ping_interval_pace
298 )
tiernoc0e42e22018-05-11 11:36:10 +0200299 if not self.pings_not_received:
300 kafka_has_received = True
301 self.pings_not_received += 1
Gabriel Cubae7898982023-05-11 01:57:21 -0500302 await asyncio.sleep(wait_time)
tiernoc0e42e22018-05-11 11:36:10 +0200303 if self.pings_not_received > 10:
304 raise LcmException("It is not receiving pings from Kafka bus")
305 consecutive_errors = 0
306 first_start = False
307 except LcmException:
308 raise
309 except Exception as e:
310 # if not first_start is the first time after starting. So leave more time and wait
311 # to allow kafka starts
312 if consecutive_errors == 8 if not first_start else 30:
garciadeblas5697b8b2021-03-24 09:17:02 +0100313 self.logger.error(
314 "Task kafka_read task exit error too many errors. Exception: {}".format(
315 e
316 )
317 )
tiernoc0e42e22018-05-11 11:36:10 +0200318 raise
319 consecutive_errors += 1
garciadeblas5697b8b2021-03-24 09:17:02 +0100320 self.logger.error(
321 "Task kafka_read retrying after Exception {}".format(e)
322 )
tierno16427352019-04-22 11:37:36 +0000323 wait_time = 2 if not first_start else 5
Gabriel Cubae7898982023-05-11 01:57:21 -0500324 await asyncio.sleep(wait_time)
tiernoc0e42e22018-05-11 11:36:10 +0200325
garciadeblas96b94f52024-07-08 16:18:21 +0200326 def get_operation_params(self, item, operation_id):
327 operation_history = item.get("operationHistory", [])
328 operation = find_in_list(
329 operation_history, lambda op: op["op_id"] == operation_id
330 )
331 return operation.get("operationParams", {})
332
Gabriel Cubab6049d32023-10-30 13:44:49 -0500333 async def kafka_read_callback(self, topic, command, params):
gcalvinoed7f6d42018-12-14 14:44:56 +0100334 order_id = 1
garciadeblas95607052024-09-23 10:12:22 +0200335 self.logger.debug(
rshri932105f2024-07-05 15:11:55 +0000336 "Topic: {} command: {} params: {} order ID: {}".format(
337 topic, command, params, order_id
338 )
339 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100340 if topic != "admin" and command != "ping":
garciadeblas95607052024-09-23 10:12:22 +0200341 self.logger.info(
garciadeblas5697b8b2021-03-24 09:17:02 +0100342 "Task kafka_read receives {} {}: {}".format(topic, command, params)
343 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100344 self.consecutive_errors = 0
345 self.first_start = False
346 order_id += 1
garciadeblas95607052024-09-23 10:12:22 +0200347 self.logger.debug(
rshri932105f2024-07-05 15:11:55 +0000348 "Consecutive error: {} First start: {}".format(
349 self.consecutive_errors, self.first_start
350 )
351 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100352 if command == "exit":
353 raise LcmExceptionExit
354 elif command.startswith("#"):
355 return
356 elif command == "echo":
357 # just for test
358 print(params)
359 sys.stdout.flush()
360 return
361 elif command == "test":
Gabriel Cubae7898982023-05-11 01:57:21 -0500362 asyncio.Task(self.test(params))
gcalvinoed7f6d42018-12-14 14:44:56 +0100363 return
364
365 if topic == "admin":
366 if command == "ping" and params["to"] == "lcm" and params["from"] == "lcm":
tierno16427352019-04-22 11:37:36 +0000367 if params.get("worker_id") != self.worker_id:
368 return
gcalvinoed7f6d42018-12-14 14:44:56 +0100369 self.pings_not_received = 0
tierno3e359b12019-02-03 02:29:13 +0100370 try:
aticig56b86c22022-06-29 10:43:05 +0300371 with open(self.health_check_file, "w") as f:
tierno3e359b12019-02-03 02:29:13 +0100372 f.write(str(time()))
373 except Exception as e:
garciadeblas5697b8b2021-03-24 09:17:02 +0100374 self.logger.error(
375 "Cannot write into '{}' for healthcheck: {}".format(
aticig56b86c22022-06-29 10:43:05 +0300376 self.health_check_file, e
garciadeblas5697b8b2021-03-24 09:17:02 +0100377 )
378 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100379 return
Gabriel Cubab6049d32023-10-30 13:44:49 -0500380 elif topic == "nslcmops":
381 if command == "cancel":
382 nslcmop_id = params["_id"]
383 self.logger.debug("Cancelling nslcmop {}".format(nslcmop_id))
384 nsr_id = params["nsInstanceId"]
385 # cancel the tasks and wait
386 for task in self.lcm_tasks.cancel("ns", nsr_id, nslcmop_id):
387 try:
388 await task
389 self.logger.debug(
390 "Cancelled task ended {},{},{}".format(
391 nsr_id, nslcmop_id, task
392 )
393 )
394 except asyncio.CancelledError:
395 self.logger.debug(
396 "Task already cancelled and finished {},{},{}".format(
397 nsr_id, nslcmop_id, task
398 )
399 )
400 # update DB
401 q_filter = {"_id": nslcmop_id}
402 update_dict = {
403 "operationState": "FAILED_TEMP",
404 "isCancelPending": False,
405 }
406 unset_dict = {
407 "cancelMode": None,
408 }
409 self.db.set_one(
410 "nslcmops",
411 q_filter=q_filter,
412 update_dict=update_dict,
413 fail_on_empty=False,
414 unset=unset_dict,
415 )
416 self.logger.debug("LCM task cancelled {},{}".format(nsr_id, nslcmop_id))
417 return
magnussonle9198bb2020-01-21 13:00:51 +0100418 elif topic == "pla":
419 if command == "placement":
420 self.ns.update_nsrs_with_pla_result(params)
421 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100422 elif topic == "k8scluster":
423 if command == "create" or command == "created":
424 k8scluster_id = params.get("_id")
425 task = asyncio.ensure_future(self.k8scluster.create(params, order_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100426 self.lcm_tasks.register(
427 "k8scluster", k8scluster_id, order_id, "k8scluster_create", task
428 )
calvinosanch9f9c6f22019-11-04 13:37:39 +0100429 return
dariofaccin8bbeeb02023-01-23 18:13:27 +0100430 elif command == "edit" or command == "edited":
431 k8scluster_id = params.get("_id")
432 task = asyncio.ensure_future(self.k8scluster.edit(params, order_id))
433 self.lcm_tasks.register(
434 "k8scluster", k8scluster_id, order_id, "k8scluster_edit", task
435 )
436 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100437 elif command == "delete" or command == "deleted":
438 k8scluster_id = params.get("_id")
439 task = asyncio.ensure_future(self.k8scluster.delete(params, order_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100440 self.lcm_tasks.register(
441 "k8scluster", k8scluster_id, order_id, "k8scluster_delete", task
442 )
calvinosanch9f9c6f22019-11-04 13:37:39 +0100443 return
David Garciac1fe90a2021-03-31 19:12:02 +0200444 elif topic == "vca":
445 if command == "create" or command == "created":
446 vca_id = params.get("_id")
447 task = asyncio.ensure_future(self.vca.create(params, order_id))
448 self.lcm_tasks.register("vca", vca_id, order_id, "vca_create", task)
449 return
Dario Faccin8e53c6d2023-01-10 10:38:41 +0000450 elif command == "edit" or command == "edited":
451 vca_id = params.get("_id")
452 task = asyncio.ensure_future(self.vca.edit(params, order_id))
453 self.lcm_tasks.register("vca", vca_id, order_id, "vca_edit", task)
454 return
David Garciac1fe90a2021-03-31 19:12:02 +0200455 elif command == "delete" or command == "deleted":
456 vca_id = params.get("_id")
457 task = asyncio.ensure_future(self.vca.delete(params, order_id))
458 self.lcm_tasks.register("vca", vca_id, order_id, "vca_delete", task)
459 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100460 elif topic == "k8srepo":
461 if command == "create" or command == "created":
462 k8srepo_id = params.get("_id")
463 self.logger.debug("k8srepo_id = {}".format(k8srepo_id))
464 task = asyncio.ensure_future(self.k8srepo.create(params, order_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100465 self.lcm_tasks.register(
466 "k8srepo", k8srepo_id, order_id, "k8srepo_create", task
467 )
calvinosanch9f9c6f22019-11-04 13:37:39 +0100468 return
469 elif command == "delete" or command == "deleted":
470 k8srepo_id = params.get("_id")
471 task = asyncio.ensure_future(self.k8srepo.delete(params, order_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100472 self.lcm_tasks.register(
473 "k8srepo", k8srepo_id, order_id, "k8srepo_delete", task
474 )
calvinosanch9f9c6f22019-11-04 13:37:39 +0100475 return
gcalvinoed7f6d42018-12-14 14:44:56 +0100476 elif topic == "ns":
tierno307425f2020-01-26 23:35:59 +0000477 if command == "instantiate":
gcalvinoed7f6d42018-12-14 14:44:56 +0100478 # self.logger.debug("Deploying NS {}".format(nsr_id))
rshri932105f2024-07-05 15:11:55 +0000479 self.logger.info("NS instantiate")
gcalvinoed7f6d42018-12-14 14:44:56 +0100480 nslcmop = params
481 nslcmop_id = nslcmop["_id"]
482 nsr_id = nslcmop["nsInstanceId"]
rshri932105f2024-07-05 15:11:55 +0000483 self.logger.info(
484 "NsLCMOP: {} NsLCMOP_ID:{} nsr_id: {}".format(
485 nslcmop, nslcmop_id, nsr_id
486 )
487 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100488 task = asyncio.ensure_future(self.ns.instantiate(nsr_id, nslcmop_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100489 self.lcm_tasks.register(
490 "ns", nsr_id, nslcmop_id, "ns_instantiate", task
491 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100492 return
tierno307425f2020-01-26 23:35:59 +0000493 elif command == "terminate":
gcalvinoed7f6d42018-12-14 14:44:56 +0100494 # self.logger.debug("Deleting NS {}".format(nsr_id))
495 nslcmop = params
496 nslcmop_id = nslcmop["_id"]
497 nsr_id = nslcmop["nsInstanceId"]
498 self.lcm_tasks.cancel(topic, nsr_id)
499 task = asyncio.ensure_future(self.ns.terminate(nsr_id, nslcmop_id))
500 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_terminate", task)
501 return
ksaikiranr3fde2c72021-03-15 10:39:06 +0530502 elif command == "vca_status_refresh":
503 nslcmop = params
504 nslcmop_id = nslcmop["_id"]
505 nsr_id = nslcmop["nsInstanceId"]
garciadeblas5697b8b2021-03-24 09:17:02 +0100506 task = asyncio.ensure_future(
507 self.ns.vca_status_refresh(nsr_id, nslcmop_id)
508 )
509 self.lcm_tasks.register(
510 "ns", nsr_id, nslcmop_id, "ns_vca_status_refresh", task
511 )
ksaikiranr3fde2c72021-03-15 10:39:06 +0530512 return
gcalvinoed7f6d42018-12-14 14:44:56 +0100513 elif command == "action":
514 # self.logger.debug("Update NS {}".format(nsr_id))
515 nslcmop = params
516 nslcmop_id = nslcmop["_id"]
517 nsr_id = nslcmop["nsInstanceId"]
518 task = asyncio.ensure_future(self.ns.action(nsr_id, nslcmop_id))
519 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_action", task)
520 return
aticigdffa6212022-04-12 15:27:53 +0300521 elif command == "update":
522 # self.logger.debug("Update NS {}".format(nsr_id))
523 nslcmop = params
524 nslcmop_id = nslcmop["_id"]
525 nsr_id = nslcmop["nsInstanceId"]
526 task = asyncio.ensure_future(self.ns.update(nsr_id, nslcmop_id))
527 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_update", task)
528 return
gcalvinoed7f6d42018-12-14 14:44:56 +0100529 elif command == "scale":
530 # self.logger.debug("Update NS {}".format(nsr_id))
531 nslcmop = params
532 nslcmop_id = nslcmop["_id"]
533 nsr_id = nslcmop["nsInstanceId"]
534 task = asyncio.ensure_future(self.ns.scale(nsr_id, nslcmop_id))
535 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_scale", task)
536 return
garciadeblas07f4e4c2022-06-09 09:42:58 +0200537 elif command == "heal":
538 # self.logger.debug("Healing NS {}".format(nsr_id))
539 nslcmop = params
540 nslcmop_id = nslcmop["_id"]
541 nsr_id = nslcmop["nsInstanceId"]
542 task = asyncio.ensure_future(self.ns.heal(nsr_id, nslcmop_id))
preethika.p28b0bf82022-09-23 07:36:28 +0000543 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_heal", task)
garciadeblas07f4e4c2022-06-09 09:42:58 +0200544 return
elumalai80bcf1c2022-04-28 18:05:01 +0530545 elif command == "migrate":
546 nslcmop = params
547 nslcmop_id = nslcmop["_id"]
548 nsr_id = nslcmop["nsInstanceId"]
549 task = asyncio.ensure_future(self.ns.migrate(nsr_id, nslcmop_id))
550 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_migrate", task)
551 return
gcalvinoed7f6d42018-12-14 14:44:56 +0100552 elif command == "show":
tiernoc2564fe2019-01-28 16:18:56 +0000553 nsr_id = params
gcalvinoed7f6d42018-12-14 14:44:56 +0100554 try:
555 db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
garciadeblas5697b8b2021-03-24 09:17:02 +0100556 print(
557 "nsr:\n _id={}\n operational-status: {}\n config-status: {}"
558 "\n detailed-status: {}\n deploy: {}\n tasks: {}"
559 "".format(
560 nsr_id,
561 db_nsr["operational-status"],
562 db_nsr["config-status"],
563 db_nsr["detailed-status"],
564 db_nsr["_admin"]["deployed"],
Gabriel Cuba411af2e2023-01-06 17:23:22 -0500565 self.lcm_tasks.task_registry["ns"].get(nsr_id, ""),
garciadeblas5697b8b2021-03-24 09:17:02 +0100566 )
567 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100568 except Exception as e:
569 print("nsr {} not found: {}".format(nsr_id, e))
570 sys.stdout.flush()
571 return
572 elif command == "deleted":
573 return # TODO cleaning of task just in case should be done
garciadeblas5697b8b2021-03-24 09:17:02 +0100574 elif command in (
elumalaica7ece02022-04-12 12:47:32 +0530575 "vnf_terminated",
elumalaib9e357c2022-04-27 09:58:38 +0530576 "policy_updated",
garciadeblas5697b8b2021-03-24 09:17:02 +0100577 "terminated",
578 "instantiated",
579 "scaled",
garciadeblas07f4e4c2022-06-09 09:42:58 +0200580 "healed",
garciadeblas5697b8b2021-03-24 09:17:02 +0100581 "actioned",
aticigdffa6212022-04-12 15:27:53 +0300582 "updated",
elumalai80bcf1c2022-04-28 18:05:01 +0530583 "migrated",
rshri932105f2024-07-05 15:11:55 +0000584 "verticalscaled",
garciadeblas5697b8b2021-03-24 09:17:02 +0100585 ): # "scaled-cooldown-time"
gcalvinoed7f6d42018-12-14 14:44:56 +0100586 return
elumalaica7ece02022-04-12 12:47:32 +0530587
gcalvinoed7f6d42018-12-14 14:44:56 +0100588 elif topic == "nsi": # netslice LCM processes (instantiate, terminate, etc)
tierno307425f2020-01-26 23:35:59 +0000589 if command == "instantiate":
gcalvinoed7f6d42018-12-14 14:44:56 +0100590 # self.logger.debug("Instantiating Network Slice {}".format(nsilcmop["netsliceInstanceId"]))
591 nsilcmop = params
592 nsilcmop_id = nsilcmop["_id"] # slice operation id
593 nsir_id = nsilcmop["netsliceInstanceId"] # slice record id
garciadeblas5697b8b2021-03-24 09:17:02 +0100594 task = asyncio.ensure_future(
595 self.netslice.instantiate(nsir_id, nsilcmop_id)
596 )
597 self.lcm_tasks.register(
598 "nsi", nsir_id, nsilcmop_id, "nsi_instantiate", task
599 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100600 return
tierno307425f2020-01-26 23:35:59 +0000601 elif command == "terminate":
gcalvinoed7f6d42018-12-14 14:44:56 +0100602 # self.logger.debug("Terminating Network Slice NS {}".format(nsilcmop["netsliceInstanceId"]))
603 nsilcmop = params
604 nsilcmop_id = nsilcmop["_id"] # slice operation id
605 nsir_id = nsilcmop["netsliceInstanceId"] # slice record id
606 self.lcm_tasks.cancel(topic, nsir_id)
garciadeblas5697b8b2021-03-24 09:17:02 +0100607 task = asyncio.ensure_future(
608 self.netslice.terminate(nsir_id, nsilcmop_id)
609 )
610 self.lcm_tasks.register(
611 "nsi", nsir_id, nsilcmop_id, "nsi_terminate", task
612 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100613 return
614 elif command == "show":
tiernoc2564fe2019-01-28 16:18:56 +0000615 nsir_id = params
gcalvinoed7f6d42018-12-14 14:44:56 +0100616 try:
617 db_nsir = self.db.get_one("nsirs", {"_id": nsir_id})
garciadeblas5697b8b2021-03-24 09:17:02 +0100618 print(
619 "nsir:\n _id={}\n operational-status: {}\n config-status: {}"
620 "\n detailed-status: {}\n deploy: {}\n tasks: {}"
621 "".format(
622 nsir_id,
623 db_nsir["operational-status"],
624 db_nsir["config-status"],
625 db_nsir["detailed-status"],
626 db_nsir["_admin"]["deployed"],
Gabriel Cuba411af2e2023-01-06 17:23:22 -0500627 self.lcm_tasks.task_registry["nsi"].get(nsir_id, ""),
garciadeblas5697b8b2021-03-24 09:17:02 +0100628 )
629 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100630 except Exception as e:
631 print("nsir {} not found: {}".format(nsir_id, e))
632 sys.stdout.flush()
633 return
634 elif command == "deleted":
635 return # TODO cleaning of task just in case should be done
garciadeblas5697b8b2021-03-24 09:17:02 +0100636 elif command in (
637 "terminated",
638 "instantiated",
639 "scaled",
garciadeblas07f4e4c2022-06-09 09:42:58 +0200640 "healed",
garciadeblas5697b8b2021-03-24 09:17:02 +0100641 "actioned",
642 ): # "scaled-cooldown-time"
gcalvinoed7f6d42018-12-14 14:44:56 +0100643 return
644 elif topic == "vim_account":
645 vim_id = params["_id"]
garciadeblas96b94f52024-07-08 16:18:21 +0200646 op_id = vim_id
647 op_params = params
yshah771dea82024-07-05 15:11:49 +0000648 db_vim = self.db.get_one("vim_accounts", {"_id": vim_id})
garciadeblas96b94f52024-07-08 16:18:21 +0200649 vim_config = db_vim.get("config", {})
garciadeblas7fa3b252024-10-01 17:45:56 +0200650 if command in ("create", "created", "edit", "edited"):
651 self.db.encrypt_decrypt_fields(
652 vim_config.get("credentials"),
653 "decrypt",
654 ["password", "secret"],
655 schema_version=db_vim["schema_version"],
656 salt=vim_id,
657 )
garciadeblas96b94f52024-07-08 16:18:21 +0200658 self.logger.debug("Db Vim: {}".format(db_vim))
tiernof210c1c2019-10-16 09:09:58 +0000659 if command in ("create", "created"):
garciadeblas96b94f52024-07-08 16:18:21 +0200660 self.logger.debug("Main config: {}".format(self.main_config.to_dict()))
garciadeblasea4ef742024-09-16 09:37:20 +0200661 if "credentials" or "credentials_base64" in vim_config:
garciadeblas96b94f52024-07-08 16:18:21 +0200662 self.logger.info("Vim add cloud credentials")
663 task = asyncio.ensure_future(
664 self.cloud_credentials.add(op_id, op_params, db_vim)
665 )
666 self.lcm_tasks.register(
667 "vim_account", vim_id, op_id, "cloud_credentials_add", task
668 )
Luis Vegaa27dc532022-11-11 20:10:49 +0000669 if not self.main_config.RO.ng:
garciadeblas96b94f52024-07-08 16:18:21 +0200670 self.logger.info("Calling RO to create VIM (no NG-RO)")
tierno2357f4e2020-10-19 16:38:59 +0000671 task = asyncio.ensure_future(self.vim.create(params, order_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100672 self.lcm_tasks.register(
673 "vim_account", vim_id, order_id, "vim_create", task
674 )
garciadeblas96b94f52024-07-08 16:18:21 +0200675 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100676 elif command == "delete" or command == "deleted":
garciadeblas96b94f52024-07-08 16:18:21 +0200677 self.lcm_tasks.cancel(topic, vim_id)
678 if "credentials" in vim_config:
679 self.logger.info("Vim remove cloud credentials")
yshah771dea82024-07-05 15:11:49 +0000680 task = asyncio.ensure_future(
garciadeblas96b94f52024-07-08 16:18:21 +0200681 self.cloud_credentials.remove(op_id, op_params, db_vim)
yshah771dea82024-07-05 15:11:49 +0000682 )
683 self.lcm_tasks.register(
garciadeblas96b94f52024-07-08 16:18:21 +0200684 "vim_account", vim_id, op_id, "cloud_credentials_remove", task
yshah771dea82024-07-05 15:11:49 +0000685 )
garciadeblas96b94f52024-07-08 16:18:21 +0200686 task = asyncio.ensure_future(self.vim.delete(params, order_id))
687 self.lcm_tasks.register(
688 "vim_account", vim_id, order_id, "vim_delete", task
689 )
690 return
gcalvinoed7f6d42018-12-14 14:44:56 +0100691 elif command == "show":
692 print("not implemented show with vim_account")
693 sys.stdout.flush()
694 return
tiernof210c1c2019-10-16 09:09:58 +0000695 elif command in ("edit", "edited"):
garciadeblas96b94f52024-07-08 16:18:21 +0200696 if "credentials" in vim_config:
697 self.logger.info("Vim update cloud credentials")
yshah771dea82024-07-05 15:11:49 +0000698 task = asyncio.ensure_future(
garciadeblas96b94f52024-07-08 16:18:21 +0200699 self.cloud_credentials.edit(op_id, op_params, db_vim)
yshah771dea82024-07-05 15:11:49 +0000700 )
701 self.lcm_tasks.register(
702 "vim_account", vim_id, op_id, "cloud_credentials_update", task
703 )
Luis Vegaa27dc532022-11-11 20:10:49 +0000704 if not self.main_config.RO.ng:
tierno2357f4e2020-10-19 16:38:59 +0000705 task = asyncio.ensure_future(self.vim.edit(params, order_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100706 self.lcm_tasks.register(
707 "vim_account", vim_id, order_id, "vim_edit", task
708 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100709 return
tiernof210c1c2019-10-16 09:09:58 +0000710 elif command == "deleted":
711 return # TODO cleaning of task just in case should be done
gcalvinoed7f6d42018-12-14 14:44:56 +0100712 elif topic == "wim_account":
713 wim_id = params["_id"]
tiernof210c1c2019-10-16 09:09:58 +0000714 if command in ("create", "created"):
Luis Vegaa27dc532022-11-11 20:10:49 +0000715 if not self.main_config.RO.ng:
tierno2357f4e2020-10-19 16:38:59 +0000716 task = asyncio.ensure_future(self.wim.create(params, order_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100717 self.lcm_tasks.register(
718 "wim_account", wim_id, order_id, "wim_create", task
719 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100720 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100721 elif command == "delete" or command == "deleted":
gcalvinoed7f6d42018-12-14 14:44:56 +0100722 self.lcm_tasks.cancel(topic, wim_id)
kuuse6a470c62019-07-10 13:52:45 +0200723 task = asyncio.ensure_future(self.wim.delete(params, order_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100724 self.lcm_tasks.register(
725 "wim_account", wim_id, order_id, "wim_delete", task
726 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100727 return
728 elif command == "show":
729 print("not implemented show with wim_account")
730 sys.stdout.flush()
731 return
tiernof210c1c2019-10-16 09:09:58 +0000732 elif command in ("edit", "edited"):
gcalvinoed7f6d42018-12-14 14:44:56 +0100733 task = asyncio.ensure_future(self.wim.edit(params, order_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100734 self.lcm_tasks.register(
735 "wim_account", wim_id, order_id, "wim_edit", task
736 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100737 return
tiernof210c1c2019-10-16 09:09:58 +0000738 elif command == "deleted":
739 return # TODO cleaning of task just in case should be done
gcalvinoed7f6d42018-12-14 14:44:56 +0100740 elif topic == "sdn":
741 _sdn_id = params["_id"]
tiernof210c1c2019-10-16 09:09:58 +0000742 if command in ("create", "created"):
Luis Vegaa27dc532022-11-11 20:10:49 +0000743 if not self.main_config.RO.ng:
tierno2357f4e2020-10-19 16:38:59 +0000744 task = asyncio.ensure_future(self.sdn.create(params, order_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100745 self.lcm_tasks.register(
746 "sdn", _sdn_id, order_id, "sdn_create", task
747 )
gcalvinoed7f6d42018-12-14 14:44:56 +0100748 return
calvinosanch9f9c6f22019-11-04 13:37:39 +0100749 elif command == "delete" or command == "deleted":
gcalvinoed7f6d42018-12-14 14:44:56 +0100750 self.lcm_tasks.cancel(topic, _sdn_id)
kuuse6a470c62019-07-10 13:52:45 +0200751 task = asyncio.ensure_future(self.sdn.delete(params, order_id))
gcalvinoed7f6d42018-12-14 14:44:56 +0100752 self.lcm_tasks.register("sdn", _sdn_id, order_id, "sdn_delete", task)
753 return
tiernof210c1c2019-10-16 09:09:58 +0000754 elif command in ("edit", "edited"):
gcalvinoed7f6d42018-12-14 14:44:56 +0100755 task = asyncio.ensure_future(self.sdn.edit(params, order_id))
756 self.lcm_tasks.register("sdn", _sdn_id, order_id, "sdn_edit", task)
757 return
tiernof210c1c2019-10-16 09:09:58 +0000758 elif command == "deleted":
759 return # TODO cleaning of task just in case should be done
rshri932105f2024-07-05 15:11:55 +0000760 elif topic == "cluster":
garciadeblas96b94f52024-07-08 16:18:21 +0200761 if command != "get_creds":
762 op_id = params["operation_id"]
763 cluster_id = params["cluster_id"]
garciadeblas446ba3c2024-11-20 12:42:49 +0100764 self.cluster.initialize_operation(cluster_id, op_id)
garciadeblas96b94f52024-07-08 16:18:21 +0200765 db_cluster = self.db.get_one("clusters", {"_id": cluster_id})
rshrib0e6ac32024-11-12 18:12:38 +0000766 """
garciadeblas7fa3b252024-10-01 17:45:56 +0200767 if command in (
768 "create",
769 "created",
770 "register",
771 "registered",
772 "upgrade",
773 "scale",
774 ):
775 self.db.encrypt_decrypt_fields(
776 db_cluster,
777 "decrypt",
778 ["age_pubkey", "age_privkey"],
779 schema_version="1.11",
780 salt=cluster_id,
781 )
rshrib0e6ac32024-11-12 18:12:38 +0000782 """
garciadeblas96b94f52024-07-08 16:18:21 +0200783 op_params = self.get_operation_params(db_cluster, op_id)
784 db_content = {
785 "cluster": db_cluster,
786 }
rshri932105f2024-07-05 15:11:55 +0000787 if command == "create" or command == "created":
rshri932105f2024-07-05 15:11:55 +0000788 self.logger.debug("cluster_id = {}".format(cluster_id))
garciadeblas96b94f52024-07-08 16:18:21 +0200789 # db_vim = self.db.get_one("vim_accounts", {"_id": db_cluster["vim_account"]})
rshrib0e6ac32024-11-12 18:12:38 +0000790 """
garciadeblas96b94f52024-07-08 16:18:21 +0200791 db_vim = self.db.get_one(
792 "vim_accounts", {"name": db_cluster["vim_account"]}
793 )
794 db_content["vim_account"] = db_vim
rshrib0e6ac32024-11-12 18:12:38 +0000795 """
garciadeblas96b94f52024-07-08 16:18:21 +0200796 task = asyncio.ensure_future(
797 self.cluster.create(op_id, op_params, db_content)
798 )
rshri932105f2024-07-05 15:11:55 +0000799 self.lcm_tasks.register(
garciadeblas96b94f52024-07-08 16:18:21 +0200800 "cluster", cluster_id, op_id, "cluster_create", task
rshri932105f2024-07-05 15:11:55 +0000801 )
802 return
803 elif command == "delete" or command == "deleted":
garciadeblas96b94f52024-07-08 16:18:21 +0200804 task = asyncio.ensure_future(
805 self.cluster.delete(op_id, op_params, db_content)
806 )
rshri932105f2024-07-05 15:11:55 +0000807 self.lcm_tasks.register(
garciadeblas96b94f52024-07-08 16:18:21 +0200808 "cluster", cluster_id, op_id, "cluster_delete", task
rshri932105f2024-07-05 15:11:55 +0000809 )
810 return
811 elif command == "add" or command == "added":
garciadeblas96b94f52024-07-08 16:18:21 +0200812 profile_type = params["profile_type"]
813 profile_collection = self.profile_collection_mapping[profile_type]
814 db_profile = self.db.get_one(
815 profile_collection, {"_id": params["profile_id"]}
816 )
817 db_profile["profile_type"] = profile_type
818 db_content["profile"] = db_profile
819 task = asyncio.ensure_future(
820 self.cluster.attach_profile(op_id, op_params, db_content)
821 )
rshri932105f2024-07-05 15:11:55 +0000822 self.lcm_tasks.register(
garciadeblas96b94f52024-07-08 16:18:21 +0200823 "cluster", cluster_id, op_id, "profile_add", task
rshri932105f2024-07-05 15:11:55 +0000824 )
825 return
826 elif command == "remove" or command == "removed":
garciadeblas96b94f52024-07-08 16:18:21 +0200827 profile_type = params["profile_type"]
828 profile_collection = self.profile_collection_mapping[profile_type]
829 db_profile = self.db.get_one(
830 profile_collection, {"_id": params["profile_id"]}
831 )
832 db_profile["profile_type"] = profile_type
833 db_content["profile"] = db_profile
834 task = asyncio.ensure_future(
835 self.cluster.detach_profile(op_id, op_params, db_content)
836 )
rshri932105f2024-07-05 15:11:55 +0000837 self.lcm_tasks.register(
garciadeblas96b94f52024-07-08 16:18:21 +0200838 "cluster", cluster_id, op_id, "profile_remove", task
rshri932105f2024-07-05 15:11:55 +0000839 )
840 return
841 elif command == "register" or command == "registered":
garciadeblas96b94f52024-07-08 16:18:21 +0200842 task = asyncio.ensure_future(
843 self.cluster.register(op_id, op_params, db_content)
844 )
rshri932105f2024-07-05 15:11:55 +0000845 self.lcm_tasks.register(
garciadeblas96b94f52024-07-08 16:18:21 +0200846 "cluster", cluster_id, op_id, "cluster_register", task
rshri932105f2024-07-05 15:11:55 +0000847 )
848 return
849 elif command == "deregister" or command == "deregistered":
garciadeblas96b94f52024-07-08 16:18:21 +0200850 task = asyncio.ensure_future(
851 self.cluster.deregister(op_id, op_params, db_content)
852 )
rshri932105f2024-07-05 15:11:55 +0000853 self.lcm_tasks.register(
garciadeblas96b94f52024-07-08 16:18:21 +0200854 "cluster", cluster_id, op_id, "cluster_deregister", task
rshri932105f2024-07-05 15:11:55 +0000855 )
856 return
yshah771dea82024-07-05 15:11:49 +0000857 elif command == "get_creds":
yshah07491142024-10-17 06:11:12 +0000858 cluster_id = params["cluster_id"]
859 op_id = params["operation_id"]
garciadeblas96b94f52024-07-08 16:18:21 +0200860 db_cluster = self.db.get_one("clusters", {"_id": cluster_id})
yshah07491142024-10-17 06:11:12 +0000861 task = asyncio.ensure_future(self.cluster.get_creds(op_id, db_cluster))
yshah771dea82024-07-05 15:11:49 +0000862 self.lcm_tasks.register(
garciadeblas96b94f52024-07-08 16:18:21 +0200863 "cluster", cluster_id, cluster_id, "cluster_get_credentials", task
yshah771dea82024-07-05 15:11:49 +0000864 )
865 return
866 elif command == "upgrade" or command == "scale":
garciadeblas96b94f52024-07-08 16:18:21 +0200867 # db_vim = self.db.get_one("vim_accounts", {"_id": db_cluster["vim_account"]})
868 db_vim = self.db.get_one(
869 "vim_accounts", {"name": db_cluster["vim_account"]}
870 )
871 db_content["vim_account"] = db_vim
872 task = asyncio.ensure_future(
873 self.cluster.update(op_id, op_params, db_content)
874 )
875 self.lcm_tasks.register(
876 "cluster", cluster_id, op_id, "cluster_update", task
877 )
yshah771dea82024-07-05 15:11:49 +0000878 return
rshri932105f2024-07-05 15:11:55 +0000879 elif topic == "k8s_app":
garciadeblas96b94f52024-07-08 16:18:21 +0200880 op_id = params["operation_id"]
881 profile_id = params["profile_id"]
garciadeblas446ba3c2024-11-20 12:42:49 +0100882 self.k8s_app.initialize_operation(profile_id, op_id)
garciadeblas96b94f52024-07-08 16:18:21 +0200883 db_profile = self.db.get_one("k8sapp", {"_id": profile_id})
884 db_profile["profile_type"] = "applications"
885 op_params = self.get_operation_params(db_profile, op_id)
rshri932105f2024-07-05 15:11:55 +0000886 if command == "profile_create" or command == "profile_created":
garciadeblas96b94f52024-07-08 16:18:21 +0200887 self.logger.debug("Create k8s_app_id = {}".format(profile_id))
888 task = asyncio.ensure_future(
889 self.k8s_app.create(op_id, op_params, db_profile)
890 )
rshri932105f2024-07-05 15:11:55 +0000891 self.lcm_tasks.register(
garciadeblas96b94f52024-07-08 16:18:21 +0200892 "k8s_app", profile_id, op_id, "k8s_app_create", task
rshri932105f2024-07-05 15:11:55 +0000893 )
894 return
895 elif command == "delete" or command == "deleted":
garciadeblas96b94f52024-07-08 16:18:21 +0200896 self.logger.debug("Delete k8s_app_id = {}".format(profile_id))
897 task = asyncio.ensure_future(
898 self.k8s_app.delete(op_id, op_params, db_profile)
899 )
rshri932105f2024-07-05 15:11:55 +0000900 self.lcm_tasks.register(
garciadeblas96b94f52024-07-08 16:18:21 +0200901 "k8s_app", profile_id, op_id, "k8s_app_delete", task
rshri932105f2024-07-05 15:11:55 +0000902 )
903 return
904 elif topic == "k8s_resource":
garciadeblas96b94f52024-07-08 16:18:21 +0200905 op_id = params["operation_id"]
906 profile_id = params["profile_id"]
garciadeblas446ba3c2024-11-20 12:42:49 +0100907 self.k8s_resource.initialize_operation(profile_id, op_id)
garciadeblas96b94f52024-07-08 16:18:21 +0200908 db_profile = self.db.get_one("k8sresource", {"_id": profile_id})
909 db_profile["profile_type"] = "managed-resources"
910 op_params = self.get_operation_params(db_profile, op_id)
rshri932105f2024-07-05 15:11:55 +0000911 if command == "profile_create" or command == "profile_created":
garciadeblas96b94f52024-07-08 16:18:21 +0200912 self.logger.debug("Create k8s_resource_id = {}".format(profile_id))
913 task = asyncio.ensure_future(
914 self.k8s_resource.create(op_id, op_params, db_profile)
915 )
rshri932105f2024-07-05 15:11:55 +0000916 self.lcm_tasks.register(
917 "k8s_resource",
garciadeblas96b94f52024-07-08 16:18:21 +0200918 profile_id,
919 op_id,
rshri932105f2024-07-05 15:11:55 +0000920 "k8s_resource_create",
921 task,
922 )
923 return
924 elif command == "delete" or command == "deleted":
garciadeblas96b94f52024-07-08 16:18:21 +0200925 self.logger.debug("Delete k8s_resource_id = {}".format(profile_id))
926 task = asyncio.ensure_future(
927 self.k8s_resource.delete(op_id, op_params, db_profile)
928 )
rshri932105f2024-07-05 15:11:55 +0000929 self.lcm_tasks.register(
930 "k8s_resource",
garciadeblas96b94f52024-07-08 16:18:21 +0200931 profile_id,
932 op_id,
rshri932105f2024-07-05 15:11:55 +0000933 "k8s_resource_delete",
934 task,
935 )
936 return
937
938 elif topic == "k8s_infra_controller":
garciadeblas96b94f52024-07-08 16:18:21 +0200939 op_id = params["operation_id"]
940 profile_id = params["profile_id"]
garciadeblas446ba3c2024-11-20 12:42:49 +0100941 self.k8s_infra_controller.initialize_operation(profile_id, op_id)
garciadeblas96b94f52024-07-08 16:18:21 +0200942 db_profile = self.db.get_one("k8sinfra_controller", {"_id": profile_id})
943 db_profile["profile_type"] = "infra-controllers"
944 op_params = self.get_operation_params(db_profile, op_id)
rshri932105f2024-07-05 15:11:55 +0000945 if command == "profile_create" or command == "profile_created":
rshri932105f2024-07-05 15:11:55 +0000946 self.logger.debug(
garciadeblas96b94f52024-07-08 16:18:21 +0200947 "Create k8s_infra_controller_id = {}".format(profile_id)
rshri932105f2024-07-05 15:11:55 +0000948 )
949 task = asyncio.ensure_future(
garciadeblas96b94f52024-07-08 16:18:21 +0200950 self.k8s_infra_controller.create(op_id, op_params, db_profile)
rshri932105f2024-07-05 15:11:55 +0000951 )
952 self.lcm_tasks.register(
953 "k8s_infra_controller",
garciadeblas96b94f52024-07-08 16:18:21 +0200954 profile_id,
955 op_id,
rshri932105f2024-07-05 15:11:55 +0000956 "k8s_infra_controller_create",
957 task,
958 )
959 return
960 elif command == "delete" or command == "deleted":
garciadeblas96b94f52024-07-08 16:18:21 +0200961 self.logger.debug(
962 "Delete k8s_infra_controller_id = {}".format(profile_id)
963 )
rshri932105f2024-07-05 15:11:55 +0000964 task = asyncio.ensure_future(
garciadeblas96b94f52024-07-08 16:18:21 +0200965 self.k8s_infra_controller.delete(op_id, op_params, db_profile)
rshri932105f2024-07-05 15:11:55 +0000966 )
967 self.lcm_tasks.register(
968 "k8s_infra_controller",
garciadeblas96b94f52024-07-08 16:18:21 +0200969 profile_id,
970 op_id,
rshri932105f2024-07-05 15:11:55 +0000971 "k8s_infra_controller_delete",
972 task,
973 )
974 return
975
976 elif topic == "k8s_infra_config":
garciadeblas96b94f52024-07-08 16:18:21 +0200977 op_id = params["operation_id"]
978 profile_id = params["profile_id"]
garciadeblas446ba3c2024-11-20 12:42:49 +0100979 self.k8s_infra_config.initialize_operation(profile_id, op_id)
garciadeblas96b94f52024-07-08 16:18:21 +0200980 db_profile = self.db.get_one("k8sinfra_config", {"_id": profile_id})
981 db_profile["profile_type"] = "infra-configs"
982 op_params = self.get_operation_params(db_profile, op_id)
rshri932105f2024-07-05 15:11:55 +0000983 if command == "profile_create" or command == "profile_created":
garciadeblas96b94f52024-07-08 16:18:21 +0200984 self.logger.debug("Create k8s_infra_config_id = {}".format(profile_id))
rshri932105f2024-07-05 15:11:55 +0000985 task = asyncio.ensure_future(
garciadeblas96b94f52024-07-08 16:18:21 +0200986 self.k8s_infra_config.create(op_id, op_params, db_profile)
rshri932105f2024-07-05 15:11:55 +0000987 )
988 self.lcm_tasks.register(
989 "k8s_infra_config",
garciadeblas96b94f52024-07-08 16:18:21 +0200990 profile_id,
991 op_id,
rshri932105f2024-07-05 15:11:55 +0000992 "k8s_infra_config_create",
993 task,
994 )
995 return
996 elif command == "delete" or command == "deleted":
garciadeblas96b94f52024-07-08 16:18:21 +0200997 self.logger.debug("Delete k8s_infra_config_id = {}".format(profile_id))
rshri932105f2024-07-05 15:11:55 +0000998 task = asyncio.ensure_future(
garciadeblas96b94f52024-07-08 16:18:21 +0200999 self.k8s_infra_config.delete(op_id, op_params, db_profile)
rshri932105f2024-07-05 15:11:55 +00001000 )
1001 self.lcm_tasks.register(
1002 "k8s_infra_config",
garciadeblas96b94f52024-07-08 16:18:21 +02001003 profile_id,
1004 op_id,
rshri932105f2024-07-05 15:11:55 +00001005 "k8s_infra_config_delete",
1006 task,
1007 )
1008 return
yshah771dea82024-07-05 15:11:49 +00001009 elif topic == "oka":
garciadeblas96b94f52024-07-08 16:18:21 +02001010 op_id = params["operation_id"]
1011 oka_id = params["oka_id"]
garciadeblas446ba3c2024-11-20 12:42:49 +01001012 self.oka.initialize_operation(oka_id, op_id)
garciadeblas96b94f52024-07-08 16:18:21 +02001013 db_oka = self.db.get_one("okas", {"_id": oka_id})
1014 op_params = self.get_operation_params(db_oka, op_id)
yshah771dea82024-07-05 15:11:49 +00001015 if command == "create":
garciadeblas96b94f52024-07-08 16:18:21 +02001016 task = asyncio.ensure_future(self.oka.create(op_id, op_params, db_oka))
1017 self.lcm_tasks.register("oka", oka_id, op_id, "oka_create", task)
yshah771dea82024-07-05 15:11:49 +00001018 return
1019 elif command == "edit":
garciadeblas96b94f52024-07-08 16:18:21 +02001020 task = asyncio.ensure_future(self.oka.edit(op_id, op_params, db_oka))
1021 self.lcm_tasks.register("oka", oka_id, op_id, "oka_edit", task)
yshah771dea82024-07-05 15:11:49 +00001022 return
1023 elif command == "delete":
garciadeblas96b94f52024-07-08 16:18:21 +02001024 task = asyncio.ensure_future(self.oka.delete(op_id, op_params, db_oka))
1025 self.lcm_tasks.register("oka", oka_id, op_id, "oka_delete", task)
yshah771dea82024-07-05 15:11:49 +00001026 return
1027 elif topic == "ksu":
garciadeblas96b94f52024-07-08 16:18:21 +02001028 op_id = params["operation_id"]
1029 op_params = None
1030 db_content = None
1031 if not (command == "clone" or command == "move"):
1032 # op_params is a list
1033 # db_content is a list of KSU
1034 db_content = []
1035 op_params = []
1036 for ksu_id in params["ksus_list"]:
garciadeblas446ba3c2024-11-20 12:42:49 +01001037 self.ksu.initialize_operation(ksu_id, op_id)
garciadeblas96b94f52024-07-08 16:18:21 +02001038 db_ksu = self.db.get_one("ksus", {"_id": ksu_id})
1039 db_content.append(db_ksu)
1040 ksu_params = {}
1041 if command == "delete":
1042 ksu_params["profile"] = {}
1043 ksu_params["profile"]["profile_type"] = db_ksu["profile"][
1044 "profile_type"
1045 ]
1046 ksu_params["profile"]["_id"] = db_ksu["profile"]["_id"]
1047 else:
1048 ksu_params = self.get_operation_params(db_ksu, op_id)
1049 # Update ksu_params["profile"] with profile name and age-pubkey
1050 profile_type = ksu_params["profile"]["profile_type"]
1051 profile_id = ksu_params["profile"]["_id"]
1052 profile_collection = self.profile_collection_mapping[profile_type]
1053 db_profile = self.db.get_one(
1054 profile_collection, {"_id": profile_id}
1055 )
1056 ksu_params["profile"]["name"] = db_profile["name"]
1057 ksu_params["profile"]["age_pubkey"] = db_profile.get(
1058 "age_pubkey", ""
1059 )
1060 if command == "create" or command == "edit" or command == "edited":
1061 # Update ksu_params["oka"] with sw_catalog_path (when missing)
1062 for oka in ksu_params["oka"]:
1063 if "sw_catalog_path" not in oka:
1064 oka_id = oka["_id"]
1065 db_oka = self.db.get_one("okas", {"_id": oka_id})
1066 oka[
1067 "sw_catalog_path"
1068 ] = f"infra-controllers/{db_oka['git_name']}"
1069 op_params.append(ksu_params)
1070 else:
1071 # db_content and op_params are single items
garciadeblas446ba3c2024-11-20 12:42:49 +01001072 self.ksu.initialize_operation(params["_id"], op_id)
garciadeblas96b94f52024-07-08 16:18:21 +02001073 db_content = self.db.get_one("ksus", {"_id": params["_id"]})
1074 db_content = db_ksu
1075 op_params = self.get_operation_params(db_ksu, op_id)
yshah771dea82024-07-05 15:11:49 +00001076 if command == "create":
garciadeblas96b94f52024-07-08 16:18:21 +02001077 task = asyncio.ensure_future(
1078 self.ksu.create(op_id, op_params, db_content)
1079 )
1080 self.lcm_tasks.register("ksu", ksu_id, op_id, "ksu_create", task)
yshah771dea82024-07-05 15:11:49 +00001081 return
1082 elif command == "edit" or command == "edited":
garciadeblas96b94f52024-07-08 16:18:21 +02001083 task = asyncio.ensure_future(
1084 self.ksu.edit(op_id, op_params, db_content)
1085 )
1086 self.lcm_tasks.register("ksu", ksu_id, op_id, "ksu_edit", task)
yshah771dea82024-07-05 15:11:49 +00001087 return
1088 elif command == "delete":
garciadeblas96b94f52024-07-08 16:18:21 +02001089 task = asyncio.ensure_future(
1090 self.ksu.delete(op_id, op_params, db_content)
1091 )
1092 self.lcm_tasks.register("ksu", ksu_id, op_id, "ksu_delete", task)
yshah771dea82024-07-05 15:11:49 +00001093 return
1094 elif command == "clone":
garciadeblas96b94f52024-07-08 16:18:21 +02001095 task = asyncio.ensure_future(
1096 self.ksu.clone(op_id, op_params, db_content)
1097 )
1098 self.lcm_tasks.register("ksu", ksu_id, op_id, "ksu_clone", task)
yshah771dea82024-07-05 15:11:49 +00001099 return
1100 elif command == "move":
garciadeblas96b94f52024-07-08 16:18:21 +02001101 task = asyncio.ensure_future(
1102 self.ksu.move(op_id, op_params, db_content)
1103 )
1104 self.lcm_tasks.register("ksu", ksu_id, op_id, "ksu_move", task)
yshah771dea82024-07-05 15:11:49 +00001105 return
rshri932105f2024-07-05 15:11:55 +00001106
gcalvinoed7f6d42018-12-14 14:44:56 +01001107 self.logger.critical("unknown topic {} and command '{}'".format(topic, command))
1108
tiernoc0e42e22018-05-11 11:36:10 +02001109 async def kafka_read(self):
garciadeblas5697b8b2021-03-24 09:17:02 +01001110 self.logger.debug(
1111 "Task kafka_read Enter with worker_id={}".format(self.worker_id)
1112 )
gcalvinoed7f6d42018-12-14 14:44:56 +01001113 self.consecutive_errors = 0
1114 self.first_start = True
1115 while self.consecutive_errors < 10:
tiernoc0e42e22018-05-11 11:36:10 +02001116 try:
garciadeblas5697b8b2021-03-24 09:17:02 +01001117 topics = (
1118 "ns",
1119 "vim_account",
1120 "wim_account",
1121 "sdn",
1122 "nsi",
1123 "k8scluster",
1124 "vca",
1125 "k8srepo",
1126 "pla",
Gabriel Cubab6049d32023-10-30 13:44:49 -05001127 "nslcmops",
rshri932105f2024-07-05 15:11:55 +00001128 "cluster",
1129 "k8s_app",
1130 "k8s_resource",
1131 "k8s_infra_controller",
1132 "k8s_infra_config",
yshah771dea82024-07-05 15:11:49 +00001133 "oka",
1134 "ksu",
rshri932105f2024-07-05 15:11:55 +00001135 )
garciadeblasa8c67642024-09-17 11:38:51 +02001136 self.logger.debug(
rshri932105f2024-07-05 15:11:55 +00001137 "Consecutive errors: {} first start: {}".format(
1138 self.consecutive_errors, self.first_start
1139 )
garciadeblas5697b8b2021-03-24 09:17:02 +01001140 )
1141 topics_admin = ("admin",)
tierno16427352019-04-22 11:37:36 +00001142 await asyncio.gather(
garciadeblas5697b8b2021-03-24 09:17:02 +01001143 self.msg.aioread(
Gabriel Cubab6049d32023-10-30 13:44:49 -05001144 topics,
1145 aiocallback=self.kafka_read_callback,
1146 from_beginning=True,
garciadeblas5697b8b2021-03-24 09:17:02 +01001147 ),
1148 self.msg_admin.aioread(
1149 topics_admin,
Gabriel Cubab6049d32023-10-30 13:44:49 -05001150 aiocallback=self.kafka_read_callback,
garciadeblas5697b8b2021-03-24 09:17:02 +01001151 group_id=False,
1152 ),
tierno16427352019-04-22 11:37:36 +00001153 )
tiernoc0e42e22018-05-11 11:36:10 +02001154
gcalvinoed7f6d42018-12-14 14:44:56 +01001155 except LcmExceptionExit:
1156 self.logger.debug("Bye!")
1157 break
tiernoc0e42e22018-05-11 11:36:10 +02001158 except Exception as e:
1159 # if not first_start is the first time after starting. So leave more time and wait
1160 # to allow kafka starts
gcalvinoed7f6d42018-12-14 14:44:56 +01001161 if self.consecutive_errors == 8 if not self.first_start else 30:
garciadeblas5697b8b2021-03-24 09:17:02 +01001162 self.logger.error(
1163 "Task kafka_read task exit error too many errors. Exception: {}".format(
1164 e
1165 )
1166 )
tiernoc0e42e22018-05-11 11:36:10 +02001167 raise
gcalvinoed7f6d42018-12-14 14:44:56 +01001168 self.consecutive_errors += 1
garciadeblas5697b8b2021-03-24 09:17:02 +01001169 self.logger.error(
1170 "Task kafka_read retrying after Exception {}".format(e)
1171 )
gcalvinoed7f6d42018-12-14 14:44:56 +01001172 wait_time = 2 if not self.first_start else 5
Gabriel Cubae7898982023-05-11 01:57:21 -05001173 await asyncio.sleep(wait_time)
tiernoc0e42e22018-05-11 11:36:10 +02001174
tiernoc0e42e22018-05-11 11:36:10 +02001175 self.logger.debug("Task kafka_read exit")
1176
Gabriel Cubae7898982023-05-11 01:57:21 -05001177 async def kafka_read_ping(self):
1178 await asyncio.gather(self.kafka_read(), self.kafka_ping())
1179
Mark Beierl1addc932023-05-18 15:11:34 -04001180 async def start(self):
rshri932105f2024-07-05 15:11:55 +00001181 self.logger.info("Start LCM")
tierno22f4f9c2018-06-11 18:53:39 +02001182 # check RO version
Mark Beierl1addc932023-05-18 15:11:34 -04001183 await self.check_RO_version()
tierno22f4f9c2018-06-11 18:53:39 +02001184
Gabriel Cubae7898982023-05-11 01:57:21 -05001185 self.ns = ns.NsLcm(self.msg, self.lcm_tasks, self.main_config)
Luis Vegaa27dc532022-11-11 20:10:49 +00001186 # TODO: modify the rest of classes to use the LcmCfg object instead of dicts
garciadeblas5697b8b2021-03-24 09:17:02 +01001187 self.netslice = netslice.NetsliceLcm(
Gabriel Cubae7898982023-05-11 01:57:21 -05001188 self.msg, self.lcm_tasks, self.main_config.to_dict(), self.ns
garciadeblas5697b8b2021-03-24 09:17:02 +01001189 )
Gabriel Cubae7898982023-05-11 01:57:21 -05001190 self.vim = vim_sdn.VimLcm(self.msg, self.lcm_tasks, self.main_config.to_dict())
1191 self.wim = vim_sdn.WimLcm(self.msg, self.lcm_tasks, self.main_config.to_dict())
1192 self.sdn = vim_sdn.SdnLcm(self.msg, self.lcm_tasks, self.main_config.to_dict())
garciadeblas5697b8b2021-03-24 09:17:02 +01001193 self.k8scluster = vim_sdn.K8sClusterLcm(
Gabriel Cubae7898982023-05-11 01:57:21 -05001194 self.msg, self.lcm_tasks, self.main_config.to_dict()
garciadeblas5697b8b2021-03-24 09:17:02 +01001195 )
Gabriel Cubae7898982023-05-11 01:57:21 -05001196 self.vca = vim_sdn.VcaLcm(self.msg, self.lcm_tasks, self.main_config.to_dict())
garciadeblas5697b8b2021-03-24 09:17:02 +01001197 self.k8srepo = vim_sdn.K8sRepoLcm(
Gabriel Cubae7898982023-05-11 01:57:21 -05001198 self.msg, self.lcm_tasks, self.main_config.to_dict()
garciadeblas5697b8b2021-03-24 09:17:02 +01001199 )
rshri932105f2024-07-05 15:11:55 +00001200 self.cluster = k8s.ClusterLcm(
1201 self.msg, self.lcm_tasks, self.main_config.to_dict()
1202 )
1203 self.k8s_app = k8s.K8sAppLcm(
1204 self.msg, self.lcm_tasks, self.main_config.to_dict()
1205 )
1206 self.k8s_resource = k8s.K8sResourceLcm(
1207 self.msg, self.lcm_tasks, self.main_config.to_dict()
1208 )
1209 self.k8s_infra_controller = k8s.K8sInfraControllerLcm(
1210 self.msg, self.lcm_tasks, self.main_config.to_dict()
1211 )
1212 self.k8s_infra_config = k8s.K8sInfraConfigLcm(
1213 self.msg, self.lcm_tasks, self.main_config.to_dict()
1214 )
yshah771dea82024-07-05 15:11:49 +00001215 self.cloud_credentials = k8s.CloudCredentialsLcm(
1216 self.msg, self.lcm_tasks, self.main_config.to_dict()
1217 )
1218 self.oka = k8s.OkaLcm(self.msg, self.lcm_tasks, self.main_config.to_dict())
1219 self.ksu = k8s.KsuLcm(self.msg, self.lcm_tasks, self.main_config.to_dict())
rshri932105f2024-07-05 15:11:55 +00001220
1221 self.logger.info(
1222 "Msg: {} lcm tasks: {} main config: {}".format(
1223 self.msg, self.lcm_tasks, self.main_config
1224 )
1225 )
tierno2357f4e2020-10-19 16:38:59 +00001226
Mark Beierl1addc932023-05-18 15:11:34 -04001227 await self.kafka_read_ping()
bravof73bac502021-05-11 07:38:47 -04001228
tiernoc0e42e22018-05-11 11:36:10 +02001229 # TODO
1230 # self.logger.debug("Terminating cancelling creation tasks")
tiernoca2e16a2018-06-29 15:25:24 +02001231 # self.lcm_tasks.cancel("ALL", "create")
tiernoc0e42e22018-05-11 11:36:10 +02001232 # timeout = 200
1233 # while self.is_pending_tasks():
1234 # self.logger.debug("Task kafka_read terminating. Waiting for tasks termination")
Gabriel Cubae7898982023-05-11 01:57:21 -05001235 # await asyncio.sleep(2)
tiernoc0e42e22018-05-11 11:36:10 +02001236 # timeout -= 2
1237 # if not timeout:
tiernoca2e16a2018-06-29 15:25:24 +02001238 # self.lcm_tasks.cancel("ALL", "ALL")
tiernoc0e42e22018-05-11 11:36:10 +02001239 if self.db:
1240 self.db.db_disconnect()
1241 if self.msg:
1242 self.msg.disconnect()
tierno16427352019-04-22 11:37:36 +00001243 if self.msg_admin:
1244 self.msg_admin.disconnect()
tiernoc0e42e22018-05-11 11:36:10 +02001245 if self.fs:
1246 self.fs.fs_disconnect()
1247
tiernoc0e42e22018-05-11 11:36:10 +02001248 def read_config_file(self, config_file):
tiernoc0e42e22018-05-11 11:36:10 +02001249 try:
Gabriel Cubaa89a5a72022-11-26 18:55:15 -05001250 with open(config_file) as f:
1251 return yaml.safe_load(f)
tiernoc0e42e22018-05-11 11:36:10 +02001252 except Exception as e:
1253 self.logger.critical("At config file '{}': {}".format(config_file, e))
Gabriel Cubaa89a5a72022-11-26 18:55:15 -05001254 exit(1)
tiernoc0e42e22018-05-11 11:36:10 +02001255
tierno16427352019-04-22 11:37:36 +00001256 @staticmethod
1257 def get_process_id():
1258 """
1259 Obtain a unique ID for this process. If running from inside docker, it will get docker ID. If not it
1260 will provide a random one
1261 :return: Obtained ID
1262 """
Gabriel Cuba4c0e6802023-10-09 13:22:38 -05001263
1264 def get_docker_id():
1265 try:
1266 with open("/proc/self/cgroup", "r") as f:
1267 text_id_ = f.readline()
1268 _, _, text_id = text_id_.rpartition("/")
1269 return text_id.replace("\n", "")[:12]
1270 except Exception:
1271 return None
1272
1273 def generate_random_id():
1274 return "".join(SystemRandom().choice("0123456789abcdef") for _ in range(12))
1275
1276 # Try getting docker id. If it fails, generate a random id
1277 docker_id = get_docker_id()
1278 return docker_id if docker_id else generate_random_id()
tierno16427352019-04-22 11:37:36 +00001279
tiernoc0e42e22018-05-11 11:36:10 +02001280
tierno275411e2018-05-16 14:33:32 +02001281def usage():
garciadeblas5697b8b2021-03-24 09:17:02 +01001282 print(
1283 """Usage: {} [options]
quilesj7e13aeb2019-10-08 13:34:55 +02001284 -c|--config [configuration_file]: loads the configuration file (default: ./lcm.cfg)
tiernoa9843d82018-10-24 10:44:20 +02001285 --health-check: do not run lcm, but inspect kafka bus to determine if lcm is healthy
tierno275411e2018-05-16 14:33:32 +02001286 -h|--help: shows this help
garciadeblas5697b8b2021-03-24 09:17:02 +01001287 """.format(
1288 sys.argv[0]
1289 )
1290 )
tierno750b2452018-05-17 16:39:29 +02001291 # --log-socket-host HOST: send logs to this host")
1292 # --log-socket-port PORT: send logs using this port (default: 9022)")
tierno275411e2018-05-16 14:33:32 +02001293
1294
garciadeblas5697b8b2021-03-24 09:17:02 +01001295if __name__ == "__main__":
tierno275411e2018-05-16 14:33:32 +02001296 try:
tierno8c16b052020-02-05 15:08:32 +00001297 # print("SYS.PATH='{}'".format(sys.path))
tierno275411e2018-05-16 14:33:32 +02001298 # load parameters and configuration
quilesj7e13aeb2019-10-08 13:34:55 +02001299 # -h
1300 # -c value
1301 # --config value
1302 # --help
1303 # --health-check
garciadeblas5697b8b2021-03-24 09:17:02 +01001304 opts, args = getopt.getopt(
1305 sys.argv[1:], "hc:", ["config=", "help", "health-check"]
1306 )
tierno275411e2018-05-16 14:33:32 +02001307 # TODO add "log-socket-host=", "log-socket-port=", "log-file="
1308 config_file = None
1309 for o, a in opts:
1310 if o in ("-h", "--help"):
1311 usage()
1312 sys.exit()
1313 elif o in ("-c", "--config"):
1314 config_file = a
tiernoa9843d82018-10-24 10:44:20 +02001315 elif o == "--health-check":
tierno94f06112020-02-11 12:38:19 +00001316 from osm_lcm.lcm_hc import health_check
garciadeblas5697b8b2021-03-24 09:17:02 +01001317
aticig56b86c22022-06-29 10:43:05 +03001318 health_check(config_file, Lcm.ping_interval_pace)
tierno275411e2018-05-16 14:33:32 +02001319 else:
Gabriel Cuba4c0e6802023-10-09 13:22:38 -05001320 print(f"Unhandled option: {o}")
1321 exit(1)
quilesj7e13aeb2019-10-08 13:34:55 +02001322
tierno275411e2018-05-16 14:33:32 +02001323 if config_file:
1324 if not path.isfile(config_file):
garciadeblas5697b8b2021-03-24 09:17:02 +01001325 print(
1326 "configuration file '{}' does not exist".format(config_file),
1327 file=sys.stderr,
1328 )
tierno275411e2018-05-16 14:33:32 +02001329 exit(1)
1330 else:
garciadeblas5697b8b2021-03-24 09:17:02 +01001331 for config_file in (
1332 __file__[: __file__.rfind(".")] + ".cfg",
1333 "./lcm.cfg",
1334 "/etc/osm/lcm.cfg",
1335 ):
tierno275411e2018-05-16 14:33:32 +02001336 if path.isfile(config_file):
1337 break
1338 else:
garciadeblas5697b8b2021-03-24 09:17:02 +01001339 print(
1340 "No configuration file 'lcm.cfg' found neither at local folder nor at /etc/osm/",
1341 file=sys.stderr,
1342 )
tierno275411e2018-05-16 14:33:32 +02001343 exit(1)
k4.rahul44306072023-05-05 14:24:31 +05301344 config_file = os.path.realpath(os.path.normpath(os.path.abspath(config_file)))
tierno275411e2018-05-16 14:33:32 +02001345 lcm = Lcm(config_file)
Mark Beierl1addc932023-05-18 15:11:34 -04001346 asyncio.run(lcm.start())
tierno22f4f9c2018-06-11 18:53:39 +02001347 except (LcmException, getopt.GetoptError) as e:
tierno275411e2018-05-16 14:33:32 +02001348 print(str(e), file=sys.stderr)
1349 # usage()
1350 exit(1)