Merge "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 from django.http import FileResponse
23
24 from catalog.packages.biz.ns_descriptor import create, query_multiple, query_single, delete_single, upload, download
25 from catalog.packages.serializers.create_nsd_info_request import \
26     CreateNsdInfoRequestSerializer
27 from catalog.packages.serializers.nsd_info import NsdInfoSerializer
28 from catalog.packages.serializers.nsd_infos import NsdInfosSerializer
29 from catalog.pub.exceptions import CatalogException
30
31 logger = logging.getLogger(__name__)
32
33
34 @swagger_auto_schema(
35     method='GET',
36     operation_description="Query an individual NS descriptor resource",
37     request_body=no_body,
38     responses={
39         status.HTTP_200_OK: NsdInfoSerializer(),
40         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
41     }
42 )
43 @swagger_auto_schema(
44     method='DELETE',
45     operation_description="Delete an individual NS descriptor resource",
46     request_body=no_body,
47     responses={
48         status.HTTP_204_NO_CONTENT: {},
49         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
50     }
51 )
52 @api_view(http_method_names=['GET', 'DELETE'])
53 def ns_info_rd(request, nsdInfoId):
54     if request.method == 'GET':
55         try:
56             data = query_single(nsdInfoId)
57             nsd_info = NsdInfoSerializer(data=data)
58             if not nsd_info.is_valid():
59                 raise CatalogException
60             return Response(data=nsd_info.data, status=status.HTTP_200_OK)
61         except CatalogException:
62             logger.error(traceback.format_exc())
63             return Response(
64                 data={'error': 'Query of an individual NS descriptor resource failed.'},
65                 status=status.HTTP_500_INTERNAL_SERVER_ERROR
66             )
67
68     if request.method == 'DELETE':
69         try:
70             data = delete_single(nsdInfoId)
71             return Response(data={}, status=status.HTTP_204_NO_CONTENT)
72         except CatalogException:
73             logger.error(traceback.format_exc())
74             return Response(
75                 data={'error': 'Deletion of an individual NS descriptor resource failed.'},
76                 status=status.HTTP_500_INTERNAL_SERVER_ERROR
77             )
78
79
80 @swagger_auto_schema(
81     method='POST',
82     operation_description="Create an individual NS descriptor resource",
83     request_body=CreateNsdInfoRequestSerializer(),
84     responses={
85         status.HTTP_201_CREATED: NsdInfoSerializer(),
86         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
87     }
88 )
89 @swagger_auto_schema(
90     method='GET',
91     operation_description="Query multiple NS descriptor resources",
92     request_body=no_body,
93     responses={
94         status.HTTP_200_OK: NsdInfosSerializer(),
95         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
96     }
97 )
98 @api_view(http_method_names=['POST', 'GET'])
99 def ns_descriptors_rc(request, *args, **kwargs):
100     if request.method == 'POST':
101         try:
102             create_nsd_info_requst = CreateNsdInfoRequestSerializer(data=request.data)
103             if not create_nsd_info_requst.is_valid():
104                 raise CatalogException
105             data = create(create_nsd_info_requst.data)
106             nsd_info = NsdInfoSerializer(data=data)
107             if not nsd_info.is_valid():
108                 raise CatalogException
109             return Response(data=nsd_info.data, status=status.HTTP_201_CREATED)
110         except CatalogException:
111             logger.error(traceback.format_exc())
112             return Response(data={'error': 'Creating nsd info failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
113
114     if request.method == 'GET':
115         try:
116             data = query_multiple()
117             nsd_infos = NsdInfosSerializer(data=data)
118             if not nsd_infos.is_valid():
119                 raise CatalogException
120             return Response(data=nsd_infos.data, status=status.HTTP_200_OK)
121         except CatalogException:
122             logger.error(traceback.format_exc())
123             return Response(
124                 data={'error': 'Query of multiple NS descriptor resources failed.'},
125                 status=status.HTTP_500_INTERNAL_SERVER_ERROR
126             )
127
128
129 @swagger_auto_schema(
130     method='PUT',
131     operation_description="Upload NSD content",
132     request_body=no_body,
133     responses={
134         status.HTTP_204_NO_CONTENT: {},
135         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
136     }
137 )
138 @swagger_auto_schema(
139     method='GET',
140     operation_description="Fetch NSD content",
141     request_body=no_body,
142     responses={
143         status.HTTP_204_NO_CONTENT: {},
144         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
145     }
146 )
147 @api_view(http_method_names=['PUT', 'GET'])
148 def nsd_content_ru(request, *args, **kwargs):
149     nsd_info_id = kwargs.get("nsdInfoId")
150     if request.method == 'PUT':
151         files = request.FILES.getlist('file')
152         try:
153             upload(files[0], nsd_info_id)
154             return Response(data={}, status=status.HTTP_204_NO_CONTENT)
155         except IOError:
156             logger.error(traceback.format_exc())
157             raise CatalogException
158             return Response(data={'error': 'Uploading nsd content failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
159
160     if request.method == 'GET':
161         try:
162             file_path = download(nsd_info_id)
163             file_name = file_path.split('/')[-1]
164             file_name = file_name.split('\\')[-1]
165             response = FileResponse(open(file_path, 'rb'), status=status.HTTP_200_OK)
166             response['Content-Disposition'] = 'attachment; filename=%s' % file_name.encode('utf-8')
167             return response
168         except IOError:
169             logger.error(traceback.format_exc())
170             raise CatalogException
171             return Response(data={'error': 'Downloading nsd content failed.'},
172                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)