X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_lcm%2Fnetslice.py;h=6f4e5479b9d62329f6838702e542dc2fedc039f8;hb=7ecbc3423bc9e08a662780b2eec10ee7cfe78b74;hp=839549b0c255af538ecb23ddc5a895ed17ad34eb;hpb=8069ce54d91918c53ec3bf9f8fd4eee8b6aacb93;p=osm%2FLCM.git diff --git a/osm_lcm/netslice.py b/osm_lcm/netslice.py index 839549b..6f4e547 100644 --- a/osm_lcm/netslice.py +++ b/osm_lcm/netslice.py @@ -17,9 +17,8 @@ import asyncio import logging import logging.handlers import traceback -from osm_lcm.ns import populate_dict as populate_dict -from osm_lcm import ROclient, ns -from osm_lcm.lcm_utils import LcmException, LcmBase +from osm_lcm import ROclient +from osm_lcm.lcm_utils import LcmException, LcmBase, populate_dict, get_iterable, deep_get from osm_common.dbbase import DbException from time import time from copy import deepcopy @@ -28,23 +27,11 @@ from copy import deepcopy __author__ = "Felipe Vicens, Pol Alemany, Alfonso Tierno" -def get_iterable(in_dict, in_key): - """ - Similar to .get(), but if value is None, False, ..., An empty tuple is returned instead - :param in_dict: a dictionary - :param in_key: the key to look for at in_dict - :return: in_dict[in_var] or () if it is None or not present - """ - if not in_dict.get(in_key): - return () - return in_dict[in_key] - - class NetsliceLcm(LcmBase): - total_deploy_timeout = 2 * 3600 # global timeout for deployment + timeout_nsi_deploy = 2 * 3600 # default global timeout for deployment a nsi - def __init__(self, db, msg, fs, lcm_tasks, ro_config, vca_config, loop): + def __init__(self, db, msg, fs, lcm_tasks, config, loop, ns): """ Init, Connect to database, filesystem storage, and messaging :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', @@ -54,8 +41,9 @@ class NetsliceLcm(LcmBase): self.logger = logging.getLogger('lcm.netslice') self.loop = loop self.lcm_tasks = lcm_tasks - self.ns = ns.NsLcm(db, msg, fs, lcm_tasks, ro_config, vca_config, loop) - self.ro_config = ro_config + self.ns = ns + self.ro_config = config["ro_config"] + self.timeout = config["timeout"] super().__init__(db, msg, fs, self.logger) @@ -160,7 +148,7 @@ class NetsliceLcm(LcmBase): break # Creating netslice-vld at RO - RO_nsir = db_nsir["_admin"].get("deployed", {}).get("RO", []) + RO_nsir = deep_get(db_nsir, ("_admin", "deployed", "RO"), []) if vld_id in RO_nsir: db_nsir_update["_admin.deployed.RO"] = RO_nsir @@ -230,7 +218,6 @@ class NetsliceLcm(LcmBase): "external": mgmt_network, "type": "bridge"}]} # self.logger.debug(logging_text + step) - db_nsir_update["detailed-status"] = "Creating netslice-vld at RO" desc = await RO.create("ns", descriptor=RO_ns_params) db_nsir_update_RO = {} db_nsir_update_RO["netslice_scenario_id"] = desc["uuid"] @@ -261,7 +248,7 @@ class NetsliceLcm(LcmBase): if nss_cp_item["nss-ref"] == nss["nss-id"]: db_nsds = self.db.get_one("nsds", {"_id": nss["nsdId"]}) # Go for nsd, and search the CP that match with nst:CP to get vld-id-ref - for cp_nsd in db_nsds["connection-point"]: + for cp_nsd in db_nsds.get("connection-point", ()): if cp_nsd["name"] == nss_cp_item["nsd-connection-point-ref"]: if nslcmop.get("operationParams"): if nslcmop["operationParams"].get("nsName") == nss["nsName"]: @@ -275,7 +262,7 @@ class NetsliceLcm(LcmBase): nslcmop_vld.update(vld) vld_op_list.append(nslcmop_vld) nslcmop["operationParams"]["vld"] = vld_op_list - self.update_db_2("nslcmops", nslcmop["_id"], nslcmop) + self.update_db_2("nslcmops", nslcmop["_id"], {"operationParams.vld": vld_op_list}) return nsr_id, nslcmop try: @@ -287,21 +274,33 @@ class NetsliceLcm(LcmBase): step = "Getting nsilcmop={} from db".format(nsilcmop_id) db_nsilcmop = self.db.get_one("nsilcmops", {"_id": nsilcmop_id}) + start_deploy = time() + nsi_params = db_nsilcmop.get("operationParams") + if nsi_params and nsi_params.get("timeout_nsi_deploy"): + timeout_nsi_deploy = nsi_params["timeout_nsi_deploy"] + else: + timeout_nsi_deploy = self.timeout.get("nsi_deploy", self.timeout_nsi_deploy) + # Empty list to keep track of network service records status in the netslice nsir_admin = db_nsir_admin = db_nsir.get("_admin") + step = "Creating slice operational-status init" # Slice status Creating db_nsir_update["detailed-status"] = "creating" db_nsir_update["operational-status"] = "init" - self.update_db_2("nsis", nsir_id, db_nsir_update) + db_nsir_update["_admin.nsiState"] = "INSTANTIATED" + step = "Instantiating netslice VLDs before NS instantiation" # Creating netslice VLDs networking before NS instantiation + db_nsir_update["detailed-status"] = step + self.update_db_2("nsis", nsir_id, db_nsir_update) db_nsir_update["_admin.deployed.RO"] = db_nsir_admin["deployed"]["RO"] for vld_item in get_iterable(nsir_admin, "netslice-vld"): await netslice_scenario_create(self, vld_item, nsir_id, db_nsir, db_nsir_admin, db_nsir_update) self.update_db_2("nsis", nsir_id, db_nsir_update) - db_nsir_update["detailed-status"] = "Creating netslice subnets at RO" + step = "Instantiating netslice subnets" + db_nsir_update["detailed-status"] = step self.update_db_2("nsis", nsir_id, db_nsir_update) db_nsir = self.db.get_one("nsis", {"_id": nsir_id}) @@ -317,9 +316,7 @@ class NetsliceLcm(LcmBase): # self.update_db_2("nsis", nsir_id, db_nsir_update) # Iterate over the network services operation ids to instantiate NSs - # TODO: (future improvement) look another way check the tasks instead of keep asking - # -> https://docs.python.org/3/library/asyncio-task.html#waiting-primitives - # steps: declare ns_tasks, add task when terminate is called, await asyncio.wait(vca_task_list, timeout=300) + step = "Instantiating Netslice Subnets" db_nsir = self.db.get_one("nsis", {"_id": nsir_id}) nslcmop_ids = db_nsilcmop["operationParams"].get("nslcmops_ids") for nslcmop_id in nslcmop_ids: @@ -331,17 +328,15 @@ class NetsliceLcm(LcmBase): self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_instantiate", task) # Wait until Network Slice is ready - step = nsir_status_detailed = " Waiting nsi ready. nsi_id={}".format(nsir_id) + step = " Waiting nsi ready." nsrs_detailed_list_old = None self.logger.debug(logging_text + step) - # TODO: substitute while for await (all task to be done or not) - deployment_timeout = 2 * 3600 # Two hours - while deployment_timeout > 0: + # For HA, it is checked from database, as the ns operation may be managed by other LCM worker + while time() <= start_deploy + timeout_nsi_deploy: # Check ns instantiation status nsi_ready = True nsir = self.db.get_one("nsis", {"_id": nsir_id}) - nsir_admin = nsir["_admin"] nsrs_detailed_list = nsir["_admin"]["nsrs-detailed-list"] nsrs_detailed_list_new = [] for nslcmop_item in nslcmop_ids: @@ -351,32 +346,33 @@ class NetsliceLcm(LcmBase): for nss in nsrs_detailed_list: if nss["nsrId"] == nslcmop["nsInstanceId"]: nss.update({"nsrId": nslcmop["nsInstanceId"], "status": nslcmop["operationState"], - "detailed-status": - nsir_status_detailed + "; {}".format(nslcmop.get("detailed-status")), + "detailed-status": nslcmop.get("detailed-status"), "instantiated": True}) nsrs_detailed_list_new.append(nss) - if status not in ["COMPLETED", "PARTIALLY_COMPLETED", "FAILED", "FAILED_TEMP"]: + if status not in ["COMPLETED", "PARTIALLY_COMPLETED", "FAILED", "FAILED_TEMP"]: nsi_ready = False if nsrs_detailed_list_new != nsrs_detailed_list_old: - nsir_admin["nsrs-detailed-list"] = nsrs_detailed_list_new nsrs_detailed_list_old = nsrs_detailed_list_new - db_nsir_update["_admin"] = nsir_admin - self.update_db_2("nsis", nsir_id, db_nsir_update) + self.update_db_2("nsis", nsir_id, {"_admin.nsrs-detailed-list": nsrs_detailed_list_new}) if nsi_ready: - step = "Network Slice Instance is ready. nsi_id={}".format(nsir_id) - for items in nsrs_detailed_list: - if "FAILED" in items.values(): - raise LcmException("Error deploying NSI: {}".format(nsir_id)) + error_list = [] + step = "Network Slice Instance instantiated" + for nss in nsrs_detailed_list: + if nss["status"] in ("FAILED", "FAILED_TEMP"): + error_list.append("NS {} {}: {}".format(nss["nsrId"], nss["status"], + nss["detailed-status"])) + if error_list: + step = "instantiating" + raise LcmException("; ".join(error_list)) break - + # TODO: future improvement due to synchronism -> await asyncio.wait(vca_task_list, timeout=300) await asyncio.sleep(5, loop=self.loop) - deployment_timeout -= 5 - - if deployment_timeout <= 0: - raise LcmException("Timeout waiting nsi to be ready. nsi_id={}".format(nsir_id)) + + else: # timeout_nsi_deploy reached: + raise LcmException("Timeout waiting nsi to be ready.") db_nsir_update["operational-status"] = "running" db_nsir_update["detailed-status"] = "done" @@ -401,13 +397,13 @@ class NetsliceLcm(LcmBase): if db_nsir: db_nsir_update["detailed-status"] = "ERROR {}: {}".format(step, exc) db_nsir_update["operational-status"] = "failed" + db_nsir_update["config-status"] = "configured" if db_nsilcmop: db_nsilcmop_update["detailed-status"] = "FAILED {}: {}".format(step, exc) db_nsilcmop_update["operationState"] = nsilcmop_operation_state = "FAILED" db_nsilcmop_update["statusEnteredTime"] = time() try: if db_nsir: - db_nsir_update["_admin.nsiState"] = "INSTANTIATED" db_nsir_update["_admin.nsilcmop"] = None self.update_db_2("nsis", nsir_id, db_nsir_update) if db_nsilcmop: @@ -464,13 +460,13 @@ class NetsliceLcm(LcmBase): self.update_db_2("nsis", nsir_id, db_nsir_update) # Gets the list to keep track of network service records status in the netslice - nsir_admin = db_nsir["_admin"] nsrs_detailed_list = [] # Iterate over the network services operation ids to terminate NSs # TODO: (future improvement) look another way check the tasks instead of keep asking # -> https://docs.python.org/3/library/asyncio-task.html#waiting-primitives # steps: declare ns_tasks, add task when terminate is called, await asyncio.wait(vca_task_list, timeout=300) + step = "Terminating Netslice Subnets" nslcmop_ids = db_nsilcmop["operationParams"].get("nslcmops_ids") nslcmop_new = [] for nslcmop_id in nslcmop_ids: @@ -503,7 +499,6 @@ class NetsliceLcm(LcmBase): # Check ns termination status nsi_ready = True db_nsir = self.db.get_one("nsis", {"_id": nsir_id}) - nsir_admin = db_nsir["_admin"] nsrs_detailed_list = db_nsir["_admin"].get("nsrs-detailed-list") nsrs_detailed_list_new = [] for nslcmop_item in nslcmop_ids: @@ -520,15 +515,12 @@ class NetsliceLcm(LcmBase): nsi_ready = False if nsrs_detailed_list_new != nsrs_detailed_list_old: - nsir_admin["nsrs-detailed-list"] = nsrs_detailed_list_new nsrs_detailed_list_old = nsrs_detailed_list_new - db_nsir_update["_admin"] = nsir_admin - self.update_db_2("nsis", nsir_id, db_nsir_update) - + self.update_db_2("nsis", nsir_id, {"_admin.nsrs-detailed-list": nsrs_detailed_list_new}) + if nsi_ready: # Check if it is the last used nss and mark isinstantiate: False db_nsir = self.db.get_one("nsis", {"_id": nsir_id}) - nsir_admin = db_nsir["_admin"] nsrs_detailed_list = db_nsir["_admin"].get("nsrs-detailed-list") for nss in nsrs_detailed_list: _filter = {"_admin.nsrs-detailed-list.ANYINDEX.nsrId": nss["nsrId"], @@ -537,8 +529,6 @@ class NetsliceLcm(LcmBase): nsis_list = self.db.get_one("nsis", _filter, fail_on_empty=False, fail_on_more=False) if not nsis_list: nss.update({"instantiated": False}) - db_nsir_update["_admin"] = nsir_admin - self.update_db_2("nsis", nsir_id, db_nsir_update) step = "Network Slice Instance is terminated. nsi_id={}".format(nsir_id) for items in nsrs_detailed_list: