code refactor for genericparser
[modeling/etsicatalog.git] / genericparser / packages / views / ns_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.ns_descriptor import NsDescriptor
24 from genericparser.packages.serializers.create_nsd_info_request import CreateNsdInfoRequestSerializer
25 from genericparser.packages.serializers.nsd_info import NsdInfoSerializer
26 from genericparser.packages.serializers.nsd_infos import NsdInfosSerializer
27 from genericparser.packages.views.common import validate_data
28 from genericparser.pub.exceptions import GenericparserException
29 from .common import view_safe_call_with_log
30
31 logger = logging.getLogger(__name__)
32
33
34 @swagger_auto_schema(
35     method='GET',
36     operation_description="Query a NSD",
37     request_body=no_body,
38     responses={
39         status.HTTP_200_OK: NsdInfoSerializer(),
40         status.HTTP_404_NOT_FOUND: 'NSDs do not exist',
41         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
42     }
43 )
44 @swagger_auto_schema(
45     method='DELETE',
46     operation_description="Delete a NSD",
47     request_body=no_body,
48     responses={
49         status.HTTP_204_NO_CONTENT: "No content",
50         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
51     }
52 )
53 @api_view(http_method_names=['GET', 'DELETE'])
54 @view_safe_call_with_log(logger=logger)
55 def ns_info_rd(request, **kwargs):
56     nsd_info_id = kwargs.get("nsdInfoId")
57     if request.method == 'GET':
58         data = NsDescriptor().query_single(nsd_info_id)
59         nsd_info = validate_data(data, NsdInfoSerializer)
60         return Response(data=nsd_info.data, status=status.HTTP_200_OK)
61     if request.method == 'DELETE':
62         NsDescriptor().delete_single(nsd_info_id)
63         return Response(status=status.HTTP_204_NO_CONTENT)
64
65
66 @swagger_auto_schema(
67     method='POST',
68     operation_description="Create a NSD",
69     request_body=CreateNsdInfoRequestSerializer(),
70     responses={
71         status.HTTP_201_CREATED: NsdInfoSerializer(),
72         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
73     }
74 )
75 @swagger_auto_schema(
76     method='GET',
77     operation_description="Query multiple NSDs",
78     request_body=no_body,
79     responses={
80         status.HTTP_200_OK: NsdInfosSerializer(),
81         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
82     }
83 )
84 @api_view(http_method_names=['POST', 'GET'])
85 @view_safe_call_with_log(logger=logger)
86 def ns_descriptors_rc(request):
87     if request.method == 'POST':
88         create_nsd_info_request = validate_data(request.data, CreateNsdInfoRequestSerializer)
89         data = NsDescriptor().create(create_nsd_info_request.data)
90         nsd_info = validate_data(data, NsdInfoSerializer)
91         return Response(data=nsd_info.data, status=status.HTTP_201_CREATED)
92
93     if request.method == 'GET':
94         nsdId = request.query_params.get("nsdId", None)
95         data = NsDescriptor().query_multiple(nsdId)
96         nsd_infos = validate_data(data, NsdInfosSerializer)
97         return Response(data=nsd_infos.data, status=status.HTTP_200_OK)
98
99
100 @swagger_auto_schema(
101     method='PUT',
102     operation_description="Upload NSD content",
103     request_body=no_body,
104     responses={
105         status.HTTP_204_NO_CONTENT: 'PNFD file',
106         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
107     }
108 )
109 @swagger_auto_schema(
110     method='GET',
111     operation_description="Download NSD content",
112     request_body=no_body,
113     responses={
114         status.HTTP_204_NO_CONTENT: "No content",
115         status.HTTP_404_NOT_FOUND: 'NSD does not exist.',
116         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
117     }
118 )
119 @api_view(http_method_names=['PUT', 'GET'])
120 @view_safe_call_with_log(logger=logger)
121 def nsd_content_ru(request, **kwargs):
122     nsd_info_id = kwargs.get("nsdInfoId")
123     if request.method == 'PUT':
124         files = request.FILES.getlist('file')
125         try:
126             local_file_name = NsDescriptor().upload(nsd_info_id, files[0])
127             NsDescriptor().parse_nsd_and_save(nsd_info_id, local_file_name)
128             return Response(data=None, status=status.HTTP_204_NO_CONTENT)
129         except GenericparserException as e:
130             NsDescriptor().handle_upload_failed(nsd_info_id)
131             raise e
132         except Exception as e:
133             NsDescriptor().handle_upload_failed(nsd_info_id)
134             raise e
135
136     if request.method == 'GET':
137         file_range = request.META.get('HTTP_RANGE')
138         file_iterator = NsDescriptor().download(nsd_info_id, file_range)
139         return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)