Swagger issue fixes from the Ericsson team
[modeling/etsicatalog.git] / catalog / packages / views / vnf_package_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 swagger_auto_schema, no_body
19 from drf_yasg import openapi
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.vnf_package import VnfPackage
25 from catalog.packages.biz.vnf_package import VnfPkgUploadThread
26 from catalog.packages.biz.vnf_package import handle_upload_failed
27 from catalog.packages.biz.vnf_package import parse_vnfd_and_save
28 from catalog.packages.const import TAG_VNF_PACKAGE_API
29 from catalog.packages.serializers.create_vnf_pkg_info_req import CreateVnfPkgInfoRequestSerializer
30 from catalog.packages.serializers.upload_vnf_pkg_from_uri_req import UploadVnfPackageFromUriRequestSerializer
31 from catalog.packages.serializers.vnf_pkg_info import VnfPkgInfoSerializer
32 from catalog.packages.serializers.vnf_pkg_infos import VnfPkgInfosSerializer
33 from .common import validate_data
34 from .common import view_safe_call_with_log
35
36 logger = logging.getLogger(__name__)
37
38
39 @swagger_auto_schema(
40     method="GET",
41     operation_description="Query multiple VNF package resource",
42     tags=[TAG_VNF_PACKAGE_API],
43     request_body=no_body,
44     responses={
45         status.HTTP_200_OK: VnfPkgInfosSerializer(),
46         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
47     }
48 )
49 @swagger_auto_schema(
50     method="POST",
51     operation_description="Create an individual VNF package resource",
52     tags=[TAG_VNF_PACKAGE_API],
53     request_body=CreateVnfPkgInfoRequestSerializer,
54     responses={
55         status.HTTP_201_CREATED: VnfPkgInfoSerializer(),
56         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
57     }
58 )
59 @api_view(http_method_names=["GET", "POST"])
60 @view_safe_call_with_log(logger=logger)
61 def vnf_packages_rc(request):
62     if request.method == 'GET':
63         logger.debug("Query VNF packages> %s" % request.data)
64         data = VnfPackage().query_multiple()
65         validate_data(data, VnfPkgInfosSerializer)
66         return Response(data=data, status=status.HTTP_200_OK)
67
68     if request.method == 'POST':
69         logger.debug("Create VNF package> %s" % request.data)
70         create_vnf_pkg_info_request = validate_data(request.data,
71                                                     CreateVnfPkgInfoRequestSerializer)
72         data = VnfPackage().create_vnf_pkg(create_vnf_pkg_info_request.data)
73         validate_data(data, VnfPkgInfoSerializer)
74         return Response(data=data, status=status.HTTP_201_CREATED)
75
76
77 @swagger_auto_schema(
78     method="GET",
79     operation_description="Read VNFD of an on-boarded VNF package",
80     tags=[TAG_VNF_PACKAGE_API],
81     request_body=no_body,
82     responses={
83         status.HTTP_200_OK: openapi.Response('VNFD of an on-boarded VNF package',
84                                              schema=openapi.Schema(format=openapi.FORMAT_BINARY,
85                                                                    type=openapi.TYPE_STRING)),
86         status.HTTP_404_NOT_FOUND: "VNF package does not exist",
87         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
88     },
89     produces='application/octet-stream',
90     operation_id='VNFD of an on-boarded VNF package'
91 )
92 @api_view(http_method_names=["GET"])
93 @view_safe_call_with_log(logger=logger)
94 def vnfd_rd(request, **kwargs):
95     vnf_pkg_id = kwargs.get("vnfPkgId")
96     logger.debug("Read VNFD for  VNF package %s" % vnf_pkg_id)
97     try:
98         file_iterator = VnfPackage().download_vnfd(vnf_pkg_id)
99         return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
100     except Exception as e:
101         logger.error(e)
102         raise e
103
104
105 @swagger_auto_schema(
106     method='PUT',
107     operation_description="Upload VNF package content",
108     tags=[TAG_VNF_PACKAGE_API],
109     request_body=no_body,
110     responses={
111         status.HTTP_202_ACCEPTED: "Successfully",
112         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
113     }
114 )
115 @swagger_auto_schema(
116     method="GET",
117     operation_description="Fetch VNF package content",
118     tags=[TAG_VNF_PACKAGE_API],
119     request_body=no_body,
120     responses={
121         status.HTTP_200_OK: openapi.Response('VNF package file',
122                                              schema=openapi.Schema(format=openapi.FORMAT_BINARY,
123                                                                    type=openapi.TYPE_STRING)),
124         status.HTTP_404_NOT_FOUND: "VNF package does not exist",
125         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
126     }
127 )
128 @api_view(http_method_names=["PUT", "GET"])
129 @view_safe_call_with_log(logger=logger)
130 def package_content_ru(request, **kwargs):
131     vnf_pkg_id = kwargs.get("vnfPkgId")
132     if request.method == "PUT":
133         logger.debug("Upload VNF package %s" % vnf_pkg_id)
134         files = request.FILES.getlist('file')
135         try:
136             local_file_name = VnfPackage().upload(vnf_pkg_id, files[0])
137             parse_vnfd_and_save(vnf_pkg_id, local_file_name)
138             return Response(None, status=status.HTTP_202_ACCEPTED)
139         except Exception as e:
140             handle_upload_failed(vnf_pkg_id)
141             raise e
142
143     if request.method == "GET":
144         file_range = request.META.get('HTTP_RANGE')
145         file_iterator = VnfPackage().download(vnf_pkg_id, file_range)
146         return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
147
148
149 @swagger_auto_schema(
150     method='POST',
151     operation_description="Upload VNF package content from uri",
152     tags=[TAG_VNF_PACKAGE_API],
153     request_body=UploadVnfPackageFromUriRequestSerializer,
154     responses={
155         status.HTTP_202_ACCEPTED: "Successfully",
156         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
157     }
158 )
159 @api_view(http_method_names=['POST'])
160 @view_safe_call_with_log(logger=logger)
161 def upload_from_uri_c(request, **kwargs):
162     vnf_pkg_id = kwargs.get("vnfPkgId")
163     try:
164         upload_vnf_from_uri_request = validate_data(request.data,
165                                                     UploadVnfPackageFromUriRequestSerializer)
166         VnfPkgUploadThread(upload_vnf_from_uri_request.data, vnf_pkg_id).start()
167         return Response(None, status=status.HTTP_202_ACCEPTED)
168     except Exception as e:
169         handle_upload_failed(vnf_pkg_id)
170         raise e
171
172
173 @swagger_auto_schema(
174     method='GET',
175     operation_description="Query an individual VNF package resource",
176     tags=[TAG_VNF_PACKAGE_API],
177     request_body=no_body,
178     responses={
179         status.HTTP_200_OK: VnfPkgInfoSerializer(),
180         status.HTTP_404_NOT_FOUND: "VNF package does not exist",
181         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
182     }
183 )
184 @swagger_auto_schema(
185     method='DELETE',
186     operation_description="Delete an individual VNF package resource",
187     tags=[TAG_VNF_PACKAGE_API],
188     request_body=no_body,
189     responses={
190         status.HTTP_204_NO_CONTENT: "No content",
191         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
192     }
193 )
194 @api_view(http_method_names=['GET', 'DELETE'])
195 @view_safe_call_with_log(logger=logger)
196 def vnf_package_rd(request, **kwargs):
197     vnf_pkg_id = kwargs.get("vnfPkgId")
198     if request.method == 'GET':
199         logger.debug("Query an individual VNF package> %s" % request.data)
200         data = VnfPackage().query_single(vnf_pkg_id)
201         validate_data(data, VnfPkgInfoSerializer)
202         return Response(data=data, status=status.HTTP_200_OK)
203
204     if request.method == 'DELETE':
205         logger.debug("Delete an individual VNF package> %s" % request.data)
206         VnfPackage().delete_vnf_pkg(vnf_pkg_id)
207         return Response(data=None, status=status.HTTP_204_NO_CONTENT)