1 # Copyright 2018 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.
17 from django.http import StreamingHttpResponse
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
22 from drf_yasg import openapi
24 from catalog.packages.biz.pnf_descriptor import PnfDescriptor
25 from catalog.packages.const import TAG_PNFD_API, TAG_PARSER_API
26 from catalog.packages.serializers.catalog_serializers import InternalErrorRequestSerializer
27 from catalog.packages.serializers.catalog_serializers import ParseModelRequestSerializer
28 from catalog.packages.serializers.catalog_serializers import ParseModelResponseSerializer
29 from catalog.packages.serializers.create_pnfd_info_request import CreatePnfdInfoRequestSerializer
30 from catalog.packages.serializers.pnfd_info import PnfdInfoSerializer
31 from catalog.packages.serializers.pnfd_infos import PnfdInfosSerializer
32 from catalog.packages.serializers.response import ProblemDetailsSerializer
33 from catalog.packages.views.common import validate_data
34 from catalog.pub.utils.syscomm import fun_name
35 from catalog.pub.utils.values import ignore_case_get
36 from .common import view_safe_call_with_log
38 logger = logging.getLogger(__name__)
43 operation_description="Query a PNFD",
47 status.HTTP_200_OK: PnfdInfoSerializer(),
48 status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
49 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
54 operation_description="Delete a PNFD",
58 status.HTTP_204_NO_CONTENT: "No content",
59 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
62 @api_view(http_method_names=['GET', 'DELETE'])
63 @view_safe_call_with_log(logger=logger)
64 def pnfd_info_rd(request, **kwargs): # TODO
65 pnfd_info_id = kwargs.get('pnfdInfoId')
66 if request.method == 'GET':
67 logger.debug("Query an individual PNF descriptor> %s" % request.data)
68 data = PnfDescriptor().query_single(pnfd_info_id)
69 pnfd_info = validate_data(data, PnfdInfoSerializer)
70 return Response(data=pnfd_info.data, status=status.HTTP_200_OK)
72 if request.method == 'DELETE':
73 logger.debug("Delete an individual PNFD resource> %s" % request.data)
74 PnfDescriptor().delete_single(pnfd_info_id)
75 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
80 operation_description="Create a PNFD",
82 request_body=CreatePnfdInfoRequestSerializer(),
84 status.HTTP_201_CREATED: PnfdInfoSerializer(),
85 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
90 operation_description="Query multiple PNFDs",
94 status.HTTP_200_OK: PnfdInfosSerializer(),
95 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
98 @api_view(http_method_names=['POST', 'GET'])
99 @view_safe_call_with_log(logger=logger)
100 def pnf_descriptors_rc(request):
101 if request.method == 'POST':
102 create_pnfd_info_request = validate_data(request.data, CreatePnfdInfoRequestSerializer)
103 data = PnfDescriptor().create(create_pnfd_info_request.data)
104 validate_data(data, PnfdInfoSerializer)
105 return Response(data=data, status=status.HTTP_201_CREATED)
107 if request.method == 'GET':
108 data = PnfDescriptor().query_multiple(request)
109 validate_data(data, PnfdInfosSerializer)
110 return Response(data=data, status=status.HTTP_200_OK)
113 @swagger_auto_schema(
115 operation_description="Upload PNFD content",
117 request_body=no_body,
119 status.HTTP_204_NO_CONTENT: "No content",
120 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
123 @swagger_auto_schema(
125 operation_description="Fetch PNFD content",
127 request_body=no_body,
129 status.HTTP_200_OK: openapi.Response('PNFD file', schema=openapi.Schema(format=openapi.FORMAT_BINARY,
130 type=openapi.TYPE_STRING)),
131 status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
132 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
134 produces='application/octet-stream',
135 operation_id='Fetch PNFD content'
137 @api_view(http_method_names=['PUT', 'GET'])
138 @view_safe_call_with_log(logger=logger)
139 def pnfd_content_ru(request, **kwargs):
140 pnfd_info_id = kwargs.get("pnfdInfoId")
141 if request.method == 'PUT':
142 files = request.FILES.getlist('file')
144 local_file_name = PnfDescriptor().upload(files[0], pnfd_info_id)
145 PnfDescriptor().parse_pnfd_and_save(pnfd_info_id, local_file_name)
146 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
147 except Exception as e:
148 PnfDescriptor().handle_upload_failed(pnfd_info_id)
151 if request.method == 'GET':
152 file_iterator = PnfDescriptor().download(pnfd_info_id)
153 return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
156 @swagger_auto_schema(
158 operation_description="Parse PNF model",
159 tags=[TAG_PARSER_API],
160 request_body=ParseModelRequestSerializer,
162 status.HTTP_202_ACCEPTED: ParseModelResponseSerializer,
163 status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
164 @api_view(http_method_names=['POST'])
165 def pnf_model_parser(request, *args, **kwargs):
166 csar_id = ignore_case_get(request.data, "csarId")
167 inputs = ignore_case_get(request.data, "inputs")
169 "Enter %s, csar_id=%s, inputs=%s",
173 ret = PnfDescriptor().parse_pnfd(csar_id, inputs)
174 logger.info("Leave %s, Return value is %s", fun_name(), ret)
176 return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
177 response = validate_data(ret[1], ParseModelResponseSerializer)
178 return Response(data=response.data, status=status.HTTP_202_ACCEPTED)