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.const import TAG_NSD_API
25 from catalog.packages.serializers.create_nsd_info_request import CreateNsdInfoRequestSerializer
26 from catalog.packages.serializers.nsd_info import NsdInfoSerializer
27 from catalog.packages.serializers.nsd_infos import NsdInfosSerializer
28 from catalog.packages.views.common import validate_data
29 from catalog.pub.exceptions import CatalogException
30 from .common import view_safe_call_with_log
32 logger = logging.getLogger(__name__)
37 operation_description="Query a NSD",
41 status.HTTP_200_OK: NsdInfoSerializer(),
42 status.HTTP_404_NOT_FOUND: 'NSDs do not exist',
43 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
48 operation_description="Delete a NSD",
52 status.HTTP_204_NO_CONTENT: "No content",
53 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
56 @api_view(http_method_names=['GET', 'DELETE'])
57 @view_safe_call_with_log(logger=logger)
58 def ns_info_rd(request, **kwargs):
59 nsd_info_id = kwargs.get("nsdInfoId")
60 if request.method == 'GET':
61 data = NsDescriptor().query_single(nsd_info_id)
62 nsd_info = validate_data(data, NsdInfoSerializer)
63 return Response(data=nsd_info.data, status=status.HTTP_200_OK)
64 if request.method == 'DELETE':
65 NsDescriptor().delete_single(nsd_info_id)
66 return Response(status=status.HTTP_204_NO_CONTENT)
71 operation_description="Create a NSD",
73 request_body=CreateNsdInfoRequestSerializer(),
75 status.HTTP_201_CREATED: NsdInfoSerializer(),
76 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
81 operation_description="Query multiple NSDs",
85 status.HTTP_200_OK: NsdInfosSerializer(),
86 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
89 @api_view(http_method_names=['POST', 'GET'])
90 @view_safe_call_with_log(logger=logger)
91 def ns_descriptors_rc(request):
92 if request.method == 'POST':
93 create_nsd_info_request = validate_data(request.data, CreateNsdInfoRequestSerializer)
94 data = NsDescriptor().create(create_nsd_info_request.data)
95 validate_data(data, NsdInfoSerializer)
96 return Response(data=data, status=status.HTTP_201_CREATED)
98 if request.method == 'GET':
99 nsdId = request.query_params.get("nsdId", None)
100 data = NsDescriptor().query_multiple(nsdId)
101 validate_data(data, NsdInfosSerializer)
102 return Response(data=data, status=status.HTTP_200_OK)
105 @swagger_auto_schema(
107 operation_description="Upload NSD content",
109 request_body=no_body,
111 status.HTTP_204_NO_CONTENT: 'PNFD file',
112 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
115 @swagger_auto_schema(
117 operation_description="Download NSD content",
119 request_body=no_body,
121 status.HTTP_204_NO_CONTENT: "No content",
122 status.HTTP_404_NOT_FOUND: 'NSD does not exist.',
123 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
126 @api_view(http_method_names=['PUT', 'GET'])
127 @view_safe_call_with_log(logger=logger)
128 def nsd_content_ru(request, **kwargs):
129 nsd_info_id = kwargs.get("nsdInfoId")
130 if request.method == 'PUT':
131 files = request.FILES.getlist('file')
133 local_file_name = NsDescriptor().upload(nsd_info_id, files[0])
134 NsDescriptor().parse_nsd_and_save(nsd_info_id, local_file_name)
135 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
136 except CatalogException as e:
137 NsDescriptor().handle_upload_failed(nsd_info_id)
139 except Exception as e:
140 NsDescriptor().handle_upload_failed(nsd_info_id)
143 if request.method == 'GET':
144 file_range = request.META.get('HTTP_RANGE')
145 file_iterator = NsDescriptor().download(nsd_info_id, file_range)
146 return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
149 @swagger_auto_schema(
151 operation_description="Update a NSD",
152 request_body=no_body,
154 status.HTTP_202_ACCEPTED: "Successfully",
155 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
158 @api_view(http_method_names=['PUT'])
159 @view_safe_call_with_log(logger=logger)
160 def ns_descriptors_u(request, **kwargs):
161 if request.method == 'PUT':
162 nsd_info_id = kwargs.get("nsdInfoId")
163 NsDescriptor().update(request.data, nsd_info_id)
164 return Response(data=None, status=status.HTTP_202_ACCEPTED)