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