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