+ @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:
+ """Find the target charm has changed or not by checking the hash of
+ old and new charm packages
+
+ Args:
+ current_charm_path (str): Existing charm package artifact path
+ target_charm_path (str): Target charm package artifact path
+
+ Returns:
+ True/False (bool): if charm has changed it returns True
+
+ """
+ try:
+ # Check if the charm artifacts are available
+ current_charm = self.fs.path + current_charm_path
+ target_charm = self.fs.path + target_charm_path
+
+ 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)
+
+ # 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
+ )
+ )
+ 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:
+ """Get the charm name from metadata file.
+
+ Args:
+ charm_metadata_file (str): charm metadata file full path
+
+ Returns:
+ charm_name (str): charm name
+
+ """
+ # Read charm metadata.yaml to get the charm name
+ with open(charm_metadata_file, "r") as metadata_file:
+ content = yaml.safe_load(metadata_file)
+ charm_name = content["name"]
+ return str(charm_name)
+
+ def _get_charm_path(
+ self, nsd_package_path: str, nsd_package_name: str, charm_folder_name: str
+ ) -> str:
+ """Get the full path of charm folder.
+
+ Args:
+ nsd_package_path (str): NSD package full path
+ nsd_package_name (str): NSD package name
+ charm_folder_name (str): folder name
+
+ Returns:
+ charm_path (str): charm folder full path
+ """
+ charm_path = (
+ self.fs.path
+ + nsd_package_path
+ + "/"
+ + nsd_package_name
+ + "/charms/"
+ + charm_folder_name
+ )
+ return charm_path
+
+ def _get_charm_metadata_file(
+ self,
+ charm_folder_name: str,
+ nsd_package_path: str,
+ nsd_package_name: str,
+ charm_path: str = None,
+ ) -> str:
+ """Get the path of charm metadata file.
+
+ Args:
+ charm_folder_name (str): folder name
+ nsd_package_path (str): NSD package full path
+ nsd_package_name (str): NSD package name
+ charm_path (str): Charm full path
+
+ Returns:
+ charm_metadata_file_path (str): charm metadata file full path
+
+ """
+ # Locate the charm metadata.yaml
+ if charm_folder_name.endswith(".charm"):
+ extract_path = (
+ self.fs.path
+ + nsd_package_path
+ + "/"
+ + nsd_package_name
+ + "/charms/"
+ + charm_folder_name.replace(".charm", "")
+ )
+ # Extract .charm to extract path
+ with ZipFile(charm_path, "r") as zipfile:
+ zipfile.extractall(extract_path)
+ return extract_path + "/metadata.yaml"
+ else:
+ return charm_path + "/metadata.yaml"
+
+ def find_charm_name(self, db_nsr: dict, charm_folder_name: str) -> str:
+ """Get the charm name from metadata.yaml of charm package.
+
+ Args:
+ db_nsr (dict): NS record as a dictionary
+ charm_folder_name (str): charm folder name
+
+ Returns:
+ charm_name (str): charm name
+ """
+ try:
+ if not charm_folder_name:
+ raise LcmException("charm_folder_name should be provided.")
+
+ # Find nsd_package details: path, name
+ revision = db_nsr.get("revision", "")
+
+ # 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)
+ self.fs.sync(from_path=nsd_package_path)
+
+ # Get the charm path
+ charm_path = self._get_charm_path(
+ nsd_package_path, nsd_package_name, charm_folder_name
+ )
+
+ # Find charm metadata file full path
+ charm_metadata_file = self._get_charm_metadata_file(
+ charm_folder_name, nsd_package_path, nsd_package_name, charm_path
+ )
+
+ # Return charm name
+ return self.get_charm_name(charm_metadata_file)
+
+ except (
+ yaml.YAMLError,
+ IOError,
+ FsException,
+ KeyError,
+ TypeError,
+ FileNotFoundError,
+ BadZipfile,
+ ) as error:
+ self.logger.debug(traceback.format_exc())
+ self.logger.error(f"{error} occured while getting the charm name")
+ raise LcmException(error)
+