1 # Copyright 2017 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 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 from django.http import FileResponse
24 from catalog.packages.biz.ns_descriptor import create, query_multiple, query_single, delete_single, upload, download
25 from catalog.packages.serializers.create_nsd_info_request import \
26 CreateNsdInfoRequestSerializer
27 from catalog.packages.serializers.nsd_info import NsdInfoSerializer
28 from catalog.packages.serializers.nsd_infos import NsdInfosSerializer
29 from catalog.pub.exceptions import CatalogException
31 logger = logging.getLogger(__name__)
36 operation_description="Query an individual NS descriptor resource",
39 status.HTTP_200_OK: NsdInfoSerializer(),
40 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
45 operation_description="Delete an individual NS descriptor resource",
48 status.HTTP_204_NO_CONTENT: {},
49 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
52 @api_view(http_method_names=['GET', 'DELETE'])
53 def ns_info_rd(request, nsdInfoId):
54 if request.method == 'GET':
56 data = query_single(nsdInfoId)
57 nsd_info = NsdInfoSerializer(data=data)
58 if not nsd_info.is_valid():
59 raise CatalogException
60 return Response(data=nsd_info.data, status=status.HTTP_200_OK)
61 except CatalogException:
62 logger.error(traceback.format_exc())
64 data={'error': 'Query of an individual NS descriptor resource failed.'},
65 status=status.HTTP_500_INTERNAL_SERVER_ERROR
68 if request.method == 'DELETE':
70 data = delete_single(nsdInfoId)
71 return Response(data={}, status=status.HTTP_204_NO_CONTENT)
72 except CatalogException:
73 logger.error(traceback.format_exc())
75 data={'error': 'Deletion of an individual NS descriptor resource failed.'},
76 status=status.HTTP_500_INTERNAL_SERVER_ERROR
82 operation_description="Create an individual NS descriptor resource",
83 request_body=CreateNsdInfoRequestSerializer(),
85 status.HTTP_201_CREATED: NsdInfoSerializer(),
86 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
91 operation_description="Query multiple NS descriptor resources",
94 status.HTTP_200_OK: NsdInfosSerializer(),
95 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
98 @api_view(http_method_names=['POST', 'GET'])
99 def ns_descriptors_rc(request, *args, **kwargs):
100 if request.method == 'POST':
102 create_nsd_info_requst = CreateNsdInfoRequestSerializer(data=request.data)
103 if not create_nsd_info_requst.is_valid():
104 raise CatalogException
105 data = create(create_nsd_info_requst.data)
106 nsd_info = NsdInfoSerializer(data=data)
107 if not nsd_info.is_valid():
108 raise CatalogException
109 return Response(data=nsd_info.data, status=status.HTTP_201_CREATED)
110 except CatalogException:
111 logger.error(traceback.format_exc())
112 return Response(data={'error': 'Creating nsd info failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
114 if request.method == 'GET':
116 data = query_multiple()
117 nsd_infos = NsdInfosSerializer(data=data)
118 if not nsd_infos.is_valid():
119 raise CatalogException
120 return Response(data=nsd_infos.data, status=status.HTTP_200_OK)
121 except CatalogException:
122 logger.error(traceback.format_exc())
124 data={'error': 'Query of multiple NS descriptor resources failed.'},
125 status=status.HTTP_500_INTERNAL_SERVER_ERROR
129 @swagger_auto_schema(
131 operation_description="Upload NSD content",
132 request_body=no_body,
134 status.HTTP_204_NO_CONTENT: {},
135 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
138 @swagger_auto_schema(
140 operation_description="Fetch NSD content",
141 request_body=no_body,
143 status.HTTP_204_NO_CONTENT: {},
144 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
147 @api_view(http_method_names=['PUT', 'GET'])
148 def nsd_content_ru(request, *args, **kwargs):
149 nsd_info_id = kwargs.get("nsdInfoId")
150 if request.method == 'PUT':
151 files = request.FILES.getlist('file')
153 upload(files[0], nsd_info_id)
154 return Response(data={}, status=status.HTTP_204_NO_CONTENT)
156 logger.error(traceback.format_exc())
157 raise CatalogException
158 return Response(data={'error': 'Uploading nsd content failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
160 if request.method == 'GET':
162 file_path = download(nsd_info_id)
163 file_name = file_path.split('/')[-1]
164 file_name = file_name.split('\\')[-1]
165 response = FileResponse(open(file_path, 'rb'), status=status.HTTP_200_OK)
166 response['Content-Disposition'] = 'attachment; filename=%s' % file_name.encode('utf-8')
169 logger.error(traceback.format_exc())
170 raise CatalogException
171 return Response(data={'error': 'Downloading nsd content failed.'},
172 status=status.HTTP_500_INTERNAL_SERVER_ERROR)