Merge "Deal with nfPackage"
[vfc/nfvo/catalog.git] / catalog / packages / views / ns_descriptor_views.py
1 # Copyright 2017 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import logging
16 import traceback
17
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
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
29
30 logger = logging.getLogger(__name__)
31
32
33 @swagger_auto_schema(
34     responses={
35         # status.HTTP_200_OK: Serializer(),
36         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
37     }
38 )
39 # @api_view(http_method_names=['GET'])
40 def ns_info_rd(request):
41     # TODO
42     return None
43
44
45 @swagger_auto_schema(
46     method='POST',
47     operation_description="Create an individual NS descriptor resource",
48     request_body=CreateNsdInfoRequestSerializer(),
49     responses={
50         status.HTTP_201_CREATED: NsdInfoSerializer(),
51         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
52     }
53 )
54 @swagger_auto_schema(
55     method='GET',
56     operation_description="Query multiple NS descriptor resources",
57     request_body=no_body,
58     responses={
59         status.HTTP_200_OK: NsdInfosSerializer(),
60         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
61     }
62 )
63 @api_view(http_method_names=['POST', 'GET'])
64 def ns_descriptors_rc(request, *args, **kwargs):
65     if request.method == 'POST':
66         try:
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)
78
79     if request.method == 'GET':
80         try:
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())
88             return Response(
89                 data={'error': 'Query of multiple NS descriptor resources failed.'},
90                 status=status.HTTP_500_INTERNAL_SERVER_ERROR
91             )