From: cubag Date: Wed, 29 Nov 2023 21:01:21 +0000 (+0200) Subject: Revert "Remove unused methods" X-Git-Tag: release-v15.0-start~3 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F77%2F14077%2F2;p=osm%2FLCM.git Revert "Remove unused methods" This reverts commit 9e9a923448e6673fc0396265732115b55f1df9da. Change-Id: I1d8868ed98d83d22da2acbfa3963543ea9dd95ca Signed-off-by: Gabriel Cuba --- diff --git a/osm_lcm/ROclient.py b/osm_lcm/ROclient.py index e731ec1..1818e08 100644 --- a/osm_lcm/ROclient.py +++ b/osm_lcm/ROclient.py @@ -319,6 +319,195 @@ class ROClient: ) ) + @staticmethod + def check_ns_status(ns_descriptor): + """ + Inspect RO instance descriptor and indicates the status + :param ns_descriptor: instance descriptor obtained with self.show("ns", ) + :return: status, message: status can be BUILD,ACTIVE,ERROR, message is a text message + """ + error_list = [] + total = {"VMs": 0, "networks": 0, "SDN_networks": 0} + done = {"VMs": 0, "networks": 0, "SDN_networks": 0} + + def _get_ref(desc): + # return an identification for the network or vm. Try vim_id if exist, if not descriptor id for net + if desc.get("vim_net_id"): + return "'vim-net-id={}'".format(desc["vim_net_id"]) + elif desc.get("ns_net_osm_id"): + return "'nsd-vld-id={}'".format(desc["ns_net_osm_id"]) + elif desc.get("vnf_net_osm_id"): + return "'vnfd-vld-id={}'".format(desc["vnf_net_osm_id"]) + # for VM + elif desc.get("vim_vm_id"): + return "'vim-vm-id={}'".format(desc["vim_vm_id"]) + elif desc.get("vdu_osm_id"): + return "'vnfd-vdu-id={}'".format(desc["vdu_osm_id"]) + else: + return "" + + def _get_sdn_ref(sce_net_id): + # look for the network associated to the SDN network and obtain the identification + net = next( + (x for x in ns_descriptor["nets"] if x.get("sce_net_id") == sce_net_id), + None, + ) + if not sce_net_id or not net: + return "" + return _get_ref(net) + + try: + total["networks"] = len(ns_descriptor["nets"]) + for net in ns_descriptor["nets"]: + if net["status"] in ("ERROR", "VIM_ERROR"): + error_list.append( + "Error at VIM network {}: {}".format( + _get_ref(net), net["error_msg"] + ) + ) + elif net["status"] == "ACTIVE": + done["networks"] += 1 + + total["SDN_networks"] = len(ns_descriptor["sdn_nets"]) + for sdn_net in ns_descriptor["sdn_nets"]: + if sdn_net["status"] in ("ERROR", "VIM_ERROR", "WIM_ERROR"): + error_list.append( + "Error at SDN network {}: {}".format( + _get_sdn_ref(sdn_net.get("sce_net_id")), + sdn_net["error_msg"], + ) + ) + elif sdn_net["status"] == "ACTIVE": + done["SDN_networks"] += 1 + + for vnf in ns_descriptor["vnfs"]: + for vm in vnf["vms"]: + total["VMs"] += 1 + if vm["status"] in ("ERROR", "VIM_ERROR"): + error_list.append( + "Error at VIM VM {}: {}".format( + _get_ref(vm), vm["error_msg"] + ) + ) + elif vm["status"] == "ACTIVE": + done["VMs"] += 1 + if error_list: + # skip errors caused because other dependendent task is on error + return "ERROR", "; ".join( + [ + el + for el in error_list + if "because depends on failed ACTION" not in el + ] + ) + if all(total[x] == done[x] for x in total): # DONE == TOTAL for all items + return "ACTIVE", str( + {x: total[x] for x in total if total[x]} + ) # print only those which value is not 0 + else: + return "BUILD", str( + {x: "{}/{}".format(done[x], total[x]) for x in total if total[x]} + ) + # print done/total for each item if total is not 0 + except Exception as e: + raise ROClientException( + "Unexpected RO ns descriptor. Wrong version? {}".format(e) + ) from e + + @staticmethod + def check_action_status(action_descriptor): + """ + Inspect RO instance descriptor and indicates the status + :param action_descriptor: action instance descriptor obtained with self.show("ns", "action") + :return: status, message: status can be BUILD,ACTIVE,ERROR, message is a text message + """ + net_total = 0 + vm_total = 0 + net_done = 0 + vm_done = 0 + other_total = 0 + other_done = 0 + + for vim_action_set in action_descriptor["actions"]: + for vim_action in vim_action_set["vim_wim_actions"]: + if vim_action["item"] == "instance_vms": + vm_total += 1 + elif vim_action["item"] == "instance_nets": + net_total += 1 + else: + other_total += 1 + if vim_action["status"] == "FAILED": + return "ERROR", vim_action["error_msg"] + elif vim_action["status"] in ("DONE", "SUPERSEDED", "FINISHED"): + if vim_action["item"] == "instance_vms": + vm_done += 1 + elif vim_action["item"] == "instance_nets": + net_done += 1 + else: + other_done += 1 + + if net_total == net_done and vm_total == vm_done and other_total == other_done: + return "ACTIVE", "VMs {}, networks: {}, other: {} ".format( + vm_total, net_total, other_total + ) + else: + return "BUILD", "VMs: {}/{}, networks: {}/{}, other: {}/{}".format( + vm_done, vm_total, net_done, net_total, other_done, other_total + ) + + @staticmethod + def get_ns_vnf_info(ns_descriptor): + """ + Get a dict with the VIM_id, ip_addresses, mac_addresses of every vnf and vdu + :param ns_descriptor: instance descriptor obtained with self.show("ns", ) + :return: dict with: + : + ip_address: XXXX, + vdur: + : + ip_address: XXX + vim_id: XXXX + interfaces: + : + ip_address: XXX + mac_address: XXX + """ + ns_info = {} + for vnf in ns_descriptor["vnfs"]: + if not vnf.get("ip_address") and vnf.get("vms"): + raise ROClientException( + "ns member_vnf_index '{}' has no IP address".format( + vnf["member_vnf_index"] + ), + http_code=409, + ) + vnfr_info = {"ip_address": vnf.get("ip_address"), "vdur": {}} + for vm in vnf["vms"]: + vdur = { + "vim_id": vm.get("vim_vm_id"), + "ip_address": vm.get("ip_address"), + "interfaces": {}, + } + for iface in vm["interfaces"]: + if iface.get("type") == "mgmt" and not iface.get("ip_address"): + raise ROClientException( + "ns member_vnf_index '{}' vm '{}' management interface '{}' has no IP " + "address".format( + vnf["member_vnf_index"], + vm["vdu_osm_id"], + iface["external_name"], + ), + http_code=409, + ) + vdur["interfaces"][iface["internal_name"]] = { + "ip_address": iface.get("ip_address"), + "mac_address": iface.get("mac_address"), + "vim_id": iface.get("vim_interface_id"), + } + vnfr_info["vdur"][vm["vdu_osm_id"]] = vdur + ns_info[str(vnf["member_vnf_index"])] = vnfr_info + return ns_info + async def _get_item_uuid(self, session, item, item_id_name, all_tenants=False): if all_tenants: tenant_text = "/any" @@ -382,6 +571,49 @@ class ROClient: ) return uuid + async def _get_item( + self, + session, + item, + item_id_name, + extra_item=None, + extra_item_id=None, + all_tenants=False, + ): + if all_tenants: + tenant_text = "/any" + elif all_tenants is None: + tenant_text = "" + else: + if not self.tenant: + await self._get_tenant(session) + tenant_text = "/" + self.tenant + + if self.check_if_uuid(item_id_name): + uuid = item_id_name + else: + # check that exist + uuid = await self._get_item_uuid(session, item, item_id_name, all_tenants) + + url = "{}{}/{}/{}".format(self.uri, tenant_text, item, uuid) + if extra_item: + url += "/" + extra_item + if extra_item_id: + url += "/" + extra_item_id + self.logger.debug("GET %s", url) + # timeout = aiohttp.ClientTimeout(total=self.timeout_short) + async with session.get(url, headers=self.headers_req) as response: + response_text = await response.read() + self.logger.debug( + "GET {} [{}] {}".format(url, response.status, response_text[:100]) + ) + if response.status >= 300: + raise ROClientException( + self._parse_error_yaml(response_text), http_code=response.status + ) + + return self._parse_yaml(response_text, response=True) + async def _get_tenant(self, session): if not self.tenant: self.tenant = await self._get_item_uuid( @@ -644,6 +876,49 @@ class ROClient: except asyncio.TimeoutError: raise ROClientException("Timeout", http_code=504) + async def show( + self, + item, + item_id_name=None, + extra_item=None, + extra_item_id=None, + all_tenants=False, + ): + """ + Obtain the information of an item from its id or name + :param item: can be 'tenant', 'vim', 'vnfd', 'nsd', 'ns' + :param item_id_name: RO id or name of the item. Raise and exception if more than one found + :param extra_item: if supplied, it is used to add to the URL. + Can be 'action' if item='ns'; 'networks' or'images' if item='vim' + :param extra_item_id: if supplied, it is used get details of a concrete extra_item. + :param all_tenants: True if not filtering by tenant. Only allowed for admin + :return: dictionary with the information or raises ROClientException on Error, NotFound, found several + """ + try: + if item not in self.client_to_RO: + raise ROClientException("Invalid item {}".format(item)) + if item == "tenant": + all_tenants = None + elif item == "vim": + all_tenants = True + elif item == "vim_account": + all_tenants = False + + async with aiohttp.ClientSession() as session: + content = await self._get_item( + session, + self.client_to_RO[item], + item_id_name, + extra_item=extra_item, + extra_item_id=extra_item_id, + all_tenants=all_tenants, + ) + return remove_envelop(item, content) + except (aiohttp.ClientOSError, aiohttp.ClientError) as e: + raise ROClientException(e, http_code=504) + except asyncio.TimeoutError: + raise ROClientException("Timeout", http_code=504) + async def delete(self, item, item_id_name=None, all_tenants=False): """ Delete the information of an item from its id or name @@ -787,6 +1062,68 @@ class ROClient: except asyncio.TimeoutError: raise ROClientException("Timeout", http_code=504) + async def create_action( + self, item, item_id_name, descriptor=None, descriptor_format=None, **kwargs + ): + """ + Performs an action over an item + :param item: can be 'tenant', 'vnfd', 'nsd', 'ns', 'vim', 'vim_account', 'sdn' + :param item_id_name: RO id or name of the item. Raise and exception if more than one found + :param descriptor: can be a dict, or a yaml/json text. Autodetect unless descriptor_format is provided + :param descriptor_format: Can be 'json' or 'yaml' + :param kwargs: Overrides descriptor with values as name, description, vim_url, vim_url_admin, vim_type + keys can be a dot separated list to specify elements inside dict + :return: dictionary with the information or raises ROClientException on Error + """ + try: + if isinstance(descriptor, str): + descriptor = self._parse(descriptor, descriptor_format) + elif descriptor: + pass + else: + descriptor = {} + + if item not in self.client_to_RO: + raise ROClientException("Invalid item {}".format(item)) + desc = remove_envelop(item, descriptor) + + # Override descriptor with kwargs + if kwargs: + desc = self.update_descriptor(desc, kwargs) + + all_tenants = False + if item in ("tenant", "vim"): + all_tenants = None + + action = None + if item == "vims": + action = "sdn_mapping" + elif item in ("vim_account", "ns"): + action = "action" + + # create_desc = self._create_envelop(item, desc) + create_desc = desc + + async with aiohttp.ClientSession() as session: + _all_tenants = all_tenants + if item == "vim": + _all_tenants = True + # item_id = await self._get_item_uuid(session, self.client_to_RO[item], item_id_name, + # all_tenants=_all_tenants) + outdata = await self._create_item( + session, + self.client_to_RO[item], + create_desc, + item_id_name=item_id_name, # item_id_name=item_id + action=action, + all_tenants=_all_tenants, + ) + return remove_envelop(item, outdata) + except (aiohttp.ClientOSError, aiohttp.ClientError) as e: + raise ROClientException(e, http_code=504) + except asyncio.TimeoutError: + raise ROClientException("Timeout", http_code=504) + async def attach( self, item, item_id_name=None, descriptor=None, descriptor_format=None, **kwargs ): @@ -906,3 +1243,210 @@ class ROClient: raise ROClientException(e, http_code=504) except asyncio.TimeoutError: raise ROClientException("Timeout", http_code=504) + + # TODO convert to asyncio + # DATACENTERS + + def edit_datacenter( + self, + uuid=None, + name=None, + descriptor=None, + descriptor_format=None, + all_tenants=False, + **kwargs + ): + """Edit the parameters of a datacenter + Params: must supply a descriptor or/and a parameter to change + uuid or/and name. If only name is supplied, there must be only one or an exception is raised + descriptor: with format {'datacenter':{params to change info}} + must be a dictionary or a json/yaml text. + parameters to change can be supplyied by the descriptor or as parameters: + new_name: the datacenter name + vim_url: the datacenter URL + vim_url_admin: the datacenter URL for administrative issues + vim_type: the datacenter type, can be openstack or openvim. + public: boolean, available to other tenants + description: datacenter description + Return: Raises an exception on error, not found or found several + Obtain a dictionary with format {'datacenter':{new_datacenter_info}} + """ + + if isinstance(descriptor, str): + descriptor = self._parse(descriptor, descriptor_format) + elif descriptor: + pass + elif kwargs: + descriptor = {"datacenter": {}} + else: + raise ROClientException("Missing descriptor") + + if "datacenter" not in descriptor or len(descriptor) != 1: + raise ROClientException( + "Descriptor must contain only one 'datacenter' field" + ) + for param in kwargs: + if param == "new_name": + descriptor["datacenter"]["name"] = kwargs[param] + else: + descriptor["datacenter"][param] = kwargs[param] + return self._edit_item("datacenters", descriptor, uuid, name, all_tenants=None) + + def edit_scenario( + self, + uuid=None, + name=None, + descriptor=None, + descriptor_format=None, + all_tenants=False, + **kwargs + ): + """Edit the parameters of a scenario + Params: must supply a descriptor or/and a parameters to change + uuid or/and name. If only name is supplied, there must be only one or an exception is raised + descriptor: with format {'scenario':{params to change info}} + must be a dictionary or a json/yaml text. + parameters to change can be supplyied by the descriptor or as parameters: + new_name: the scenario name + public: boolean, available to other tenants + description: scenario description + tenant_id. Propietary tenant + Return: Raises an exception on error, not found or found several + Obtain a dictionary with format {'scenario':{new_scenario_info}} + """ + + if isinstance(descriptor, str): + descriptor = self._parse(descriptor, descriptor_format) + elif descriptor: + pass + elif kwargs: + descriptor = {"scenario": {}} + else: + raise ROClientException("Missing descriptor") + + if "scenario" not in descriptor or len(descriptor) > 2: + raise ROClientException("Descriptor must contain only one 'scenario' field") + for param in kwargs: + if param == "new_name": + descriptor["scenario"]["name"] = kwargs[param] + else: + descriptor["scenario"][param] = kwargs[param] + return self._edit_item("scenarios", descriptor, uuid, name, all_tenants=None) + + # VIM ACTIONS + def vim_action(self, action, item, uuid=None, all_tenants=False, **kwargs): + """Perform an action over a vim + Params: + action: can be 'list', 'get'/'show', 'delete' or 'create' + item: can be 'tenants' or 'networks' + uuid: uuid of the tenant/net to show or to delete. Ignore otherwise + other parameters: + datacenter_name, datacenter_id: datacenters to act on, if missing uses classes store datacenter + descriptor, descriptor_format: descriptor needed on creation, can be a dict or a yaml/json str + must be a dictionary or a json/yaml text. + name: for created tenant/net Overwrite descriptor name if any + description: tenant descriptor. Overwrite descriptor description if any + + Return: Raises an exception on error + Obtain a dictionary with format {'tenant':{new_tenant_info}} + """ + session = None # TODO remove when changed to asyncio + if item not in ("tenants", "networks", "images"): + raise ROClientException( + "Unknown value for item '{}', must be 'tenants', 'nets' or " + "images".format(str(item)) + ) + + image_actions = ["list", "get", "show", "delete"] + if item == "images" and action not in image_actions: + raise ROClientException( + "Only available actions for item '{}' are {}\n" + "Requested action was '{}'".format( + item, ", ".join(image_actions), action + ) + ) + if all_tenants: + tenant_text = "/any" + else: + tenant_text = "/" + self._get_tenant(session) + + if "datacenter_id" in kwargs or "datacenter_name" in kwargs: + datacenter = self._get_item_uuid( + session, + "datacenters", + kwargs.get("datacenter"), + all_tenants=all_tenants, + ) + else: + datacenter = self._get_datacenter(session) + + if action == "list": + url = "{}{}/vim/{}/{}".format(self.uri, tenant_text, datacenter, item) + self.logger.debug("GET %s", url) + mano_response = requests.get(url, headers=self.headers_req) + self.logger.debug("RO response: %s", mano_response.text) + content = self._parse_yaml(mano_response.text, response=True) + if mano_response.status_code == 200: + return content + else: + raise ROClientException(str(content), http_code=mano_response.status) + elif action == "get" or action == "show": + url = "{}{}/vim/{}/{}/{}".format( + self.uri, tenant_text, datacenter, item, uuid + ) + self.logger.debug("GET %s", url) + mano_response = requests.get(url, headers=self.headers_req) + self.logger.debug("RO response: %s", mano_response.text) + content = self._parse_yaml(mano_response.text, response=True) + if mano_response.status_code == 200: + return content + else: + raise ROClientException(str(content), http_code=mano_response.status) + elif action == "delete": + url = "{}{}/vim/{}/{}/{}".format( + self.uri, tenant_text, datacenter, item, uuid + ) + self.logger.debug("DELETE %s", url) + mano_response = requests.delete(url, headers=self.headers_req) + self.logger.debug("RO response: %s", mano_response.text) + content = self._parse_yaml(mano_response.text, response=True) + if mano_response.status_code == 200: + return content + else: + raise ROClientException(str(content), http_code=mano_response.status) + elif action == "create": + if "descriptor" in kwargs: + if isinstance(kwargs["descriptor"], str): + descriptor = self._parse( + kwargs["descriptor"], kwargs.get("descriptor_format") + ) + else: + descriptor = kwargs["descriptor"] + elif "name" in kwargs: + descriptor = {item[:-1]: {"name": kwargs["name"]}} + else: + raise ROClientException("Missing descriptor") + + if item[:-1] not in descriptor or len(descriptor) != 1: + raise ROClientException( + "Descriptor must contain only one 'tenant' field" + ) + if "name" in kwargs: + descriptor[item[:-1]]["name"] = kwargs["name"] + if "description" in kwargs: + descriptor[item[:-1]]["description"] = kwargs["description"] + payload_req = yaml.safe_dump(descriptor) + # print payload_req + url = "{}{}/vim/{}/{}".format(self.uri, tenant_text, datacenter, item) + self.logger.debug("RO POST %s %s", url, payload_req) + mano_response = requests.post( + url, headers=self.headers_req, data=payload_req + ) + self.logger.debug("RO response: %s", mano_response.text) + content = self._parse_yaml(mano_response.text, response=True) + if mano_response.status_code == 200: + return content + else: + raise ROClientException(str(content), http_code=mano_response.status) + else: + raise ROClientException("Unknown value for action '{}".format(str(action))) diff --git a/osm_lcm/data_utils/database/wim_account.py b/osm_lcm/data_utils/database/wim_account.py index 8c958c1..a1f1799 100644 --- a/osm_lcm/data_utils/database/wim_account.py +++ b/osm_lcm/data_utils/database/wim_account.py @@ -32,6 +32,16 @@ class WimAccountDB: def initialize_db(cls): cls.db = Database().instance.db + @classmethod + def get_wim_account_with_id(cls, wim_account_id): + if not cls.db: + cls.initialize_db() + if wim_account_id in cls.db_wims: + return cls.db_wims[wim_account_id] + db_wim = cls.db.get_one("wim_accounts", {"_id": wim_account_id}) or {} + cls.db_wims[wim_account_id] = db_wim + return db_wim + @classmethod def get_all_wim_accounts(cls): if not cls.db: diff --git a/osm_lcm/data_utils/nsd.py b/osm_lcm/data_utils/nsd.py index d436e1f..9b6fb81 100644 --- a/osm_lcm/data_utils/nsd.py +++ b/osm_lcm/data_utils/nsd.py @@ -21,6 +21,7 @@ # For those usages not covered by the Apache License, Version 2.0 please # contact: fbravo@whitestack.com ## +import ast from osm_lcm.data_utils.list_utils import find_in_list @@ -40,6 +41,12 @@ def get_virtual_link_profiles(nsd): return nsd.get("df")[0].get("virtual-link-profile", ()) +def replace_vnf_id(nsd, old_vnf_id, new_vnf_id): + dict_str = str(nsd) + dict_str.replace(old_vnf_id, new_vnf_id) + return ast.literal_eval(dict_str) + + def get_ns_configuration(nsd): return nsd.get("ns-configuration", {}) diff --git a/osm_lcm/data_utils/nsr.py b/osm_lcm/data_utils/nsr.py index 9b0570c..f2fccb6 100644 --- a/osm_lcm/data_utils/nsr.py +++ b/osm_lcm/data_utils/nsr.py @@ -26,6 +26,10 @@ from osm_lcm.data_utils import list_utils from osm_lcm.lcm_utils import get_iterable +def get_vlds(nsr): + return nsr.get("vld", ()) + + def get_deployed_kdu(nsr_deployed, kdu_name, member_vnf_index): deployed_kdu = None index = None diff --git a/osm_lcm/data_utils/vnfd.py b/osm_lcm/data_utils/vnfd.py index 954a84c..9f8104a 100644 --- a/osm_lcm/data_utils/vnfd.py +++ b/osm_lcm/data_utils/vnfd.py @@ -25,6 +25,10 @@ from osm_lcm.data_utils import list_utils +def get_lcm_operations_configuration(vnfd): + return vnfd.get("df", ())[0].get("lcm-operations-configuration", ()) + + def get_vdu_list(vnfd): return vnfd.get("vdu", ()) diff --git a/osm_lcm/data_utils/vnfr.py b/osm_lcm/data_utils/vnfr.py index 653a7ed..fc64145 100644 --- a/osm_lcm/data_utils/vnfr.py +++ b/osm_lcm/data_utils/vnfr.py @@ -22,9 +22,14 @@ # contact: fbravo@whitestack.com ## +from osm_lcm.data_utils import list_utils from osm_lcm.lcm_utils import get_iterable +def find_VNFR_by_VDU_ID(vnfr, vdu_id): + list_utils.find_in_list(vnfr, lambda vnfr: False) + + def get_osm_params(db_vnfr, vdu_id=None, vdu_count_index=0): osm_params = { x.replace("-", "_"): db_vnfr[x] diff --git a/osm_lcm/lcm_utils.py b/osm_lcm/lcm_utils.py index 12fd7fb..6e9c7f6 100644 --- a/osm_lcm/lcm_utils.py +++ b/osm_lcm/lcm_utils.py @@ -40,6 +40,10 @@ class LcmException(Exception): pass +class LcmExceptionNoMgmtIP(LcmException): + pass + + class LcmExceptionExit(LcmException): pass diff --git a/osm_lcm/ns.py b/osm_lcm/ns.py index 8238a24..e00f0d2 100644 --- a/osm_lcm/ns.py +++ b/osm_lcm/ns.py @@ -53,6 +53,7 @@ from osm_lcm.data_utils.vca import ( from osm_lcm.ng_ro import NgRoClient, NgRoException from osm_lcm.lcm_utils import ( LcmException, + LcmExceptionNoMgmtIP, LcmBase, deep_get, get_iterable, @@ -238,6 +239,23 @@ class NsLcm(LcmBase): pass return None + def _on_update_ro_db(self, nsrs_id, ro_descriptor): + # self.logger.debug('_on_update_ro_db(nsrs_id={}'.format(nsrs_id)) + + try: + # TODO filter RO descriptor fields... + + # write to database + db_dict = dict() + # db_dict['deploymentStatus'] = yaml.dump(ro_descriptor, default_flow_style=False, indent=2) + db_dict["deploymentStatus"] = ro_descriptor + self.update_db_2("nsrs", nsrs_id, db_dict) + + except Exception as e: + self.logger.warn( + "Cannot write database RO deployment for ns={} -> {}".format(nsrs_id, e) + ) + async def _on_update_n2vc_db(self, table, filter, path, updated_data, vca_id=None): # remove last dot from path (if exists) if path.endswith("."): @@ -436,6 +454,32 @@ class NsLcm(LcmBase): additional_params = vdur.get("additionalParams") return parse_yaml_strings(additional_params) + def vnfd2RO(self, vnfd, new_id=None, additionalParams=None, nsrId=None): + """ + Converts creates a new vnfd descriptor for RO base on input OSM IM vnfd + :param vnfd: input vnfd + :param new_id: overrides vnf id if provided + :param additionalParams: Instantiation params for VNFs provided + :param nsrId: Id of the NSR + :return: copy of vnfd + """ + vnfd_RO = deepcopy(vnfd) + # remove unused by RO configuration, monitoring, scaling and internal keys + vnfd_RO.pop("_id", None) + vnfd_RO.pop("_admin", None) + vnfd_RO.pop("monitoring-param", None) + vnfd_RO.pop("scaling-group-descriptor", None) + vnfd_RO.pop("kdu", None) + vnfd_RO.pop("k8s-cluster", None) + if new_id: + vnfd_RO["id"] = new_id + + # parse cloud-init or cloud-init-file with the provided variables using Jinja2 + for vdu in get_iterable(vnfd_RO, "vdu"): + vdu.pop("cloud-init-file", None) + vdu.pop("cloud-init", None) + return vnfd_RO + @staticmethod def ip_profile_2_RO(ip_profile): RO_ip_profile = deepcopy(ip_profile) @@ -454,6 +498,31 @@ class NsLcm(LcmBase): RO_ip_profile["dhcp"] = RO_ip_profile.pop("dhcp-params") return RO_ip_profile + def _get_ro_vim_id_for_vim_account(self, vim_account): + db_vim = self.db.get_one("vim_accounts", {"_id": vim_account}) + if db_vim["_admin"]["operationalState"] != "ENABLED": + raise LcmException( + "VIM={} is not available. operationalState={}".format( + vim_account, db_vim["_admin"]["operationalState"] + ) + ) + RO_vim_id = db_vim["_admin"]["deployed"]["RO"] + return RO_vim_id + + def get_ro_wim_id_for_wim_account(self, wim_account): + if isinstance(wim_account, str): + db_wim = self.db.get_one("wim_accounts", {"_id": wim_account}) + if db_wim["_admin"]["operationalState"] != "ENABLED": + raise LcmException( + "WIM={} is not available. operationalState={}".format( + wim_account, db_wim["_admin"]["operationalState"] + ) + ) + RO_wim_id = db_wim["_admin"]["deployed"]["RO-account"] + return RO_wim_id + else: + return wim_account + def scale_vnfr(self, db_vnfr, vdu_create=None, vdu_delete=None, mark_delete=False): db_vdu_push_list = [] template_vdur = [] @@ -606,6 +675,103 @@ class NsLcm(LcmBase): except DbException as e: self.logger.error("Cannot update vnf. {}".format(e)) + def ns_update_vnfr(self, db_vnfrs, nsr_desc_RO): + """ + Updates database vnfr with the RO info, e.g. ip_address, vim_id... Descriptor db_vnfrs is also updated + :param db_vnfrs: dictionary with member-vnf-index: vnfr-content + :param nsr_desc_RO: nsr descriptor from RO + :return: Nothing, LcmException is raised on errors + """ + for vnf_index, db_vnfr in db_vnfrs.items(): + for vnf_RO in nsr_desc_RO["vnfs"]: + if vnf_RO["member_vnf_index"] != vnf_index: + continue + vnfr_update = {} + if vnf_RO.get("ip_address"): + db_vnfr["ip-address"] = vnfr_update["ip-address"] = vnf_RO[ + "ip_address" + ].split(";")[0] + elif not db_vnfr.get("ip-address"): + if db_vnfr.get("vdur"): # if not VDUs, there is not ip_address + raise LcmExceptionNoMgmtIP( + "ns member_vnf_index '{}' has no IP address".format( + vnf_index + ) + ) + + for vdu_index, vdur in enumerate(get_iterable(db_vnfr, "vdur")): + vdur_RO_count_index = 0 + if vdur.get("pdu-type"): + continue + for vdur_RO in get_iterable(vnf_RO, "vms"): + if vdur["vdu-id-ref"] != vdur_RO["vdu_osm_id"]: + continue + if vdur["count-index"] != vdur_RO_count_index: + vdur_RO_count_index += 1 + continue + vdur["vim-id"] = vdur_RO.get("vim_vm_id") + if vdur_RO.get("ip_address"): + vdur["ip-address"] = vdur_RO["ip_address"].split(";")[0] + else: + vdur["ip-address"] = None + vdur["vdu-id-ref"] = vdur_RO.get("vdu_osm_id") + vdur["name"] = vdur_RO.get("vim_name") + vdur["status"] = vdur_RO.get("status") + vdur["status-detailed"] = vdur_RO.get("error_msg") + for ifacer in get_iterable(vdur, "interfaces"): + for interface_RO in get_iterable(vdur_RO, "interfaces"): + if ifacer["name"] == interface_RO.get("internal_name"): + ifacer["ip-address"] = interface_RO.get( + "ip_address" + ) + ifacer["mac-address"] = interface_RO.get( + "mac_address" + ) + break + else: + raise LcmException( + "ns_update_vnfr: Not found member_vnf_index={} vdur={} interface={} " + "from VIM info".format( + vnf_index, vdur["vdu-id-ref"], ifacer["name"] + ) + ) + vnfr_update["vdur.{}".format(vdu_index)] = vdur + break + else: + raise LcmException( + "ns_update_vnfr: Not found member_vnf_index={} vdur={} count_index={} from " + "VIM info".format( + vnf_index, vdur["vdu-id-ref"], vdur["count-index"] + ) + ) + + for vld_index, vld in enumerate(get_iterable(db_vnfr, "vld")): + for net_RO in get_iterable(nsr_desc_RO, "nets"): + if vld["id"] != net_RO.get("vnf_net_osm_id"): + continue + vld["vim-id"] = net_RO.get("vim_net_id") + vld["name"] = net_RO.get("vim_name") + vld["status"] = net_RO.get("status") + vld["status-detailed"] = net_RO.get("error_msg") + vnfr_update["vld.{}".format(vld_index)] = vld + break + else: + raise LcmException( + "ns_update_vnfr: Not found member_vnf_index={} vld={} from VIM info".format( + vnf_index, vld["id"] + ) + ) + + self.update_db_2("vnfrs", db_vnfr["_id"], vnfr_update) + break + + else: + raise LcmException( + "ns_update_vnfr: Not found member_vnf_index={} from VIM info".format( + vnf_index + ) + ) + def _get_ns_config_info(self, nsr_id): """ Generates a mapping between vnf,vdu elements and the N2VC id @@ -4062,6 +4228,41 @@ class NsLcm(LcmBase): member_vnf_index or "", vdu_id or "" ) + @staticmethod + def _create_nslcmop(nsr_id, operation, params): + """ + Creates a ns-lcm-opp content to be stored at database. + :param nsr_id: internal id of the instance + :param operation: instantiate, terminate, scale, action, ... + :param params: user parameters for the operation + :return: dictionary following SOL005 format + """ + # Raise exception if invalid arguments + if not (nsr_id and operation and params): + raise LcmException( + "Parameters 'nsr_id', 'operation' and 'params' needed to create primitive not provided" + ) + now = time() + _id = str(uuid4()) + nslcmop = { + "id": _id, + "_id": _id, + # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK + "operationState": "PROCESSING", + "statusEnteredTime": now, + "nsInstanceId": nsr_id, + "lcmOperationType": operation, + "startTime": now, + "isAutomaticInvocation": False, + "operationParams": params, + "isCancelPending": False, + "links": { + "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id, + "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id, + }, + } + return nslcmop + def _format_additional_params(self, params): params = params or {} for key, value in params.items(): @@ -4257,6 +4458,12 @@ class NsLcm(LcmBase): # Function to return execution_environment id + def _get_ee_id(self, vnf_index, vdu_id, vca_deployed_list): + # TODO vdu_index_count + for vca in vca_deployed_list: + if vca["member-vnf-index"] == vnf_index and vca["vdu_id"] == vdu_id: + return vca.get("ee_id") + async def destroy_N2VC( self, logging_text, @@ -7642,6 +7849,28 @@ class NsLcm(LcmBase): ) return "FAILED", "Error in operate VNF {}".format(exc) + def get_vca_cloud_and_credentials(self, vim_account_id: str) -> (str, str): + """ + Get VCA Cloud and VCA Cloud Credentials for the VIM account + + :param: vim_account_id: VIM Account ID + + :return: (cloud_name, cloud_credential) + """ + config = VimAccountDB.get_vim_account_with_id(vim_account_id).get("config", {}) + return config.get("vca_cloud"), config.get("vca_cloud_credential") + + def get_vca_k8s_cloud_and_credentials(self, vim_account_id: str) -> (str, str): + """ + Get VCA K8s Cloud and VCA K8s Cloud Credentials for the VIM account + + :param: vim_account_id: VIM Account ID + + :return: (cloud_name, cloud_credential) + """ + config = VimAccountDB.get_vim_account_with_id(vim_account_id).get("config", {}) + return config.get("vca_k8s_cloud"), config.get("vca_k8s_cloud_credential") + async def migrate(self, nsr_id, nslcmop_id): """ Migrate VNFs and VDUs instances in a NS