Ns descriptor related stuffs.
[vfc/nfvo/catalog.git] / catalog / packages / views / ns_descriptor_views.py
1 # Copyright 2018 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 django.http import StreamingHttpResponse
19 from drf_yasg.utils import no_body, swagger_auto_schema
20 from rest_framework import status
21 from rest_framework.decorators import api_view
22 from rest_framework.response import Response
23
24 from catalog.packages.biz.ns_descriptor import NsDescriptor
25 from catalog.packages.serializers.create_nsd_info_request import CreateNsdInfoRequestSerializer
26 from catalog.packages.serializers.nsd_info import NsdInfoSerializer
27 from catalog.packages.serializers.nsd_infos import NsdInfosSerializer
28 from catalog.packages.views.common import validate_data
29 from catalog.pub.exceptions import CatalogException, ResourceNotFoundException
30
31 logger = logging.getLogger(__name__)
32
33
34 @swagger_auto_schema(
35     method='GET',
36     operation_description="Query a NSD",
37     request_body=no_body,
38     responses={
39         status.HTTP_200_OK: NsdInfoSerializer(),
40         status.HTTP_404_NOT_FOUND: 'NSDs do not exist',
41         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
42     }
43 )
44 @swagger_auto_schema(
45     method='DELETE',
46     operation_description="Delete a NSD",
47     request_body=no_body,
48     responses={
49         status.HTTP_204_NO_CONTENT: None,
50         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
51     }
52 )
53 @api_view(http_method_names=['GET', 'DELETE'])
54 def ns_info_rd(request, **kwargs):
55     nsd_info_id = kwargs.get("nsdInfoId")
56     if request.method == 'GET':
57         try:
58             data = NsDescriptor().query_single(nsd_info_id)
59             nsd_info = validate_data(data, NsdInfoSerializer)
60             return Response(data=nsd_info.data, status=status.HTTP_200_OK)
61         except ResourceNotFoundException as e:
62             logger.error(e.message)
63             error_data = {'error': e.message}
64             error_code = status.HTTP_404_NOT_FOUND
65         except Exception as e:
66             logger.error(e.message)
67             logger.error(traceback.format_exc())
68             error_data = {'error': 'Query of NSD(%s) failed.' % nsd_info_id}
69             error_code = status.HTTP_500_INTERNAL_SERVER_ERROR
70         return Response(data=error_data, status=error_code)
71
72     if request.method == 'DELETE':
73         try:
74             NsDescriptor().delete_single(nsd_info_id)
75             return Response(status=status.HTTP_204_NO_CONTENT)
76         except Exception as e:
77             logger.error(e.message)
78             logger.error(traceback.format_exc())
79             error_data = {'error': 'Deletion of NSD(%s) failed.' % nsd_info_id}
80         return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
81
82
83 @swagger_auto_schema(
84     method='POST',
85     operation_description="Create a NSD",
86     request_body=CreateNsdInfoRequestSerializer(),
87     responses={
88         status.HTTP_201_CREATED: NsdInfoSerializer(),
89         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
90     }
91 )
92 @swagger_auto_schema(
93     method='GET',
94     operation_description="Query multiple NSDs",
95     request_body=no_body,
96     responses={
97         status.HTTP_200_OK: NsdInfosSerializer(),
98         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
99     }
100 )
101 @api_view(http_method_names=['POST', 'GET'])
102 def ns_descriptors_rc(request):
103     if request.method == 'POST':
104         try:
105             create_nsd_info_request = validate_data(request.data, CreateNsdInfoRequestSerializer)
106             data = NsDescriptor().create(create_nsd_info_request.data)
107             nsd_info = validate_data(data, NsdInfoSerializer)
108             return Response(data=nsd_info.data, status=status.HTTP_201_CREATED)
109         except Exception as e:
110             logger.error(e.message)
111             logger.error(traceback.format_exc())
112             error_data = {'error': 'Creating a NSD failed.'}
113         return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
114
115     if request.method == 'GET':
116         try:
117             data = NsDescriptor().query_multiple()
118             nsd_infos = validate_data(data, NsdInfosSerializer)
119             return Response(data=nsd_infos.data, status=status.HTTP_200_OK)
120         except Exception as e:
121             logger.error(e.message)
122             logger.error(traceback.format_exc())
123             error_data = {'error': 'Query of multiple NSDs failed.'}
124         return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
125
126
127 @swagger_auto_schema(
128     method='PUT',
129     operation_description="Upload NSD content",
130     request_body=no_body,
131     responses={
132         status.HTTP_204_NO_CONTENT: 'PNFD file',
133         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
134     }
135 )
136 @swagger_auto_schema(
137     method='GET',
138     operation_description="Download NSD content",
139     request_body=no_body,
140     responses={
141         status.HTTP_204_NO_CONTENT: None,
142         status.HTTP_404_NOT_FOUND: 'NSD does not exist.',
143         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
144     }
145 )
146 @api_view(http_method_names=['PUT', 'GET'])
147 def nsd_content_ru(request, **kwargs):
148     nsd_info_id = kwargs.get("nsdInfoId")
149     if request.method == 'PUT':
150         files = request.FILES.getlist('file')
151         try:
152             local_file_name = NsDescriptor().upload(nsd_info_id, files[0])
153             NsDescriptor().parse_nsd_and_save(nsd_info_id, local_file_name)
154             return Response(data=None, status=status.HTTP_204_NO_CONTENT)
155         except CatalogException as e:
156             NsDescriptor().handle_upload_failed(nsd_info_id)
157             logger.error(e.message)
158             error_data = {'error': e.message}
159         except Exception as e:
160             NsDescriptor().handle_upload_failed(nsd_info_id)
161             logger.error(e.message)
162             logger.error(traceback.format_exc())
163             error_data = {'error': 'Uploading NSD(%s) failed.'}
164         return Response(data=error_data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
165
166     if request.method == 'GET':
167         try:
168             file_range = request.META.get('RANGE')
169             file_iterator = NsDescriptor().download(nsd_info_id, file_range)
170             return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
171         except ResourceNotFoundException as e:
172             logger.error(e.message)
173             error_data = {'error': e.message}
174             error_code = status.HTTP_404_NOT_FOUND
175         except CatalogException as e:
176             logger.error(e.message)
177             error_data = {'error': e.message}
178             error_code = status.HTTP_500_INTERNAL_SERVER_ERROR
179         except Exception as e:
180             logger.error(e.message)
181             logger.error(traceback.format_exc())
182             error_data = {'error': 'Downloading NSD(%s) failed.' % nsd_info_id}
183             error_code = status.HTTP_500_INTERNAL_SERVER_ERROR
184         return Response(data=error_data, status=error_code)