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
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 # status.HTTP_200_OK: Serializer(),
36 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
39 # @api_view(http_method_names=['GET'])
40 def ns_info_rd(request):
47 operation_description="Create an individual NS descriptor resource",
48 request_body=CreateNsdInfoRequestSerializer(),
50 status.HTTP_201_CREATED: NsdInfoSerializer(),
51 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
56 operation_description="Query multiple NS descriptor resources",
59 status.HTTP_200_OK: NsdInfosSerializer(),
60 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
63 @api_view(http_method_names=['POST', 'GET'])
64 def ns_descriptors_rc(request, *args, **kwargs):
65 if request.method == 'POST':
67 create_nsd_info_requst = CreateNsdInfoRequestSerializer(data=request.data)
68 if not create_nsd_info_requst.is_valid():
69 raise CatalogException
70 data = create(create_nsd_info_requst.data)
71 nsd_info = NsdInfoSerializer(data=data)
72 if not nsd_info.is_valid():
73 raise CatalogException
74 return Response(data=nsd_info.data, status=status.HTTP_201_CREATED)
75 except CatalogException:
76 logger.error(traceback.format_exc())
77 return Response(data={'error': 'Creating nsd info failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
79 if request.method == 'GET':
81 data = query_multiple()
82 nsd_infos = NsdInfosSerializer(data=data)
83 if not nsd_infos.is_valid():
84 raise CatalogException
85 return Response(data=nsd_infos.data, status=status.HTTP_200_OK)
86 except CatalogException:
87 logger.error(traceback.format_exc())
89 data={'error': 'Query of multiple NS descriptor resources failed.'},
90 status=status.HTTP_500_INTERNAL_SERVER_ERROR