diff options
Diffstat (limited to 'catalog/packages')
-rw-r--r-- | catalog/packages/biz/vnf_package.py | 52 | ||||
-rw-r--r-- | catalog/packages/tests/test_vnf_package.py | 36 |
2 files changed, 87 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) |