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.serializers.upload_vnf_pkg_from_uri_req import UploadVnfPackageFromUriRequestSerializer
24 from catalog.packages.serializers.create_vnf_pkg_info_req import CreateVnfPkgInfoRequestSerializer
25 from catalog.packages.serializers.vnf_pkg_info import VnfPkgInfoSerializer
26 from catalog.packages.serializers.vnf_pkg_infos import VnfPkgInfosSerializer
27 from catalog.packages.biz.vnf_package import VnfPackage
28 from catalog.packages.biz.vnf_package import VnfPkgUploadThread
29 from catalog.packages.biz.vnf_package import parse_vnfd_and_save
30 from catalog.packages.biz.vnf_package import handle_upload_failed
31 from .common import validate_data
32 from .common import view_safe_call_with_log
34 logger = logging.getLogger(__name__)
39 operation_description="Query multiple VNF package resource",
40 tags=["VNF Package API"],
43 status.HTTP_200_OK: VnfPkgInfosSerializer(),
44 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
49 operation_description="Create an individual VNF package resource",
50 tags=["VNF Package API"],
51 request_body=CreateVnfPkgInfoRequestSerializer,
53 status.HTTP_201_CREATED: VnfPkgInfoSerializer(),
54 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
57 @api_view(http_method_names=["GET", "POST"])
58 @view_safe_call_with_log(logger=logger)
59 def vnf_packages_rc(request):
60 if request.method == 'GET':
61 logger.debug("Query VNF packages> %s" % request.data)
62 data = VnfPackage().query_multiple()
63 validate_data(data, VnfPkgInfosSerializer)
64 return Response(data=data, status=status.HTTP_200_OK)
66 if request.method == 'POST':
67 logger.debug("Create VNF package> %s" % request.data)
68 create_vnf_pkg_info_request = validate_data(request.data,
69 CreateVnfPkgInfoRequestSerializer)
70 data = VnfPackage().create_vnf_pkg(create_vnf_pkg_info_request.data)
71 validate_data(data, VnfPkgInfoSerializer)
72 return Response(data=data, status=status.HTTP_201_CREATED)
77 operation_description="Read VNFD of an on-boarded VNF package",
78 tags=["VNF Package API"],
81 status.HTTP_200_OK: VnfPkgInfosSerializer(),
82 status.HTTP_404_NOT_FOUND: "VNF package does not exist",
83 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
86 @api_view(http_method_names=["GET"])
87 @view_safe_call_with_log(logger=logger)
88 def vnfd_rd(request, **kwargs):
89 vnf_pkg_id = kwargs.get("vnfPkgId")
90 logger.debug("Read VNFD for VNF package %s" % vnf_pkg_id)
92 file_iterator = VnfPackage().download_vnfd(vnf_pkg_id)
93 return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
94 except Exception as e:
101 operation_description="Upload VNF package content",
102 tags=["VNF Package API"],
103 request_body=no_body,
105 status.HTTP_202_ACCEPTED: "Successfully",
106 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
109 @swagger_auto_schema(
111 operation_description="Fetch VNF package content",
112 tags=["VNF Package API"],
113 request_body=no_body,
115 status.HTTP_200_OK: VnfPkgInfosSerializer(),
116 status.HTTP_404_NOT_FOUND: "VNF package does not exist",
117 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
120 @api_view(http_method_names=["PUT", "GET"])
121 @view_safe_call_with_log(logger=logger)
122 def package_content_ru(request, **kwargs):
123 vnf_pkg_id = kwargs.get("vnfPkgId")
124 if request.method == "PUT":
125 logger.debug("Upload VNF package %s" % vnf_pkg_id)
126 files = request.FILES.getlist('file')
128 local_file_name = VnfPackage().upload(vnf_pkg_id, files[0])
129 parse_vnfd_and_save(vnf_pkg_id, local_file_name)
130 return Response(None, status=status.HTTP_202_ACCEPTED)
131 except Exception as e:
132 handle_upload_failed(vnf_pkg_id)
135 if request.method == "GET":
136 file_range = request.META.get('HTTP_RANGE')
137 file_iterator = VnfPackage().download(vnf_pkg_id, file_range)
138 return StreamingHttpResponse(file_iterator, status=status.HTTP_200_OK)
141 @swagger_auto_schema(
143 operation_description="Upload VNF package content from uri",
144 tags=["VNF Package API"],
145 request_body=UploadVnfPackageFromUriRequestSerializer,
147 status.HTTP_202_ACCEPTED: "Successfully",
148 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
151 @api_view(http_method_names=['POST'])
152 @view_safe_call_with_log(logger=logger)
153 def upload_from_uri_c(request, **kwargs):
154 vnf_pkg_id = kwargs.get("vnfPkgId")
156 upload_vnf_from_uri_request = validate_data(request.data,
157 UploadVnfPackageFromUriRequestSerializer)
158 VnfPkgUploadThread(upload_vnf_from_uri_request.data, vnf_pkg_id).start()
159 return Response(None, status=status.HTTP_202_ACCEPTED)
160 except Exception as e:
161 handle_upload_failed(vnf_pkg_id)
165 @swagger_auto_schema(
167 operation_description="Query an individual VNF package resource",
168 tags=["VNF Package API"],
169 request_body=no_body,
171 status.HTTP_200_OK: VnfPkgInfoSerializer(),
172 status.HTTP_404_NOT_FOUND: "VNF package does not exist",
173 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
176 @swagger_auto_schema(
178 operation_description="Delete an individual VNF package resource",
179 tags=["VNF Package API"],
180 request_body=no_body,
182 status.HTTP_204_NO_CONTENT: "No content",
183 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
186 @api_view(http_method_names=['GET', 'DELETE'])
187 @view_safe_call_with_log(logger=logger)
188 def vnf_package_rd(request, **kwargs):
189 vnf_pkg_id = kwargs.get("vnfPkgId")
190 if request.method == 'GET':
191 logger.debug("Query an individual VNF package> %s" % request.data)
192 data = VnfPackage().query_single(vnf_pkg_id)
193 validate_data(data, VnfPkgInfoSerializer)
194 return Response(data=data, status=status.HTTP_200_OK)
196 if request.method == 'DELETE':
197 logger.debug("Delete an individual VNF package> %s" % request.data)
198 VnfPackage().delete_vnf_pkg(vnf_pkg_id)
199 return Response(data=None, status=status.HTTP_204_NO_CONTENT)