summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbiancunkang <bian.cunkang@zte.com.cn>2018-08-24 15:31:16 +0800
committerbiancunkang <bian.cunkang@zte.com.cn>2018-08-24 15:31:16 +0800
commitc71ba79b3d426364b701ef52a9a6fb7b3223afa0 (patch)
tree7f7fa08fd1c3ddd2ccc527f215304dced328df1a
parenta27f6de826fb35e35b1571c730d8a12b792959c8 (diff)
Deal with nfPackage
Modify upload function Change-Id: I9012fb70a53669a4910879bac28e137417177838 Issue-ID: VFC-1038 Signed-off-by: biancunkang <bian.cunkang@zte.com.cn>
-rw-r--r--catalog/packages/biz/vnf_package.py47
-rw-r--r--catalog/packages/tests/test_vnf_package.py19
-rw-r--r--catalog/packages/views/vnf_package_views.py18
3 files changed, 52 insertions, 32 deletions
diff --git a/catalog/packages/biz/vnf_package.py b/catalog/packages/biz/vnf_package.py
index 1fb1aee4..e9eef520 100644
--- a/catalog/packages/biz/vnf_package.py
+++ b/catalog/packages/biz/vnf_package.py
@@ -33,16 +33,16 @@ logger = logging.getLogger(__name__)
def create_vnf_pkg(data):
user_defined_data = ignore_case_get(data, "userDefinedData")
- vnfPkgId = str(uuid.uuid4())
+ vnf_pkg_id = str(uuid.uuid4())
VnfPackageModel.objects.create(
- vnfPackageId=vnfPkgId,
+ vnfPackageId=vnf_pkg_id,
onboardingState="CREATED",
operationalState="DISABLED",
usageState="NOT_IN_USE",
userDefinedData=user_defined_data
)
data = {
- "id": vnfPkgId,
+ "id": vnf_pkg_id,
"onboardingState": "CREATED",
"operationalState": "DISABLED",
"usageState": "NOT_IN_USE",
@@ -63,11 +63,11 @@ def query_multiple():
return pkgs_info
-def query_single(vnfPkgId):
+def query_single(vnf_pkg_id):
pkg_info = {}
- nf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId)
+ nf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnf_pkg_id)
if not nf_pkg.exists():
- raise CatalogException('VNF package(%s) does not exist.' % vnfPkgId)
+ raise CatalogException('VNF package(%s) does not exist.' % vnf_pkg_id)
pkg_info["id"] = nf_pkg[0].vnfPackageId
pkg_info["vnfdId"] = nf_pkg[0].vnfdId
pkg_info["vnfdProvider"] = nf_pkg[0].vnfVendor
@@ -85,23 +85,23 @@ def query_single(vnfPkgId):
return pkg_info
-def delete_vnf_pkg(vnfPkgId):
- vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId)
+def delete_vnf_pkg(vnf_pkg_id):
+ vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnf_pkg_id)
if not vnf_pkg.exists():
- logger.debug('VNF package(%s) is deleted.' % vnfPkgId)
+ logger.debug('VNF package(%s) is deleted.' % vnf_pkg_id)
return
if vnf_pkg[0].onboardingState != "CREATED":
- raise CatalogException("The VNF package (%s) is not on-boarded" % vnfPkgId)
+ raise CatalogException("The VNF package (%s) is not on-boarded" % vnf_pkg_id)
if vnf_pkg[0].operationalState != "DISABLED":
- raise CatalogException("The VNF package (%s) is not disabled" % vnfPkgId)
+ raise CatalogException("The VNF package (%s) is not disabled" % vnf_pkg_id)
if vnf_pkg[0].usageState != "NOT_IN_USE":
- raise CatalogException("The VNF package (%s) is in use" % vnfPkgId)
+ raise CatalogException("The VNF package (%s) is in use" % vnf_pkg_id)
vnf_pkg.delete()
- vnf_pkg_path = os.path.join(CATALOG_ROOT_PATH, vnfPkgId)
+ vnf_pkg_path = os.path.join(CATALOG_ROOT_PATH, vnf_pkg_id)
fileutil.delete_dirs(vnf_pkg_path)
-def parse_vnfd_and_save(vnfPkgId, vnf_pkg_path):
+def parse_vnfd_and_save(vnf_pkg_id, vnf_pkg_path):
vnfd_json = toscaparser.parse_vnfd(vnf_pkg_path)
vnfd = json.JSONDecoder().decode(vnfd_json)
@@ -113,7 +113,7 @@ def parse_vnfd_and_save(vnfPkgId, vnf_pkg_path):
if not vnfd_ver:
vnfd_ver = vnfd["metadata"].get("vnfdVersion", "undefined")
VnfPackageModel(
- vnfPackageId=vnfPkgId,
+ vnfPackageId=vnf_pkg_id,
vnfdId=vnfd_id,
vnfVendor=vnfd["metadata"].get("vendor", "undefined"),
vnfdVersion=vnfd_ver,
@@ -125,15 +125,15 @@ def parse_vnfd_and_save(vnfPkgId, vnf_pkg_path):
).save()
-class VnfpkgUploadThread(threading.Thread):
- def __init__(self, data, vnfPkgId):
+class VnfPkgUploadThread(threading.Thread):
+ def __init__(self, data, vnf_pkg_id):
threading.Thread.__init__(self)
- self.vnfPkgId = vnfPkgId
+ self.vnf_pkg_id = vnf_pkg_id
self.data = data
def run(self):
try:
- self.upload_vnfPkg_from_uri()
+ self.upload_vnf_pkg_from_uri()
except CatalogException as e:
logger.error(e.message)
except Exception as e:
@@ -141,10 +141,13 @@ class VnfpkgUploadThread(threading.Thread):
logger.error(traceback.format_exc())
logger.error(str(sys.exc_info()))
- def upload_vnfPkg_from_uri(self):
- logger.debug("UploadVnf %s" % self.vnfPkgId)
+ def upload_vnf_pkg_from_uri(self):
+ logger.debug("UploadVnf %s" % self.vnf_pkg_id)
+ vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=self.vnf_pkg_id)
+ if vnf_pkg[0].onboardingState != "CREATED":
+ raise CatalogException("VNF package (%s) is not created" % self.vnf_pkg_id)
uri = ignore_case_get(self.data, "addressInformation")
- upload_path = os.path.join(CATALOG_ROOT_PATH, self.vnfPkgId)
+ upload_path = os.path.join(CATALOG_ROOT_PATH, self.vnf_pkg_id)
if not os.path.exists(upload_path):
os.makedirs(upload_path, 0o777)
r = urllib2.Request(uri)
diff --git a/catalog/packages/tests/test_vnf_package.py b/catalog/packages/tests/test_vnf_package.py
index bbb52a45..923a3f36 100644
--- a/catalog/packages/tests/test_vnf_package.py
+++ b/catalog/packages/tests/test_vnf_package.py
@@ -21,7 +21,8 @@ from rest_framework.test import APIClient
from django.test import TestCase
from rest_framework import status
from catalog.pub.config.config import CATALOG_ROOT_PATH
-from catalog.packages.biz.vnf_package import VnfpkgUploadThread
+from catalog.packages.biz.vnf_package import VnfPkgUploadThread
+from catalog.pub.database.models import VnfPackageModel
class MockReq():
@@ -39,17 +40,25 @@ class TestVnfPackage(TestCase):
def tearDown(self):
pass
- def test_upload_vnfPkg(self):
+ def test_upload_vnf_pkg(self):
data = {'file': open(os.path.join(CATALOG_ROOT_PATH, "empty.txt"), "rb")}
+ VnfPackageModel.objects.create(
+ vnfPackageId="222",
+ onboardingState="CREATED"
+ )
response = self.client.put("/api/vnfpkgm/v1/vnf_packages/222/package_content", data=data)
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
@mock.patch.object(urllib2, 'urlopen')
- def test_upload_nf_pkg(self, mock_urlopen):
+ def test_upload_nf_pkg_from_uri(self, mock_urlopen):
+ vnf_pkg = VnfPackageModel.objects.create(
+ vnfPackageId="222",
+ onboardingState="CREATED"
+ )
req_data = {"addressInformation": "https://127.0.0.1:1234/sdc/v1/hss.csar"}
mock_urlopen.return_value = MockReq()
- vnfPkgId = "222"
- VnfpkgUploadThread(req_data, vnfPkgId).run()
+ vnf_pkg_id = vnf_pkg.vnfPackageId
+ VnfPkgUploadThread(req_data, vnf_pkg_id).run()
def test_create_vnf_pkg(self):
req_data = {
diff --git a/catalog/packages/views/vnf_package_views.py b/catalog/packages/views/vnf_package_views.py
index 16596613..4bc2fd6a 100644
--- a/catalog/packages/views/vnf_package_views.py
+++ b/catalog/packages/views/vnf_package_views.py
@@ -26,8 +26,9 @@ from catalog.packages.serializers.upload_vnf_pkg_from_uri_req import UploadVnfPa
from catalog.packages.serializers.create_vnf_pkg_info_req import CreateVnfPkgInfoRequestSerializer
from catalog.packages.serializers.vnf_pkg_info import VnfPkgInfoSerializer
from catalog.packages.serializers.vnf_pkg_infos import VnfPkgInfosSerializer
-from catalog.packages.biz.vnf_package import create_vnf_pkg, query_multiple, VnfpkgUploadThread, \
+from catalog.packages.biz.vnf_package import create_vnf_pkg, query_multiple, VnfPkgUploadThread, \
query_single, delete_vnf_pkg
+from catalog.pub.database.models import VnfPackageModel
logger = logging.getLogger(__name__)
@@ -100,6 +101,9 @@ def vnf_packages_rc(request):
@api_view(http_method_names=['PUT'])
def upload_vnf_pkg_content(request, vnfPkgId):
logger.debug("Upload VNF package %s" % vnfPkgId)
+ vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId)
+ if vnf_pkg[0].onboardingState != "CREATED":
+ raise CatalogException("VNF package (%s) is not created" % vnfPkgId)
file_object = request.FILES.get('file')
upload_path = os.path.join(CATALOG_ROOT_PATH, vnfPkgId)
if not os.path.exists(upload_path):
@@ -109,10 +113,14 @@ def upload_vnf_pkg_content(request, vnfPkgId):
with open(upload_file_name, 'wb+') as dest_file:
for chunk in file_object.chunks():
dest_file.write(chunk)
+ return Response(None, status=status.HTTP_202_ACCEPTED)
+ except CatalogException:
+ logger.error(traceback.format_exc())
+ return Response(data={'error': 'Upload VNF package failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
except Exception as e:
- logger.error("File upload exception.[%s:%s]" % (type(e), str(e)))
- logger.error("%s", traceback.format_exc())
- return Response(None, status.HTTP_202_ACCEPTED)
+ logger.error(e.message)
+ logger.error(traceback.format_exc())
+ return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@swagger_auto_schema(
@@ -130,7 +138,7 @@ def upload_vnf_pkg_from_uri(request, vnfPkgId):
req_serializer = UploadVnfPackageFromUriRequestSerializer(data=request.data)
if not req_serializer.is_valid():
raise CatalogException
- VnfpkgUploadThread(req_serializer.data, vnfPkgId).start()
+ VnfPkgUploadThread(req_serializer.data, vnfPkgId).start()
return Response(None, status=status.HTTP_202_ACCEPTED)
except CatalogException:
logger.error(traceback.format_exc())