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 catalog.packages.biz.ns_descriptor import NsDescriptor
24 from catalog.packages.serializers.create_nsd_info_request import CreateNsdInfoRequestSerializer
25 from catalog.packages.serializers.nsd_info import NsdInfoSerializer
26 from catalog.packages.serializers.nsd_infos import NsdInfosSerializer
27 from catalog.packages.views.common import validate_data
28 from catalog.pub.exceptions import CatalogException
29 from .common import view_safe_call_with_log
31 logger = logging.getLogger(__name__)
36 operation_description="Query a NSD",
40 status.HTTP_200_OK: NsdInfoSerializer(),
41 status.HTTP_404_NOT_FOUND: 'NSDs do not exist',
42 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
47 operation_description="Delete a NSD",
51 status.HTTP_204_NO_CONTENT: "No content",
52 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
55 @api_view(http_method_names=['GET', 'DELETE'])
56 @view_safe_call_with_log(logger=logger)
57 def ns_info_rd(request, **kwargs):
58 nsd_info_id = kwargs.get("nsdInfoId")
59 if request.method == 'GET':
60 data = NsDescriptor().query_single(nsd_info_id)
61 nsd_info = validate_data(data, NsdInfoSerializer)
62 return Response(data=nsd_info.data, status=status.HTTP_200_OK)
63 if request.method == 'DELETE':
64 NsDescriptor().delete_single(nsd_info_id)
65 return Response(status=status.HTTP_204_NO_CONTENT)
70 operation_description="Create a NSD",
72 request_body=CreateNsdInfoRequestSerializer(),
74 status.HTTP_201_CREATED: NsdInfoSerializer(),
75 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
80 operation_description="Query multiple NSDs",
84 status.HTTP_200_OK: NsdInfosSerializer(),
85 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
88 @api_view(http_method_names=['POST', 'GET'])
89 @view_safe_call_with_log(logger=logger)
90 def ns_descriptors_rc(request):
91 if request.method == 'POST':
92 create_nsd_info_request = validate_data(request.data, CreateNsdInfoRequestSerializer)
93 data = NsDescriptor().create(create_nsd_info_request.data)
94 validate_data(data, NsdInfoSerializer)
95 return Response(data=data, status=status.HTTP_201_CREATED)
97 if request.method == 'GET':
98 nsdId = request.query_params.get("nsdId", None)
99 data = NsDescriptor().query_multiple(nsdId)
100 validate_data(data, NsdInfosSerializer)
101 return Response(data=data, status=status.HTTP_200_OK)
104 @swagger_auto_schema(
106 operation_description="Upload NSD content",
108 request_body=no_body,
110 status.HTTP_204_NO_CONTENT: 'PNFD file',
111 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
114 @swagger_auto_schema(
116 operation_description="Download NSD content",
118 request_body=no_body,
120 status.HTTP_204_NO_CONTENT: "No content",
121 status.HTTP_404_NOT_FOUND: 'NSD does not exist.',
122 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
125 @api_view(http_method_names=['PUT', 'GET'])
126 @view_safe_call_with_log(logger=logger)
127 def nsd_content_ru(request, **kwargs):
128 nsd_info_id = kwargs.get("nsdInfoId")
129 if request.method == 'PUT':
130 files = request.FILES.getlist('file')
132 local_file_name = NsDescriptor().upload(nsd_info_id, files[0])
133 NsDescriptor().parse_nsd_and_save(nsd_info_id, local_file_name)
134 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
135 except CatalogException as e:
136 NsDescriptor().handle_upload_failed(nsd_info_id)
138 except Exception as e:
139 NsDescriptor().handle_upload_failed(nsd_info_id)
142 if request.method == 'GET':
143 file_range = request.META.get('HTTP_RANGE')
144 file_iterator = NsDescriptor().download(nsd_info_id, file_range)
145 return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)