def test_nf_pkg_get_one(self):
        VnfPackageModel(vnfPackageId="4",
                        vnfdId="4",
                        vnfVendor='4',
                        vnfdVersion='4',
                        vnfSoftwareVersion='',
                        vnfPackageUri='',
                        vnfdModel='').save()

        resp = self.client.get("/api/parser/v1/vnfpackages/4")
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        expect_data = {
            "imageInfo": [],
            "csarId": "4",
            "packageInfo": {
                "csarName": "",
                "vnfdModel": "",
                "vnfdProvider": "4",
                "vnfdId": "4",
                "downloadUrl": "http://127.0.0.1:8806/static/genericparser/4/",
                "vnfVersion": "",
                "vnfdVersion": "4",
                "vnfPackageId": "4"
            }
        }
        self.assertEqual(expect_data, resp.data)
 def test_nf_pkg_distribute_when_csar_already_exist(self):
     VnfPackageModel(vnfPackageId="1", vnfdId="vcpe_vfw_zte_1_0").save()
     NfDistributeThread(csar_id="1",
                        vim_ids=["1"],
                        lab_vim_id="",
                        job_id="2").run()
     self.assert_job_result("2", 255, "NF CSAR(1) already exists.")
    def test_nsd_content_upload_normal(self, mock_parse_nsd):
        user_defined_data_json = json.JSONEncoder().encode(
            self.user_defined_data)
        mock_parse_nsd.return_value = json.JSONEncoder().encode(nsd_data)
        VnfPackageModel(vnfPackageId="111", vnfdId="vcpe_vfw_zte_1_0").save()

        PnfPackageModel(pnfPackageId="112", pnfdId="m6000_s").save()

        NSPackageModel(
            nsPackageId='22',
            operationalState='DISABLED',
            usageState='NOT_IN_USE',
            userDefinedData=user_defined_data_json,
        ).save()

        with open('nsd_content.txt', 'wt') as fp:
            fp.write('test')
        with open('nsd_content.txt', 'rt') as fp:
            resp = self.client.put(
                "/api/nsd/v1/ns_descriptors/22/nsd_content",
                {'file': fp},
            )
        file_content = ''
        with open(os.path.join(GENERICPARSER_ROOT_PATH,
                               '22/nsd_content.txt')) as fp:
            data = fp.read()
            file_content = '%s%s' % (file_content, data)
        ns_pkg = NSPackageModel.objects.filter(nsPackageId="22")
        self.assertEqual("VCPE_NS", ns_pkg[0].nsdId)
        self.assertEqual(PKG_STATUS.ONBOARDED, ns_pkg[0].onboardingState)
        self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
        self.assertEqual(None, resp.data)
        self.assertEqual(file_content, 'test')
        os.remove('nsd_content.txt')
 def test_vnfd_parse_normal(self, mock_parse_vnfd):
     VnfPackageModel(vnfPackageId="8", vnfdId="10").save()
     mock_parse_vnfd.return_value = json.JSONEncoder().encode({"c": "d"})
     req_data = {"csarId": "8", "inputs": []}
     resp = self.client.post("/api/parser/v1/parservnfd",
                             req_data,
                             format='json')
     self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
     self.assertEqual({"model": '{"c": "d"}'}, resp.data)
