aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfujinhua <fu.jinhua@zte.com.cn>2017-09-22 12:01:15 +0800
committerfujinhua <fu.jinhua@zte.com.cn>2017-09-22 12:01:15 +0800
commitb5cb295b5f938c1f4d0004aa5df14328b0938aa3 (patch)
tree41a8caa48d155b88a3387554e2d704563c44c9ad
parentae0ef170004ebc397c070efdad99e24b1e24d3d9 (diff)
Refactor ns package manager api
Change-Id: I4cb493792c42b16aedbe1b51fbb69f30bb75a0c9 Issue-Id: VFC-421 Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
-rw-r--r--catalog/packages/ns_package.py60
-rw-r--r--catalog/packages/tests.py116
2 files changed, 24 insertions, 152 deletions
diff --git a/catalog/packages/ns_package.py b/catalog/packages/ns_package.py
index 5bd98082..8eb27fcd 100644
--- a/catalog/packages/ns_package.py
+++ b/catalog/packages/ns_package.py
@@ -19,7 +19,7 @@ import sys
import traceback
from catalog.pub.config.config import CATALOG_ROOT_PATH
-from catalog.pub.database.models import NSPackageModel, VnfPackageModel
+from catalog.pub.database.models import NSPackageModel
from catalog.pub.exceptions import CatalogException
from catalog.pub.msapi import nslcm
from catalog.pub.msapi import sdc
@@ -52,22 +52,14 @@ 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,True)
- return fmt_ns_pkg_rsp(STATUS_SUCCESS, ret[1], "")
- 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:
+ ret = NsPackage().delete_csar(csar_id, force_delete)
+ except NSLCMException as e:
return fmt_ns_pkg_rsp(STATUS_FAILED, e.message)
except:
logger.error(traceback.format_exc())
return fmt_ns_pkg_rsp(STATUS_FAILED, str(sys.exc_info()))
+ return fmt_ns_pkg_rsp(STATUS_SUCCESS, ret[1], "")
def ns_get_csars():
@@ -116,20 +108,26 @@ class NsPackage(object):
pass
def on_distribute(self, csar_id):
- if NSPackageModel.objects.filter(nsPackageId=csar_id):
- raise CatalogException("NS CSAR(%s) already exists." % csar_id)
+ if NSDModel.objects.filter(id=csar_id):
+ raise NSLCMException("NS CSAR(%s) already exists." % csar_id)
- nsd, local_file_name, nsd_json = self.get_nsd(csar_id)
+ artifact = sdc.get_artifact(sdc.ASSETTYPE_SERVICES, csar_id)
+ local_path = os.path.join(CATALOG_ROOT_PATH, csar_id)
+ local_file_name = sdc.download_artifacts(artifact["toscaModelURL"],
+ local_path, "%s.csar" % artifact.get("name", csar_id))
+
+ nsd_json = toscaparser.parse_nsd(local_file_name)
+ nsd = json.JSONDecoder().decode(nsd_json)
nsd_id = nsd["metadata"]["id"]
- if NSPackageModel.objects.filter(nsdId=nsd_id):
- raise CatalogException("NSD(%s) already exists." % nsd_id)
+ if NSDModel.objects.filter(nsd_id=nsd_id):
+ raise NSLCMException("NSD(%s) already exists." % nsd_id)
for vnf in nsd["vnfs"]:
vnfd_id = vnf["properties"]["id"]
- pkg = VnfPackageModel.objects.filter(vnfdId = vnfd_id)
+ pkg = NfPackageModel.objects.filter(vnfdid=vnfd_id)
if not pkg:
- raise CatalogException("VNF package(%s) is not distributed." % vnfd_id)
+ raise NSLCMException("VNF package(%s) is not distributed." % vnfd_id)
NSPackageModel(
nsPackageId=csar_id,
@@ -146,28 +144,14 @@ class NsPackage(object):
return [0, "CSAR(%s) distributed successfully." % csar_id]
- def get_nsd(self, csar_id):
- artifact = sdc.get_artifact(sdc.ASSETTYPE_SERVICES, csar_id)
- local_path = os.path.join(CATALOG_ROOT_PATH, csar_id)
- local_file_name = sdc.download_artifacts(artifact["toscaModelURL"], local_path)
-
- nsd_json = toscaparser.parse_nsd(local_file_name)
- nsd = json.JSONDecoder().decode(nsd_json)
-
- return nsd, local_file_name, nsd_json
-
- def delete_csar(self, csar_id,force_delete = False):
+ def delete_csar(self, csar_id, force_delete):
if force_delete:
- nslcm.delete_all_nsinst(csar_id)
- else:
- 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()
+ nslcm.delete_nf_inst(csar_id)
+ elif nslcm.get_nsInstances(csar_id):
+ raise NSLCMException("CSAR(%s) is in using, cannot be deleted." % csar_id)
+ NSDModel.objects.filter(id=csar_id).delete()
return [0, "Delete CSAR(%s) successfully." % csar_id]
-
def get_csars(self):
csars = []
nss = NSPackageModel.objects.filter()
diff --git a/catalog/packages/tests.py b/catalog/packages/tests.py
index 55adfca4..50cf15ef 100644
--- a/catalog/packages/tests.py
+++ b/catalog/packages/tests.py
@@ -66,46 +66,14 @@ class PackageTest(unittest.TestCase):
response = self.client.get("/api/catalog/v1/nspackages")
self.assertEqual(status.HTTP_200_OK, response.status_code, response.content)
self.assertEquals(self.csars,response.data)
-
- @mock.patch.object(NsPackage,'get_nsd')
- def test_ns_distribute_2(self, mock_get_nsd):
- local_file_name = "/url/local/filename"
- nsd = json.JSONEncoder().encode(nsd_json)
- mock_get_nsd.return_value = nsd_json,local_file_name,nsd
- response = self.client.post("/api/catalog/v1/nspackages",self.nsdata)
-
- self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
- self.assert_nsdmodel_result("VCPE_NS", 0)
- self.assertEqual("VNF package(456) is not distributed.", response.data["statusDescription"], response.content)
- NSPackageModel.objects.filter(nsPackageId="VCPE_NS").delete()
-
+
def test_nfpackages_get(self):
response = self.client.get("/api/catalog/v1/vnfpackages")
self.assertEqual(status.HTTP_200_OK, response.status_code, response.content)
nsdModel = NSPackageModel.objects.filter(nsdId="VCPE_NS")
self.assertEqual(len(nsdModel),0)
-
-
- @mock.patch.object(NfDistributeThread, 'get_vnfd')
- @mock.patch.object(NsPackage,'get_nsd')
- def test_ns_distribute(self, mock_get_nsd,mock_get_vnfd):
- # First distribute a VNF
- local_file_name = "/url/local/filename"
- vnfd = json.JSONEncoder().encode(vnfd_json)
- mock_get_vnfd.return_value = vnfd_json,local_file_name,vnfd
- NfDistributeThread(str(self.nf_csarId), ["1"], "1", "4").run()
-
- # Then distribute a NS associated with the below VNF
- local_file_name = "/url/local/filename"
- nsd = json.JSONEncoder().encode(nsd_json)
- mock_get_nsd.return_value = nsd_json,local_file_name,nsd
- response = self.client.post("/api/catalog/v1/nspackages",self.nsdata)
- self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
- self.assertEqual("CSAR(123) distributed successfully.", response.data["statusDescription"], response.content)
- self.assert_nsdmodel_result("VCPE_NS", 1)
- VnfPackageModel.objects.filter(vnfdId=str(self.nf_csarId)).delete()
- NSPackageModel.objects.filter(nsdId="VCPE_NS").delete()
+
@mock.patch.object(NfDistributeThread, 'get_vnfd')
def test_nf_distribute(self, mock_get_vnfd):
@@ -117,86 +85,6 @@ class PackageTest(unittest.TestCase):
self.assert_job_result("5", 100, "CSAR(dd) distribute successfully.")
VnfPackageModel.objects.filter(vnfPackageId="dd").delete()
- @mock.patch.object(NfDistributeThread, 'get_vnfd')
- @mock.patch.object(NsPackage,'get_nsd')
- @mock.patch.object(nslcm, 'get_nsInstances')
- @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"
- vnfd = json.JSONEncoder().encode(vnfd_json)
- mock_get_vnfd.return_value = vnfd_json,local_file_name,vnfd
- NfDistributeThread(str(self.nf_csarId), ["1"], "1", "4").run()
- self.assert_nfmodel_result(str(self.nf_csarId), 1)
-
- # Then distribute a NS associated with the below VNF
- local_file_name = "/url/local/filename"
- nsd = json.JSONEncoder().encode(nsd_json)
- mock_get_nsd.return_value = nsd_json,local_file_name,nsd
- response = self.client.post("/api/catalog/v1/nspackages",self.nsdata)
- self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
- self.assertEqual("CSAR(123) distributed successfully.", response.data["statusDescription"], response.content)
- self.assert_nfmodel_result(str(self.nf_csarId), 1)
- self.assert_nsdmodel_result("VCPE_NS", 1)
-
- # 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)
- self.assert_nsdmodel_result("VCPE_NS", 0)
-
- @mock.patch.object(NfDistributeThread, 'get_vnfd')
- @mock.patch.object(NsPackage,'get_nsd')
- @mock.patch.object(nslcm, 'get_nsInstances')
- @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"
- vnfd = json.JSONEncoder().encode(vnfd_json)
- mock_get_vnfd.return_value = vnfd_json,local_file_name,vnfd
- NfDistributeThread(str(self.nf_csarId), ["1"], "1", "4").run()
- self.assert_nfmodel_result(str(self.nf_csarId), 1)
-
- # Then distribute a NS associated with the below VNF
- local_file_name = "/url/local/filename"
- nsd = json.JSONEncoder().encode(nsd_json)
- mock_get_nsd.return_value = nsd_json,local_file_name,nsd
- response = self.client.post("/api/catalog/v1/nspackages",self.nsdata)
- self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
- self.assertEqual("CSAR(123) distributed successfully.", response.data["statusDescription"], response.content)
- self.assert_nfmodel_result(str(self.nf_csarId), 1)
- self.assert_nsdmodel_result("VCPE_NS", 1)
-
- # 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)
- self.assert_nsdmodel_result("VCPE_NS", 0)
-
- @mock.patch.object(NfDistributeThread, 'get_vnfd')
- @mock.patch.object(nslcm,'get_vnfInstances')
- def test_nf_package_delete_error(self, mock_get_vnfInstances, mock_get_vnfd):
- # First distribute a VNF package
- local_file_name = "/url/local/filename"
- vnfd = json.JSONEncoder().encode(vnfd_json)
- mock_get_vnfd.return_value = vnfd_json,local_file_name,vnfd
- NfDistributeThread(str(self.nf_csarId), ["1"], "1", "4").run()
- self.assert_nfmodel_result(str(self.nf_csarId), 1)
-
- # Then instantiate a VNF using this package
- mock_get_vnfInstances.return_values = [{"csarid":"1"}]
-
- # Delete it directly
- self.assert_nfmodel_result("bb",0)
- NfPkgDeleteThread("bb", "6", False).run()
- self.assert_job_result("6", 100, "Error! CSAR(bb) does not exist.")
-
@mock.patch.object(NfDistributeThread, 'get_vnfd')
@mock.patch.object(nslcm,'get_vnfInstances')