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
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
31 from catalog.pub.database.models import VnfPackageModel
33 logger = logging.getLogger(__name__)
38 operation_description="Query multiple VNF package resource",
41 status.HTTP_200_OK: VnfPkgInfosSerializer(),
42 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
47 operation_description="Create an individual VNF package resource",
48 request_body=CreateVnfPkgInfoRequestSerializer,
50 status.HTTP_201_CREATED: VnfPkgInfoSerializer(),
51 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
54 @api_view(http_method_names=["GET", "POST"])
55 def vnf_packages_rc(request):
56 if request.method == 'GET':
57 logger.debug("Query VNF packages> %s" % request.data)
59 res = query_multiple()
60 query_serializer = VnfPkgInfosSerializer(data=res)
61 if not query_serializer.is_valid():
62 raise CatalogException
63 return Response(data=query_serializer.data, status=status.HTTP_200_OK)
64 except CatalogException:
65 logger.error(traceback.format_exc())
66 return Response(data={'error': 'Query VNF package failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
67 except Exception as e:
68 logger.error(e.message)
69 logger.error(traceback.format_exc())
70 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
72 if request.method == 'POST':
73 logger.debug("Create VNF package> %s" % request.data)
75 req_serializer = CreateVnfPkgInfoRequestSerializer(data=request.data)
76 if not req_serializer.is_valid():
77 raise CatalogException
78 res = create_vnf_pkg(req_serializer.data)
79 create_vnf_pkg_resp_serializer = VnfPkgInfoSerializer(data=res)
80 if not create_vnf_pkg_resp_serializer.is_valid():
81 raise CatalogException
82 return Response(data=create_vnf_pkg_resp_serializer.data, status=status.HTTP_201_CREATED)
83 except CatalogException:
84 logger.error(traceback.format_exc())
85 return Response(data={'error': 'Create VNF package failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
86 except Exception as e:
87 logger.error(e.message)
88 logger.error(traceback.format_exc())
89 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
94 operation_description="Upload VNF package content",
97 status.HTTP_202_ACCEPTED: "Successfully",
98 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
101 @api_view(http_method_names=['PUT'])
102 def upload_vnf_pkg_content(request, vnfPkgId):
103 logger.debug("Upload VNF package %s" % vnfPkgId)
105 vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId)
106 if vnf_pkg[0].onboardingState != "CREATED":
107 raise CatalogException("VNF package (%s) is not created" % vnfPkgId)
108 file_object = request.FILES.get('file')
109 upload_path = os.path.join(CATALOG_ROOT_PATH, vnfPkgId)
110 if not os.path.exists(upload_path):
111 os.makedirs(upload_path, 0o777)
113 upload_file_name = os.path.join(upload_path, file_object.name)
114 with open(upload_file_name, 'wb+') as dest_file:
115 for chunk in file_object.chunks():
116 dest_file.write(chunk)
118 parse_vnfd_and_save(vnfPkgId, upload_file_name)
119 return Response(None, status=status.HTTP_202_ACCEPTED)
120 except CatalogException:
121 logger.error(traceback.format_exc())
122 return Response(data={'error': 'Upload VNF package failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
123 except Exception as e:
124 logger.error(e.message)
125 logger.error(traceback.format_exc())
126 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
129 @swagger_auto_schema(
131 operation_description="Upload VNF package content from uri",
132 request_body=UploadVnfPackageFromUriRequestSerializer,
134 status.HTTP_202_ACCEPTED: "Successfully",
135 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
138 @api_view(http_method_names=['POST'])
139 def upload_vnf_pkg_from_uri(request, vnfPkgId):
141 req_serializer = UploadVnfPackageFromUriRequestSerializer(data=request.data)
142 if not req_serializer.is_valid():
143 raise CatalogException
144 VnfPkgUploadThread(req_serializer.data, vnfPkgId).start()
145 return Response(None, status=status.HTTP_202_ACCEPTED)
146 except CatalogException:
147 logger.error(traceback.format_exc())
148 return Response(data={'error': 'Upload VNF package failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
149 except Exception as e:
150 logger.error(e.message)
151 logger.error(traceback.format_exc())
152 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
155 @swagger_auto_schema(
157 operation_description="Query an individual VNF package resource",
158 request_body=no_body,
160 status.HTTP_200_OK: VnfPkgInfoSerializer(),
161 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
164 @swagger_auto_schema(
166 operation_description="Delete an individual VNF package resource",
167 request_body=no_body,
169 status.HTTP_204_NO_CONTENT: None,
170 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
173 @api_view(http_method_names=['GET', 'DELETE'])
174 def vnf_package_rd(request, vnfPkgId):
175 if request.method == 'GET':
176 logger.debug("Query an individual VNF package> %s" % request.data)
178 res = query_single(vnfPkgId)
179 query_serializer = VnfPkgInfoSerializer(data=res)
180 if not query_serializer.is_valid():
181 raise CatalogException
182 return Response(data=query_serializer.data, status=status.HTTP_200_OK)
183 except CatalogException:
184 logger.error(traceback.format_exc())
185 return Response(data={'error': 'Query an individual VNF package failed.'},
186 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
187 except Exception as e:
188 logger.error(e.message)
189 logger.error(traceback.format_exc())
190 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
192 if request.method == 'DELETE':
193 logger.debug("Delete an individual VNF package> %s" % request.data)
195 delete_vnf_pkg(vnfPkgId)
196 return Response(data=None, status=status.HTTP_204_NO_CONTENT)
197 except CatalogException:
198 logger.error(traceback.format_exc())
199 return Response(data={'error': 'Delete an individual VNF package failed.'},
200 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
201 except Exception as e:
202 logger.error(e.message)
203 logger.error(traceback.format_exc())
204 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)