Ns descriptor related stuffs.
[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, 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
29
30 logger = logging.getLogger(__name__)
31
32
33 @swagger_auto_schema(
34     method='GET',
35     operation_description="Query an individual NS descriptor resource",
36     request_body=no_body,
37     responses={
38         status.HTTP_200_OK: NsdInfoSerializer(),
39         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
40     }
41 )
42 @swagger_auto_schema(
43     method='DELETE',
44     operation_description="Delete an individual NS descriptor resource",
45     request_body=no_body,
46     responses={
47         status.HTTP_204_NO_CONTENT: {},
48         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
49     }
50 )
51 @api_view(http_method_names=['GET', 'DELETE'])
52 def ns_info_rd(request, nsdInfoId):
53     if request.method == 'GET':
54         try:
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())
62             return Response(
63                 data={'error': 'Query of an individual NS descriptor resource failed.'},
64                 status=status.HTTP_500_INTERNAL_SERVER_ERROR
65             )
66
67     if request.method == 'DELETE':
68         try:
69             data = delete_single(nsdInfoId)
70             return Response(data={}, status=status.HTTP_204_NO_CONTENT)
71         except CatalogException:
72             logger.error(traceback.format_exc())
73             return Response(
74                 data={'error': 'Deletion of an individual NS descriptor resource failed.'},
75                 status=status.HTTP_500_INTERNAL_SERVER_ERROR
76             )
77
78
79 @swagger_auto_schema(
80     method='POST',
81     operation_description="Create an individual NS descriptor resource",
82     request_body=CreateNsdInfoRequestSerializer(),
83     responses={
84         status.HTTP_201_CREATED: NsdInfoSerializer(),
85         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
86     }
87 )
88 @swagger_auto_schema(
89     method='GET',
90     operation_description="Query multiple NS descriptor resources",
91     request_body=no_body,
92     responses={
93         status.HTTP_200_OK: NsdInfosSerializer(),
94         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
95     }
96 )
97 @api_view(http_method_names=['POST', 'GET'])
98 def ns_descriptors_rc(request, *args, **kwargs):
99     if request.method == 'POST':
100         try:
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)
112
113     if request.method == 'GET':
114         try:
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())
122             return Response(
123                 data={'error': 'Query of multiple NS descriptor resources failed.'},
124                 status=status.HTTP_500_INTERNAL_SERVER_ERROR
125             )
126
127
128 @swagger_auto_schema(
129     method='PUT',
130     operation_description="Upload NSD content",
131     request_body=no_body,
132     responses={
133         status.HTTP_204_NO_CONTENT: {},
134         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
135     }
136 )
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')
141     try:
142         upload(files[0], nsd_info_id)
143         return Response(data={}, status=status.HTTP_204_NO_CONTENT)
144     except IOError:
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)