Beispiel #5
0
    def on_distribute(self):
        JobUtil.create_job(
            inst_type='nf',
            jobaction='on_distribute',
            inst_id=self.csar_id,
            job_id=self.job_id)
        JobUtil.add_job_status(self.job_id, 5, "Start CSAR(%s) distribute." % self.csar_id)

        if VnfPackageModel.objects.filter(vnfPackageId=self.csar_id):
            err_msg = "NF CSAR(%s) already exists." % self.csar_id
            JobUtil.add_job_status(self.job_id, JOB_ERROR, err_msg)
            return

        artifact = sdc.get_artifact(sdc.ASSETTYPE_RESOURCES, self.csar_id)
        local_path = os.path.join(GENERICPARSER_ROOT_PATH, self.csar_id)
        csar_name = "%s.csar" % artifact.get("name", self.csar_id)
        local_file_name = sdc.download_artifacts(artifact["toscaModelURL"], local_path, csar_name)
        if local_file_name.endswith(".csar") or local_file_name.endswith(".zip"):
            artifact_vnf_file = fileutil.unzip_file(local_file_name, local_path, "Artifacts/Deployment/OTHER/vnf.csar")
            if os.path.exists(artifact_vnf_file):
                local_file_name = artifact_vnf_file

        vnfd_json = toscaparsers.parse_vnfd(local_file_name)
        vnfd = json.JSONDecoder().decode(vnfd_json)

        if not vnfd.get("vnf"):
            raise GenericparserException("VNF properties and metadata in VNF Package(id=%s) are empty." % self.csar_id)

        vnfd_id = vnfd["vnf"]["properties"].get("descriptor_id", "")
        if VnfPackageModel.objects.filter(vnfdId=vnfd_id):
            logger.error("VNF package(%s) already exists.", vnfd_id)
            raise GenericparserException("VNF package(%s) already exists." % vnfd_id)
        JobUtil.add_job_status(self.job_id, 30, "Save CSAR(%s) to database." % self.csar_id)
        vnfd_ver = vnfd["vnf"]["properties"].get("descriptor_version", "")
        vnf_provider = vnfd["vnf"]["properties"].get("provider", "")
        vnf_software_version = vnfd["vnf"]["properties"].get("software_version", "")
        vnfd_product_name = vnfd["vnf"]["properties"].get("product_name", "")
        VnfPackageModel(
            vnfPackageId=self.csar_id,
            vnfdId=vnfd_id,
            vnfVendor=vnf_provider,
            vnfdProductName=vnfd_product_name,
            vnfdVersion=vnfd_ver,
            vnfSoftwareVersion=vnf_software_version,
            vnfdModel=vnfd_json,
            localFilePath=local_file_name,
            vnfPackageUri=csar_name,
            onboardingState=PKG_STATUS.ONBOARDED,
            operationalState=PKG_STATUS.ENABLED,
            usageState=PKG_STATUS.NOT_IN_USE
        ).save()
        JobUtil.add_job_status(self.job_id, 100, "CSAR(%s) distribute successfully." % self.csar_id)
    def test_nf_pkg_get_one_failed(self):
        VnfPackageModel(vnfPackageId="4",
                        vnfdId="4",
                        vnfVendor='4',
                        vnfdVersion='4',
                        vnfSoftwareVersion='',
                        vnfPackageUri='',
                        vnfdModel='').save()

        resp = self.client.get("/api/parser/v1/vnfpackages/2")
        self.assertEqual(resp.status_code,
                         status.HTTP_500_INTERNAL_SERVER_ERROR)
        self.assertEqual({'error': 'Vnf package[2] not Found.'}, resp.data)
