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
23 from catalog.packages.biz.pnf_descriptor import PnfDescriptor
24 from catalog.packages.const import TAG_PNFD_API, TAG_PARSER_API
25 from catalog.packages.serializers.catalog_serializers import InternalErrorRequestSerializer
26 from catalog.packages.serializers.catalog_serializers import ParseModelRequestSerializer
27 from catalog.packages.serializers.catalog_serializers import ParseModelResponseSerializer
28 from catalog.packages.serializers.create_pnfd_info_request import CreatePnfdInfoRequestSerializer
29 from catalog.packages.serializers.pnfd_info import PnfdInfoSerializer
30 from catalog.packages.serializers.pnfd_infos import PnfdInfosSerializer
31 from catalog.packages.serializers.response import ProblemDetailsSerializer
32 from catalog.packages.views.common import validate_data
33 from catalog.pub.utils.syscomm import fun_name
34 from catalog.pub.utils.values import ignore_case_get
35 from .common import view_safe_call_with_log
37 logger = logging.getLogger(__name__)
42 operation_description="Query a PNFD",
46 status.HTTP_200_OK: PnfdInfoSerializer(),
47 status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
48 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
53 operation_description="Delete a PNFD",
57 status.HTTP_204_NO_CONTENT: "No content",
58 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
61 @api_view(http_method_names=['GET', 'DELETE'])
62 @view_safe_call_with_log(logger=logger)
63 def pnfd_info_rd(request, **kwargs): # TODO
64 pnfd_info_id = kwargs.get('pnfdInfoId')
65 if request.method == 'GET':
66 logger.debug("Query an individual PNF descriptor> %s" % request.data)
67 data = PnfDescriptor().query_single(pnfd_info_id)
68 pnfd_info = validate_data(data, PnfdInfoSerializer)
69 return Response(data=pnfd_info.data, status=status.HTTP_200_OK)
71 if request.method == 'DELETE':
72 logger.debug("Delete an individual PNFD resource> %s" % request.data)
73 PnfDescriptor().delete_single(pnfd_info_id)
74 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
79 operation_description="Create a PNFD",
81 request_body=CreatePnfdInfoRequestSerializer(),
83 status.HTTP_201_CREATED: PnfdInfoSerializer(),
84 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
89 operation_description="Query multiple PNFDs",
93 status.HTTP_200_OK: PnfdInfosSerializer(),
94 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
97 @api_view(http_method_names=['POST', 'GET'])
98 @view_safe_call_with_log(logger=logger)
99 def pnf_descriptors_rc(request):
100 if request.method == 'POST':
101 create_pnfd_info_request = validate_data(request.data, CreatePnfdInfoRequestSerializer)
102 data = PnfDescriptor().create(create_pnfd_info_request.data)
103 validate_data(data, PnfdInfoSerializer)
104 return Response(data=data, status=status.HTTP_201_CREATED)
106 if request.method == 'GET':
107 data = PnfDescriptor().query_multiple(request)
108 validate_data(data, PnfdInfosSerializer)
109 return Response(data=data, status=status.HTTP_200_OK)
112 @swagger_auto_schema(
114 operation_description="Upload PNFD content",
116 request_body=no_body,
118 status.HTTP_204_NO_CONTENT: "No content",
119 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
122 @swagger_auto_schema(
124 operation_description="Fetch PNFD content",
126 request_body=no_body,
128 status.HTTP_204_NO_CONTENT: 'PNFD file',
129 status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
130 status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
133 @api_view(http_method_names=['PUT', 'GET'])
134 @view_safe_call_with_log(logger=logger)
135 def pnfd_content_ru(request, **kwargs):
136 pnfd_info_id = kwargs.get("pnfdInfoId")
137 if request.method == 'PUT':
138 files = request.FILES.getlist('file')
140 local_file_name = PnfDescriptor().upload(files[0], pnfd_info_id)
141 PnfDescriptor().parse_pnfd_and_save(pnfd_info_id, local_file_name)
142 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
143 except Exception as e:
144 PnfDescriptor().handle_upload_failed(pnfd_info_id)
147 if request.method == 'GET':
148 file_iterator = PnfDescriptor().download(pnfd_info_id)
149 return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
152 @swagger_auto_schema(
154 operation_description="Parse PNF model",
155 tags=[TAG_PARSER_API],
156 request_body=ParseModelRequestSerializer,
158 status.HTTP_202_ACCEPTED: ParseModelResponseSerializer,
159 status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
160 @api_view(http_method_names=['POST'])
161 def pnf_model_parser(request, *args, **kwargs):
162 csar_id = ignore_case_get(request.data, "csarId")
163 inputs = ignore_case_get(request.data, "inputs")
165 "Enter %s, csar_id=%s, inputs=%s",
169 ret = PnfDescriptor().parse_pnfd(csar_id, inputs)
170 logger.info("Leave %s, Return value is %s", fun_name(), ret)
172 return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
173 response = validate_data(ret[1], ParseModelResponseSerializer)
174 return Response(data=response.data, status=status.HTTP_202_ACCEPTED)