Add log and comment
[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         # Read information about an individual NS descriptor resource.
62         data = NsDescriptor().query_single(nsd_info_id)
63         nsd_info = validate_data(data, NsdInfoSerializer)
64         return Response(data=nsd_info.data, status=status.HTTP_200_OK)
65     if request.method == 'DELETE':
66         # Delete an individual NS descriptor resource.
67         NsDescriptor().delete_single(nsd_info_id)
68         return Response(status=status.HTTP_204_NO_CONTENT)
69
70
71 @swagger_auto_schema(
72     method='POST',
73     operation_description="Create a NSD",
74     tags=[TAG_NSD_API],
75     request_body=CreateNsdInfoRequestSerializer(),
76     responses={
77         status.HTTP_201_CREATED: NsdInfoSerializer(),
78         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
79     }
80 )
81 @swagger_auto_schema(
82     method='GET',
83     operation_description="Query multiple NSDs",
84     tags=[TAG_NSD_API],
85     request_body=no_body,
86     responses={
87         status.HTTP_200_OK: NsdInfosSerializer(),
88         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
89     }
90 )
91 @api_view(http_method_names=['POST', 'GET'])
92 @view_safe_call_with_log(logger=logger)
93 def ns_descriptors_rc(request):
94     if request.method == 'POST':
95         # Create a new NS descriptor resource.
96         create_nsd_info_request = validate_data(request.data, CreateNsdInfoRequestSerializer)
97         data = NsDescriptor().create(create_nsd_info_request.data)
98         validate_data(data, NsdInfoSerializer)
99         return Response(data=data, status=status.HTTP_201_CREATED)
100
101     if request.method == 'GET':
102         # Query information about multiple NS descriptor resources.
103         nsdId = request.query_params.get("nsdId", None)
104         data = NsDescriptor().query_multiple(nsdId)
105         validate_data(data, NsdInfosSerializer)
106         return Response(data=data, status=status.HTTP_200_OK)
107
108
109 @swagger_auto_schema(
110     method='PUT',
111     operation_description="Upload NSD content",
112     tags=[TAG_NSD_API],
113     request_body=no_body,
114     responses={
115         status.HTTP_204_NO_CONTENT: 'PNFD file',
116         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
117     }
118 )
119 @swagger_auto_schema(
120     method='GET',
121     operation_description="Download NSD content",
122     tags=[TAG_NSD_API],
123     request_body=no_body,
124     responses={
125         status.HTTP_204_NO_CONTENT: "No content",
126         status.HTTP_404_NOT_FOUND: 'NSD does not exist.',
127         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
128     }
129 )
130 @api_view(http_method_names=['PUT', 'GET'])
131 @view_safe_call_with_log(logger=logger)
132 def nsd_content_ru(request, **kwargs):
133     nsd_info_id = kwargs.get("nsdInfoId")
134     if request.method == 'PUT':
135         # Upload the content of a NSD.
136         files = request.FILES.getlist('file')
137         try:
138             local_file_name = NsDescriptor().upload(nsd_info_id, files[0])
139             NsDescriptor().parse_nsd_and_save(nsd_info_id, local_file_name)
140             return Response(data=None, status=status.HTTP_204_NO_CONTENT)
141         except CatalogException as e:
142             NsDescriptor().handle_upload_failed(nsd_info_id)
143             raise e
144         except Exception as e:
145             NsDescriptor().handle_upload_failed(nsd_info_id)
146             raise e
147
148     if request.method == 'GET':
149         # Fetch the content of a NSD.
150         file_range = request.META.get('HTTP_RANGE')
151         file_iterator = NsDescriptor().download(nsd_info_id, file_range)
152         return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
153
154
155 @swagger_auto_schema(
156     method='PUT',
157     operation_description="Update a NSD",
158     request_body=no_body,
159     responses={
160         status.HTTP_202_ACCEPTED: "Successfully",
161         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
162     }
163 )
164 @api_view(http_method_names=['PUT'])
165 @view_safe_call_with_log(logger=logger)
166 def ns_descriptors_u(request, **kwargs):
167     if request.method == 'PUT':
168         nsd_info_id = kwargs.get("nsdInfoId")
169         NsDescriptor().update(request.data, nsd_info_id)
170         return Response(data=None, status=status.HTTP_202_ACCEPTED)