1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
# Copyright 2017 ZTE Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import uuid
from drf_yasg import openapi
from drf_yasg.utils import no_body, swagger_auto_schema
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from genericparser.packages.biz import sdc_vnf_package, sdc_ns_package
from genericparser.packages.biz.pnf_descriptor import PnfDescriptor
from genericparser.packages.biz.sdc_service_package import ServicePackage
from genericparser.packages.serializers.genericparser_serializers import InternalErrorRequestSerializer, \
ServicePackageDistributeRequestSerializer, ServicePackagesSerializer, ServicePackageSerializer
from genericparser.packages.serializers.genericparser_serializers import NfPackageDistributeRequestSerializer
from genericparser.packages.serializers.genericparser_serializers import NfPackageSerializer
from genericparser.packages.serializers.genericparser_serializers import NfPackagesSerializer
from genericparser.packages.serializers.genericparser_serializers import NsPackageDistributeRequestSerializer
from genericparser.packages.serializers.genericparser_serializers import NsPackageDistributeResponseSerializer
from genericparser.packages.serializers.genericparser_serializers import NsPackageSerializer
from genericparser.packages.serializers.genericparser_serializers import NsPackagesSerializer
from genericparser.packages.serializers.genericparser_serializers import ParseModelRequestSerializer
from genericparser.packages.serializers.genericparser_serializers import ParseModelResponseSerializer
from genericparser.packages.serializers.genericparser_serializers import PostJobResponseSerializer
from genericparser.packages.views.common import fmt_error_rsp
from genericparser.pub.exceptions import PackageNotFoundException, PackageHasExistsException
from genericparser.pub.utils.syscomm import fun_name
from genericparser.pub.utils.values import ignore_case_get
logger = logging.getLogger(__name__)
@swagger_auto_schema(
method='POST',
operation_description="On distribute NS package",
request_body=NsPackageDistributeRequestSerializer,
responses={
status.HTTP_202_ACCEPTED: NsPackageDistributeResponseSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
@swagger_auto_schema(
method='GET',
operation_description="Query NS packages",
request_body=no_body,
responses={
status.HTTP_200_OK: NsPackagesSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
@api_view(http_method_names=['POST', 'GET'])
def nspackages_rc(request, *args, **kwargs):
logger.debug("Enter %s, method is %s", fun_name(), request.method)
ret, normal_status, response_serializer, validation_error = None, None, None, None
if request.method == 'GET':
# Gets ns package list
ret = sdc_ns_package.ns_get_csars()
normal_status = status.HTTP_200_OK
if ret[0] == 0:
response_serializer = NsPackagesSerializer(data=ret[1])
validation_error = handleValidatonError(
response_serializer, False)
if validation_error:
return validation_error
elif request.method == 'POST':
# Distributes the package accroding to the given csarId
request_serializer = NsPackageDistributeRequestSerializer(data=request.data)
validation_error = handleValidatonError(request_serializer, True)
if validation_error:
return validation_error
csar_id = ignore_case_get(request.data, "csarId")
logger.debug("csar_id is %s", csar_id)
ret = sdc_ns_package.ns_on_distribute(csar_id)
normal_status = status.HTTP_202_ACCEPTED
logger.debug("Leave %s, Return value is %s", fun_name(), ret)
if ret[0] != 0:
return Response(
data={
'error': ret[1]},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return Response(data=ret[1], status=normal_status)
@swagger_auto_schema(
method='POST',
operation_description="On distribute Nf package",
request_body=NfPackageDistributeRequestSerializer(),
responses={
status.HTTP_202_ACCEPTED: PostJobResponseSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
@swagger_auto_schema(
method='GET',
operation_description="Query Nf packages",
request_body=no_body,
responses={
status.HTTP_200_OK: NfPackagesSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
@api_view(http_method_names=['POST', 'GET'])
def nfpackages_rc(request, *args, **kwargs):
logger.debug(
"Enter %s%s, method is %s",
fun_name(),
request.data,
request.method)
ret, normal_status, response_serializer, validation_error = None, None, None, None
if request.method == 'GET':
ret = sdc_vnf_package.nf_get_csars()
normal_status = status.HTTP_200_OK
response_serializer = NfPackagesSerializer(data=ret[1])
elif request.method == 'POST':
request_serivalizer = NfPackageDistributeRequestSerializer(
data=request.data)
validation_error = handleValidatonError(
request_serivalizer, True)
if validation_error:
return validation_error
csar_id = ignore_case_get(request_serivalizer.data, "csarId")
vim_ids = ignore_case_get(request_serivalizer.data, "vimIds")
lab_vim_id = ignore_case_get(request_serivalizer.data, "labVimId")
job_id = str(uuid.uuid4())
sdc_vnf_package.NfDistributeThread(
csar_id, vim_ids, lab_vim_id, job_id).start()
ret = [0, {"jobId": job_id}]
normal_status = status.HTTP_202_ACCEPTED
response_serializer = PostJobResponseSerializer(data=ret[1])
logger.debug("Leave %s, Return value is %s", fun_name(), ret)
if ret[0] != 0:
return Response(
data={
'error': ret[1]},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
validation_error = handleValidatonError(
response_serializer, False)
if validation_error:
return validation_error
return Response(data=response_serializer.data, status=normal_status)
@swagger_auto_schema(
method='DELETE',
operation_description="Delete one NS package",
request_body=no_body,
manual_parameters=[
openapi.Parameter(
'csarId',
openapi.IN_QUERY,
"csarId",
type=openapi.TYPE_STRING)],
responses={
status.HTTP_200_OK: NsPackageDistributeResponseSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response(
'error message',
openapi.Schema(
type=openapi.TYPE_STRING))})
@swagger_auto_schema(
method='GET',
operation_description="Query one NS package",
request_body=no_body,
manual_parameters=[
openapi.Parameter(
'csarId',
openapi.IN_QUERY,
"csarId",
type=openapi.TYPE_STRING)],
responses={
status.HTTP_200_OK: NsPackageSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response(
'error message',
openapi.Schema(
type=openapi.TYPE_STRING))})
@api_view(http_method_names=['DELETE', 'GET'])
def ns_rd_csar(request, *args, **kwargs):
csar_id = ignore_case_get(kwargs, "csarId")
logger.info("Enter %s, method is %s, csar_id is %s",
fun_name(), request.method, csar_id)
ret, normal_status, response_serializer, validation_error = None, None, None, None
if request.method == 'GET':
ret = sdc_ns_package.ns_get_csar(csar_id)
normal_status = status.HTTP_200_OK
if ret[0] == 0:
response_serializer = NsPackageSerializer(data=ret[1])
validation_error = handleValidatonError(response_serializer, False)
if validation_error:
return validation_error
elif request.method == 'DELETE':
ret = sdc_ns_package.ns_delete_csar(csar_id)
normal_status = status.HTTP_200_OK
logger.info("Leave %s, Return value is %s", fun_name(), ret)
if ret[0] != 0:
return Response(
data={
'error': ret[1]},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return Response(data=ret[1], status=normal_status)
@swagger_auto_schema(
method='POST',
operation_description="On distribute Service package",
request_body=ServicePackageDistributeRequestSerializer,
responses={
status.HTTP_202_ACCEPTED: "",
status.HTTP_400_BAD_REQUEST: InternalErrorRequestSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
@swagger_auto_schema(
method='GET',
operation_description="Query Service packages",
request_body=no_body,
responses={
status.HTTP_200_OK: ServicePackagesSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
@api_view(http_method_names=['POST', 'GET'])
def servicepackages_rc(request, *args, **kwargs):
logger.debug("Enter %s, method is %s", fun_name(), request.method)
if request.method == 'GET':
# Gets service package list
try:
csar_list = ServicePackage().get_csars()
response_serializer = ServicePackagesSerializer(data=csar_list)
validation_error = handleValidatonError(response_serializer, False)
if validation_error:
return validation_error
return Response(data=csar_list, status=status.HTTP_200_OK)
except Exception as e:
error_status = status.HTTP_500_INTERNAL_SERVER_ERROR
return Response(data=fmt_error_rsp(e.message, error_status), status=error_status)
elif request.method == 'POST':
# Distributes the package according to the given csarId
request_serializer = ServicePackageDistributeRequestSerializer(data=request.data)
validation_error = handleValidatonError(request_serializer, True)
if validation_error:
return validation_error
csar_id = ignore_case_get(request.data, "csarId")
logger.debug("csar_id is %s", csar_id)
try:
ServicePackage().on_distribute(csar_id)
return Response(status=status.HTTP_202_ACCEPTED)
except PackageHasExistsException as e:
error_status = status.HTTP_400_BAD_REQUEST
return Response(data=fmt_error_rsp(e.message, error_status), status=error_status)
except Exception as e:
error_status = status.HTTP_500_INTERNAL_SERVER_ERROR
return Response(data=fmt_error_rsp(e.message, error_status), status=error_status)
@swagger_auto_schema(
method='DELETE',
operation_description="Delete one Service package",
request_body=no_body,
manual_parameters=[
openapi.Parameter(
'csarId',
openapi.IN_QUERY,
"csarId",
type=openapi.TYPE_STRING)],
responses={
status.HTTP_204_NO_CONTENT: "",
status.HTTP_404_NOT_FOUND: InternalErrorRequestSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
@swagger_auto_schema(
method='GET',
operation_description="Query one Service package",
request_body=no_body,
manual_parameters=[
openapi.Parameter(
'csarId',
openapi.IN_QUERY,
"csarId",
type=openapi.TYPE_STRING)],
responses={
status.HTTP_200_OK: ServicePackageSerializer,
status.HTTP_404_NOT_FOUND: InternalErrorRequestSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
@api_view(http_method_names=['DELETE', 'GET'])
def service_rd_csar(request, *args, **kwargs):
csar_id = ignore_case_get(kwargs, "csarId")
logger.info("Enter %s, method is %s, csar_id is %s", fun_name(), request.method, csar_id)
if request.method == 'GET':
try:
ret = ServicePackage().get_csar(csar_id)
response_serializer = ServicePackageSerializer(data=ret)
validation_error = handleValidatonError(response_serializer, False)
if validation_error:
return validation_error
return Response(data=ret, status=status.HTTP_200_OK)
except PackageNotFoundException as e:
error_status = status.HTTP_404_NOT_FOUND
return Response(data=fmt_error_rsp(e.message, error_status), status=error_status)
except Exception as e:
error_status = status.HTTP_500_INTERNAL_SERVER_ERROR
return Response(data=fmt_error_rsp(e.message, error_status), status=error_status)
elif request.method == 'DELETE':
try:
ServicePackage().delete_csar(csar_id)
return Response(status=status.HTTP_204_NO_CONTENT)
except PackageNotFoundException as e:
error_status = status.HTTP_404_NOT_FOUND
return Response(data=fmt_error_rsp(e.message, error_status), status=error_status)
except Exception as e:
error_status = status.HTTP_500_INTERNAL_SERVER_ERROR
return Response(data=fmt_error_rsp(e.message, error_status), status=error_status)
@swagger_auto_schema(
method='DELETE',
operation_description="Delete one Nf package",
request_body=no_body,
manual_parameters=[
openapi.Parameter(
'csarId',
openapi.IN_QUERY,
"csarId",
type=openapi.TYPE_STRING)],
responses={
status.HTTP_202_ACCEPTED: PostJobResponseSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response(
'error message',
openapi.Schema(
type=openapi.TYPE_STRING))})
@swagger_auto_schema(
method='GET',
operation_description="Query one Nf package",
request_body=no_body,
manual_parameters=[
openapi.Parameter(
'csarId',
openapi.IN_QUERY,
"csarId",
type=openapi.TYPE_STRING)],
responses={
status.HTTP_200_OK: NfPackageSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: openapi.Response(
'error message',
openapi.Schema(
type=openapi.TYPE_STRING))})
@api_view(http_method_names=['DELETE', 'GET'])
def nf_rd_csar(request, *args, **kwargs):
csar_id = ignore_case_get(kwargs, "csarId")
logger.info("Enter %s, method is %s, csar_id is %s",
fun_name(), request.method, csar_id)
ret, normal_status, response_serializer, validation_error = None, None, None, None
if request.method == 'GET':
ret = sdc_vnf_package.nf_get_csar(csar_id)
normal_status = status.HTTP_200_OK
response_serializer = NfPackageSerializer(data=ret[1])
elif request.method == 'DELETE':
job_id = str(uuid.uuid4())
sdc_vnf_package.NfPkgDeleteThread(csar_id, job_id).start()
ret = [0, {"jobId": job_id}]
normal_status = status.HTTP_202_ACCEPTED
response_serializer = PostJobResponseSerializer(data=ret[1])
logger.info("Leave %s, Return value is %s", fun_name(), ret)
if ret[0] != 0:
return Response(
data={
'error': ret[1]},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
validation_error = handleValidatonError(
response_serializer, False)
if validation_error:
return validation_error
return Response(data=response_serializer.data, status=normal_status)
@swagger_auto_schema(
method='POST',
operation_description="Parse model(NS, Service, VNF, PNF)",
request_body=ParseModelRequestSerializer,
responses={
status.HTTP_202_ACCEPTED: ParseModelResponseSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
@api_view(http_method_names=['POST'])
def model_parser(request, *args, **kwargs):
csar_id = ignore_case_get(request.data, "csarId")
package_type = ignore_case_get(request.data, "packageType")
inputs = ignore_case_get(request.data, "inputs")
logger.debug(
"Enter %s, csar_id=%s, package_type=%s, inputs=%s",
fun_name(),
csar_id,
package_type,
inputs)
if package_type.lower().__eq__("service"):
try:
ret = ServicePackage().parse_serviced(csar_id, inputs)
response_serializer = ParseModelResponseSerializer(data=ret)
validation_error = handleValidatonError(
response_serializer, False)
if validation_error:
return validation_error
return Response(data=response_serializer.data, status=status.HTTP_202_ACCEPTED)
except PackageNotFoundException as e:
error_status = status.HTTP_404_NOT_FOUND
return Response(data=fmt_error_rsp(e.message, error_status), status=error_status)
except Exception as e:
error_status = status.HTTP_500_INTERNAL_SERVER_ERROR
return Response(data=fmt_error_rsp(e.message, error_status), status=error_status)
elif package_type.lower().__eq__("ns"):
ret = sdc_ns_package.parse_nsd(csar_id, inputs)
elif package_type.lower().__eq__("vnf"):
ret = sdc_vnf_package.parse_vnfd(csar_id, inputs)
elif package_type.lower().__eq__("pnf"):
ret = PnfDescriptor().parse_pnfd(csar_id, inputs)
else:
error_status = status.HTTP_400_BAD_REQUEST
error_message = "Invalid package type, it should be one of [VNF, PNF, NS, Service]"
return Response(data=fmt_error_rsp(error_message, error_status), status=error_status)
if ret[0] != 0:
return Response(
data={
'error': ret[1]},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
response_serializer = ParseModelResponseSerializer(data=ret[1])
validation_error = handleValidatonError(
response_serializer, False)
if validation_error:
return validation_error
return Response(data=response_serializer.data, status=status.HTTP_202_ACCEPTED)
@swagger_auto_schema(
method='POST',
operation_description="Parse NS model",
request_body=ParseModelRequestSerializer,
responses={
status.HTTP_202_ACCEPTED: ParseModelResponseSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
@api_view(http_method_names=['POST'])
def ns_model_parser(request, *args, **kwargs):
csar_id = ignore_case_get(request.data, "csarId")
inputs = ignore_case_get(request.data, "inputs")
logger.debug(
"Enter %s, csar_id=%s, inputs=%s",
fun_name(),
csar_id,
inputs)
ret = sdc_ns_package.parse_nsd(csar_id, inputs)
logger.info("Leave %s, Return value is %s", fun_name(), ret)
if ret[0] != 0:
return Response(
data={
'error': ret[1]},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
response_serializer = ParseModelResponseSerializer(data=ret[1])
validation_error = handleValidatonError(
response_serializer, False)
if validation_error:
return validation_error
return Response(data=response_serializer.data, status=status.HTTP_202_ACCEPTED)
@swagger_auto_schema(
method='POST',
operation_description="Parse NF model",
request_body=ParseModelRequestSerializer,
responses={
status.HTTP_202_ACCEPTED: ParseModelResponseSerializer,
status.HTTP_500_INTERNAL_SERVER_ERROR: InternalErrorRequestSerializer})
@api_view(http_method_names=['POST'])
def vnf_model_parser(request, *args, **kwargs):
csar_id = ignore_case_get(request.data, "csarId")
inputs = ignore_case_get(request.data, "inputs")
logger.debug(
"Enter %s, csar_id=%s, inputs=%s",
fun_name(),
csar_id,
inputs)
ret = sdc_vnf_package.parse_vnfd(csar_id, inputs)
logger.info("Leave %s, Return value is %s", fun_name(), ret)
if ret[0] != 0:
return Response(
data={
'error': ret[1]},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
response_serializer = ParseModelResponseSerializer(data=ret[1])
validation_error = handleValidatonError(
response_serializer, False)
if validation_error:
return validation_error
return Response(data=response_serializer.data, status=status.HTTP_202_ACCEPTED)
def handleValidatonError(base_serializer, is_request):
response = None
if not base_serializer.is_valid():
errormessage = base_serializer.errors
logger.error(errormessage)
if is_request:
message = 'Invalid request'
else:
message = 'Invalid response'
logger.error(message)
response = Response(
data={'error': errormessage},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return response
|