update swagger info, add tags for swagger_auto_schema
[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
23 from catalog.packages.biz.pnf_descriptor import PnfDescriptor
24 from catalog.packages.serializers.create_pnfd_info_request import CreatePnfdInfoRequestSerializer
25 from catalog.packages.serializers.pnfd_info import PnfdInfoSerializer
26 from catalog.packages.serializers.pnfd_infos import PnfdInfosSerializer
27 from catalog.packages.views.common import validate_data
28 from catalog.packages.serializers.catalog_serializers import ParseModelRequestSerializer
29 from catalog.packages.serializers.catalog_serializers import ParseModelResponseSerializer
30 from catalog.packages.serializers.catalog_serializers import InternalErrorRequestSerializer
31 from catalog.packages.serializers.response import ProblemDetailsSerializer
32 from catalog.pub.utils.syscomm import fun_name
33 from catalog.pub.utils.values import ignore_case_get
34 from .common import view_safe_call_with_log
35
36 logger = logging.getLogger(__name__)
37
38
39 @swagger_auto_schema(
40     method='GET',
41     operation_description="Query a PNFD",
42     tags=["PNFD API"],
43     request_body=no_body,
44     responses={
45         status.HTTP_200_OK: PnfdInfoSerializer(),
46         status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
47         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
48     }
49 )
50 @swagger_auto_schema(
51     method='DELETE',
52     operation_description="Delete a PNFD",
53     tags=["PNFD API"],
54     request_body=no_body,
55     responses={
56         status.HTTP_204_NO_CONTENT: "No content",
57         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
58     }
59 )
60 @api_view(http_method_names=['GET', 'DELETE'])
61 @view_safe_call_with_log(logger=logger)
62 def pnfd_info_rd(request, **kwargs):  # TODO
63     pnfd_info_id = kwargs.get('pnfdInfoId')
64     if request.method == 'GET':
65         logger.debug("Query an individual PNF descriptor> %s" % request.data)
66         data = PnfDescriptor().query_single(pnfd_info_id)
67         pnfd_info = validate_data(data, PnfdInfoSerializer)
68         return Response(data=pnfd_info.data, status=status.HTTP_200_OK)
69
70     if request.method == 'DELETE':
71         logger.debug("Delete an individual PNFD resource> %s" % request.data)
72         PnfDescriptor().delete_single(pnfd_info_id)
73         return Response(data=None, status=status.HTTP_204_NO_CONTENT)
74
75
76 @swagger_auto_schema(
77     method='POST',
78     operation_description="Create a  PNFD",
79     tags=["PNFD API"],
80     request_body=CreatePnfdInfoRequestSerializer(),
81     responses={
82         status.HTTP_201_CREATED: PnfdInfoSerializer(),
83         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
84     }
85 )
86 @swagger_auto_schema(
87     method='GET',
88     operation_description="Query multiple PNFDs",
89     tags=["PNFD API"],
90     request_body=no_body,
91     responses={
92         status.HTTP_200_OK: PnfdInfosSerializer(),
93         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
94     }
95 )
96 @api_view(http_method_names=['POST', 'GET'])
97 @view_safe_call_with_log(logger=logger)
98 def pnf_descriptors_rc(request):
99     if request.method == 'POST':
100         create_pnfd_info_request = validate_data(request.data, CreatePnfdInfoRequestSerializer)
101         data = PnfDescriptor().create(create_pnfd_info_request.data)
102         validate_data(data, PnfdInfoSerializer)
103         return Response(data=data, status=status.HTTP_201_CREATED)
104
105     if request.method == 'GET':
106         data = PnfDescriptor().query_multiple(request)
107         validate_data(data, PnfdInfosSerializer)
108         return Response(data=data, status=status.HTTP_200_OK)
109
110
111 @swagger_auto_schema(
112     method='PUT',
113     operation_description="Upload PNFD content",
114     tags=["PNFD API"],
115     request_body=no_body,
116     responses={
117         status.HTTP_204_NO_CONTENT: "No content",
118         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
119     }
120 )
121 @swagger_auto_schema(
122     method='GET',
123     operation_description="Fetch PNFD content",
124     tags=["PNFD API"],
125     request_body=no_body,
126     responses={
127         status.HTTP_204_NO_CONTENT: 'PNFD file',
128         status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
129         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
130     }
131 )
132 @api_view(http_method_names=['PUT', 'GET'])
133 @view_safe_call_with_log(logger=logger)
134 def pnfd_content_ru(request, **kwargs):
135     pnfd_info_id = kwargs.get("pnfdInfoId")
136     if request.method == 'PUT':
137         files = request.FILES.getlist('file')
138         try:
139             local_file_name = PnfDescriptor().upload(files[0], pnfd_info_id)
140             PnfDescriptor().parse_pnfd_and_save(pnfd_info_id, local_file_name)
141             return Response(data=None, status=status.HTTP_204_NO_CONTENT)
142         except Exception as e:
143             PnfDescriptor().handle_upload_failed(pnfd_info_id)
144             raise e
145
146     if request.method == 'GET':
147         file_iterator = PnfDescriptor().download(pnfd_info_id)
148         return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
149
150
151 @swagger_auto_schema(
152     method='POST',
153     operation_description="Parse PNF model",
154     tags=["Parser API"],
155     request_body=ParseModelRequestSerializer,
156     responses={
157         status.HTTP_202_ACCEPTED: ParseModelResponseSerializer,
158         status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
159 @api_view(http_method_names=['POST'])
160 def pnf_model_parser(request, *args, **kwargs):
161     csar_id = ignore_case_get(request.data, "csarId")
162     inputs = ignore_case_get(request.data, "inputs")
163     logger.debug(
164         "Enter %s, csar_id=%s, inputs=%s",
165         fun_name(),
166         csar_id,
167         inputs)
168     ret = PnfDescriptor().parse_pnfd(csar_id, inputs)
169     logger.info("Leave %s, Return value is %s", fun_name(), ret)
170     if ret[0] != 0:
171         return Response(data={'error': ret[1]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
172     response = validate_data(ret[1], ParseModelResponseSerializer)
173     return Response(data=response.data, status=status.HTTP_202_ACCEPTED)