diff options
Diffstat (limited to 'catalog')
-rw-r--r-- | catalog/packages/biz/vnf_package.py | 52 | ||||
-rw-r--r-- | catalog/packages/tests/test_vnf_package.py | 36 | ||||
-rw-r--r-- | catalog/pub/utils/fileutil.py | 35 |
3 files changed, 122 insertions, 1 deletions
diff --git a/catalog/packages/biz/vnf_package.py b/catalog/packages/biz/vnf_package.py index daf2fb2..6dec204 100644 --- a/catalog/packages/biz/vnf_package.py +++ b/catalog/packages/biz/vnf_package.py @@ -229,6 +229,56 @@ class VnfPkgUploadThread(threading.Thread): logger.info('VNF packge(%s) has been uploaded.' % self.vnf_pkg_id) +def get_mfile_data(path): + logger.debug('get_mfile_data path %s' % path) + files = fileutil.filter_files(path, '.mf') + if files: + src_file = os.path.join(path, files[0]) + src_dict_list = [] + with open(src_file, 'r') as f: + data = f.readlines() + for line in data: + if line.strip() == "": + continue + src_dict = {} + k, v = line.split(':', maxsplit=1) + if k.strip() in ["Source", "Algorithm", "Hash"]: + if k.strip() == "Source" and src_dict: + src_dict_list.extend(src_dict) + src_dict = {} + src_dict[k.strip()] = v.strip() + print("src_dict:%s" % src_dict) + if src_dict: + src_dict_list.append(src_dict) + + logger.debug('get_mfile_data: %s' % src_dict_list) + return src_dict_list + + +def fill_artifacts_data(vnf_pkg_id): + vnf_pkg_path = os.path.join(CATALOG_ROOT_PATH, vnf_pkg_id) + if os.path.exists(vnf_pkg_path) is False: + return None + files = fileutil.filter_files(vnf_pkg_path, '.csar') + for filename in files: + logger.info('fill_artifacts_data filename (%s)...' % filename) + dst_file_path = os.path.join(vnf_pkg_path, "tmp") + src_file = os.path.join(vnf_pkg_path, filename) + dst_file = os.path.join(dst_file_path, filename) + fileutil.recreate_dir(dst_file_path) + fileutil.copy(src_file, vnf_pkg_path, dst_file) + artifact_vnf_file = fileutil.unzip_file(dst_file, dst_file_path, "") + artifacts = get_mfile_data(artifact_vnf_file) + if artifacts: + return [{ + "artifactPath": artifact.get("Source", ""), + "checksum": { + "algorithm": artifact.get("Hash", "Null"), + "hash": artifact.get("Algorithm", "Null") + } + } for artifact in artifacts] + + def fill_response_data(nf_pkg): pkg_info = {} pkg_info["id"] = nf_pkg.vnfPackageId @@ -239,7 +289,7 @@ def fill_response_data(nf_pkg): if nf_pkg.checksum: pkg_info["checksum"] = json.JSONDecoder().decode(nf_pkg.checksum) pkg_info["softwareImages"] = None # TODO - pkg_info["additionalArtifacts"] = None # TODO + pkg_info["additionalArtifacts"] = fill_artifacts_data(nf_pkg.vnfPackageId) pkg_info["onboardingState"] = nf_pkg.onboardingState pkg_info["operationalState"] = nf_pkg.operationalState pkg_info["usageState"] = nf_pkg.usageState diff --git a/catalog/packages/tests/test_vnf_package.py b/catalog/packages/tests/test_vnf_package.py index 5422361..8deb9ec 100644 --- a/catalog/packages/tests/test_vnf_package.py +++ b/catalog/packages/tests/test_vnf_package.py @@ -423,3 +423,39 @@ class TestVnfPackage(TestCase): self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) response = self.client.get(VNF_BASE_URL + "/222/artifacts/image1") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) + + def test_upload_vnf_pkg_with_artifacts(self): + data = {'file': open(os.path.join(CATALOG_ROOT_PATH, "vgw.csar"), "rb")} + VnfPackageModel.objects.create( + vnfPackageId="222", + onboardingState="CREATED" + ) + response = self.client.put("%s/222/package_content" % VNF_BASE_URL, data=data) + vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId="222") + self.assertEqual(PKG_STATUS.ONBOARDED, vnf_pkg[0].onboardingState) + self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) + response = self.client.get("%s/222" % VNF_BASE_URL) + print(response.data) + self.assertEqual(response.status_code, status.HTTP_200_OK) + expact_response_data = { + "id": "222", + "vnfdId": "b1bb0ce7-2222-4fa7-95ed-4840d70a1177", + "vnfProductName": "vcpe_vgw", + "vnfSoftwareVersion": "1.0", + "vnfdVersion": "1.0", + "softwareImages": None, + "additionalArtifacts": [ + { + "artifactPath": "MainServiceTemplate.yaml", + "checksum": { + "algorithm": "Null", + "hash": "Null" + } + } + ], + "onboardingState": "ONBOARDED", + "operationalState": "ENABLED", + "usageState": "NOT_IN_USE", + "_links": None + } + self.assertEqual(response.data, expact_response_data) diff --git a/catalog/pub/utils/fileutil.py b/catalog/pub/utils/fileutil.py index 9344f72..e6eb6f2 100644 --- a/catalog/pub/utils/fileutil.py +++ b/catalog/pub/utils/fileutil.py @@ -54,7 +54,9 @@ def download_file_from_http(url, local_dir, file_name): def unzip_file(zip_src, dst_dir, csar_path): + logger.debug("unzip_file %s to %s.", zip_src, dst_dir) if os.path.exists(zip_src): + logger.debug("unzip_file %s.", zip_src) fz = zipfile.ZipFile(zip_src, 'r') for file in fz.namelist(): fz.extract(file, dst_dir) @@ -86,3 +88,36 @@ def get_artifact_path(vnf_path, artifact_file): if artifact_file in files: return os.path.join(root, artifact_file) return None + + +def end_with(_s_in, *suffix): + array = map(_s_in.endswith, suffix) + if True in array: + return True + return False + + +def filter_files(search_path, suffix): + f_find = [] + file_list = os.listdir(search_path) + for file_item in file_list: + if end_with(file_item, suffix): + f_find.append(file_item) + return f_find + + +def recreate_dir(path): + if os.path.exists(path): + shutil.rmtree(path) + os.makedirs(path, mode=0o777) + + +def copy(src_file, dest_dir, new_file_name=None): + if not os.path.exists(dest_dir): + os.makedirs(dest_dir) + if new_file_name is None: + dst = os.path.join(dest_dir, os.path.basename(src_file)) + else: + dst = os.path.join(dest_dir, new_file_name) + shutil.copyfile(src_file, dst) + shutil.copymode(src_file, dst) |