| 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 |
| Daniel Arndt | c56d336 | 2023-04-18 11:26:16 -0300 | [diff] [blame] | 25 | from os import path |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 26 | |
| Daniel Arndt | c56d336 | 2023-04-18 11:26:16 -0300 | [diff] [blame] | 27 | import yaml |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 28 | from osm_common.dbbase import DbException |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 29 | from osm_common.temporal_constants import LCM_TASK_QUEUE |
| Daniel Arndt | c56d336 | 2023-04-18 11:26:16 -0300 | [diff] [blame] | 30 | from temporalio.client import Client |
| 31 | from temporalio.worker import Worker |
| 32 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 33 | from osm_lcm.data_utils.database.database import Database |
| 34 | from osm_lcm.data_utils.lcm_config import LcmCfg |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 35 | from osm_lcm.lcm_utils import LcmException |
| Daniel Arndt | c56d336 | 2023-04-18 11:26:16 -0300 | [diff] [blame] | 36 | from osm_lcm.temporal.juju_paas_activities import JujuPaasConnector |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 37 | from osm_lcm.temporal.lcm_activities import NsLcmActivity |
| 38 | from osm_lcm.temporal.lcm_workflows import NsNoOpWorkflow |
| Daniel Arndt | c56d336 | 2023-04-18 11:26:16 -0300 | [diff] [blame] | 39 | from osm_lcm.temporal.ns_activities import NsDbActivity, NsOperations |
| 40 | from osm_lcm.temporal.ns_workflows import NsInstantiateWorkflow |
| 41 | from osm_lcm.temporal.vdu_workflows import VduInstantiateWorkflow |
| Mark Beierl | 0c202d2 | 2023-04-06 13:58:31 +0000 | [diff] [blame] | 42 | from osm_lcm.temporal.vim_activities import VimDbActivity |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 43 | from osm_lcm.temporal.vim_workflows import ( |
| 44 | VimCreateWorkflow, |
| 45 | VimDeleteWorkflow, |
| 46 | VimUpdateWorkflow, |
| 47 | ) |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 48 | from osm_lcm.temporal.vnf_activities import ( |
| 49 | VnfDbActivity, |
| 50 | VnfOperations, |
| 51 | VnfSendNotifications, |
| 52 | ) |
| Daniel Arndt | c56d336 | 2023-04-18 11:26:16 -0300 | [diff] [blame] | 53 | from osm_lcm.temporal.vnf_workflows import VnfInstantiateWorkflow, VnfPrepareWorkflow |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 54 | |
| 55 | |
| 56 | class NGLcm: |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 57 | main_config = LcmCfg() |
| 58 | |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 59 | def __init__(self, config_file): |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 60 | """ |
| 61 | Init, Connect to database, filesystem storage, and messaging |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 62 | :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] | 63 | :return: None |
| 64 | """ |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 65 | self.db: Database = None |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 66 | self.logger = logging.getLogger("lcm") |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 67 | self._load_configuration(config_file) |
| 68 | self._configure_logging() |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 69 | |
| 70 | try: |
| 71 | self.db = Database(self.main_config.to_dict()).instance.db |
| Mark Beierl | 90f700d | 2023-02-09 15:01:33 -0500 | [diff] [blame] | 72 | except DbException as e: |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 73 | self.logger.critical(str(e), exc_info=True) |
| 74 | raise LcmException(str(e)) |
| 75 | |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 76 | def _load_configuration(self, config_file): |
| 77 | config = self._read_config_file(config_file) |
| 78 | self.main_config.set_from_dict(config) |
| 79 | self.main_config.transform() |
| 80 | self.main_config.load_from_env() |
| 81 | self.logger.critical("Loaded configuration:" + str(self.main_config.to_dict())) |
| 82 | |
| 83 | def _read_config_file(self, config_file): |
| 84 | try: |
| 85 | with open(config_file) as f: |
| 86 | return yaml.safe_load(f) |
| 87 | except Exception as e: |
| 88 | self.logger.critical("At config file '{}': {}".format(config_file, e)) |
| 89 | exit(1) |
| 90 | |
| 91 | @staticmethod |
| 92 | def _get_log_formatter_simple(): |
| 93 | log_format_simple = ( |
| 94 | "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s" |
| 95 | ) |
| 96 | return logging.Formatter(log_format_simple, datefmt="%Y-%m-%dT%H:%M:%S") |
| 97 | |
| 98 | def _create_file_handler(self): |
| 99 | return logging.handlers.RotatingFileHandler( |
| 100 | self.main_config.globalConfig.logfile, |
| 101 | maxBytes=100e6, |
| 102 | backupCount=9, |
| 103 | delay=0, |
| 104 | ) |
| 105 | |
| 106 | def _log_other_modules(self): |
| 107 | for logger in ("message", "database", "storage", "tsdb", "temporal"): |
| 108 | logger_config = self.main_config.to_dict()[logger] |
| 109 | logger_module = logging.getLogger(logger_config["logger_name"]) |
| 110 | if logger_config["logfile"]: |
| 111 | file_handler = logging.handlers.RotatingFileHandler( |
| 112 | logger_config["logfile"], maxBytes=100e6, backupCount=9, delay=0 |
| 113 | ) |
| 114 | file_handler.setFormatter(self._get_log_formatter_simple()) |
| 115 | logger_module.addHandler(file_handler) |
| 116 | if logger_config["loglevel"]: |
| 117 | logger_module.setLevel(logger_config["loglevel"]) |
| Mark Beierl | dceed59 | 2023-04-11 21:03:56 +0000 | [diff] [blame] | 118 | logging.getLogger("juju.client.connection").setLevel(logging.CRITICAL) |
| Patricia Reinoso | 199fbfc | 2023-03-02 08:53:58 +0000 | [diff] [blame] | 119 | |
| 120 | def _configure_logging(self): |
| 121 | if self.main_config.globalConfig.logfile: |
| 122 | file_handler = self._create_file_handler() |
| 123 | file_handler.setFormatter(self._get_log_formatter_simple()) |
| 124 | self.logger.addHandler(file_handler) |
| 125 | |
| 126 | if not self.main_config.globalConfig.to_dict()["nologging"]: |
| 127 | str_handler = logging.StreamHandler() |
| 128 | str_handler.setFormatter(self._get_log_formatter_simple()) |
| 129 | self.logger.addHandler(str_handler) |
| 130 | |
| 131 | if self.main_config.globalConfig.to_dict()["loglevel"]: |
| 132 | self.logger.setLevel(self.main_config.globalConfig.loglevel) |
| 133 | |
| 134 | self._log_other_modules() |
| 135 | self.logger.critical("starting osm/nglcm") |
| 136 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 137 | async def start(self): |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 138 | temporal_api = ( |
| 139 | f"{self.main_config.temporal.host}:{str(self.main_config.temporal.port)}" |
| 140 | ) |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 141 | client = await Client.connect(temporal_api) |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 142 | |
| 143 | ns_operation_instance = NsOperations(self.db) |
| 144 | ns_data_activity_instance = NsDbActivity(self.db) |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 145 | nslcm_activity_instance = NsLcmActivity(self.db) |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 146 | paas_connector_instance = JujuPaasConnector(self.db) |
| 147 | vim_data_activity_instance = VimDbActivity(self.db) |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 148 | vnf_data_activity_instance = VnfDbActivity(self.db) |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 149 | vnf_operation_instance = VnfOperations(self.db) |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 150 | vnf_send_notifications_instance = VnfSendNotifications() |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 151 | |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 152 | workflows = [ |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 153 | NsInstantiateWorkflow, |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 154 | NsNoOpWorkflow, |
| 155 | VimCreateWorkflow, |
| 156 | VimDeleteWorkflow, |
| 157 | VimUpdateWorkflow, |
| Patricia Reinoso | 1fa7b6d | 2023-04-05 15:27:20 +0000 | [diff] [blame] | 158 | VduInstantiateWorkflow, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 159 | VnfInstantiateWorkflow, |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 160 | VnfPrepareWorkflow, |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 161 | ] |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 162 | activities = [ |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 163 | ns_data_activity_instance.update_ns_state, |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 164 | ns_operation_instance.get_vnf_record_ids, |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 165 | nslcm_activity_instance.update_ns_lcm_operation_state, |
| 166 | nslcm_activity_instance.no_op, |
| Patricia Reinoso | 911946d | 2023-04-12 15:54:43 +0000 | [diff] [blame] | 167 | paas_connector_instance.create_model, |
| Patricia Reinoso | 1fa7b6d | 2023-04-05 15:27:20 +0000 | [diff] [blame] | 168 | paas_connector_instance.deploy_charm, |
| 169 | paas_connector_instance.check_charm_status, |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 170 | paas_connector_instance.test_vim_connectivity, |
| 171 | vim_data_activity_instance.update_vim_operation_state, |
| 172 | vim_data_activity_instance.update_vim_state, |
| 173 | vim_data_activity_instance.delete_vim_record, |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 174 | vnf_data_activity_instance.change_vnf_state, |
| 175 | vnf_data_activity_instance.change_vnf_instantiation_state, |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 176 | vnf_operation_instance.get_task_queue, |
| Gulsum Atici | a1898b4 | 2023-04-25 15:48:10 +0300 | [diff] [blame] | 177 | vnf_operation_instance.get_vim_cloud, |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 178 | vnf_operation_instance.get_vnf_details, |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 179 | vnf_send_notifications_instance.send_notification_for_vnf, |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 180 | vnf_data_activity_instance.set_vnf_model, |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 181 | ] |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 182 | |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 183 | # Check if we are running under a debugger |
| 184 | debug = os.getenv("VSCODE_IPC_HOOK_CLI") is not None |
| 185 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 186 | worker = Worker( |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 187 | client, |
| 188 | task_queue=LCM_TASK_QUEUE, |
| 189 | workflows=workflows, |
| 190 | activities=activities, |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 191 | debug_mode=debug, |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 192 | ) |
| 193 | |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 194 | self.logger.info("Starting LCM temporal worker") |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 195 | await worker.run() |
| 196 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 197 | |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 198 | if __name__ == "__main__": |
| Mark Beierl | 821bfc9 | 2023-01-24 21:15:25 -0500 | [diff] [blame] | 199 | try: |
| 200 | opts, args = getopt.getopt( |
| 201 | sys.argv[1:], "hc:", ["config=", "help", "health-check"] |
| 202 | ) |
| 203 | # TODO add "log-socket-host=", "log-socket-port=", "log-file=" |
| 204 | config_file = None |
| 205 | for o, a in opts: |
| 206 | if o in ("-c", "--config"): |
| 207 | config_file = a |
| 208 | else: |
| 209 | assert False, "Unhandled option" |
| 210 | |
| 211 | if config_file: |
| 212 | if not path.isfile(config_file): |
| 213 | print( |
| 214 | "configuration file '{}' does not exist".format(config_file), |
| 215 | file=sys.stderr, |
| 216 | ) |
| 217 | exit(1) |
| 218 | else: |
| 219 | for config_file in ( |
| 220 | __file__[: __file__.rfind(".")] + ".cfg", |
| 221 | "./lcm.cfg", |
| 222 | "/etc/osm/lcm.cfg", |
| 223 | ): |
| 224 | print(f"{config_file}") |
| 225 | if path.isfile(config_file): |
| 226 | break |
| 227 | else: |
| 228 | print( |
| 229 | "No configuration file 'lcm.cfg' found neither at local folder nor at /etc/osm/", |
| 230 | file=sys.stderr, |
| 231 | ) |
| 232 | exit(1) |
| 233 | lcm = NGLcm(config_file) |
| 234 | asyncio.run(lcm.start()) |
| 235 | except (LcmException, getopt.GetoptError) as e: |
| 236 | print(str(e), file=sys.stderr) |
| 237 | # usage() |
| 238 | exit(1) |