diff options
-rw-r--r-- | catalog/packages/biz/vnf_package.py | 83 | ||||
-rw-r--r-- | catalog/packages/serializers/vnf_pkg_infos.py | 20 | ||||
-rw-r--r-- | catalog/packages/urls.py | 2 | ||||
-rw-r--r-- | catalog/packages/views/vnf_package_views.py | 72 |
4 files changed, 157 insertions, 20 deletions
diff --git a/catalog/packages/biz/vnf_package.py b/catalog/packages/biz/vnf_package.py index b4440500..1fb1aee4 100644 --- a/catalog/packages/biz/vnf_package.py +++ b/catalog/packages/biz/vnf_package.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import logging import os import sys @@ -24,6 +25,8 @@ from catalog.pub.config.config import CATALOG_ROOT_PATH from catalog.pub.database.models import VnfPackageModel from catalog.pub.exceptions import CatalogException from catalog.pub.utils.values import ignore_case_get +from catalog.pub.utils import fileutil, toscaparser + logger = logging.getLogger(__name__) @@ -50,16 +53,76 @@ def create_vnf_pkg(data): def query_multiple(): - # TODO - data = { - "id": "1", - "onboardingState": "CREATED", - "operationalState": "DISABLED", - "usageState": "NOT_IN_USE", - "userDefinedData": "1", - "_links": None - } - return data + pkgs_info = [] + nf_pkgs = VnfPackageModel.objects.filter() + if not nf_pkgs.exists(): + raise CatalogException('VNF packages do not exist.') + for nf_pkg in nf_pkgs: + ret = query_single(nf_pkg.vnfPackageId) + pkgs_info.append(ret) + return pkgs_info + + +def query_single(vnfPkgId): + pkg_info = {} + nf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId) + if not nf_pkg.exists(): + raise CatalogException('VNF package(%s) does not exist.' % vnfPkgId) + pkg_info["id"] = nf_pkg[0].vnfPackageId + pkg_info["vnfdId"] = nf_pkg[0].vnfdId + pkg_info["vnfdProvider"] = nf_pkg[0].vnfVendor + pkg_info["vnfProductName"] = nf_pkg[0].vnfdProductName + pkg_info["vnfSoftwareVersion"] = nf_pkg[0].vnfSoftwareVersion + pkg_info["vnfdVersion"] = nf_pkg[0].vnfdVersion + pkg_info["checksum"] = nf_pkg[0].checksum + pkg_info["softwareImages"] = "" # TODO + pkg_info["additionalArtifacts"] = "" # TODO + pkg_info["onboardingState"] = nf_pkg[0].onboardingState + pkg_info["operationalState"] = nf_pkg[0].operationalState + pkg_info["usageState"] = nf_pkg[0].usageState + pkg_info["userDefinedData"] = nf_pkg[0].userDefinedData + pkg_info["_links"] = "" # TODO + return pkg_info + + +def delete_vnf_pkg(vnfPkgId): + vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId) + if not vnf_pkg.exists(): + logger.debug('VNF package(%s) is deleted.' % vnfPkgId) + return + if vnf_pkg[0].onboardingState != "CREATED": + raise CatalogException("The VNF package (%s) is not on-boarded" % vnfPkgId) + if vnf_pkg[0].operationalState != "DISABLED": + raise CatalogException("The VNF package (%s) is not disabled" % vnfPkgId) + if vnf_pkg[0].usageState != "NOT_IN_USE": + raise CatalogException("The VNF package (%s) is in use" % vnfPkgId) + vnf_pkg.delete() + vnf_pkg_path = os.path.join(CATALOG_ROOT_PATH, vnfPkgId) + fileutil.delete_dirs(vnf_pkg_path) + + +def parse_vnfd_and_save(vnfPkgId, vnf_pkg_path): + vnfd_json = toscaparser.parse_vnfd(vnf_pkg_path) + vnfd = json.JSONDecoder().decode(vnfd_json) + + vnfd_id = vnfd["metadata"]["id"] + if VnfPackageModel.objects.filter(vnfdId=vnfd_id): + raise CatalogException("VNFD(%s) already exists." % vnfd_id) + + vnfd_ver = vnfd["metadata"].get("vnfd_version") + if not vnfd_ver: + vnfd_ver = vnfd["metadata"].get("vnfdVersion", "undefined") + VnfPackageModel( + vnfPackageId=vnfPkgId, + vnfdId=vnfd_id, + vnfVendor=vnfd["metadata"].get("vendor", "undefined"), + vnfdVersion=vnfd_ver, + vnfSoftwareVersion=vnfd["metadata"].get("version", "undefined"), + vnfdModel=vnfd_json, + onboardingState="ONBOARDED", + operationalState="ENABLED", + usageState="NOT_IN_USE" + ).save() class VnfpkgUploadThread(threading.Thread): diff --git a/catalog/packages/serializers/vnf_pkg_infos.py b/catalog/packages/serializers/vnf_pkg_infos.py new file mode 100644 index 00000000..592ed071 --- /dev/null +++ b/catalog/packages/serializers/vnf_pkg_infos.py @@ -0,0 +1,20 @@ +# Copyright 2018 ZTE Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from rest_framework import serializers +from vnf_pkg_info import VnfPkgInfoSerializer + + +class VnfPkgInfosSerializer(serializers.Serializer): + child = VnfPkgInfoSerializer() diff --git a/catalog/packages/urls.py b/catalog/packages/urls.py index e51fd413..859c9c13 100644 --- a/catalog/packages/urls.py +++ b/catalog/packages/urls.py @@ -41,7 +41,7 @@ urlpatterns = [ # url(r'^api/nsd/v1/subscriptions', nsd_subscriptions.as_view(), name='subscriptions_rc'), # url(r'^api/nsd/v1/subscriptions/(?P<subscriptionId>[0-9a-zA-Z\-\_]+)$', nsd_subscription.as_view(), name='subscription_rd'), 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<vnfPkgId>[0-9a-zA-Z\-\_]+)$', vnf_package.as_view(), name='vnf_package_rd'), + url(r'^api/vnfpkgm/v1/vnf_packages/(?P<vnfPkgId>[0-9a-zA-Z\-\_]+)$', vnf_package_views.vnf_package_rd, name='vnf_package_rd'), # url(r'^api/vnfpkgm/v1/vnf_packages/(?P<vnfPkgId>[0-9a-zA-Z\-\_]+)/vnfd$', vnfd.as_view(), name='vnfd_r'), url(r'^api/vnfpkgm/v1/vnf_packages/(?P<vnfPkgId>[0-9a-zA-Z\-\_]+)/package_content$', vnf_package_views.upload_vnf_pkg_content, name='package_content_ru'), diff --git a/catalog/packages/views/vnf_package_views.py b/catalog/packages/views/vnf_package_views.py index 11de348d..16596613 100644 --- a/catalog/packages/views/vnf_package_views.py +++ b/catalog/packages/views/vnf_package_views.py @@ -25,7 +25,9 @@ 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.vnf_package import create_vnf_pkg, query_multiple, VnfpkgUploadThread +from catalog.packages.serializers.vnf_pkg_infos import VnfPkgInfosSerializer +from catalog.packages.biz.vnf_package import create_vnf_pkg, query_multiple, VnfpkgUploadThread, \ + query_single, delete_vnf_pkg logger = logging.getLogger(__name__) @@ -35,7 +37,7 @@ logger = logging.getLogger(__name__) operation_description="Query multiple VNF package resource", request_body=no_body, responses={ - status.HTTP_200_OK: VnfPkgInfoSerializer(), + status.HTTP_200_OK: VnfPkgInfosSerializer(), status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" } ) @@ -51,23 +53,23 @@ logger = logging.getLogger(__name__) @api_view(http_method_names=["GET", "POST"]) def vnf_packages_rc(request): if request.method == 'GET': - logger.debug("Query VNF Packages> %s" % request.data) + logger.debug("Query VNF packages> %s" % request.data) try: res = query_multiple() - query_serializer = VnfPkgInfoSerializer(data=res) + query_serializer = VnfPkgInfosSerializer(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) + return Response(data={'error': 'Query VNF package 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) if request.method == 'POST': - logger.debug("CreateVnfPkg> %s" % request.data) + logger.debug("Create VNF package> %s" % request.data) try: req_serializer = CreateVnfPkgInfoRequestSerializer(data=request.data) if not req_serializer.is_valid(): @@ -79,7 +81,7 @@ def vnf_packages_rc(request): 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) + return Response(data={'error': 'Create VNF package failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) except Exception as e: logger.error(e.message) logger.error(traceback.format_exc()) @@ -97,7 +99,7 @@ def vnf_packages_rc(request): ) @api_view(http_method_names=['PUT']) def upload_vnf_pkg_content(request, vnfPkgId): - logger.debug("UploadVnf %s" % vnfPkgId) + logger.debug("Upload VNF package %s" % vnfPkgId) file_object = request.FILES.get('file') upload_path = os.path.join(CATALOG_ROOT_PATH, vnfPkgId) if not os.path.exists(upload_path): @@ -132,8 +134,60 @@ def upload_vnf_pkg_from_uri(request, vnfPkgId): 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) + return Response(data={'error': 'Upload VNF package 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( + method='GET', + operation_description="Query an individual VNF package resource", + request_body=no_body, + responses={ + status.HTTP_200_OK: VnfPkgInfoSerializer(), + status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" + } +) +@swagger_auto_schema( + method='DELETE', + operation_description="Delete an individual VNF package resource", + request_body=no_body, + responses={ + status.HTTP_204_NO_CONTENT: None, + status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error" + } +) +@api_view(http_method_names=['GET', 'DELETE']) +def vnf_package_rd(request, vnfPkgId): + if request.method == 'GET': + logger.debug("Query an individual VNF package> %s" % request.data) + try: + res = query_single(vnfPkgId) + 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': 'Query an individual VNF package 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) + + if request.method == 'DELETE': + logger.debug("Delete an individual VNF package> %s" % request.data) + try: + delete_vnf_pkg(vnfPkgId) + return Response(data=None, status=status.HTTP_204_NO_CONTENT) + except CatalogException: + logger.error(traceback.format_exc()) + return Response(data={'error': 'Delete an individual VNF package 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) |