1 # Copyright 2018 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 from django.http import StreamingHttpResponse
18 from drf_yasg.utils import swagger_auto_schema, no_body
19 from rest_framework import status
20 from rest_framework.decorators import api_view
21 from rest_framework.response import Response
23 from catalog.packages.biz.vnf_package import VnfPackage
24 from catalog.packages.biz.vnf_package import VnfPkgUploadThread
25 from catalog.packages.biz.vnf_package import handle_upload_failed
26 from catalog.packages.biz.vnf_package import parse_vnfd_and_save
27 from catalog.packages.const import TAG_VNF_PACKAGE_API
28 from catalog.packages.serializers.create_vnf_pkg_info_req import CreateVnfPkgInfoRequestSerializer
29 from catalog.packages.serializers.upload_vnf_pkg_from_uri_req import UploadVnfPackageFromUriRequestSerializer
30 from catalog.packages.serializers.vnf_pkg_info import VnfPkgInfoSerializer
31 from catalog.packages.serializers.vnf_pkg_infos import VnfPkgInfosSerializer
32 from .common import validate_data
33 from .common import view_safe_call_with_log
35 logger = logging.getLogger(__name__)
40 operation_description="Query multiple VNF package resource",
41 tags=[TAG_VNF_PACKAGE_API],
44 status.HTTP_200_OK: VnfPkgInfosSerializer(),
45 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
50 operation_description="Create an individual VNF package resource",
51 tags=[TAG_VNF_PACKAGE_API],
52 request_body=CreateVnfPkgInfoRequestSerializer,
54 status.HTTP_201_CREATED: VnfPkgInfoSerializer(),
55 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
58 @api_view(http_method_names=["GET", "POST"])
59 @view_safe_call_with_log(logger=logger)
60 def vnf_packages_rc(request):
61 if request.method == 'GET':
62 logger.debug("Query VNF packages> %s" % request.data)
63 data = VnfPackage().query_multiple()
64 validate_data(data, VnfPkgInfosSerializer)
65 return Response(data=data, status=status.HTTP_200_OK)
67 if request.method == 'POST':
68 logger.debug("Create VNF package> %s" % request.data)
69 create_vnf_pkg_info_request = validate_data(request.data,
70 CreateVnfPkgInfoRequestSerializer)
71 data = VnfPackage().create_vnf_pkg(create_vnf_pkg_info_request.data)
72 validate_data(data, VnfPkgInfoSerializer)
73 return Response(data=data, status=status.HTTP_201_CREATED)
78 operation_description="Read VNFD of an on-boarded VNF package",
79 tags=[TAG_VNF_PACKAGE_API],
82 status.HTTP_200_OK: VnfPkgInfosSerializer(),
83 status.HTTP_404_NOT_FOUND: "VNF package does not exist",
84 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
87 @api_view(http_method_names=["GET"])
88 @view_safe_call_with_log(logger=logger)
89 def vnfd_rd(request, **kwargs):
90 vnf_pkg_id = kwargs.get("vnfPkgId")
91 logger.debug("Read VNFD for VNF package %s" % vnf_pkg_id)
93 file_iterator = VnfPackage().download_vnfd(vnf_pkg_id)
94 return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
95 except Exception as e:
100 @swagger_auto_schema(
102 operation_description="Upload VNF package content",
103 tags=[TAG_VNF_PACKAGE_API],
104 request_body=no_body,
106 status.HTTP_202_ACCEPTED: "Successfully",
107 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
110 @swagger_auto_schema(
112 operation_description="Fetch VNF package content",
113 tags=[TAG_VNF_PACKAGE_API],
114 request_body=no_body,
116 status.HTTP_200_OK: "Return csar file of VNF package",
117 status.HTTP_404_NOT_FOUND: "VNF package does not exist",
118 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
121 @api_view(http_method_names=["PUT", "GET"])
122 @view_safe_call_with_log(logger=logger)
123 def package_content_ru(request, **kwargs):
124 vnf_pkg_id = kwargs.get("vnfPkgId")
125 if request.method == "PUT":
126 logger.debug("Upload VNF package %s" % vnf_pkg_id)
127 files = request.FILES.getlist('file')
129 local_file_name = VnfPackage().upload(vnf_pkg_id, files[0])
130 parse_vnfd_and_save(vnf_pkg_id, local_file_name)
131 return Response(None, status=status.HTTP_202_ACCEPTED)
132 except Exception as e:
133 handle_upload_failed(vnf_pkg_id)
136 if request.method == "GET":
137 file_range = request.META.get('HTTP_RANGE')
138 file_iterator = VnfPackage().download(vnf_pkg_id, file_range)
139 return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
142 @swagger_auto_schema(
144 operation_description="Upload VNF package content from uri",
145 tags=[TAG_VNF_PACKAGE_API],
146 request_body=UploadVnfPackageFromUriRequestSerializer,
148 status.HTTP_202_ACCEPTED: "Successfully",
149 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
152 @api_view(http_method_names=['POST'])
153 @view_safe_call_with_log(logger=logger)
154 def upload_from_uri_c(request, **kwargs):
155 vnf_pkg_id = kwargs.get("vnfPkgId")
157 upload_vnf_from_uri_request = validate_data(request.data,
158 UploadVnfPackageFromUriRequestSerializer)
159 VnfPkgUploadThread(upload_vnf_from_uri_request.data, vnf_pkg_id).start()
160 return Response(None, status=status.HTTP_202_ACCEPTED)
161 except Exception as e:
162 handle_upload_failed(vnf_pkg_id)
166 @swagger_auto_schema(
168 operation_description="Query an individual VNF package resource",
169 tags=[TAG_VNF_PACKAGE_API],
170 request_body=no_body,
172 status.HTTP_200_OK: VnfPkgInfoSerializer(),
173 status.HTTP_404_NOT_FOUND: "VNF package does not exist",
174 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
177 @swagger_auto_schema(
179 operation_description="Delete an individual VNF package resource",
180 tags=[TAG_VNF_PACKAGE_API],
181 request_body=no_body,
183 status.HTTP_204_NO_CONTENT: "No content",
184 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
187 @api_view(http_method_names=['GET', 'DELETE'])
188 @view_safe_call_with_log(logger=logger)
189 def vnf_package_rd(request, **kwargs):
190 vnf_pkg_id = kwargs.get("vnfPkgId")
191 if request.method == 'GET':
192 logger.debug("Query an individual VNF package> %s" % request.data)
193 data = VnfPackage().query_single(vnf_pkg_id)
194 validate_data(data, VnfPkgInfoSerializer)
195 return Response(data=data, status=status.HTTP_200_OK)
197 if request.method == 'DELETE':
198 logger.debug("Delete an individual VNF package> %s" % request.data)
199 VnfPackage().delete_vnf_pkg(vnf_pkg_id)
200 return Response(data=None, status=status.HTTP_204_NO_CONTENT)