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.
19 from catalog.pub.config.config import CATALOG_ROOT_PATH
20 from drf_yasg.utils import swagger_auto_schema, no_body
21 from rest_framework import status
22 from rest_framework.decorators import api_view
23 from rest_framework.response import Response
24 from catalog.pub.exceptions import CatalogException, VnfPkgNotFoundException
25 from catalog.packages.serializers.upload_vnf_pkg_from_uri_req import UploadVnfPackageFromUriRequestSerializer
26 from catalog.packages.serializers.create_vnf_pkg_info_req import CreateVnfPkgInfoRequestSerializer
27 from catalog.packages.serializers.vnf_pkg_info import VnfPkgInfoSerializer
28 from catalog.packages.serializers.vnf_pkg_infos import VnfPkgInfosSerializer
29 from catalog.packages.biz.vnf_package import create_vnf_pkg, query_multiple, VnfPkgUploadThread, \
30 query_single, delete_vnf_pkg, parse_vnfd_and_save, fetch_vnf_pkg, handle_upload_failed
31 from catalog.pub.database.models import VnfPackageModel
32 from catalog.packages.views.ns_descriptor_views import validate_data
34 logger = logging.getLogger(__name__)
39 operation_description="Query multiple VNF package resource",
42 status.HTTP_200_OK: VnfPkgInfosSerializer(),
43 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
48 operation_description="Create an individual VNF package resource",
49 request_body=CreateVnfPkgInfoRequestSerializer,
51 status.HTTP_201_CREATED: VnfPkgInfoSerializer(),
52 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
55 @api_view(http_method_names=["GET", "POST"])
56 def vnf_packages_rc(request):
57 if request.method == 'GET':
58 logger.debug("Query VNF packages> %s" % request.data)
60 res = query_multiple()
61 query_serializer = validate_data(res, VnfPkgInfosSerializer)
62 return Response(data=query_serializer.data, status=status.HTTP_200_OK)
63 except CatalogException as e:
64 logger.error(e.message)
65 error_msg = {'error': 'Query VNF package failed.'}
66 except Exception as e:
67 logger.error(e.message)
68 logger.error(traceback.format_exc())
69 error_msg = {'error': 'unexpected exception'}
70 return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
72 if request.method == 'POST':
73 logger.debug("Create VNF package> %s" % request.data)
75 req_serializer = validate_data(request.data, CreateVnfPkgInfoRequestSerializer)
76 res = create_vnf_pkg(req_serializer.data)
77 create_vnf_pkg_resp_serializer = validate_data(res, VnfPkgInfoSerializer)
78 return Response(data=create_vnf_pkg_resp_serializer.data, status=status.HTTP_201_CREATED)
79 except CatalogException as e:
80 logger.error(e.message)
81 error_msg = {'error': 'Create VNF package failed.'}
82 except Exception as e:
83 logger.error(e.message)
84 logger.error(traceback.format_exc())
85 error_msg = {'error': 'unexpected exception'}
86 return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
91 operation_description="Upload VNF package content",
94 status.HTTP_202_ACCEPTED: "Successfully",
95 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
100 operation_description="Fetch VNF package content",
101 request_body=no_body,
103 status.HTTP_200_OK: VnfPkgInfosSerializer(),
104 status.HTTP_404_NOT_FOUND: "VNF package does not exist",
105 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
108 @api_view(http_method_names=["PUT", "GET"])
109 def upload_vnf_pkg_content(request, vnfPkgId):
110 if request.method == "PUT":
111 logger.debug("Upload VNF package %s" % vnfPkgId)
113 vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId)
114 if vnf_pkg[0].onboardingState != "CREATED":
115 raise CatalogException("VNF package (%s) is not created" % vnfPkgId)
116 file_object = request.FILES.get('file')
117 upload_path = os.path.join(CATALOG_ROOT_PATH, vnfPkgId)
118 if not os.path.exists(upload_path):
119 os.makedirs(upload_path, 0o777)
121 upload_file_name = os.path.join(upload_path, file_object.name)
122 with open(upload_file_name, 'wb+') as dest_file:
123 for chunk in file_object.chunks():
124 dest_file.write(chunk)
126 parse_vnfd_and_save(vnfPkgId, upload_file_name)
127 return Response(None, status=status.HTTP_202_ACCEPTED)
128 except CatalogException as e:
129 handle_upload_failed(vnfPkgId)
130 logger.debug(e.message)
131 error_msg = {'error': 'Upload VNF package failed.'}
132 except Exception as e:
133 handle_upload_failed(vnfPkgId)
134 logger.error(e.message)
135 logger.error(traceback.format_exc())
136 error_msg = {'error': 'unexpected exception'}
137 return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
139 if request.method == "GET":
141 response = fetch_vnf_pkg(request, vnfPkgId)
143 except VnfPkgNotFoundException as e:
144 logger.error(e.message)
145 return Response(data={'error': "VNF package does not exist"}, status=status.HTTP_404_NOT_FOUND)
146 except CatalogException as e:
147 logger.error(e.message)
148 error_msg = {'error': 'Fetch VNF package failed.'}
149 except Exception as e:
150 logger.error(e.message)
151 logger.error(traceback.format_exc())
152 error_msg = {'error': 'unexpected exception'}
153 return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
156 @swagger_auto_schema(
158 operation_description="Upload VNF package content from uri",
159 request_body=UploadVnfPackageFromUriRequestSerializer,
161 status.HTTP_202_ACCEPTED: "Successfully",
162 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
165 @api_view(http_method_names=['POST'])
166 def upload_vnf_pkg_from_uri(request, vnfPkgId):
168 req_serializer = validate_data(request.data, UploadVnfPackageFromUriRequestSerializer)
169 VnfPkgUploadThread(req_serializer.data, vnfPkgId).start()
170 return Response(None, status=status.HTTP_202_ACCEPTED)
171 except CatalogException as e:
172 handle_upload_failed(vnfPkgId)
173 logger.debug(e.message)
174 error_msg = {'error': 'Upload VNF package failed.'}
175 except Exception as e:
176 handle_upload_failed(vnfPkgId)
177 logger.error(e.message)
178 logger.error(traceback.format_exc())
179 error_msg = {'error': 'unexpected exception'}
180 return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
183 @swagger_auto_schema(
185 operation_description="Query an individual VNF package resource",
186 request_body=no_body,
188 status.HTTP_200_OK: VnfPkgInfoSerializer(),
189 status.HTTP_404_NOT_FOUND: "VNF package does not exist",
190 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
193 @swagger_auto_schema(
195 operation_description="Delete an individual VNF package resource",
196 request_body=no_body,
198 status.HTTP_204_NO_CONTENT: None,
199 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
202 @api_view(http_method_names=['GET', 'DELETE'])
203 def vnf_package_rd(request, vnfPkgId):
204 if request.method == 'GET':
205 logger.debug("Query an individual VNF package> %s" % request.data)
207 res = query_single(vnfPkgId)
208 query_serializer = validate_data(res, VnfPkgInfoSerializer)
209 return Response(data=query_serializer.data, status=status.HTTP_200_OK)
210 except VnfPkgNotFoundException as e:
211 logger.error(e.message)
212 return Response(data={'error': "VNF package does not exist"}, status=status.HTTP_404_NOT_FOUND)
213 except CatalogException as e:
214 logger.error(e.message)
215 error_msg = {'error': 'Query an individual VNF package failed.'}
216 except Exception as e:
217 logger.error(e.message)
218 logger.error(traceback.format_exc())
219 error_msg = {'error': 'unexpected exception'}
220 return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
222 if request.method == 'DELETE':
223 logger.debug("Delete an individual VNF package> %s" % request.data)
225 delete_vnf_pkg(vnfPkgId)
226 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
227 except CatalogException as e:
228 logger.error(e.message)
229 error_msg = {'error': 'Delete an individual VNF package failed.'}
230 except Exception as e:
231 logger.error(e.message)
232 logger.error(traceback.format_exc())
233 error_msg = {'error': 'unexpected exception'}
234 return Response(data=error_msg, status=status.HTTP_500_INTERNAL_SERVER_ERROR)