Swagger issue fixes from the Ericsson team:content-type extends the "application...
[modeling/etsicatalog.git] / catalog / packages / views / pnf_descriptor_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 logging
16
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
23
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
38
39 logger = logging.getLogger(__name__)
40
41
42 @swagger_auto_schema(
43     method='GET',
44     operation_description="Query a PNFD",
45     tags=[TAG_PNFD_API],
46     request_body=no_body,
47     responses={
48         status.HTTP_200_OK: PnfdInfoSerializer(),
49         status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
50         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
51     }
52 )
53 @swagger_auto_schema(
54     method='DELETE',
55     operation_description="Delete a PNFD",
56     tags=[TAG_PNFD_API],
57     request_body=no_body,
58     responses={
59         status.HTTP_204_NO_CONTENT: "No content",
60         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
61     }
62 )
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)
72
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)
77
78
79 @swagger_auto_schema(
80     method='POST',
81     operation_description="Create a  PNFD",
82     tags=[TAG_PNFD_API],
83     request_body=CreatePnfdInfoRequestSerializer(),
84     responses={
85         status.HTTP_201_CREATED: PnfdInfoSerializer(),
86         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
87     }
88 )
89 @swagger_auto_schema(
90     method='GET',
91     operation_description="Query multiple PNFDs",
92     tags=[TAG_PNFD_API],
93     request_body=no_body,
94     responses={
95         status.HTTP_200_OK: PnfdInfosSerializer(),
96         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
97     }
98 )
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)
107
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)
112
113
114 @swagger_auto_schema(
115     method='PUT',
116     operation_description="Upload PNFD content",
117     tags=[TAG_PNFD_API],
118     request_body=no_body,
119     responses={
120         status.HTTP_204_NO_CONTENT: "No content",
121         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
122     }
123 )
124 @swagger_auto_schema(
125     auto_schema=EtsiCatalogFileAutoSchema,
126     method='GET',
127     operation_description="Fetch PNFD content",
128     tags=[TAG_PNFD_API],
129     request_body=no_body,
130     responses={
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()
135     },
136     operation_id='Fetch PNFD content'
137 )
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')
144         try:
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)
150             raise e
151
152     if request.method == 'GET':
153         file_iterator = PnfDescriptor().download(pnfd_info_id)
154         return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
155
156
157 @swagger_auto_schema(
158     method='POST',
159     operation_description="Parse PNF model",
160     tags=[TAG_PARSER_API],
161     request_body=ParseModelRequestSerializer,
162     responses={
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")
169     logger.debug(
170         "Enter %s, csar_id=%s, inputs=%s",
171         fun_name(),
172         csar_id,
173         inputs)
174     ret = PnfDescriptor().parse_pnfd(csar_id, inputs)
175     logger.info("Leave %s, Return value is %s", fun_name(), ret)
176     if ret[0] != 0:
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)