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
37 from catalog.swagger.views import EtsiCatalogFileAutoSchema
39 logger = logging.getLogger(__name__)
44 operation_description="Query a PNFD",
48 status.HTTP_200_OK: PnfdInfoSerializer(),
49 status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
50 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
55 operation_description="Delete a PNFD",
59 status.HTTP_204_NO_CONTENT: "No content",
60 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
63 @api_view(http_method_names=['GET', 'DELETE'])
64 @view_safe_call_with_log(logger=logger)
65 def pnfd_info_rd(request, **kwargs): # TODO
66 pnfd_info_id = kwargs.get('pnfdInfoId')
67 if request.method == 'GET':
68 logger.debug("Query an individual PNF descriptor> %s" % request.data)
69 data = PnfDescriptor().query_single(pnfd_info_id)
70 pnfd_info = validate_data(data, PnfdInfoSerializer)
71 return Response(data=pnfd_info.data, status=status.HTTP_200_OK)
73 if request.method == 'DELETE':
74 logger.debug("Delete an individual PNFD resource> %s" % request.data)
75 PnfDescriptor().delete_single(pnfd_info_id)
76 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
81 operation_description="Create a PNFD",
83 request_body=CreatePnfdInfoRequestSerializer(),
85 status.HTTP_201_CREATED: PnfdInfoSerializer(),
86 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
91 operation_description="Query multiple PNFDs",
95 status.HTTP_200_OK: PnfdInfosSerializer(),
96 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
99 @api_view(http_method_names=['POST', 'GET'])
100 @view_safe_call_with_log(logger=logger)
101 def pnf_descriptors_rc(request):
102 if request.method == 'POST':
103 create_pnfd_info_request = validate_data(request.data, CreatePnfdInfoRequestSerializer)
104 data = PnfDescriptor().create(create_pnfd_info_request.data)
105 validate_data(data, PnfdInfoSerializer)
106 return Response(data=data, status=status.HTTP_201_CREATED)
108 if request.method == 'GET':
109 data = PnfDescriptor().query_multiple(request)
110 validate_data(data, PnfdInfosSerializer)
111 return Response(data=data, status=status.HTTP_200_OK)
114 @swagger_auto_schema(
116 operation_description="Upload PNFD content",
118 request_body=no_body,
120 status.HTTP_204_NO_CONTENT: "No content",
121 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
124 @swagger_auto_schema(
125 auto_schema=EtsiCatalogFileAutoSchema,
127 operation_description="Fetch PNFD content",
129 request_body=no_body,
131 status.HTTP_200_OK: openapi.Response('PNFD file', schema=openapi.Schema(format=openapi.FORMAT_BINARY,
132 type=openapi.TYPE_STRING)),
133 status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
134 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
136 operation_id='Fetch PNFD content'
138 @api_view(http_method_names=['PUT', 'GET'])
139 @view_safe_call_with_log(logger=logger)
140 def pnfd_content_ru(request, **kwargs):
141 pnfd_info_id = kwargs.get("pnfdInfoId")
142 if request.method == 'PUT':
143 files = request.FILES.getlist('file')
145 local_file_name = PnfDescriptor().upload(files[0], pnfd_info_id)
146 PnfDescriptor().parse_pnfd_and_save(pnfd_info_id, local_file_name)
147 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
148 except Exception as e:
149 PnfDescriptor().handle_upload_failed(pnfd_info_id)
152 if request.method == 'GET':
153 file_iterator = PnfDescriptor().download(pnfd_info_id)
154 return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
157 @swagger_auto_schema(
159 operation_description="Parse PNF model",
160 tags=[TAG_PARSER_API],
161 request_body=ParseModelRequestSerializer,
163 status.HTTP_202_ACCEPTED: ParseModelResponseSerializer,
164 status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
165 @api_view(http_method_names=['POST'])
166 def pnf_model_parser(request, *args, **kwargs):
167 csar_id = ignore_case_get(request.data, "csarId")
168 inputs = ignore_case_get(request.data, "inputs")
170 "Enter %s, csar_id=%s, inputs=%s",
174 ret = PnfDescriptor().parse_pnfd(csar_id, inputs)
175 logger.info("Leave %s, Return value is %s", fun_name(), ret)
177 return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
178 response = validate_data(ret[1], ParseModelResponseSerializer)
179 return Response(data=response.data, status=status.HTTP_202_ACCEPTED)