From: biancunkang Date: Thu, 23 Aug 2018 03:15:43 +0000 (+0800) Subject: Deal with nfPackage X-Git-Tag: 1.2.0~94 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F47%2F61947%2F2;p=vfc%2Fnfvo%2Fcatalog.git Deal with nfPackage Modify url and delete functions in old files Change-Id: Ie90c59fda9f73335afee61eb7090a57ccdc0e04e Issue-ID: VFC-1038 Signed-off-by: biancunkang --- diff --git a/catalog/packages/biz/vnfpackage.py b/catalog/packages/biz/vnfpackage.py index 44f2f463..55de8096 100644 --- a/catalog/packages/biz/vnfpackage.py +++ b/catalog/packages/biz/vnfpackage.py @@ -18,8 +18,6 @@ import os import sys import threading import traceback -import urllib2 -import uuid from catalog.pub.config.config import CATALOG_ROOT_PATH, CATALOG_URL_PATH, MSB_SERVICE_IP from catalog.pub.config.config import REG_TO_MSB_REG_PARAM @@ -29,7 +27,6 @@ from catalog.pub.msapi import sdc from catalog.pub.utils import fileutil from catalog.pub.utils import toscaparser from catalog.pub.utils.jobutil import JobUtil -from catalog.pub.utils.values import ignore_case_get logger = logging.getLogger(__name__) @@ -79,40 +76,6 @@ def parse_vnfd(csar_id, inputs): return [0, ret] -def create_vnf_pkg(data): - user_defined_data = ignore_case_get(data, "userDefinedData") - vnfPkgId = str(uuid.uuid4()) - VnfPackageModel.objects.create( - vnfPackageId=vnfPkgId, - onboardingState="CREATED", - operationalState="DISABLED", - usageState="NOT_IN_USE", - userDefinedData=user_defined_data - ) - data = { - "id": vnfPkgId, - "onboardingState": "CREATED", - "operationalState": "DISABLED", - "usageState": "NOT_IN_USE", - "userDefinedData": user_defined_data, - "_links": None - } - return data - - -def query_multiple(): - # TODO - data = { - "id": "1", - "onboardingState": "CREATED", - "operationalState": "DISABLED", - "usageState": "NOT_IN_USE", - "userDefinedData": "1", - "_links": None - } - return data - - class NfDistributeThread(threading.Thread): """ Sdc NF Package Distribute @@ -232,38 +195,6 @@ class NfPkgDeleteThread(threading.Thread): JobUtil.add_job_status(self.job_id, 100, "Delete CSAR(%s) successfully." % self.csar_id) -class VnfpkgUploadThread(threading.Thread): - def __init__(self, data, vnfPkgId): - threading.Thread.__init__(self) - self.vnfPkgId = vnfPkgId - self.data = data - - def run(self): - try: - self.upload_vnfPkg_from_uri() - except CatalogException as e: - logger.error(e.message) - except Exception as e: - logger.error(e.message) - logger.error(traceback.format_exc()) - logger.error(str(sys.exc_info())) - - def upload_vnfPkg_from_uri(self): - logger.debug("UploadVnf %s" % self.vnfPkgId) - uri = ignore_case_get(self.data, "addressInformation") - upload_path = os.path.join(CATALOG_ROOT_PATH, self.vnfPkgId) - if not os.path.exists(upload_path): - os.makedirs(upload_path, 0o777) - r = urllib2.Request(uri) - req = urllib2.urlopen(r) - - upload_file_name = os.path.join(upload_path, os.path.basename(uri)) - save_file = open(upload_file_name, "wb") - save_file.write(req.read()) - save_file.close() - req.close() - - class NfPackage(object): """ Actions for sdc nf package. diff --git a/catalog/packages/tests/test_vnf_package.py b/catalog/packages/tests/test_vnf_package.py index 2f90e7bb..bbb52a45 100644 --- a/catalog/packages/tests/test_vnf_package.py +++ b/catalog/packages/tests/test_vnf_package.py @@ -21,7 +21,7 @@ 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.vnfpackage import VnfpkgUploadThread +from catalog.packages.biz.vnf_package import VnfpkgUploadThread class MockReq(): diff --git a/catalog/packages/urls.py b/catalog/packages/urls.py index 3dec2ebe..f5dfecc5 100644 --- a/catalog/packages/urls.py +++ b/catalog/packages/urls.py @@ -14,7 +14,7 @@ from django.conf.urls import url -from catalog.packages.views.vnfpkg_views import package_content, upload_from_uri, vnf_packages +from catalog.packages.views import vnf_package_views from catalog.packages.views import (catalog_views, ns_descriptor_views, nsd_content_views, pnf_descriptor_views, pnfd_content_views) @@ -42,13 +42,13 @@ urlpatterns = [ # url(r'^api/nsd/v1/subscriptions', nsd_subscriptions.as_view(), name='subscriptions_rc'), # url(r'^api/nsd/v1/subscriptions/(?P[0-9a-zA-Z\-\_]+)$', nsd_subscription.as_view(), name='subscription_rd'), - url(r'^api/vnfpkgm/v1/vnf_packages$', vnf_packages.as_view(), name='vnf_packages_rc'), + url(r'^api/vnfpkgm/v1/vnf_packages$', vnf_package_views.vnf_packages_rc, name='vnf_packages_rc'), # url(r'^api/vnfpkgm/v1/vnf_packages/(?P[0-9a-zA-Z\-\_]+)$', vnf_package.as_view(), name='vnf_package_rd'), # url(r'^api/vnfpkgm/v1/vnf_packages/(?P[0-9a-zA-Z\-\_]+)/vnfd$', vnfd.as_view(), name='vnfd_r'), url(r'^api/vnfpkgm/v1/vnf_packages/(?P[0-9a-zA-Z\-\_]+)/package_content$', - package_content.as_view(), name='package_content_ru'), + vnf_package_views.upload_vnf_pkg_content, name='package_content_ru'), url(r'^api/vnfpkgm/v1/vnf_packages/(?P[0-9a-zA-Z\-\_]+)/package_content/upload_from_uri$', - upload_from_uri.as_view(), name='upload_from_uri_c'), + vnf_package_views.upload_vnf_pkg_from_uri, name='upload_from_uri_c'), # url(r'^api/vnfpkgm/v1/vnf_packages/(?P[0-9a-zA-Z\-\_]+)/artifacts/artifactPath$', artifacts.as_view(), name='artifacts_r'), # url(r'^api/vnfpkgm/v1/subscriptions', vnfpkg_subscriptions.as_view(), name='subscriptions_rc'), # url(r'^api/vnfpkgm/v1/subscriptions/(?P[0-9a-zA-Z\-\_]+)$', vnfpkg_subscription.as_view(), name='subscription_rd'), diff --git a/catalog/packages/views/vnfpkg_views.py b/catalog/packages/views/vnfpkg_views.py index 5b5e4e86..9f1bbc9f 100644 --- a/catalog/packages/views/vnfpkg_views.py +++ b/catalog/packages/views/vnfpkg_views.py @@ -12,72 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os -import traceback import logging -from catalog.pub.config.config import CATALOG_ROOT_PATH + from drf_yasg.utils import swagger_auto_schema from rest_framework import status from rest_framework.views import APIView -from rest_framework.response import Response -from catalog.packages.biz.vnfpackage import VnfpkgUploadThread -from catalog.pub.exceptions import CatalogException -from catalog.packages.serializers.upload_vnf_pkg_from_uri_req import UploadVnfPackageFromUriRequestSerializer -from catalog.packages.serializers.create_vnf_pkg_info_req import CreateVnfPkgInfoRequestSerializer -from catalog.packages.serializers.vnf_pkg_info import VnfPkgInfoSerializer -from catalog.packages.biz.vnfpackage import create_vnf_pkg, query_multiple - -logger = logging.getLogger(__name__) -class vnf_packages(APIView): - @swagger_auto_schema( - responses={ - status.HTTP_200_OK: VnfPkgInfoSerializer(), - status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" - } - ) - def get(self, request): - logger.debug("Query VNF Packages> %s" % request.data) - try: - res = query_multiple() - query_serializer = VnfPkgInfoSerializer(data=res) - if not query_serializer.is_valid(): - raise CatalogException - return Response(data=query_serializer.data, status=status.HTTP_200_OK) - except CatalogException: - logger.error(traceback.format_exc()) - return Response(data={'error': 'Querying vnfPkg failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - except Exception as e: - 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( - request_body=CreateVnfPkgInfoRequestSerializer(), - responses={ - status.HTTP_201_CREATED: VnfPkgInfoSerializer(), - status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" - } - ) - def post(self, request): - logger.debug("CreateVnfPkg> %s" % request.data) - try: - req_serializer = CreateVnfPkgInfoRequestSerializer(data=request.data) - if not req_serializer.is_valid(): - raise CatalogException - res = create_vnf_pkg(req_serializer.data) - create_vnf_pkg_resp_serializer = VnfPkgInfoSerializer(data=res) - if not create_vnf_pkg_resp_serializer.is_valid(): - raise CatalogException - return Response(data=create_vnf_pkg_resp_serializer.data, status=status.HTTP_201_CREATED) - except CatalogException: - logger.error(traceback.format_exc()) - return Response(data={'error': 'Creating vnfPkg failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - except Exception as e: - logger.error(e.message) - logger.error(traceback.format_exc()) - return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) +logger = logging.getLogger(__name__) class vnf_package(APIView): @@ -126,79 +68,6 @@ class vnfd(APIView): return None -class package_content(APIView): - @swagger_auto_schema( - responses={ - # status.HTTP_200_OK: Serializer(), - status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" - } - ) - def get(self, request): - # TODO - return None - - @swagger_auto_schema( - # request_body=CreateVnfReqSerializer(), - responses={ - # status.HTTP_201_CREATED: CreateVnfRespSerializer(), - status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" - } - ) - def post(self, request): - # TODO - return None - - def put(self, request, vnfPkgId): - logger.debug("UploadVnf %s" % vnfPkgId) - file_object = request.FILES.get('file') - upload_path = os.path.join(CATALOG_ROOT_PATH, vnfPkgId) - if not os.path.exists(upload_path): - os.makedirs(upload_path, 0o777) - try: - upload_file_name = os.path.join(upload_path, file_object.name) - with open(upload_file_name, 'wb+') as dest_file: - for chunk in file_object.chunks(): - dest_file.write(chunk) - 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) - - -class upload_from_uri(APIView): - @swagger_auto_schema( - responses={ - # status.HTTP_200_OK: Serializer(), - status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" - } - ) - def get(self, request): - # TODO - return None - - @swagger_auto_schema( - request_body=UploadVnfPackageFromUriRequestSerializer(), - responses={ - status.HTTP_202_ACCEPTED: "Successfully", - status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" - } - ) - def post(self, request, vnfPkgId): - try: - req_serializer = UploadVnfPackageFromUriRequestSerializer(data=request.data) - if not req_serializer.is_valid(): - raise CatalogException - VnfpkgUploadThread(req_serializer.data, vnfPkgId).start() - return Response(None, status=status.HTTP_202_ACCEPTED) - except CatalogException: - logger.error(traceback.format_exc()) - return Response(data={'error': 'Upload vnfPkg failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - except Exception as e: - logger.error(e.message) - logger.error(traceback.format_exc()) - return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - - class artifacts(APIView): @swagger_auto_schema( responses={