diff options
author | 2017-09-21 10:34:37 +0800 | |
---|---|---|
committer | 2017-09-21 10:37:43 +0800 | |
commit | 4b8b03ff7c6acdb02d566a877c42fe3c08412955 (patch) | |
tree | dc1a18d6e2c7f0c86f4a8028eec83cf21268b7f9 | |
parent | 2bc2a4c677b5896254f4e957cb5974e4028dfb50 (diff) |
Add code to delete ns by force
Change-Id: Iae931b6001a347c35abc368ab3a2b94ed414ae89
Issue-Id: VFC-373
Signed-off-by: fengyuanxing <feng.yuanxing@zte.com.cn>
-rw-r--r-- | catalog/packages/nf_package.py | 2 | ||||
-rw-r--r-- | catalog/packages/ns_package.py | 20 | ||||
-rw-r--r-- | catalog/packages/tests.py | 8 | ||||
-rw-r--r-- | catalog/pub/msapi/nslcm.py | 16 |
4 files changed, 34 insertions, 12 deletions
diff --git a/catalog/packages/nf_package.py b/catalog/packages/nf_package.py index a89c826b..ec8bdcd8 100644 --- a/catalog/packages/nf_package.py +++ b/catalog/packages/nf_package.py @@ -184,7 +184,7 @@ class NfPkgDeleteThread(threading.Thread): if NfInstModel.objects.filter(package_id=self.csar_id): raise CatalogException("NfInst by csar(%s) exists, cannot delete." % self.csar_id) ''' - nslcm.delete_nf_inst_mock() + nslcm.delete_nf_inst(self.csar_id) JobUtil.add_job_status(self.job_id, 50, "Delete CSAR(%s) from Database." % self.csar_id) if not VnfPackageModel.objects.filter(vnfPackageId=self.csar_id): diff --git a/catalog/packages/ns_package.py b/catalog/packages/ns_package.py index a8141303..5bd98082 100644 --- a/catalog/packages/ns_package.py +++ b/catalog/packages/ns_package.py @@ -52,13 +52,15 @@ def ns_on_distribute(csar_id): def ns_delete_csar(csar_id, force_delete): ret = None + nsinstances = [] try: if force_delete: - ret = NsPackage().delete_csar(csar_id) + ret = NsPackage().delete_csar(csar_id,True) return fmt_ns_pkg_rsp(STATUS_SUCCESS, ret[1], "") - if nslcm.get_nsInstances(csar_id): - return fmt_ns_pkg_rsp(STATUS_FAILED, - "NS instances using CSAR(%s) already exists!" % csar_id) + nsinstances = nslcm.get_nsInstances(csar_id) + if nsinstances: + if len(nsinstances) > 0: + return fmt_ns_pkg_rsp(STATUS_FAILED, "NS instances using the CSAR exists!",status.HTTP_412_PRECONDITION_FAILED) ret = NsPackage().delete_csar(csar_id) return fmt_ns_pkg_rsp(STATUS_SUCCESS, ret[1], "") except CatalogException as e: @@ -154,14 +156,14 @@ class NsPackage(object): return nsd, local_file_name, nsd_json - def delete_csar(self, csar_id): - ''' + def delete_csar(self, csar_id,force_delete = False): if force_delete: - NSInstModel.objects.filter(nspackage_id=csar_id).delete() + nslcm.delete_all_nsinst(csar_id) else: - if NSInstModel.objects.filter(nspackage_id=csar_id): + nsinstances = nslcm.get_nsInstances(csar_id) + if nsinstances and len(nsinstances) > 0: raise CatalogException("CSAR(%s) is in using, cannot be deleted." % csar_id) - ''' + NSPackageModel.objects.filter(nsPackageId=csar_id).delete() return [0, "Delete CSAR(%s) successfully." % csar_id] diff --git a/catalog/packages/tests.py b/catalog/packages/tests.py index dd3d0592..b895f114 100644 --- a/catalog/packages/tests.py +++ b/catalog/packages/tests.py @@ -120,7 +120,8 @@ class PackageTest(unittest.TestCase): @mock.patch.object(NfDistributeThread, 'get_vnfd') @mock.patch.object(NsPackage,'get_nsd') @mock.patch.object(nslcm, 'get_nsInstances') - def test_ns_package_delete(self, mock_get_nsInstances,mock_get_nsd,mock_get_vnfd): + @mock.patch.object(nslcm, 'delete_all_nsinst') + def test_ns_package_delete(self, mock_delete_all_nsinst,mock_get_nsInstances,mock_get_nsd,mock_get_vnfd): # First distribute a VNF local_file_name = "/url/local/filename" @@ -141,6 +142,7 @@ class PackageTest(unittest.TestCase): # Finally delete ns package mock_get_nsInstances.return_value = [] + mock_delete_all_nsinst.return_values = [0, "success"] response = self.client.delete("/api/catalog/v1/nspackages/" + str(self.ns_csarId)) self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content) self.assertEqual("Delete CSAR(123) successfully.", response.data["statusDescription"], response.content) @@ -149,7 +151,8 @@ class PackageTest(unittest.TestCase): @mock.patch.object(NfDistributeThread, 'get_vnfd') @mock.patch.object(NsPackage,'get_nsd') @mock.patch.object(nslcm, 'get_nsInstances') - def test_ns_package_delete_force(self, mock_get_nsInstances,mock_get_nsd,mock_get_vnfd): + @mock.patch.object(nslcm, 'delete_all_nsinst') + def test_ns_package_delete_force(self, mock_delete_all_nsinst, mock_get_nsInstances,mock_get_nsd,mock_get_vnfd): # First distribute a VNF local_file_name = "/url/local/filename" @@ -170,6 +173,7 @@ class PackageTest(unittest.TestCase): # Finally delete ns package mock_get_nsInstances.return_value = [{"csarid":"1"},{"csarid":"2"}] + mock_delete_all_nsinst.return_values = [0, "success"] response = self.client.delete("/api/catalog/v1/nspackages/%sforce"% str(self.ns_csarId)) self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content) self.assertEqual("Delete CSAR(123) successfully.", response.data["statusDescription"], response.content) diff --git a/catalog/pub/msapi/nslcm.py b/catalog/pub/msapi/nslcm.py index 33e169c6..27054b01 100644 --- a/catalog/pub/msapi/nslcm.py +++ b/catalog/pub/msapi/nslcm.py @@ -50,6 +50,22 @@ def get_vnfInstances(csarid): raise CatalogException("Failed to query VNF Instances(%s) from NSLCM." % csarid) return json.JSONDecoder().decode(ret[1]) +def delete_all_nsinst(csarid): + nsinstances = get_nsInstances(csarid) + for ns in nsinstances: + nsInstanceId = ns["nsInstanceId"] + ret=req_by_msb("/nslcm/v1/ns/%s" % nsInstanceId,"delete") + if ret[0] != 0: + logger.error("Status code is %s, detail is %s.", ret[2], ret[1]) + raise CatalogException("Failed to delete NS Instances(%s) from NSLCM." % nsInstanceId) + + return [0,'success'] + +def delete_nf_inst(csar_id): + #vnf_instance = get_vnfInstances(csar_id) + # REST API from nslcm to delete nf instance is not implemented + # ret=req_by_msb("/nslcm/v1/nf/%s" % csar_id,"delete") + return [0,'success'] # def delete_ns(asset_type): # resource = "/nfvolcm/v1/ns/" |