Beispiel #7
0
    def test_parse_serviced_and_save(self, mock_parse_sd):
        mock_parse_sd.return_value = json.JSONEncoder().encode(self.sd_data)
        servcie_desc = ServiceDescriptor()
        csar_id = '0b667470-e6b3-4ee8-8f08-186317a04dc2'
        servcie_desc.create(self.data, csar_id)
        VnfPackageModel(vnfPackageId="1",
                        vnfdId="cd557883-ac4b-462d-aa01-421b5fa606b1").save()
        PnfPackageModel(pnfPackageId="1", pnfdId="m6000_s").save()
        local_file_name = "/test.csar"
        servcie_desc.parse_serviced_and_save(csar_id, local_file_name)

        service_package = ServicePackageModel.objects.filter(
            servicePackageId=csar_id)[0]
        self.assertIsNotNone(service_package)
    def test_service_pkg_distribute_when_fail_download_artifacts(
            self, mock_get_asset, mock_download_artifacts):
        mock_get_asset.return_value = self.asset_data
        mock_download_artifacts.side_effect = GenericparserException(
            "Failed to download 1 from sdc.")
        csar_id = "1"
        VnfPackageModel(vnfPackageId="cd557883-ac4b-462d-aa01-421b5fa606b1",
                        vnfdId="cd557883-ac4b-462d-aa01-421b5fa606b1").save()
        PnfPackageModel(pnfPackageId="m6000_s", pnfdId="m6000_s").save()

        try:
            ServicePackage().on_distribute(csar_id)
        except Exception as e:
            self.assertTrue(isinstance(e, GenericparserException))
            self.assertEqual("Failed to download 1 from sdc.", e.args[0])
    def test_service_pkg_distribute(self, mock_parse_sd,
                                    mock_download_artifacts, mock_get_asset):
        mock_parse_sd.return_value = json.JSONEncoder().encode(self.sd_data)
        mock_download_artifacts.return_value = "/test.csar"
        mock_get_asset.return_value = self.asset_data
        VnfPackageModel(vnfPackageId="cd557883-ac4b-462d-aa01-421b5fa606b1",
                        vnfdId="cd557883-ac4b-462d-aa01-421b5fa606b1").save()
        PnfPackageModel(pnfPackageId="m6000_s", pnfdId="m6000_s").save()
        ServicePackage().on_distribute(csar_id="1")

        service_package = ServicePackageModel.objects.filter(
            servicePackageId="1").first()
        self.assertEqual("5de07996-7ff0-4ec1-b93c-e3a00bb3f207",
                         service_package.invariantId)
        self.assertEqual("Enhance_Service", service_package.servicedName)
        self.assertEqual(PKG_STATUS.ONBOARDED, service_package.onboardingState)
        self.assertEqual(PKG_STATUS.ENABLED, service_package.operationalState)
        self.assertEqual(PKG_STATUS.NOT_IN_USE, service_package.usageState)
 def test_ns_pkg_distribute_when_successfully(self, mock_parse_nsd,
                                              mock_download_artifacts,
                                              mock_call_req):
     mock_parse_nsd.return_value = json.JSONEncoder().encode(self.nsd_data)
     mock_download_artifacts.return_value = "/home/vcpe.csar"
     mock_call_req.return_value = [
         0,
         json.JSONEncoder().encode([{
             "uuid": "1",
             "toscaModelURL": "https://127.0.0.1:1234/sdc/v1/vcpe.csar",
             "distributionStatus": "DISTRIBUTED"
         }]), '200'
     ]
     VnfPackageModel(vnfPackageId="1", vnfdId="vcpe_vfw_zte_1_0").save()
     PnfPackageModel(pnfPackageId="1", pnfdId="m6000_s").save()
     resp = self.client.post("/api/parser/v1/nspackages", {"csarId": "1"},
                             format='json')
     self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
     self.assertEqual("success", resp.data["status"])
     self.assertEqual("CSAR(1) distributed successfully.",
                      resp.data["statusDescription"])
 def test_nf_pkg_distribute_when_vnfd_already_exist(self, mock_parse_vnfd,
                                                    mock_download_artifacts,
                                                    mock_call_req):
     mock_parse_vnfd.return_value = json.JSONEncoder().encode(
         self.vnfd_data)
     mock_download_artifacts.return_value = "/home/hss.csar"
     mock_call_req.return_value = [
         0,
         json.JSONEncoder().encode([{
             "uuid":
             "1",
             "toscaModelURL":
             "https://127.0.0.1:1234/sdc/v1/hss.csar"
         }]), '200'
     ]
     VnfPackageModel(vnfPackageId="2", vnfdId="zte-hss-1.0").save()
     NfDistributeThread(csar_id="1",
                        vim_ids=["1"],
                        lab_vim_id="",
                        job_id="2").run()
     self.assert_job_result("2", 255,
                            "VNF package(zte-hss-1.0) already exists.")
 def test_nf_pkg_normal_delete(self):
     VnfPackageModel(vnfPackageId="2", vnfdId="vcpe_vfw_zte_1_0").save()
     NfPkgDeleteThread(csar_id="2", job_id="2").run()
     self.assert_job_result("2", 100, "Delete CSAR(2) successfully.")