1 # Copyright 2017 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 catalog.pub.utils.syscomm import fun_name
18 from rest_framework.response import Response
19 from rest_framework import status
20 from rest_framework.decorators import api_view
21 from catalog.pub.utils.values import ignore_case_get
22 from catalog.packages import nf_package
23 from catalog.packages import ns_package
25 from drf_yasg import openapi
26 from drf_yasg.utils import no_body, swagger_auto_schema
28 from catalog.serializers import NsPackagesSerializer
29 from catalog.serializers import NfPackageSerializer
30 from catalog.serializers import NfPackageDistributeRequestSerializer
31 from catalog.serializers import PostJobResponseResultSerializer
34 logger = logging.getLogger(__name__)
39 operation_description="On distribute NS package",
42 status.HTTP_202_ACCEPTED: openapi.Response(
45 type=openapi.TYPE_STRING,
46 pattern='CSAR(\w+) distributed successfully.')),
47 status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response(
50 type=openapi.TYPE_STRING))})
53 operation_description="Query NS packages",
56 status.HTTP_200_OK: NsPackagesSerializer,
57 status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response(
60 type=openapi.TYPE_STRING,
62 @api_view(http_method_names=['POST', 'GET'])
63 def nspackages_rc(request, *args, **kwargs):
64 logger.debug("Enter %s, method is %s", fun_name(), request.method)
65 ret, normal_status, validation_response = None, None, None
67 if request.method == 'GET':
68 # Gets ns package list
69 ret = ns_package.ns_get_csars()
70 normal_status = status.HTTP_200_OK
71 responseSerializer = NsPackagesSerializer(data=ret[1])
73 if not responseSerializer.is_valid():
74 validation_response = handleValidatonError(
75 responseSerializer, False)
76 elif request.method == 'POST':
77 # Distributes the package accroding to the given csarId
78 csar_id = ignore_case_get(request.data, "csarId")
79 logger.debug("csar_id is %s", csar_id)
80 ret = ns_package.ns_on_distribute(csar_id)
81 normal_status = status.HTTP_202_ACCEPTED
83 if validation_response:
84 return validation_response
86 logger.debug("Leave %s, Return value is %s", fun_name(), ret)
91 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
92 return Response(data=ret[1], status=normal_status)
97 operation_description="On distribute Nf package",
98 request_body=NfPackageDistributeRequestSerializer(),
100 status.HTTP_202_ACCEPTED: PostJobResponseResultSerializer,
101 status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response(
104 type=openapi.TYPE_STRING))})
105 @swagger_auto_schema(
107 operation_description="Query Nf packages",
108 request_body=no_body,
110 status.HTTP_200_OK: NfPackageSerializer(
112 status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response(
115 type=openapi.TYPE_STRING))})
116 @api_view(http_method_names=['POST', 'GET'])
117 def nfpackages_rc(request, *args, **kwargs):
119 "Enter %s%s, method is %s",
123 ret, normal_status = None, None
124 if request.method == 'GET':
125 ret = nf_package.nf_get_csars()
126 normal_status = status.HTTP_200_OK
127 elif request.method == 'POST':
128 csar_id = ignore_case_get(request.data, "csarId")
129 vim_ids = ignore_case_get(request.data, "vimIds")
130 lab_vim_id = ignore_case_get(request.data, "labVimId")
131 job_id = str(uuid.uuid4())
132 nf_package.NfDistributeThread(
133 csar_id, vim_ids, lab_vim_id, job_id).start()
134 ret = [0, {"jobId": job_id}]
135 normal_status = status.HTTP_202_ACCEPTED
136 logger.debug("Leave %s, Return value is %s", fun_name(), ret)
141 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
142 return Response(data=ret[1], status=normal_status)
145 @api_view(http_method_names=['DELETE', 'GET'])
146 def ns_rd_csar(request, *args, **kwargs):
147 csar_id = ignore_case_get(kwargs, "csarId")
148 logger.info("Enter %s, method is %s, csar_id is %s",
149 fun_name(), request.method, csar_id)
150 ret, normal_status = None, None
151 if request.method == 'GET':
152 ret = ns_package.ns_get_csar(csar_id)
153 normal_status = status.HTTP_200_OK
154 elif request.method == 'DELETE':
155 ret = ns_package.ns_delete_csar(csar_id)
156 normal_status = status.HTTP_202_ACCEPTED
157 logger.info("Leave %s, Return value is %s", fun_name(), ret)
162 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
163 return Response(data=ret[1], status=normal_status)
166 @api_view(http_method_names=['DELETE', 'GET'])
167 def nf_rd_csar(request, *args, **kwargs):
168 csar_id = ignore_case_get(kwargs, "csarId")
169 logger.info("Enter %s, method is %s, csar_id is %s",
170 fun_name(), request.method, csar_id)
171 ret, normal_status = None, None
172 if request.method == 'GET':
173 ret = nf_package.nf_get_csar(csar_id)
174 normal_status = status.HTTP_200_OK
175 elif request.method == 'DELETE':
176 job_id = str(uuid.uuid4())
177 nf_package.NfPkgDeleteThread(csar_id, job_id).start()
178 ret = [0, {"jobId": job_id}]
179 normal_status = status.HTTP_202_ACCEPTED
180 logger.info("Leave %s, Return value is %s", fun_name(), ret)
185 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
186 return Response(data=ret[1], status=normal_status)
189 @api_view(http_method_names=['POST'])
190 def ns_model_parser(request, *args, **kwargs):
191 csar_id = ignore_case_get(request.data, "csarId")
192 inputs = ignore_case_get(request.data, "inputs")
194 "Enter %s, csar_id=%s, inputs=%s",
198 ret = ns_package.parse_nsd(csar_id, inputs)
199 logger.info("Leave %s, Return value is %s", fun_name(), ret)
204 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
205 return Response(data=ret[1], status=status.HTTP_202_ACCEPTED)
208 @api_view(http_method_names=['POST'])
209 def vnf_model_parser(request, *args, **kwargs):
210 csar_id = ignore_case_get(request.data, "csarId")
211 inputs = ignore_case_get(request.data, "inputs")
213 "Enter %s, csar_id=%s, inputs=%s",
217 ret = nf_package.parse_vnfd(csar_id, inputs)
218 logger.info("Leave %s, Return value is %s", fun_name(), ret)
223 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
224 return Response(data=ret[1], status=status.HTTP_202_ACCEPTED)
227 def handleValidatonError(base_serializer, is_request):
228 errormessage = base_serializer.errors
229 logger.error(errormessage)
232 message = 'Invalid request'
234 message = 'Invalid response'
235 logger.error(message)
237 return Response(data={'error': errormessage},
238 status=status.HTTP_500_INTERNAL_SERVER_ERROR)