Ns descriptor related stuffs.
[vfc/nfvo/catalog.git] / catalog / packages / views / pnf_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 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.pnf_descriptor import create
24 from catalog.packages.serializers.create_pnfd_info_request import \
25     CreatePnfdInfoRequestSerializer
26 from catalog.packages.serializers.pnfd_info import PnfdInfoSerializer
27 from catalog.pub.exceptions import CatalogException
28
29 logger = logging.getLogger(__name__)
30
31
32 @swagger_auto_schema(
33     responses={
34         # status.HTTP_200_OK: Serializer(),
35         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
36     }
37 )
38 # @api_view(http_method_names=['GET'])
39 def query_multiple_pnfds(self, request):
40     # TODO
41     return None
42
43
44 @swagger_auto_schema(
45     responses={
46         # status.HTTP_200_OK: Serializer(),
47         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
48     }
49 )
50 # @api_view(http_method_names=['GET'])
51 def query_single_pnfd(self, request):
52     # TODO
53     return None
54
55
56 @swagger_auto_schema(
57     method='POST',
58     operation_description="Create an individual PNF descriptor resource",
59     request_body=CreatePnfdInfoRequestSerializer(),
60     responses={
61         status.HTTP_201_CREATED: PnfdInfoSerializer(),
62         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
63     }
64 )
65 @api_view(http_method_names=['POST'])
66 def create_pnf_descriptors(request, *args, **kwargs):
67     try:
68         create_pnfd_info_request = CreatePnfdInfoRequestSerializer(data=request.data)
69         if not create_pnfd_info_request.is_valid():
70             raise CatalogException
71         data = create(create_pnfd_info_request.data)
72         pnfd_info = PnfdInfoSerializer(data=data)
73         if not pnfd_info.is_valid():
74             raise CatalogException
75         return Response(data=pnfd_info.data, status=status.HTTP_201_CREATED)
76     except CatalogException:
77         logger.error(traceback.format_exc())
78         return Response(data={'error': 'Creating pnfd info failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)