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.
18 from django.http import StreamingHttpResponse
19 from drf_yasg.utils import no_body, swagger_auto_schema
20 from rest_framework import status
21 from rest_framework.decorators import api_view
22 from rest_framework.response import Response
24 from catalog.packages.biz.ns_descriptor import NsDescriptor
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, ResourceNotFoundException
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 def ns_info_rd(request, **kwargs):
55 nsd_info_id = kwargs.get("nsdInfoId")
56 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 except ResourceNotFoundException as e:
62 logger.error(e.message)
63 error_data = {'error': e.message}
64 error_code = status.HTTP_404_NOT_FOUND
65 except Exception as e:
66 logger.error(e.message)
67 logger.error(traceback.format_exc())
68 error_data = {'error': 'Query of NSD(%s) failed.' % nsd_info_id}
69 error_code = status.HTTP_500_INTERNAL_SERVER_ERROR
70 return Response(data=error_data, status=error_code)
72 if request.method == 'DELETE':
74 NsDescriptor().delete_single(nsd_info_id)
75 return Response(status=status.HTTP_204_NO_CONTENT)
76 except Exception as e:
77 logger.error(e.message)
78 logger.error(traceback.format_exc())
79 error_data = {'error': 'Deletion of NSD(%s) failed.' % nsd_info_id}
80 return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
85 operation_description="Create a NSD",
86 request_body=CreateNsdInfoRequestSerializer(),
88 status.HTTP_201_CREATED: NsdInfoSerializer(),
89 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
94 operation_description="Query multiple NSDs",
97 status.HTTP_200_OK: NsdInfosSerializer(),
98 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
101 @api_view(http_method_names=['POST', 'GET'])
102 def ns_descriptors_rc(request):
103 if request.method == 'POST':
105 create_nsd_info_request = validate_data(request.data, CreateNsdInfoRequestSerializer)
106 data = NsDescriptor().create(create_nsd_info_request.data)
107 nsd_info = validate_data(data, NsdInfoSerializer)
108 return Response(data=nsd_info.data, status=status.HTTP_201_CREATED)
109 except Exception as e:
110 logger.error(e.message)
111 logger.error(traceback.format_exc())
112 error_data = {'error': 'Creating a NSD failed.'}
113 return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
115 if request.method == 'GET':
117 nsdId = request.query_params.get("nsdId", None)
118 data = NsDescriptor().query_multiple(nsdId)
119 nsd_infos = validate_data(data, NsdInfosSerializer)
120 return Response(data=nsd_infos.data, status=status.HTTP_200_OK)
121 except Exception as e:
122 logger.error(e.message)
123 logger.error(traceback.format_exc())
124 error_data = {'error': 'Query of multiple NSDs failed.'}
125 return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
128 @swagger_auto_schema(
130 operation_description="Upload NSD content",
131 request_body=no_body,
133 status.HTTP_204_NO_CONTENT: 'PNFD file',
134 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
137 @swagger_auto_schema(
139 operation_description="Download NSD content",
140 request_body=no_body,
142 status.HTTP_204_NO_CONTENT: "No content",
143 status.HTTP_404_NOT_FOUND: 'NSD does not exist.',
144 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
147 @api_view(http_method_names=['PUT', 'GET'])
148 def nsd_content_ru(request, **kwargs):
149 nsd_info_id = kwargs.get("nsdInfoId")
150 if request.method == 'PUT':
151 files = request.FILES.getlist('file')
153 local_file_name = NsDescriptor().upload(nsd_info_id, files[0])
154 NsDescriptor().parse_nsd_and_save(nsd_info_id, local_file_name)
155 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
156 except CatalogException as e:
157 NsDescriptor().handle_upload_failed(nsd_info_id)
158 logger.error(e.message)
159 error_data = {'error': e.message}
160 except Exception as e:
161 NsDescriptor().handle_upload_failed(nsd_info_id)
162 logger.error(e.message)
163 logger.error(traceback.format_exc())
164 error_data = {'error': 'Uploading NSD(%s) failed.'}
165 return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
167 if request.method == 'GET':
169 file_range = request.META.get('RANGE')
170 file_iterator = NsDescriptor().download(nsd_info_id, file_range)
171 return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
172 except ResourceNotFoundException as e:
173 logger.error(e.message)
174 error_data = {'error': e.message}
175 error_code = status.HTTP_404_NOT_FOUND
176 except CatalogException as e:
177 logger.error(e.message)
178 error_data = {'error': e.message}
179 error_code = status.HTTP_500_INTERNAL_SERVER_ERROR
180 except Exception as e:
181 logger.error(e.message)
182 logger.error(traceback.format_exc())
183 error_data = {'error': 'Downloading NSD(%s) failed.' % nsd_info_id}
184 error_code = status.HTTP_500_INTERNAL_SERVER_ERROR
185 return Response(data=error_data, status=error_code)