1 # Copyright 2018 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
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
31 logger = logging.getLogger(__name__)
36 operation_description="Query a NSD",
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"
46 operation_description="Delete a NSD",
49 status.HTTP_204_NO_CONTENT: "No content",
50 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
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)
68 operation_description="Create a NSD",
69 request_body=CreateNsdInfoRequestSerializer(),
71 status.HTTP_201_CREATED: NsdInfoSerializer(),
72 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
77 operation_description="Query multiple NSDs",
80 status.HTTP_200_OK: NsdInfosSerializer(),
81 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
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)
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)
100 @swagger_auto_schema(
102 operation_description="Upload NSD content",
103 request_body=no_body,
105 status.HTTP_204_NO_CONTENT: 'PNFD file',
106 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
109 @swagger_auto_schema(
111 operation_description="Download NSD content",
112 request_body=no_body,
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"
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')
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)
132 except Exception as e:
133 NsDescriptor().handle_upload_failed(nsd_info_id)
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)