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 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.pnf_descriptor import create, query_multiple, upload
24 from catalog.packages.serializers.create_pnfd_info_request import \
25     CreatePnfdInfoRequestSerializer
26 from catalog.packages.serializers.pnfd_info import PnfdInfoSerializer
27 from catalog.packages.serializers.pnfd_infos import PnfdInfosSerializer
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 query_multiple_pnfds(self, request):
41     # TODO
42     return None
43
44
45 @swagger_auto_schema(
46     responses={
47         # status.HTTP_200_OK: Serializer(),
48         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
49     }
50 )
51 # @api_view(http_method_names=['GET'])
52 def query_single_pnfd(self, request):
53     # TODO
54     return None
55
56
57 @swagger_auto_schema(
58     method='POST',
59     operation_description="Create an individual PNF descriptor resource",
60     request_body=CreatePnfdInfoRequestSerializer(),
61     responses={
62         status.HTTP_201_CREATED: PnfdInfoSerializer(),
63         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
64     }
65 )
66 @swagger_auto_schema(
67     method='GET',
68     operation_description="Query multiple PNF descriptor resources",
69     request_body=no_body,
70     responses={
71         status.HTTP_200_OK: PnfdInfosSerializer(),
72         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
73     }
74 )
75 @api_view(http_method_names=['POST', 'GET'])
76 def pnf_descriptors_rc(request, *args, **kwargs):
77     if request.method == 'POST':
78         try:
79             create_pnfd_info_request = CreatePnfdInfoRequestSerializer(data=request.data)
80             if not create_pnfd_info_request.is_valid():
81                 raise CatalogException
82             data = create(create_pnfd_info_request.data)
83             pnfd_info = PnfdInfoSerializer(data=data)
84             if not pnfd_info.is_valid():
85                 raise CatalogException
86             return Response(data=pnfd_info.data, status=status.HTTP_201_CREATED)
87         except CatalogException:
88             logger.error(traceback.format_exc())
89             return Response(data={'error': 'Creating pnfd info failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
90
91     if request.method == 'GET':
92         try:
93             data = query_multiple()
94             pnfd_infos = PnfdInfosSerializer(data=data)
95             if not pnfd_infos.is_valid():
96                 raise CatalogException
97             return Response(data=pnfd_infos.data, status=status.HTTP_200_OK)
98         except CatalogException:
99             logger.error(traceback.format_exc())
100             return Response(
101                 data={'error': 'Query of multiple PNF descriptor resources failed.'},
102                 status=status.HTTP_500_INTERNAL_SERVER_ERROR
103             )
104
105
106 @swagger_auto_schema(
107     method='PUT',
108     operation_description="Upload PNFD content",
109     request_body=no_body,
110     responses={
111         status.HTTP_204_NO_CONTENT: {},
112         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
113     }
114 )
115 @api_view(http_method_names=['PUT'])
116 def pnfd_content_ru(request, *args, **kwargs):
117     pnfd_info_id = kwargs.get("pnfdInfoId")
118     files = request.FILES.getlist('file')
119     try:
120         upload(files, pnfd_info_id)
121         return Response(data={}, status=status.HTTP_204_NO_CONTENT)
122     except IOError:
123         logger.error(traceback.format_exc())
124         raise CatalogException
125         return Response(data={'error': 'Uploading pnfd content failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)