1.Update API endpoint; 2. update swagger information.
[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.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
36
37 logger = logging.getLogger(__name__)
38
39
40 @swagger_auto_schema(
41     method='GET',
42     operation_description="Query a PNFD",
43     tags=[TAG_PNFD_API],
44     request_body=no_body,
45     responses={
46         status.HTTP_200_OK: PnfdInfoSerializer(),
47         status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
48         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
49     }
50 )
51 @swagger_auto_schema(
52     method='DELETE',
53     operation_description="Delete a PNFD",
54     tags=[TAG_PNFD_API],
55     request_body=no_body,
56     responses={
57         status.HTTP_204_NO_CONTENT: "No content",
58         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
59     }
60 )
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)
70
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)
75
76
77 @swagger_auto_schema(
78     method='POST',
79     operation_description="Create a  PNFD",
80     tags=[TAG_PNFD_API],
81     request_body=CreatePnfdInfoRequestSerializer(),
82     responses={
83         status.HTTP_201_CREATED: PnfdInfoSerializer(),
84         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
85     }
86 )
87 @swagger_auto_schema(
88     method='GET',
89     operation_description="Query multiple PNFDs",
90     tags=[TAG_PNFD_API],
91     request_body=no_body,
92     responses={
93         status.HTTP_200_OK: PnfdInfosSerializer(),
94         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
95     }
96 )
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)
105
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)
110
111
112 @swagger_auto_schema(
113     method='PUT',
114     operation_description="Upload PNFD content",
115     tags=[TAG_PNFD_API],
116     request_body=no_body,
117     responses={
118         status.HTTP_204_NO_CONTENT: "No content",
119         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
120     }
121 )
122 @swagger_auto_schema(
123     method='GET',
124     operation_description="Fetch PNFD content",
125     tags=[TAG_PNFD_API],
126     request_body=no_body,
127     responses={
128         status.HTTP_204_NO_CONTENT: 'PNFD file',
129         status.HTTP_404_NOT_FOUND: ProblemDetailsSerializer(),
130         status.HTTP_500_INTERNAL_SERVER_ERROR: ProblemDetailsSerializer()
131     }
132 )
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')
139         try:
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)
145             raise e
146
147     if request.method == 'GET':
148         file_iterator = PnfDescriptor().download(pnfd_info_id)
149         return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
150
151
152 @swagger_auto_schema(
153     method='POST',
154     operation_description="Parse PNF model",
155     tags=[TAG_PARSER_API],
156     request_body=ParseModelRequestSerializer,
157     responses={
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")
164     logger.debug(
165         "Enter %s, csar_id=%s, inputs=%s",
166         fun_name(),
167         csar_id,
168         inputs)
169     ret = PnfDescriptor().parse_pnfd(csar_id, inputs)
170     logger.info("Leave %s, Return value is %s", fun_name(), ret)
171     if ret[0] != 0:
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)