update swagger info, add tags for swagger_auto_schema
[modeling/etsicatalog.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
17 from django.http import StreamingHttpResponse
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 NsDescriptor
24 from catalog.packages.serializers.create_nsd_info_request import CreateNsdInfoRequestSerializer
25 from catalog.packages.serializers.nsd_info import NsdInfoSerializer
26 from catalog.packages.serializers.nsd_infos import NsdInfosSerializer
27 from catalog.packages.views.common import validate_data
28 from catalog.pub.exceptions import CatalogException
29 from .common import view_safe_call_with_log
30
31 logger = logging.getLogger(__name__)
32
33
34 @swagger_auto_schema(
35     method='GET',
36     operation_description="Query a NSD",
37     tags=["NSD API"],
38     request_body=no_body,
39     responses={
40         status.HTTP_200_OK: NsdInfoSerializer(),
41         status.HTTP_404_NOT_FOUND: 'NSDs do not exist',
42         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
43     }
44 )
45 @swagger_auto_schema(
46     method='DELETE',
47     operation_description="Delete a NSD",
48     tags=["NSD API"],
49     request_body=no_body,
50     responses={
51         status.HTTP_204_NO_CONTENT: "No content",
52         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
53     }
54 )
55 @api_view(http_method_names=['GET', 'DELETE'])
56 @view_safe_call_with_log(logger=logger)
57 def ns_info_rd(request, **kwargs):
58     nsd_info_id = kwargs.get("nsdInfoId")
59     if request.method == 'GET':
60         data = NsDescriptor().query_single(nsd_info_id)
61         nsd_info = validate_data(data, NsdInfoSerializer)
62         return Response(data=nsd_info.data, status=status.HTTP_200_OK)
63     if request.method == 'DELETE':
64         NsDescriptor().delete_single(nsd_info_id)
65         return Response(status=status.HTTP_204_NO_CONTENT)
66
67
68 @swagger_auto_schema(
69     method='POST',
70     operation_description="Create a NSD",
71     tags=["NSD API"],
72     request_body=CreateNsdInfoRequestSerializer(),
73     responses={
74         status.HTTP_201_CREATED: NsdInfoSerializer(),
75         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
76     }
77 )
78 @swagger_auto_schema(
79     method='GET',
80     operation_description="Query multiple NSDs",
81     tags=["NSD API"],
82     request_body=no_body,
83     responses={
84         status.HTTP_200_OK: NsdInfosSerializer(),
85         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
86     }
87 )
88 @api_view(http_method_names=['POST', 'GET'])
89 @view_safe_call_with_log(logger=logger)
90 def ns_descriptors_rc(request):
91     if request.method == 'POST':
92         create_nsd_info_request = validate_data(request.data, CreateNsdInfoRequestSerializer)
93         data = NsDescriptor().create(create_nsd_info_request.data)
94         validate_data(data, NsdInfoSerializer)
95         return Response(data=data, status=status.HTTP_201_CREATED)
96
97     if request.method == 'GET':
98         nsdId = request.query_params.get("nsdId", None)
99         data = NsDescriptor().query_multiple(nsdId)
100         validate_data(data, NsdInfosSerializer)
101         return Response(data=data, status=status.HTTP_200_OK)
102
103
104 @swagger_auto_schema(
105     method='PUT',
106     operation_description="Upload NSD content",
107     tags=["NSD API"],
108     request_body=no_body,
109     responses={
110         status.HTTP_204_NO_CONTENT: 'PNFD file',
111         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
112     }
113 )
114 @swagger_auto_schema(
115     method='GET',
116     operation_description="Download NSD content",
117     tags=["NSD API"],
118     request_body=no_body,
119     responses={
120         status.HTTP_204_NO_CONTENT: "No content",
121         status.HTTP_404_NOT_FOUND: 'NSD does not exist.',
122         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
123     }
124 )
125 @api_view(http_method_names=['PUT', 'GET'])
126 @view_safe_call_with_log(logger=logger)
127 def nsd_content_ru(request, **kwargs):
128     nsd_info_id = kwargs.get("nsdInfoId")
129     if request.method == 'PUT':
130         files = request.FILES.getlist('file')
131         try:
132             local_file_name = NsDescriptor().upload(nsd_info_id, files[0])
133             NsDescriptor().parse_nsd_and_save(nsd_info_id, local_file_name)
134             return Response(data=None, status=status.HTTP_204_NO_CONTENT)
135         except CatalogException as e:
136             NsDescriptor().handle_upload_failed(nsd_info_id)
137             raise e
138         except Exception as e:
139             NsDescriptor().handle_upload_failed(nsd_info_id)
140             raise e
141
142     if request.method == 'GET':
143         file_range = request.META.get('HTTP_RANGE')
144         file_iterator = NsDescriptor().download(nsd_info_id, file_range)
145         return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)