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
23 from catalog.packages.biz.ns_descriptor import create, query_multiple, query_single, delete_single, upload
24 from catalog.packages.serializers.create_nsd_info_request import \
25 CreateNsdInfoRequestSerializer
26 from catalog.packages.serializers.nsd_info import NsdInfoSerializer
27 from catalog.packages.serializers.nsd_infos import NsdInfosSerializer
28 from catalog.pub.exceptions import CatalogException
30 logger = logging.getLogger(__name__)
35 operation_description="Query an individual NS descriptor resource",
38 status.HTTP_200_OK: NsdInfoSerializer(),
39 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
44 operation_description="Delete an individual NS descriptor resource",
47 status.HTTP_204_NO_CONTENT: {},
48 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
51 @api_view(http_method_names=['GET', 'DELETE'])
52 def ns_info_rd(request, nsdInfoId):
53 if request.method == 'GET':
55 data = query_single(nsdInfoId)
56 nsd_info = NsdInfoSerializer(data=data)
57 if not nsd_info.is_valid():
58 raise CatalogException
59 return Response(data=nsd_info.data, status=status.HTTP_200_OK)
60 except CatalogException:
61 logger.error(traceback.format_exc())
63 data={'error': 'Query of an individual NS descriptor resource failed.'},
64 status=status.HTTP_500_INTERNAL_SERVER_ERROR
67 if request.method == 'DELETE':
69 data = delete_single(nsdInfoId)
70 return Response(data={}, status=status.HTTP_204_NO_CONTENT)
71 except CatalogException:
72 logger.error(traceback.format_exc())
74 data={'error': 'Deletion of an individual NS descriptor resource failed.'},
75 status=status.HTTP_500_INTERNAL_SERVER_ERROR
81 operation_description="Create an individual NS descriptor resource",
82 request_body=CreateNsdInfoRequestSerializer(),
84 status.HTTP_201_CREATED: NsdInfoSerializer(),
85 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
90 operation_description="Query multiple NS descriptor resources",
93 status.HTTP_200_OK: NsdInfosSerializer(),
94 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
97 @api_view(http_method_names=['POST', 'GET'])
98 def ns_descriptors_rc(request, *args, **kwargs):
99 if request.method == 'POST':
101 create_nsd_info_requst = CreateNsdInfoRequestSerializer(data=request.data)
102 if not create_nsd_info_requst.is_valid():
103 raise CatalogException
104 data = create(create_nsd_info_requst.data)
105 nsd_info = NsdInfoSerializer(data=data)
106 if not nsd_info.is_valid():
107 raise CatalogException
108 return Response(data=nsd_info.data, status=status.HTTP_201_CREATED)
109 except CatalogException:
110 logger.error(traceback.format_exc())
111 return Response(data={'error': 'Creating nsd info failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
113 if request.method == 'GET':
115 data = query_multiple()
116 nsd_infos = NsdInfosSerializer(data=data)
117 if not nsd_infos.is_valid():
118 raise CatalogException
119 return Response(data=nsd_infos.data, status=status.HTTP_200_OK)
120 except CatalogException:
121 logger.error(traceback.format_exc())
123 data={'error': 'Query of multiple NS descriptor resources failed.'},
124 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: {},
134 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
137 @api_view(http_method_names=['PUT'])
138 def nsd_content_ru(request, *args, **kwargs):
139 nsd_info_id = kwargs.get("nsdInfoId")
140 files = request.FILES.getlist('file')
142 upload(files[0], nsd_info_id)
143 return Response(data={}, status=status.HTTP_204_NO_CONTENT)
145 logger.error(traceback.format_exc())
146 raise CatalogException
147 return Response(data={'error': 'Uploading nsd content failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)