1 # Copyright 2017 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from drf_yasg.utils import no_body, swagger_auto_schema
19 from rest_framework import status
20 from rest_framework.decorators import api_view
21 from rest_framework.response import Response
23 from catalog.packages.biz.pnf_descriptor import create, query_multiple, upload
24 from catalog.packages.serializers.create_pnfd_info_request import \
25 CreatePnfdInfoRequestSerializer
26 from catalog.packages.serializers.pnfd_info import PnfdInfoSerializer
27 from catalog.packages.serializers.pnfd_infos import PnfdInfosSerializer
28 from catalog.pub.exceptions import CatalogException
30 logger = logging.getLogger(__name__)
35 # status.HTTP_200_OK: Serializer(),
36 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
39 # @api_view(http_method_names=['GET'])
40 def query_multiple_pnfds(self, request):
47 # status.HTTP_200_OK: Serializer(),
48 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
51 # @api_view(http_method_names=['GET'])
52 def query_single_pnfd(self, request):
59 operation_description="Create an individual PNF descriptor resource",
60 request_body=CreatePnfdInfoRequestSerializer(),
62 status.HTTP_201_CREATED: PnfdInfoSerializer(),
63 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
68 operation_description="Query multiple PNF descriptor resources",
71 status.HTTP_200_OK: PnfdInfosSerializer(),
72 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
75 @api_view(http_method_names=['POST', 'GET'])
76 def pnf_descriptors_rc(request, *args, **kwargs):
77 if request.method == 'POST':
79 create_pnfd_info_request = CreatePnfdInfoRequestSerializer(data=request.data)
80 if not create_pnfd_info_request.is_valid():
81 raise CatalogException
82 data = create(create_pnfd_info_request.data)
83 pnfd_info = PnfdInfoSerializer(data=data)
84 if not pnfd_info.is_valid():
85 raise CatalogException
86 return Response(data=pnfd_info.data, status=status.HTTP_201_CREATED)
87 except CatalogException:
88 logger.error(traceback.format_exc())
89 return Response(data={'error': 'Creating pnfd info failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
91 if request.method == 'GET':
93 data = query_multiple()
94 pnfd_infos = PnfdInfosSerializer(data=data)
95 if not pnfd_infos.is_valid():
96 raise CatalogException
97 return Response(data=pnfd_infos.data, status=status.HTTP_200_OK)
98 except CatalogException:
99 logger.error(traceback.format_exc())
101 data={'error': 'Query of multiple PNF descriptor resources failed.'},
102 status=status.HTTP_500_INTERNAL_SERVER_ERROR
106 @swagger_auto_schema(
108 operation_description="Upload PNFD content",
109 request_body=no_body,
111 status.HTTP_204_NO_CONTENT: {},
112 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
115 @api_view(http_method_names=['PUT'])
116 def pnfd_content_ru(request, *args, **kwargs):
117 pnfd_info_id = kwargs.get("pnfdInfoId")
118 files = request.FILES.getlist('file')
120 upload(files, pnfd_info_id)
121 return Response(data={}, status=status.HTTP_204_NO_CONTENT)
123 logger.error(traceback.format_exc())
124 raise CatalogException
125 return Response(data={'error': 'Uploading pnfd content failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)