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