| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | ## |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | ## |
| 18 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 19 | import asyncio |
| 20 | import getopt |
| 21 | import logging |
| 22 | import logging.handlers |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 23 | import os |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 24 | import sys |
| 25 | import yaml |
| 26 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 27 | from osm_common.dbbase import DbException |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 28 | from osm_common.temporal_constants import LCM_TASK_QUEUE |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 29 | from osm_lcm.data_utils.database.database import Database |
| 30 | from osm_lcm.data_utils.lcm_config import LcmCfg |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 31 | from osm_lcm.lcm_utils import LcmException |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 32 | from os import path |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 33 | from osm_lcm.temporal.lcm_activities import NsLcmActivity |
| 34 | from osm_lcm.temporal.lcm_workflows import NsNoOpWorkflow |
| Mark Beierl | 0c202d2 | 2023-04-06 13:58:31 +0000 | [diff] [blame] | 35 | from osm_lcm.temporal.vim_activities import VimDbActivity |
| 36 | from osm_lcm.temporal.juju_paas_activities import JujuPaasConnector |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 37 | from osm_lcm.temporal.vim_workflows import ( |
| 38 | VimCreateWorkflow, |
| 39 | VimDeleteWorkflow, |
| 40 | VimUpdateWorkflow, |
| 41 | ) |
| Patricia Reinoso | 1fa7b6d | 2023-04-05 15:27:20 +0000 | [diff] [blame] | 42 | from osm_lcm.temporal.vdu_workflows import VduInstantiateWorkflow |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 43 | from osm_lcm.temporal.vnf_workflows import VnfInstantiateWorkflow |
| 44 | from osm_lcm.temporal.vnf_activities import VnfDbActivity, VnfOperations |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 45 | from osm_lcm.temporal.ns_workflows import NsInstantiateWorkflow |
| 46 | from osm_lcm.temporal.ns_activities import NsOperations, NsDbActivity |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 47 | from temporalio.client import Client |
| 48 | from temporalio.worker import Worker |
| 49 | |
| 50 | |
| 51 | class NGLcm: |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 52 | main_config = LcmCfg() |
| 53 | |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 54 | def __init__(self, config_file): |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 55 | """ |
| 56 | Init, Connect to database, filesystem storage, and messaging |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 57 | :param config_file: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 58 | :return: None |
| 59 | """ |
| 60 | self.db = None |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 61 | self.logger = logging.getLogger("lcm") |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 62 | self._load_configuration(config_file) |
| 63 | self._configure_logging() |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 64 | |
| 65 | try: |
| 66 | self.db = Database(self.main_config.to_dict()).instance.db |
| Mark Beierl | 90f700d | 2023-02-09 15:01:33 -0500 | [diff] [blame] | 67 | except DbException as e: |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 68 | self.logger.critical(str(e), exc_info=True) |
| 69 | raise LcmException(str(e)) |
| 70 | |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 71 | def _load_configuration(self, config_file): |
| 72 | config = self._read_config_file(config_file) |
| 73 | self.main_config.set_from_dict(config) |
| 74 | self.main_config.transform() |
| 75 | self.main_config.load_from_env() |
| 76 | self.logger.critical("Loaded configuration:" + str(self.main_config.to_dict())) |
| 77 | |
| 78 | def _read_config_file(self, config_file): |
| 79 | try: |
| 80 | with open(config_file) as f: |
| 81 | return yaml.safe_load(f) |
| 82 | except Exception as e: |
| 83 | self.logger.critical("At config file '{}': {}".format(config_file, e)) |
| 84 | exit(1) |
| 85 | |
| 86 | @staticmethod |
| 87 | def _get_log_formatter_simple(): |
| 88 | log_format_simple = ( |
| 89 | "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s" |
| 90 | ) |
| 91 | return logging.Formatter(log_format_simple, datefmt="%Y-%m-%dT%H:%M:%S") |
| 92 | |
| 93 | def _create_file_handler(self): |
| 94 | return logging.handlers.RotatingFileHandler( |
| 95 | self.main_config.globalConfig.logfile, |
| 96 | maxBytes=100e6, |
| 97 | backupCount=9, |
| 98 | delay=0, |
| 99 | ) |
| 100 | |
| 101 | def _log_other_modules(self): |
| 102 | for logger in ("message", "database", "storage", "tsdb", "temporal"): |
| 103 | logger_config = self.main_config.to_dict()[logger] |
| 104 | logger_module = logging.getLogger(logger_config["logger_name"]) |
| 105 | if logger_config["logfile"]: |
| 106 | file_handler = logging.handlers.RotatingFileHandler( |
| 107 | logger_config["logfile"], maxBytes=100e6, backupCount=9, delay=0 |
| 108 | ) |
| 109 | file_handler.setFormatter(self._get_log_formatter_simple()) |
| 110 | logger_module.addHandler(file_handler) |
| 111 | if logger_config["loglevel"]: |
| 112 | logger_module.setLevel(logger_config["loglevel"]) |
| 113 | |
| 114 | def _configure_logging(self): |
| 115 | if self.main_config.globalConfig.logfile: |
| 116 | file_handler = self._create_file_handler() |
| 117 | file_handler.setFormatter(self._get_log_formatter_simple()) |
| 118 | self.logger.addHandler(file_handler) |
| 119 | |
| 120 | if not self.main_config.globalConfig.to_dict()["nologging"]: |
| 121 | str_handler = logging.StreamHandler() |
| 122 | str_handler.setFormatter(self._get_log_formatter_simple()) |
| 123 | self.logger.addHandler(str_handler) |
| 124 | |
| 125 | if self.main_config.globalConfig.to_dict()["loglevel"]: |
| 126 | self.logger.setLevel(self.main_config.globalConfig.loglevel) |
| 127 | |
| 128 | self._log_other_modules() |
| 129 | self.logger.critical("starting osm/nglcm") |
| 130 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 131 | async def start(self): |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 132 | temporal_api = ( |
| 133 | f"{self.main_config.temporal.host}:{str(self.main_config.temporal.port)}" |
| 134 | ) |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 135 | client = await Client.connect(temporal_api) |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 136 | |
| 137 | ns_operation_instance = NsOperations(self.db) |
| 138 | ns_data_activity_instance = NsDbActivity(self.db) |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 139 | nslcm_activity_instance = NsLcmActivity(self.db) |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 140 | paas_connector_instance = JujuPaasConnector(self.db) |
| 141 | vim_data_activity_instance = VimDbActivity(self.db) |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 142 | vnf_data_activity_instance = VnfDbActivity(self.db) |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 143 | vnf_operation_instance = VnfOperations(self.db) |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 144 | |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 145 | workflows = [ |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 146 | NsInstantiateWorkflow, |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 147 | NsNoOpWorkflow, |
| 148 | VimCreateWorkflow, |
| 149 | VimDeleteWorkflow, |
| 150 | VimUpdateWorkflow, |
| Patricia Reinoso | 1fa7b6d | 2023-04-05 15:27:20 +0000 | [diff] [blame] | 151 | VduInstantiateWorkflow, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 152 | VnfInstantiateWorkflow, |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 153 | ] |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 154 | activities = [ |
| Patricia Reinoso | 911946d | 2023-04-12 15:54:43 +0000 | [diff] [blame^] | 155 | ns_data_activity_instance.get_model_info, |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 156 | ns_data_activity_instance.prepare_vnf_records, |
| 157 | ns_data_activity_instance.update_ns_state, |
| 158 | ns_operation_instance.check_ns_instantiate_finished, |
| 159 | ns_operation_instance.deploy_ns, |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 160 | nslcm_activity_instance.update_ns_lcm_operation_state, |
| 161 | nslcm_activity_instance.no_op, |
| Patricia Reinoso | 911946d | 2023-04-12 15:54:43 +0000 | [diff] [blame^] | 162 | paas_connector_instance.create_model, |
| Patricia Reinoso | 1fa7b6d | 2023-04-05 15:27:20 +0000 | [diff] [blame] | 163 | paas_connector_instance.deploy_charm, |
| 164 | paas_connector_instance.check_charm_status, |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 165 | paas_connector_instance.test_vim_connectivity, |
| 166 | vim_data_activity_instance.update_vim_operation_state, |
| 167 | vim_data_activity_instance.update_vim_state, |
| 168 | vim_data_activity_instance.delete_vim_record, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 169 | vnf_operation_instance.get_task_queue, |
| 170 | vnf_data_activity_instance.change_nf_state, |
| 171 | vnf_data_activity_instance.change_nf_instantiation_state, |
| 172 | vnf_data_activity_instance.change_nf_notification_state, |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 173 | ] |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 174 | |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 175 | # Check if we are running under a debugger |
| 176 | debug = os.getenv("VSCODE_IPC_HOOK_CLI") is not None |
| 177 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 178 | worker = Worker( |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 179 | client, |
| 180 | task_queue=LCM_TASK_QUEUE, |
| 181 | workflows=workflows, |
| 182 | activities=activities, |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 183 | debug_mode=debug, |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 184 | ) |
| 185 | |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 186 | self.logger.info("Starting LCM temporal worker") |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 187 | await worker.run() |
| 188 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 189 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 190 | if __name__ == "__main__": |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 191 | try: |
| 192 | opts, args = getopt.getopt( |
| 193 | sys.argv[1:], "hc:", ["config=", "help", "health-check"] |
| 194 | ) |
| 195 | # TODO add "log-socket-host=", "log-socket-port=", "log-file=" |
| 196 | config_file = None |
| 197 | for o, a in opts: |
| 198 | if o in ("-c", "--config"): |
| 199 | config_file = a |
| 200 | else: |
| 201 | assert False, "Unhandled option" |
| 202 | |
| 203 | if config_file: |
| 204 | if not path.isfile(config_file): |
| 205 | print( |
| 206 | "configuration file '{}' does not exist".format(config_file), |
| 207 | file=sys.stderr, |
| 208 | ) |
| 209 | exit(1) |
| 210 | else: |
| 211 | for config_file in ( |
| 212 | __file__[: __file__.rfind(".")] + ".cfg", |
| 213 | "./lcm.cfg", |
| 214 | "/etc/osm/lcm.cfg", |
| 215 | ): |
| 216 | print(f"{config_file}") |
| 217 | if path.isfile(config_file): |
| 218 | break |
| 219 | else: |
| 220 | print( |
| 221 | "No configuration file 'lcm.cfg' found neither at local folder nor at /etc/osm/", |
| 222 | file=sys.stderr, |
| 223 | ) |
| 224 | exit(1) |
| 225 | lcm = NGLcm(config_file) |
| 226 | asyncio.run(lcm.start()) |
| 227 | except (LcmException, getopt.GetoptError) as e: |
| 228 | print(str(e), file=sys.stderr) |
| 229 | # usage() |
| 230 | exit(1) |