X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_lcm%2Flcm_utils.py;h=6e9c7f6186cdf33d100307ccb29842e87fbe2998;hb=3ff2325f97dda222d286d0d8ceb720de8b1a537c;hp=6d20318b8f7303ed2dea1a6cd362c6e8b8c0d22b;hpb=9bc63ac2c1365535af344954b951b1ff04868ea1;p=osm%2FLCM.git diff --git a/osm_lcm/lcm_utils.py b/osm_lcm/lcm_utils.py index 6d20318..6e9c7f6 100644 --- a/osm_lcm/lcm_utils.py +++ b/osm_lcm/lcm_utils.py @@ -19,6 +19,7 @@ import asyncio import checksumdir from collections import OrderedDict +import hashlib import os import shutil import traceback @@ -161,6 +162,46 @@ def populate_dict(target_dict, key_list, value): target_dict[key_list[-1]] = value +def get_ee_id_parts(ee_id): + """ + Parses ee_id stored at database that can be either 'version:namespace.helm_id' or only + namespace.helm_id for backward compatibility + If exists helm version can be helm-v3 or helm (helm-v2 old version) + """ + version, _, part_id = ee_id.rpartition(":") + namespace, _, helm_id = part_id.rpartition(".") + return version, namespace, helm_id + + +def vld_to_ro_ip_profile(source_data): + if source_data: + return { + "ip_version": "IPv4" + if "v4" in source_data.get("ip-version", "ipv4") + else "IPv6", + "subnet_address": source_data.get("cidr") + or source_data.get("subnet-address"), + "gateway_address": source_data.get("gateway-ip") + or source_data.get("gateway-address"), + "dns_address": ";".join( + [v["address"] for v in source_data["dns-server"] if v.get("address")] + ) + if source_data.get("dns-server") + else None, + "dhcp_enabled": source_data.get("dhcp-params", {}).get("enabled", False) + or source_data.get("dhcp-enabled", False), + "dhcp_start_address": source_data["dhcp-params"].get("start-address") + if source_data.get("dhcp-params") + else None, + "dhcp_count": source_data["dhcp-params"].get("count") + if source_data.get("dhcp-params") + else None, + "ipv6_address_mode": source_data["ipv6-address-mode"] + if "ipv6-address-mode" in source_data + else None, + } + + class LcmBase: def __init__(self, msg, logger): """ @@ -189,6 +230,54 @@ class LcmBase: # except DbException as e: # self.logger.error("Updating {} _id={} with '{}'. Error: {}".format(item, _id, _desc, e)) + @staticmethod + def calculate_charm_hash(zipped_file): + """Calculate the hash of charm files which ends with .charm + + Args: + zipped_file (str): Existing charm package full path + + Returns: + hex digest (str): The hash of the charm file + """ + filehash = hashlib.sha256() + with open(zipped_file, mode="rb") as file: + contents = file.read() + filehash.update(contents) + return filehash.hexdigest() + + @staticmethod + def compare_charm_hash(current_charm, target_charm): + """Compare the existing charm and the target charm if the charms + are given as zip files ends with .charm + + Args: + current_charm (str): Existing charm package full path + target_charm (str): Target charm package full path + + Returns: + True/False (bool): if charm has changed it returns True + """ + return LcmBase.calculate_charm_hash( + current_charm + ) != LcmBase.calculate_charm_hash(target_charm) + + @staticmethod + def compare_charmdir_hash(current_charm_dir, target_charm_dir): + """Compare the existing charm and the target charm if the charms + are given as directories + + Args: + current_charm_dir (str): Existing charm package directory path + target_charm_dir (str): Target charm package directory path + + Returns: + True/False (bool): if charm has changed it returns True + """ + return checksumdir.dirhash(current_charm_dir) != checksumdir.dirhash( + target_charm_dir + ) + def check_charm_hash_changed( self, current_charm_path: str, target_charm_path: str ) -> bool: @@ -203,25 +292,29 @@ class LcmBase: True/False (bool): if charm has changed it returns True """ - # Check if the charm artifacts are available - if os.path.exists(self.fs.path + current_charm_path) and os.path.exists( - self.fs.path + target_charm_path - ): - # Compare the hash of charm folders - if checksumdir.dirhash( - self.fs.path + current_charm_path - ) != checksumdir.dirhash(self.fs.path + target_charm_path): + try: + # Check if the charm artifacts are available + current_charm = self.fs.path + current_charm_path + target_charm = self.fs.path + target_charm_path - return True + if os.path.exists(current_charm) and os.path.exists(target_charm): + # Compare the hash of .charm files + if current_charm.endswith(".charm"): + return LcmBase.compare_charm_hash(current_charm, target_charm) - return False + # Compare the hash of charm folders + return LcmBase.compare_charmdir_hash(current_charm, target_charm) - else: - raise LcmException( - "Charm artifact {} does not exist in the VNF Package".format( - self.fs.path + target_charm_path + else: + raise LcmException( + "Charm artifact {} does not exist in the VNF Package".format( + self.fs.path + target_charm_path + ) ) - ) + except (IOError, OSError, TypeError) as error: + self.logger.debug(traceback.format_exc()) + self.logger.error(f"{error} occured while checking the charm hashes") + raise LcmException(error) @staticmethod def get_charm_name(charm_metadata_file: str) -> str: @@ -290,7 +383,7 @@ class LcmBase: + "/" + nsd_package_name + "/charms/" - + charm_folder_name.split(".")[0] + + charm_folder_name.replace(".charm", "") ) # Extract .charm to extract path with ZipFile(charm_path, "r") as zipfile: @@ -315,10 +408,19 @@ class LcmBase: # Find nsd_package details: path, name revision = db_nsr.get("revision", "") - nsd_package_path = ( - db_nsr["nsd-id"] + ":" + str(revision) if revision else db_nsr["nsd-id"] - ) - nsd_package_name = os.listdir(self.fs.path + nsd_package_path)[0] + + # Get the NSD package path + if revision: + nsd_package_path = db_nsr["nsd-id"] + ":" + str(revision) + db_nsd = self.db.get_one("nsds_revisions", {"_id": nsd_package_path}) + + else: + nsd_package_path = db_nsr["nsd-id"] + + db_nsd = self.db.get_one("nsds", {"_id": nsd_package_path}) + + # Get the NSD package name + nsd_package_name = db_nsd["_admin"]["storage"]["pkg-dir"] # Remove the existing nsd package and sync from FsMongo shutil.rmtree(self.fs.path + nsd_package_path, ignore_errors=True) @@ -469,17 +571,19 @@ class TaskRegistry(LcmBase): """ Cancel all active tasks of a concrete ns, nsi, vim_account, sdn identified for _id. If op_id is supplied only this is cancelled, and the same with task_name + :return: cancelled task to be awaited if needed """ if not self.task_registry[topic].get(_id): return for op_id in reversed(self.task_registry[topic][_id]): if target_op_id and target_op_id != op_id: continue - for task_name, task in self.task_registry[topic][_id][op_id].items(): + for task_name, task in list(self.task_registry[topic][_id][op_id].items()): if target_task_name and target_task_name != task_name: continue # result = task.cancel() + yield task # if result: # self.logger.debug("{} _id={} order_id={} task={} cancelled".format(topic, _id, op_id, task_name))