1 # Copyright 2017 ZTE Corporation.
\r
3 # Licensed under the Apache License, Version 2.0 (the "License");
\r
4 # you may not use this file except in compliance with the License.
\r
5 # You may obtain a copy of the License at
\r
7 # http://www.apache.org/licenses/LICENSE-2.0
\r
9 # Unless required by applicable law or agreed to in writing, software
\r
10 # distributed under the License is distributed on an "AS IS" BASIS,
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
12 # See the License for the specific language governing permissions and
\r
13 # limitations under the License.
\r
18 from catalog.pub.config.config import CATALOG_ROOT_PATH
\r
19 from drf_yasg.utils import swagger_auto_schema
\r
20 from rest_framework import status
\r
21 from rest_framework.views import APIView
\r
22 from rest_framework.response import Response
\r
23 from catalog.packages.biz.nf_package import VnfpkgUploadThread
\r
24 from catalog.pub.exceptions import CatalogException
\r
25 from catalog.packages.serializers.upload_vnf_pkg_from_uri_req import UploadVnfPackageFromUriRequestSerializer
\r
27 logger = logging.getLogger(__name__)
\r
30 class vnf_packages(APIView):
\r
31 @swagger_auto_schema(
\r
33 # status.HTTP_200_OK: Serializer(),
\r
34 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
37 def get(self, request):
\r
41 @swagger_auto_schema(
\r
42 # request_body=CreateVnfReqSerializer(),
\r
44 # status.HTTP_201_CREATED: CreateVnfRespSerializer(),
\r
45 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
48 def post(self, request):
\r
53 class vnf_package(APIView):
\r
54 @swagger_auto_schema(
\r
56 # status.HTTP_200_OK: Serializer(),
\r
57 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
60 def get(self, request):
\r
64 @swagger_auto_schema(
\r
65 # request_body=CreateVnfReqSerializer(),
\r
67 # status.HTTP_201_CREATED: CreateVnfRespSerializer(),
\r
68 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
71 def post(self, request):
\r
76 class vnfd(APIView):
\r
77 @swagger_auto_schema(
\r
79 # status.HTTP_200_OK: Serializer(),
\r
80 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
83 def get(self, request):
\r
87 @swagger_auto_schema(
\r
88 # request_body=CreateVnfReqSerializer(),
\r
90 # status.HTTP_201_CREATED: CreateVnfRespSerializer(),
\r
91 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
94 def post(self, request):
\r
99 class package_content(APIView):
\r
100 @swagger_auto_schema(
\r
102 # status.HTTP_200_OK: Serializer(),
\r
103 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
106 def get(self, request):
\r
110 @swagger_auto_schema(
\r
111 # request_body=CreateVnfReqSerializer(),
\r
113 # status.HTTP_201_CREATED: CreateVnfRespSerializer(),
\r
114 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
117 def post(self, request):
\r
121 def put(self, request, vnfPkgId):
\r
122 logger.debug("UploadVnf %s" % vnfPkgId)
\r
123 file_object = request.FILES.get('file')
\r
124 upload_path = os.path.join(CATALOG_ROOT_PATH, vnfPkgId)
\r
125 if not os.path.exists(upload_path):
\r
126 os.makedirs(upload_path, 0o777)
\r
128 upload_file_name = os.path.join(upload_path, file_object.name)
\r
129 with open(upload_file_name, 'wb+') as dest_file:
\r
130 for chunk in file_object.chunks():
\r
131 dest_file.write(chunk)
\r
132 except Exception as e:
\r
133 logger.error("File upload exception.[%s:%s]" % (type(e), str(e)))
\r
134 logger.error("%s", traceback.format_exc())
\r
135 return Response(None, status.HTTP_202_ACCEPTED)
\r
138 class upload_from_uri(APIView):
\r
139 @swagger_auto_schema(
\r
141 # status.HTTP_200_OK: Serializer(),
\r
142 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
145 def get(self, request):
\r
149 @swagger_auto_schema(
\r
150 request_body=UploadVnfPackageFromUriRequestSerializer(),
\r
152 status.HTTP_202_ACCEPTED: "Successfully",
\r
153 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
156 def post(self, request, vnfPkgId):
\r
158 req_serializer = UploadVnfPackageFromUriRequestSerializer(data=request.data)
\r
159 if not req_serializer.is_valid():
\r
160 raise CatalogException
\r
161 VnfpkgUploadThread(req_serializer.data, vnfPkgId).start()
\r
162 return Response(None, status=status.HTTP_202_ACCEPTED)
\r
163 except CatalogException:
\r
164 logger.error(traceback.format_exc())
\r
165 return Response(data={'error': 'Upload vnfPkg failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
\r
166 except Exception as e:
\r
167 logger.error(e.message)
\r
168 logger.error(traceback.format_exc())
\r
169 return Response(data={'error': 'unexpected exception'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
\r
172 class artifacts(APIView):
\r
173 @swagger_auto_schema(
\r
175 # status.HTTP_200_OK: Serializer(),
\r
176 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
179 def get(self, request):
\r
183 @swagger_auto_schema(
\r
184 # request_body=CreateVnfReqSerializer(),
\r
186 # status.HTTP_201_CREATED: CreateVnfRespSerializer(),
\r
187 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
190 def post(self, request):
\r
195 class vnfpkg_subscriptions(APIView):
\r
196 @swagger_auto_schema(
\r
198 # status.HTTP_200_OK: Serializer(),
\r
199 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
202 def get(self, request):
\r
206 @swagger_auto_schema(
\r
207 # request_body=CreateVnfReqSerializer(),
\r
209 # status.HTTP_201_CREATED: CreateVnfRespSerializer(),
\r
210 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
213 def post(self, request):
\r
218 class vnfpkg_subscription(APIView):
\r
219 @swagger_auto_schema(
\r
221 # status.HTTP_200_OK: Serializer(),
\r
222 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
225 def get(self, request):
\r
229 @swagger_auto_schema(
\r
230 # request_body=CreateVnfReqSerializer(),
\r
232 # status.HTTP_201_CREATED: CreateVnfRespSerializer(),
\r
233 status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
\r
236 def post(self, request):
\r