e3a5d698223fe4ff310a11677463726f31578b6b
[vfc/nfvo/catalog.git] / catalog / packages / views / vnf_package_views.py
1 # Copyright 2018 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import traceback
16 import logging
17 from drf_yasg.utils import swagger_auto_schema, no_body
18 from rest_framework import status
19 from rest_framework.decorators import api_view
20 from rest_framework.response import Response
21 from catalog.pub.exceptions import CatalogException
22 from catalog.packages.serializers.create_vnf_pkg_info_req import CreateVnfPkgInfoRequestSerializer
23 from catalog.packages.serializers.vnf_pkg_info import VnfPkgInfoSerializer
24 from catalog.packages.biz.vnf_package import create_vnf_pkg, query_multiple
25
26 logger = logging.getLogger(__name__)
27
28
29 @swagger_auto_schema(
30     method="GET",
31     operation_description="Query multiple VNF package resource",
32     request_body=no_body,
33     responses={
34         status.HTTP_200_OK: VnfPkgInfoSerializer(),
35         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
36     }
37 )
38 @swagger_auto_schema(
39     method="POST",
40     operation_description="Create an individual VNF package resource",
41     request_body=CreateVnfPkgInfoRequestSerializer,
42     responses={
43         status.HTTP_201_CREATED: VnfPkgInfoSerializer(),
44         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
45     }
46 )
47 @api_view(http_method_names=["GET", "POST"])
48 def vnf_packages_rc(request):
49     if request.method == 'GET':
50         logger.debug("Query VNF Packages> %s" % request.data)
51         try:
52             res = query_multiple()
53             query_serializer = VnfPkgInfoSerializer(data=res)
54             if not query_serializer.is_valid():
55                 raise CatalogException
56             return Response(data=query_serializer.data, status=status.HTTP_200_OK)
57         except CatalogException:
58             logger.error(traceback.format_exc())
59             return Response(data={'error': 'Querying vnfPkg failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
60         except Exception as e:
61             logger.error(e.message)
62             logger.error(traceback.format_exc())
63             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
64
65     if request.method == 'POST':
66         logger.debug("CreateVnfPkg> %s" % request.data)
67         try:
68             req_serializer = CreateVnfPkgInfoRequestSerializer(data=request.data)
69             if not req_serializer.is_valid():
70                 raise CatalogException
71             res = create_vnf_pkg(req_serializer.data)
72             create_vnf_pkg_resp_serializer = VnfPkgInfoSerializer(data=res)
73             if not create_vnf_pkg_resp_serializer.is_valid():
74                 raise CatalogException
75             return Response(data=create_vnf_pkg_resp_serializer.data, status=status.HTTP_201_CREATED)
76         except CatalogException:
77             logger.error(traceback.format_exc())
78             return Response(data={'error': 'Creating vnfPkg failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
79         except Exception as e:
80             logger.error(e.message)
81             logger.error(traceback.format_exc())
82             return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)