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