db37e49dc701b25b521494738b325f65f90183a3
[osm/LW-UI.git] / lib / osm / osmclient / clientv2.py
1 #
2 # Copyright 2018 EveryUP Srl
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 import errno
17 import requests
18 import logging
19 import tarfile
20 import yaml
21 import StringIO
22 from lib.util import Util
23 import hashlib
24 import os
25 import re
26 from requests.packages.urllib3.exceptions import InsecureRequestWarning
27
28 requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
29
30 logging.basicConfig(level=logging.INFO)
31 log = logging.getLogger('helper.py')
32 logging.getLogger("urllib3").setLevel(logging.INFO)
33
34
35 class Client(object):
36 def __init__(self):
37 self._token_endpoint = 'admin/v1/tokens'
38 self._user_endpoint = 'admin/v1/users'
39 self._host = os.getenv('OSM_SERVER', "localhost")
40 self._so_port = 9999
41 self._base_path = 'https://{0}:{1}/osm'.format(
42 self._host, self._so_port)
43
44 def auth(self, args):
45 result = {'error': True, 'data': ''}
46 token_url = "{0}/{1}".format(self._base_path, self._token_endpoint)
47 headers = {"Content-Type": "application/yaml",
48 "accept": "application/json"}
49 try:
50 r = requests.post(token_url, json=args,
51 verify=False, headers=headers)
52 except Exception as e:
53 log.exception(e)
54 result['data'] = str(e)
55 return result
56 if r.status_code in (200, 201, 202, 204):
57 result['error'] = False
58
59 result['data'] = Util.json_loads_byteified(r.text)
60
61 return result
62
63 def switch_project(self, args):
64 result = {'error': True, 'data': ''}
65 token_url = "{0}/{1}".format(self._base_path, self._token_endpoint)
66 headers = {"Content-Type": "application/yaml",
67 "accept": "application/json"}
68 try:
69 r = requests.post(token_url, json=args,
70 verify=False, headers=headers)
71 except Exception as e:
72 log.exception(e)
73 result['data'] = str(e)
74 return result
75 if r.status_code in (200, 201, 202, 204):
76 result['error'] = False
77
78 result['data'] = Util.json_loads_byteified(r.text)
79
80 return result
81
82 def role_list(self, token):
83 result = {'error': True, 'data': ''}
84 headers = {"Content-Type": "application/json", "accept": "application/json",
85 'Authorization': 'Bearer {}'.format(token['id'])}
86
87 _url = "{0}/admin/v1/roles".format(self._base_path)
88 try:
89 r = requests.get(_url, params=None, verify=False,
90 stream=True, headers=headers)
91 except Exception as e:
92 log.exception(e)
93 result['data'] = str(e)
94 return result
95 if r.status_code in (200, 201, 202, 204):
96 result['error'] = False
97 result['data'] = Util.json_loads_byteified(r.text)
98
99 return result
100
101 def role_create(self, token, role_data):
102 result = {'error': True, 'data': ''}
103 headers = {"Content-Type": "application/json", "accept": "application/json",
104 'Authorization': 'Bearer {}'.format(token['id'])}
105 _url = "{0}/admin/v1/roles".format(self._base_path)
106
107 try:
108 r = requests.post(_url, json=role_data,
109 verify=False, headers=headers)
110 except Exception as e:
111 log.exception(e)
112 result['data'] = str(e)
113 return result
114 if r.status_code in (200, 201, 202, 204):
115 result['error'] = False
116 result['data'] = Util.json_loads_byteified(r.text)
117 return result
118
119 def role_update(self, token, role_id, role_data):
120 result = {'error': True, 'data': ''}
121 headers = {"Content-Type": "application/json", "accept": "application/json",
122 'Authorization': 'Bearer {}'.format(token['id'])}
123 _url = "{0}/admin/v1/roles/{1}".format(self._base_path, role_id)
124 try:
125 r = requests.patch(_url, json=role_data,
126 verify=False, headers=headers)
127 except Exception as e:
128 log.exception(e)
129 result['data'] = str(e)
130 return result
131 if r.status_code in (200, 201, 202, 204):
132 result['error'] = False
133 else:
134 result['data'] = Util.json_loads_byteified(r.text)
135 return result
136
137 def role_delete(self, token, id, force=None):
138 result = {'error': True, 'data': ''}
139 headers = {"Content-Type": "application/json", "accept": "application/json",
140 'Authorization': 'Bearer {}'.format(token['id'])}
141 query_path = ''
142 if force:
143 query_path = '?FORCE=true'
144 _url = "{0}/admin/v1/roles/{1}{2}".format(
145 self._base_path, id, query_path)
146 try:
147 r = requests.delete(_url, params=None,
148 verify=False, headers=headers)
149 except Exception as e:
150 log.exception(e)
151 result['data'] = str(e)
152 return result
153 if r.status_code in (200, 201, 202, 204):
154 result['error'] = False
155 else:
156 result['data'] = Util.json_loads_byteified(r.text)
157 return result
158
159 def role_get(self, token, id):
160 result = {'error': True, 'data': ''}
161 headers = {"Content-Type": "application/json", "accept": "application/json",
162 'Authorization': 'Bearer {}'.format(token['id'])}
163
164 _url = "{0}/admin/v1/roles/{1}".format(self._base_path, id)
165 try:
166 r = requests.get(_url, params=None, verify=False, headers=headers)
167 except Exception as e:
168 log.exception(e)
169 result['data'] = str(e)
170 return result
171 if r.status_code in (200, 201, 202, 204):
172 result['error'] = False
173 result['data'] = Util.json_loads_byteified(r.text)
174 return result
175
176 def user_list(self, token):
177 result = {'error': True, 'data': ''}
178 headers = {"Content-Type": "application/json", "accept": "application/json",
179 'Authorization': 'Bearer {}'.format(token['id'])}
180
181 _url = "{0}/admin/v1/users".format(self._base_path)
182 try:
183 r = requests.get(_url, params=None, verify=False,
184 stream=True, headers=headers)
185 except Exception as e:
186 log.exception(e)
187 result['data'] = str(e)
188 return result
189 if r.status_code in (200, 201, 202, 204):
190 result['error'] = False
191 result['data'] = Util.json_loads_byteified(r.text)
192
193 return result
194
195 def user_create(self, token, user_data):
196 result = {'error': True, 'data': ''}
197 headers = {"Content-Type": "application/json", "accept": "application/json",
198 'Authorization': 'Bearer {}'.format(token['id'])}
199
200 _url = "{0}/admin/v1/users".format(self._base_path)
201
202 try:
203 r = requests.post(_url, json=user_data,
204 verify=False, headers=headers)
205 except Exception as e:
206 log.exception(e)
207 result['data'] = str(e)
208 return result
209 if r.status_code in (200, 201, 202, 204):
210 result['error'] = False
211 result['data'] = Util.json_loads_byteified(r.text)
212 return result
213
214 def user_update(self, token, id, user_data):
215 result = {'error': True, 'data': ''}
216 headers = {"Content-Type": "application/json", "accept": "application/json",
217 'Authorization': 'Bearer {}'.format(token['id'])}
218
219 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
220 try:
221 r = requests.patch(_url, json=user_data,
222 verify=False, headers=headers)
223 except Exception as e:
224 log.exception(e)
225 result['data'] = str(e)
226 return result
227 if r.status_code in (200, 201, 202, 204):
228 result['error'] = False
229 else:
230 result['data'] = Util.json_loads_byteified(r.text)
231 return result
232
233 def user_delete(self, token, id):
234 result = {'error': True, 'data': ''}
235 headers = {"Content-Type": "application/yaml", "accept": "application/json",
236 'Authorization': 'Bearer {}'.format(token['id'])}
237
238 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
239 try:
240 r = requests.delete(_url, params=None,
241 verify=False, headers=headers)
242 except Exception as e:
243 log.exception(e)
244 result['data'] = str(e)
245 return result
246 if r.status_code in (200, 201, 202, 204):
247 result['error'] = False
248 else:
249 result['data'] = Util.json_loads_byteified(r.text)
250 return result
251
252 def get_user_info(self, token, id):
253 result = {'error': True, 'data': ''}
254 headers = {"Content-Type": "application/yaml", "accept": "application/json",
255 'Authorization': 'Bearer {}'.format(token['id'])}
256 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
257 try:
258 r = requests.get(_url, params=None, verify=False,
259 stream=True, headers=headers)
260 except Exception as e:
261 log.exception(e)
262 result['data'] = str(e)
263 return result
264 if r.status_code in (200, 201, 202, 204):
265 result['error'] = False
266 result['data'] = Util.json_loads_byteified(r.text)
267 return result
268
269 def get_domains(self, token):
270 result = {'error': False, 'data': ''}
271 headers = {"accept": "application/json", 'Authorization': 'Bearer {}'.format(token['id'])}
272
273 _url = "{0}/admin/v1/domains".format(self._base_path)
274 try:
275 r = requests.get(_url, params=None, verify=False,
276 stream=True, headers=headers)
277 except Exception as e:
278 log.exception(e)
279 result['data'] = str(e)
280 return result
281 if r.status_code in (200, 201, 202, 204):
282 result['error'] = False
283
284 result['data'] = Util.json_loads_byteified(r.text)
285 return result
286
287 def get_projects(self, token, uuids):
288 result = {'error': False, 'data': ''}
289 headers = {"Content-Type": "application/yaml", "accept": "application/json",
290 'Authorization': 'Bearer {}'.format(token['id'])}
291
292 projects = []
293 try:
294 for uuid in uuids:
295 _url = "{0}/admin/v1/projects/{1}".format(
296 self._base_path, uuid)
297 r = requests.get(_url, params=None, verify=False,
298 stream=True, headers=headers)
299 if r.status_code not in (200, 201, 202, 204):
300 raise Exception()
301 projects.append(Util.json_loads_byteified(r.text))
302 except Exception as e:
303 log.exception(e)
304 result['error'] = True
305 result['data'] = str(e)
306 return result
307 result['data'] = projects
308 return result
309
310 def project_list(self, token):
311 result = {'error': True, 'data': ''}
312 headers = {"Content-Type": "application/yaml", "accept": "application/json",
313 'Authorization': 'Bearer {}'.format(token['id'])}
314
315 _url = "{0}/admin/v1/projects".format(self._base_path)
316 try:
317 r = requests.get(_url, params=None, verify=False,
318 stream=True, headers=headers)
319 except Exception as e:
320 log.exception(e)
321 result['data'] = str(e)
322 return result
323 if r.status_code in (200, 201, 202, 204):
324 result['error'] = False
325 result['data'] = Util.json_loads_byteified(r.text)
326
327 return result
328
329 def project_get(self, token, id):
330 result = {'error': True, 'data': ''}
331 headers = {"Content-Type": "application/yaml", "accept": "application/json",
332 'Authorization': 'Bearer {}'.format(token['id'])}
333 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
334 try:
335 r = requests.get(_url, params=None, verify=False,
336 stream=True, headers=headers)
337 except Exception as e:
338 log.exception(e)
339 result['data'] = str(e)
340 return result
341 if r.status_code in (200, 201, 202, 204):
342 result['error'] = False
343 result['data'] = Util.json_loads_byteified(r.text)
344 return result
345
346 def project_create(self, token, project_data):
347
348 result = {'error': True, 'data': ''}
349 headers = {"Content-Type": "application/json", "accept": "application/json",
350 'Authorization': 'Bearer {}'.format(token['id'])}
351
352 _url = "{0}/admin/v1/projects".format(self._base_path)
353
354 try:
355 r = requests.post(_url, json=project_data,
356 verify=False, headers=headers)
357 except Exception as e:
358 log.exception(e)
359 result['data'] = str(e)
360 return result
361 if r.status_code in (200, 201, 202, 204):
362 result['error'] = False
363 result['data'] = Util.json_loads_byteified(r.text)
364 return result
365
366 def project_edit(self, token, id, project_data):
367
368 result = {'error': True, 'data': ''}
369 headers = {"Content-Type": "application/json", "accept": "application/json",
370 'Authorization': 'Bearer {}'.format(token['id'])}
371
372 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
373
374 try:
375 r = requests.patch(_url, json=project_data,
376 verify=False, headers=headers)
377 except Exception as e:
378 log.exception(e)
379 result['data'] = str(e)
380 return result
381 if r.status_code in (200, 201, 202, 204):
382 result['error'] = False
383 return result
384
385 def project_delete(self, token, id):
386 result = {'error': True, 'data': ''}
387 headers = {"Content-Type": "application/yaml", "accept": "application/json",
388 'Authorization': 'Bearer {}'.format(token['id'])}
389
390 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
391 try:
392 r = requests.delete(_url, params=None,
393 verify=False, headers=headers)
394 except Exception as e:
395 log.exception(e)
396 result['data'] = str(e)
397 return result
398 if r.status_code in (200, 201, 202, 204):
399 result['error'] = False
400 else:
401 result['data'] = Util.json_loads_byteified(r.text)
402 return result
403
404 def nst_details(self, token, id):
405 result = {'error': True, 'data': ''}
406 headers = {"Content-Type": "application/json", "accept": "application/json",
407 'Authorization': 'Bearer {}'.format(token['id'])}
408 _url = "{0}/nst/v1/netslice_templates/{1}".format(self._base_path, id)
409 try:
410 r = requests.get(_url, params=None, verify=False,
411 stream=True, headers=headers)
412 except Exception as e:
413 log.exception(e)
414 result['data'] = str(e)
415 return result
416 if r.status_code in (200, 201, 202, 204):
417 result['error'] = False
418 result['data'] = Util.json_loads_byteified(r.text)
419
420 return result
421
422 def nst_content(self, token, id):
423 result = {'error': True, 'data': ''}
424 headers = {"Content-Type": "application/json", "accept": "text/plain",
425 'Authorization': 'Bearer {}'.format(token['id'])}
426 _url = "{0}/nst/v1/netslice_templates/{1}/nst".format(
427 self._base_path, id)
428 try:
429 r = requests.get(_url, params=None, verify=False,
430 stream=True, headers=headers)
431 except Exception as e:
432 log.exception(e)
433 result['data'] = str(e)
434 return result
435 if r.status_code in (200, 201, 202, 204):
436 result['error'] = False
437 result['data'] = Util.json2yaml(yaml.load(str(r.text)))
438
439 return result
440
441 def nst_list(self, token):
442 result = {'error': True, 'data': ''}
443 headers = {"Content-Type": "application/yaml", "accept": "application/json",
444 'Authorization': 'Bearer {}'.format(token['id'])}
445
446 _url = "{0}/nst/v1/netslice_templates".format(self._base_path)
447 try:
448 r = requests.get(_url, params=None, verify=False,
449 stream=True, headers=headers)
450 except Exception as e:
451 log.exception(e)
452 result['data'] = str(e)
453 return result
454 if r.status_code in (200, 201, 202, 204):
455 result['error'] = False
456 result['data'] = Util.json_loads_byteified(r.text)
457
458 return result
459
460 def nsd_list(self, token, filter=None):
461 result = {'error': True, 'data': ''}
462 headers = {"Content-Type": "application/yaml", "accept": "application/json",
463 'Authorization': 'Bearer {}'.format(token['id'])}
464 query_path = ''
465 if filter:
466 query_path = '?_admin.type='+filter
467 _url = "{0}/nsd/v1/ns_descriptors_content{1}".format(
468 self._base_path, query_path)
469 try:
470 r = requests.get(_url, params=None, verify=False,
471 stream=True, headers=headers)
472 except Exception as e:
473 log.exception(e)
474 result['data'] = str(e)
475 return result
476 if r.status_code in (200, 201, 202, 204):
477 result['error'] = False
478 result['data'] = Util.json_loads_byteified(r.text)
479
480 return result
481
482 def vnfd_list(self, token, filter=None):
483 result = {'error': True, 'data': ''}
484 headers = {"Content-Type": "application/yaml", "accept": "application/json",
485 'Authorization': 'Bearer {}'.format(token['id'])}
486 query_path = ''
487 if filter:
488 query_path = '?_admin.type='+filter
489 _url = "{0}/vnfpkgm/v1/vnf_packages_content{1}".format(
490 self._base_path, query_path)
491 try:
492 r = requests.get(_url, params=None, verify=False,
493 stream=True, headers=headers)
494 except Exception as e:
495 log.exception(e)
496 result['data'] = str(e)
497 return result
498 if r.status_code in (200, 201, 202, 204):
499 result['error'] = False
500 result['data'] = Util.json_loads_byteified(r.text)
501
502 return result
503
504 def nsi_list(self, token):
505 result = {'error': True, 'data': ''}
506 headers = {"Content-Type": "application/yaml", "accept": "application/json",
507 'Authorization': 'Bearer {}'.format(token['id'])}
508 _url = "{0}/nsilcm/v1/netslice_instances".format(self._base_path)
509 try:
510 r = requests.get(_url, params=None, verify=False,
511 stream=True, headers=headers)
512 except Exception as e:
513 log.exception(e)
514 result['data'] = str(e)
515 return result
516 if r.status_code in (200, 201, 202, 204):
517 result['error'] = False
518 result['data'] = Util.json_loads_byteified(r.text)
519
520 return result
521
522 def ns_list(self, token):
523 result = {'error': True, 'data': ''}
524 headers = {"Content-Type": "application/yaml", "accept": "application/json",
525 'Authorization': 'Bearer {}'.format(token['id'])}
526 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
527 try:
528 r = requests.get(_url, params=None, verify=False,
529 stream=True, headers=headers)
530 except Exception as e:
531 log.exception(e)
532 result['data'] = str(e)
533 return result
534 if r.status_code in (200, 201, 202, 204):
535 result['error'] = False
536 result['data'] = Util.json_loads_byteified(r.text)
537
538 return result
539
540 def vnf_list(self, token):
541 result = {'error': True, 'data': ''}
542 headers = {"Content-Type": "application/yaml", "accept": "application/json",
543 'Authorization': 'Bearer {}'.format(token['id'])}
544 _url = "{0}/nslcm/v1/vnfrs".format(self._base_path)
545 try:
546 r = requests.get(_url, params=None, verify=False,
547 stream=True, headers=headers)
548 except Exception as e:
549 log.exception(e)
550 result['data'] = str(e)
551 return result
552 if r.status_code in (200, 201, 202, 204):
553 result['error'] = False
554 result['data'] = Util.json_loads_byteified(r.text)
555
556 return result
557
558 def pdu_list(self, token):
559 result = {'error': True, 'data': ''}
560 headers = {"Content-Type": "application/yaml", "accept": "application/json",
561 'Authorization': 'Bearer {}'.format(token['id'])}
562 _url = "{0}/pdu/v1/pdu_descriptors".format(self._base_path)
563 try:
564 r = requests.get(_url, params=None, verify=False,
565 stream=True, headers=headers)
566 except Exception as e:
567 log.exception(e)
568 result['data'] = str(e)
569 return result
570 if r.status_code in (200, 201, 202, 204):
571 result['error'] = False
572 result['data'] = Util.json_loads_byteified(r.text)
573
574 return result
575
576 def nst_delete(self, token, id):
577 result = {'error': True, 'data': ''}
578 headers = {"Content-Type": "application/yaml", "accept": "application/json",
579 'Authorization': 'Bearer {}'.format(token['id'])}
580
581 _url = "{0}/nst/v1/netslice_templates/{1}?FORCE=True".format(
582 self._base_path, id)
583 try:
584 r = requests.delete(_url, params=None,
585 verify=False, headers=headers)
586 except Exception as e:
587 log.exception(e)
588 result['data'] = str(e)
589 return result
590 if r.status_code in (200, 201, 202, 204):
591 result['error'] = False
592
593 return result
594
595 def nsd_delete(self, token, id):
596 result = {'error': True, 'data': ''}
597 headers = {"Content-Type": "application/yaml", "accept": "application/json",
598 'Authorization': 'Bearer {}'.format(token['id'])}
599
600 _url = "{0}/nsd/v1/ns_descriptors_content/{1}".format(
601 self._base_path, id)
602 try:
603 r = requests.delete(_url, params=None,
604 verify=False, headers=headers)
605 except Exception as e:
606 log.exception(e)
607 result['data'] = str(e)
608 return result
609 if r:
610 result['error'] = False
611 if r.status_code != requests.codes.no_content:
612 result['data'] = Util.json_loads_byteified(r.text)
613 return result
614
615 def vnfd_delete(self, token, id):
616 result = {'error': True, 'data': ''}
617 headers = {"Content-Type": "application/yaml", "accept": "application/json",
618 'Authorization': 'Bearer {}'.format(token['id'])}
619
620 _url = "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(
621 self._base_path, id)
622 try:
623 r = requests.delete(_url, params=None,
624 verify=False, headers=headers)
625 except Exception as e:
626 log.exception(e)
627 result['data'] = str(e)
628 return result
629 if r:
630 result['error'] = False
631 if r.status_code != requests.codes.no_content:
632 result['data'] = Util.json_loads_byteified(r.text)
633 return result
634
635 def nst_onboard(self, token, template):
636 result = {'error': True, 'data': ''}
637 headers = {"Content-Type": "application/gzip", "accept": "application/json",
638 'Authorization': 'Bearer {}'.format(token['id'])}
639 _url = "{0}/nst/v1/netslice_templates_content".format(self._base_path)
640 try:
641 fileName, fileExtension = os.path.splitext(template.name)
642 if fileExtension == '.gz':
643 headers["Content-Type"] = "application/gzip"
644 else:
645 headers["Content-Type"] = "application/yaml"
646 r = requests.post(_url, data=template,
647 verify=False, headers=headers)
648 except Exception as e:
649 log.exception(e)
650 result['data'] = str(e)
651 return result
652 if r.status_code in (200, 201, 202, 204):
653 result['error'] = False
654 result['data'] = Util.json_loads_byteified(r.text)
655 return result
656
657 def nsd_onboard(self, token, package):
658 result = {'error': True, 'data': ''}
659 headers = {"Content-Type": "application/gzip", "accept": "application/json",
660 'Authorization': 'Bearer {}'.format(token['id'])}
661 with open('/tmp/' + package.name, 'wb+') as destination:
662 for chunk in package.chunks():
663 destination.write(chunk)
664 headers['Content-File-MD5'] = self.md5(
665 open('/tmp/' + package.name, 'rb'))
666 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
667 try:
668 r = requests.post(_url, data=open(
669 '/tmp/' + package.name, 'rb'), verify=False, headers=headers)
670 except Exception as e:
671 log.exception(e)
672 result['data'] = str(e)
673 return result
674 if r.status_code in (200, 201, 202, 204):
675 result['error'] = False
676 result['data'] = Util.json_loads_byteified(r.text)
677 return result
678
679 def vnfd_onboard(self, token, package):
680 result = {'error': True, 'data': ''}
681 headers = {"Content-Type": "application/gzip", "accept": "application/json",
682 'Authorization': 'Bearer {}'.format(token['id'])}
683 with open('/tmp/' + package.name, 'wb+') as destination:
684 for chunk in package.chunks():
685 destination.write(chunk)
686 headers['Content-File-MD5'] = self.md5(
687 open('/tmp/' + package.name, 'rb'))
688 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
689 try:
690 r = requests.post(_url, data=open(
691 '/tmp/' + package.name, 'rb'), verify=False, headers=headers)
692 except Exception as e:
693 log.exception(e)
694 result['data'] = str(e)
695 return result
696 if r.status_code in (200, 201, 202, 204):
697 result['error'] = False
698 result['data'] = Util.json_loads_byteified(r.text)
699 return result
700
701 def nsd_create_pkg_base(self, token, pkg_name):
702 result = {'error': True, 'data': ''}
703 headers = {"Content-Type": "application/gzip", "accept": "application/json",
704 'Authorization': 'Bearer {}'.format(token['id'])}
705
706 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
707
708 try:
709 self._create_base_pkg('nsd', pkg_name)
710 headers['Content-Filename'] = pkg_name + '.tar.gz'
711 r = requests.post(_url, data=open(
712 '/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
713 except Exception as e:
714 log.exception(e)
715 result['data'] = str(e)
716 return result
717 if r.status_code in (200, 201, 202, 204):
718 result['data'] = r.json()
719 result['error'] = False
720 if r.status_code == requests.codes.conflict:
721 result['data'] = "Invalid ID."
722 return result
723
724 def vnfd_create_pkg_base(self, token, pkg_name):
725 result = {'error': True, 'data': ''}
726 headers = {"Content-Type": "application/gzip", "accept": "application/json",
727 'Authorization': 'Bearer {}'.format(token['id'])}
728
729 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
730
731 try:
732 self._create_base_pkg('vnfd', pkg_name)
733 r = requests.post(_url, data=open(
734 '/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
735 except Exception as e:
736 log.exception(e)
737 result['data'] = str(e)
738 return result
739 if r.status_code in (200, 201, 202, 204):
740 result['data'] = r.json()
741 result['error'] = False
742 if r.status_code == requests.codes.conflict:
743 result['data'] = "Invalid ID."
744 return result
745
746 def nsd_clone(self, token, id):
747 result = {'error': True, 'data': ''}
748 headers = {"Content-Type": "application/gzip", "accept": "application/json",
749 'Authorization': 'Bearer {}'.format(token['id'])}
750
751 # get the package onboarded
752 tar_pkg = self.get_nsd_pkg(token, id)
753 tarf = tarfile.open(fileobj=tar_pkg)
754 tarf = self._descriptor_clone(tarf, 'nsd')
755 headers['Content-File-MD5'] = self.md5(
756 open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
757
758 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
759
760 try:
761 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
762 headers=headers)
763 except Exception as e:
764 log.exception(e)
765 result['data'] = str(e)
766 return result
767 if r.status_code in (200, 201, 202, 204):
768 result['error'] = False
769 if r.status_code == requests.codes.conflict:
770 result['data'] = "Invalid ID."
771
772 return result
773
774 def vnfd_clone(self, token, id):
775 result = {'error': True, 'data': ''}
776 headers = {"Content-Type": "application/gzip", "accept": "application/json",
777 'Authorization': 'Bearer {}'.format(token['id'])}
778
779 # get the package onboarded
780 tar_pkg = self.get_vnfd_pkg(token, id)
781 tarf = tarfile.open(fileobj=tar_pkg)
782
783 tarf = self._descriptor_clone(tarf, 'vnfd')
784 headers['Content-File-MD5'] = self.md5(
785 open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
786
787 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
788
789 try:
790 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
791 headers=headers)
792 except Exception as e:
793 log.exception(e)
794 result['data'] = str(e)
795 return result
796 if r.status_code in (200, 201, 202, 204):
797 result['error'] = False
798 if r.status_code == requests.codes.conflict:
799 result['data'] = "Invalid ID."
800
801 return result
802
803 def nst_content_update(self, token, id, template):
804 result = {'error': True, 'data': ''}
805 headers = {"Content-Type": "application/yaml", "accept": "application/json",
806 'Authorization': 'Bearer {}'.format(token['id'])}
807 _url = "{0}/nst/v1/netslice_templates/{1}/nst_content".format(
808 self._base_path, id)
809 try:
810 r = requests.put(_url, data=template,
811 verify=False, headers=headers)
812 except Exception as e:
813 log.exception(e)
814 result['data'] = str(e)
815 return result
816 if r.status_code in (200, 201, 202, 204):
817 result['error'] = False
818 return result
819
820 def nsd_update(self, token, id, data):
821 result = {'error': True, 'data': ''}
822 headers = {"Content-Type": "application/gzip", "accept": "application/json",
823 'Authorization': 'Bearer {}'.format(token['id'])}
824
825 # get the package onboarded
826 tar_pkg = self.get_nsd_pkg(token, id)
827 tarf = tarfile.open(fileobj=tar_pkg)
828
829 tarf = self._descriptor_update(tarf, data)
830 headers['Content-File-MD5'] = self.md5(
831 open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
832
833 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(
834 self._base_path, id)
835
836 try:
837 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
838 headers=headers)
839 except Exception as e:
840 log.exception(e)
841 result['data'] = str(e)
842 return result
843 if r.status_code in (200, 201, 202, 204):
844 result['error'] = False
845 else:
846 try:
847 result['data'] = r.json()
848 except Exception as e:
849 result['data'] = {}
850
851 return result
852
853 def vnfd_update(self, token, id, data):
854 result = {'error': True, 'data': ''}
855 headers = {"Content-Type": "application/gzip", "accept": "application/json",
856 'Authorization': 'Bearer {}'.format(token['id'])}
857
858 # get the package onboarded
859 tar_pkg = self.get_vnfd_pkg(token, id)
860 tarf = tarfile.open(fileobj=tar_pkg)
861
862 tarf = self._descriptor_update(tarf, data)
863 headers['Content-File-MD5'] = self.md5(
864 open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
865
866 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(
867 self._base_path, id)
868
869 try:
870 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
871 headers=headers)
872 except Exception as e:
873 log.exception(e)
874 result['data'] = str(e)
875 return result
876 if r.status_code in (200, 201, 202, 204):
877 result['error'] = False
878 else:
879 try:
880 result['data'] = r.json()
881 except Exception as e:
882 result['data'] = {}
883
884 return result
885
886 def get_nsd_pkg(self, token, id):
887 result = {'error': True, 'data': ''}
888 headers = {"accept": "application/zip",
889 'Authorization': 'Bearer {}'.format(token['id'])}
890
891 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(
892 self._base_path, id)
893 try:
894 r = requests.get(_url, params=None, verify=False,
895 stream=True, headers=headers)
896 except Exception as e:
897 log.exception(e)
898 result['data'] = str(e)
899 return result
900 if r.status_code in (200, 201, 202, 204):
901 result['error'] = False
902 tarf = StringIO.StringIO(r.content)
903 return tarf
904 return result
905
906 def get_vnfd_pkg(self, token, id):
907 result = {'error': True, 'data': ''}
908 headers = {"accept": "application/zip",
909 'Authorization': 'Bearer {}'.format(token['id'])}
910 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(
911 self._base_path, id)
912 try:
913 r = requests.get(_url, params=None, verify=False,
914 stream=True, headers=headers)
915 except Exception as e:
916 log.exception(e)
917 result['data'] = str(e)
918 return result
919 if r.status_code in (200, 201, 202, 204):
920 result['error'] = False
921 tarf = StringIO.StringIO(r.content)
922 return tarf
923 return result
924
925 def _descriptor_update(self, tarf, data):
926 # extract the package on a tmp directory
927 tarf.extractall('/tmp')
928 regex = re.compile(r"^[^/]+(/[^/]+\.(yaml|yml))$", re.U)
929 for name in tarf.getnames():
930 if regex.match(name):
931 with open('/tmp/' + name, 'w') as outfile:
932 yaml.safe_dump(data, outfile, default_flow_style=False)
933 break
934
935 tarf_temp = tarfile.open(
936 '/tmp/' + tarf.getnames()[0] + ".tar.gz", "w:gz")
937
938 for tarinfo in tarf:
939 tarf_temp.add('/tmp/' + tarinfo.name,
940 tarinfo.name, recursive=False)
941 tarf_temp.close()
942 return tarf
943
944 def _create_base_pkg(self, descriptor_type, pkg_name):
945 filename = '/tmp/'+pkg_name+'/' + pkg_name + '.yaml'
946 if descriptor_type == 'nsd':
947 descriptor = {
948 "nsd:nsd-catalog": {
949 "nsd": [
950 {
951 "short-name": str(pkg_name),
952 "vendor": "OSM Composer",
953 "description": str(pkg_name) + " descriptor",
954 "vld": [],
955 "constituent-vnfd": [],
956 "version": "1.0",
957 "id": str(pkg_name),
958 "name": str(pkg_name)
959 }
960 ]
961 }
962 }
963
964 elif descriptor_type == 'vnfd':
965 descriptor = {
966 "vnfd:vnfd-catalog": {
967 "vnfd": [
968 {
969 "short-name": str(pkg_name),
970 "vdu": [],
971 "description": "",
972 "mgmt-interface": {
973 "cp": ""
974 },
975 "id": str(pkg_name),
976 "version": "1.0",
977 "internal-vld": [],
978 "connection-point": [],
979 "name": str(pkg_name)
980 }
981 ]
982 }
983 }
984
985 if not os.path.exists(os.path.dirname(filename)):
986 try:
987 os.makedirs(os.path.dirname(filename))
988 except OSError as exc: # Guard against race condition
989 if exc.errno != errno.EEXIST:
990 raise
991
992 with open('/tmp/' + pkg_name + '/' + pkg_name + '.yaml', 'w') as yaml_file:
993 yaml_file.write(yaml.dump(descriptor, default_flow_style=False))
994
995 tarf_temp = tarfile.open('/tmp/' + pkg_name + '.tar.gz', "w:gz")
996 tarf_temp.add('/tmp/'+pkg_name+'/' + pkg_name + '.yaml',
997 pkg_name + '/' + pkg_name + '.yaml', recursive=False)
998 tarf_temp.close()
999
1000 def _descriptor_clone(self, tarf, descriptor_type):
1001 # extract the package on a tmp directory
1002 tarf.extractall('/tmp')
1003
1004 for name in tarf.getnames():
1005 if name.endswith(".yaml") or name.endswith(".yml"):
1006 with open('/tmp/' + name, 'r') as outfile:
1007 yaml_object = yaml.load(outfile)
1008
1009 if descriptor_type == 'nsd':
1010 nsd_list = yaml_object['nsd:nsd-catalog']['nsd']
1011 for nsd in nsd_list:
1012 nsd['id'] = 'clone_' + nsd['id']
1013 nsd['name'] = 'clone_' + nsd['name']
1014 nsd['short-name'] = 'clone_' + nsd['short-name']
1015 elif descriptor_type == 'vnfd':
1016 vnfd_list = yaml_object['vnfd:vnfd-catalog']['vnfd']
1017 for vnfd in vnfd_list:
1018 vnfd['id'] = 'clone_' + vnfd['id']
1019 vnfd['name'] = 'clone_' + vnfd['name']
1020 vnfd['short-name'] = 'clone_' + vnfd['short-name']
1021
1022 with open('/tmp/' + name, 'w') as yaml_file:
1023 yaml_file.write(
1024 yaml.dump(yaml_object, default_flow_style=False))
1025 break
1026
1027 tarf_temp = tarfile.open(
1028 '/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", "w:gz")
1029
1030 for tarinfo in tarf:
1031 tarf_temp.add('/tmp/' + tarinfo.name,
1032 tarinfo.name, recursive=False)
1033 tarf_temp.close()
1034 return tarf
1035
1036 def nsd_get(self, token, id):
1037 result = {'error': True, 'data': ''}
1038 headers = {'Content-Type': 'application/yaml',
1039 'Authorization': 'Bearer {}'.format(token['id'])}
1040 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self._base_path, id)
1041 try:
1042 r = requests.get(_url, params=None, verify=False,
1043 stream=True, headers=headers)
1044 except Exception as e:
1045 log.exception(e)
1046 result['data'] = str(e)
1047 return result
1048 if r.status_code in (200, 201, 202, 204):
1049 result['error'] = False
1050 return yaml.load(r.text)
1051 else:
1052 try:
1053 result['data'] = r.json()
1054 except Exception as e:
1055 result['data'] = {}
1056 return result
1057
1058 def vnfd_get(self, token, id):
1059 result = {'error': True, 'data': ''}
1060 headers = {'Content-Type': 'application/yaml',
1061 'Authorization': 'Bearer {}'.format(token['id'])}
1062 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(
1063 self._base_path, id)
1064 try:
1065 r = requests.get(_url, params=None, verify=False,
1066 stream=True, headers=headers)
1067 except Exception as e:
1068 log.exception(e)
1069 result['data'] = str(e)
1070 return result
1071 if r.status_code in (200, 201, 202, 204):
1072 result['error'] = False
1073 return yaml.load(r.text)
1074 else:
1075 try:
1076 result['data'] = r.json()
1077 except Exception as e:
1078 result['data'] = {}
1079 return result
1080
1081 def nsd_artifacts(self, token, id):
1082 result = {'error': True, 'data': ''}
1083 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
1084 'Authorization': 'Bearer {}'.format(token['id'])}
1085 _url = "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(
1086 self._base_path, id)
1087 try:
1088 r = requests.get(_url, params=None, verify=False,
1089 stream=True, headers=headers)
1090 except Exception as e:
1091 log.exception(e)
1092 result['data'] = str(e)
1093 return result
1094 if r.status_code in (200, 201, 202, 204):
1095 result['error'] = False
1096 result['data'] = r.text
1097 else:
1098 try:
1099 result['data'] = r.json()
1100 except Exception as e:
1101 result['data'] = {}
1102
1103 return result
1104
1105 def vnf_packages_artifacts(self, token, id):
1106 result = {'error': True, 'data': ''}
1107 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
1108 'Authorization': 'Bearer {}'.format(token['id'])}
1109 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(
1110 self._base_path, id)
1111 try:
1112 r = requests.get(_url, params=None, verify=False,
1113 stream=True, headers=headers)
1114 except Exception as e:
1115 log.exception(e)
1116 result['data'] = str(e)
1117 return result
1118 if r.status_code in (200, 201, 202, 204):
1119 result['error'] = False
1120 result['data'] = r.text
1121 else:
1122 try:
1123 result['data'] = r.json()
1124 except Exception as e:
1125 result['data'] = {}
1126
1127 return result
1128
1129 def nsi_create(self, token, nsi_data):
1130 result = {'error': True, 'data': ''}
1131 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1132 'Authorization': 'Bearer {}'.format(token['id'])}
1133
1134 _url = "{0}/nsilcm/v1/netslice_instances_content".format(
1135 self._base_path)
1136
1137 try:
1138 r = requests.post(_url, json=nsi_data,
1139 verify=False, headers=headers)
1140 except Exception as e:
1141 log.exception(e)
1142 result['data'] = str(e)
1143 return result
1144 if r.status_code in (200, 201, 202, 204):
1145 result['error'] = False
1146 result['data'] = Util.json_loads_byteified(r.text)
1147 return result
1148
1149 def ns_create(self, token, ns_data):
1150 result = {'error': True, 'data': ''}
1151 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1152 'Authorization': 'Bearer {}'.format(token['id'])}
1153
1154 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
1155
1156 try:
1157 r = requests.post(_url, json=ns_data,
1158 verify=False, headers=headers)
1159 except Exception as e:
1160 log.exception(e)
1161 result['data'] = str(e)
1162 return result
1163 if r.status_code in (200, 201, 202, 204):
1164 result['error'] = False
1165 result['data'] = Util.json_loads_byteified(r.text)
1166 return result
1167
1168 def pdu_create(self, token, pdu_data):
1169 result = {'error': True, 'data': ''}
1170 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1171 'Authorization': 'Bearer {}'.format(token['id'])}
1172
1173 _url = "{0}/pdu/v1/pdu_descriptors".format(self._base_path)
1174
1175 try:
1176 r = requests.post(_url, json=pdu_data,
1177 verify=False, headers=headers)
1178 except Exception as e:
1179 log.exception(e)
1180 result['data'] = str(e)
1181 return result
1182 if r.status_code in (200, 201, 202, 204):
1183 result['error'] = False
1184 result['data'] = Util.json_loads_byteified(r.text)
1185 return result
1186
1187 def ns_op_list(self, token, id):
1188 result = {'error': True, 'data': ''}
1189 headers = {"Content-Type": "application/json", "accept": "application/json",
1190 'Authorization': 'Bearer {}'.format(token['id'])}
1191 _url = "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(
1192 self._base_path, id)
1193
1194 try:
1195 r = requests.get(_url, params=None, verify=False,
1196 stream=True, headers=headers)
1197 except Exception as e:
1198 log.exception(e)
1199 result['data'] = str(e)
1200 return result
1201 if r.status_code in (200, 201, 202, 204):
1202 result['error'] = False
1203 result['data'] = Util.json_loads_byteified(r.text)
1204
1205 return result
1206
1207 def nsi_op_list(self, token, id):
1208 result = {'error': True, 'data': ''}
1209 headers = {"Content-Type": "application/json", "accept": "application/json",
1210 'Authorization': 'Bearer {}'.format(token['id'])}
1211 _url = "{0}/nsilcm/v1/nsi_lcm_op_occs/?netsliceInstanceId={1}".format(
1212 self._base_path, id)
1213
1214 try:
1215 r = requests.get(_url, params=None, verify=False,
1216 stream=True, headers=headers)
1217 except Exception as e:
1218 log.exception(e)
1219 result['data'] = str(e)
1220 return result
1221 if r.status_code in (200, 201, 202, 204):
1222 result['error'] = False
1223 result['data'] = Util.json_loads_byteified(r.text)
1224
1225 return result
1226
1227 def ns_op(self, token, id):
1228 result = {'error': True, 'data': ''}
1229 headers = {"Content-Type": "application/json", "accept": "application/json",
1230 'Authorization': 'Bearer {}'.format(token['id'])}
1231 _url = "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self._base_path, id)
1232
1233 try:
1234 r = requests.get(_url, params=None, verify=False,
1235 stream=True, headers=headers)
1236 except Exception as e:
1237 log.exception(e)
1238 result['data'] = str(e)
1239 return result
1240 if r.status_code in (200, 201, 202, 204):
1241 result['error'] = False
1242 result['data'] = Util.json_loads_byteified(r.text)
1243
1244 return result
1245
1246 def ns_action(self, token, id, action_payload):
1247 result = {'error': True, 'data': ''}
1248 headers = {"Content-Type": "application/json", "accept": "application/json",
1249 'Authorization': 'Bearer {}'.format(token['id'])}
1250
1251 _url = "{0}/nslcm/v1/ns_instances/{1}/action".format(
1252 self._base_path, id)
1253
1254 try:
1255 r = requests.post(_url, json=action_payload,
1256 verify=False, headers=headers)
1257 except Exception as e:
1258 log.exception(e)
1259 result['data'] = str(e)
1260 return result
1261 if r.status_code in (200, 201, 202, 204):
1262 result['error'] = False
1263 result['data'] = Util.json_loads_byteified(r.text)
1264 return result
1265
1266 def nsi_delete(self, token, id, force=None):
1267 result = {'error': True, 'data': ''}
1268 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1269 'Authorization': 'Bearer {}'.format(token['id'])}
1270 query_path = ''
1271 if force:
1272 query_path = '?FORCE=true'
1273 _url = "{0}/nsilcm/v1/netslice_instances_content/{1}{2}".format(
1274 self._base_path, id, query_path)
1275 try:
1276 r = requests.delete(_url, params=None,
1277 verify=False, headers=headers)
1278 except Exception as e:
1279 log.exception(e)
1280 result['data'] = str(e)
1281 return result
1282 if r:
1283 result['error'] = False
1284 if r.status_code != requests.codes.no_content:
1285 result['data'] = Util.json_loads_byteified(r.text)
1286 return result
1287
1288 def ns_delete(self, token, id, force=None):
1289 result = {'error': True, 'data': ''}
1290 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1291 'Authorization': 'Bearer {}'.format(token['id'])}
1292 query_path = ''
1293 if force:
1294 query_path = '?FORCE=true'
1295 _url = "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(
1296 self._base_path, id, query_path)
1297 try:
1298 r = requests.delete(_url, params=None,
1299 verify=False, headers=headers)
1300 except Exception as e:
1301 log.exception(e)
1302 result['data'] = str(e)
1303 return result
1304 if r:
1305 result['error'] = False
1306 if r.status_code != requests.codes.no_content:
1307 result['data'] = Util.json_loads_byteified(r.text)
1308 return result
1309
1310 def pdu_delete(self, token, id):
1311 result = {'error': True, 'data': ''}
1312 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1313 'Authorization': 'Bearer {}'.format(token['id'])}
1314 _url = "{0}/pdu/v1/pdu_descriptors/{1}".format(self._base_path, id)
1315 try:
1316 r = requests.delete(_url, params=None,
1317 verify=False, headers=headers)
1318 except Exception as e:
1319 log.exception(e)
1320 result['data'] = str(e)
1321 return result
1322 if r:
1323 result['error'] = False
1324 if r.status_code != requests.codes.no_content:
1325 result['data'] = Util.json_loads_byteified(r.text)
1326 return result
1327
1328 def nsi_get(self, token, id):
1329 result = {'error': True, 'data': ''}
1330 headers = {"Content-Type": "application/json", "accept": "application/json",
1331 'Authorization': 'Bearer {}'.format(token['id'])}
1332 _url = "{0}/nsilcm/v1/netslice_instances/{1}".format(
1333 self._base_path, id)
1334
1335 try:
1336 r = requests.get(_url, params=None, verify=False,
1337 stream=True, headers=headers)
1338 except Exception as e:
1339 log.exception(e)
1340 result['data'] = str(e)
1341 return result
1342 if r.status_code in (200, 201, 202, 204):
1343 result['error'] = False
1344 result['data'] = Util.json_loads_byteified(r.text)
1345 return result
1346
1347 def ns_get(self, token, id):
1348 result = {'error': True, 'data': ''}
1349 headers = {"Content-Type": "application/json", "accept": "application/json",
1350 'Authorization': 'Bearer {}'.format(token['id'])}
1351 _url = "{0}/nslcm/v1/ns_instances_content/{1}".format(
1352 self._base_path, id)
1353
1354 try:
1355 r = requests.get(_url, params=None, verify=False,
1356 stream=True, headers=headers)
1357 except Exception as e:
1358 log.exception(e)
1359 result['data'] = str(e)
1360 return result
1361 if r.status_code in (200, 201, 202, 204):
1362 result['error'] = False
1363 result['data'] = Util.json_loads_byteified(r.text)
1364 return result
1365
1366 def vnf_get(self, token, id):
1367 result = {'error': True, 'data': ''}
1368 headers = {"Content-Type": "application/json", "accept": "application/json",
1369 'Authorization': 'Bearer {}'.format(token['id'])}
1370 _url = "{0}/nslcm/v1/vnfrs/{1}".format(self._base_path, id)
1371
1372 try:
1373 r = requests.get(_url, params=None, verify=False,
1374 stream=True, headers=headers)
1375 except Exception as e:
1376 log.exception(e)
1377 result['data'] = str(e)
1378 return result
1379 if r.status_code in (200, 201, 202, 204):
1380 result['error'] = False
1381 result['data'] = Util.json_loads_byteified(r.text)
1382 return result
1383
1384 def pdu_get(self, token, id):
1385 result = {'error': True, 'data': ''}
1386 headers = {"Content-Type": "application/json", "accept": "application/json",
1387 'Authorization': 'Bearer {}'.format(token['id'])}
1388 _url = "{0}/pdu/v1/pdu_descriptors/{1}".format(self._base_path, id)
1389
1390 try:
1391 r = requests.get(_url, params=None, verify=False,
1392 stream=True, headers=headers)
1393 except Exception as e:
1394 log.exception(e)
1395 result['data'] = str(e)
1396 return result
1397 if r.status_code in (200, 201, 202, 204):
1398 result['error'] = False
1399 result['data'] = Util.json_loads_byteified(r.text)
1400 return result
1401
1402 def ns_alarm_create(self, token, id, alarm_payload):
1403 result = {'error': True, 'data': ''}
1404 headers = {"Content-Type": "application/json",
1405 'Authorization': 'Bearer {}'.format(token['id'])}
1406 _url = "{0}/test/message/alarm_request".format(self._base_path)
1407 try:
1408 r = requests.post(_url, json=alarm_payload,
1409 verify=False, headers=headers)
1410 except Exception as e:
1411 log.exception(e)
1412 result['data'] = str(e)
1413 return result
1414 if r.status_code in (200, 201, 202, 204):
1415 result['error'] = False
1416 # result['data'] = Util.json_loads_byteified(r.text)
1417 result['data'] = r.text
1418 return result
1419
1420 def ns_metric_export(self, token, id, metric_payload):
1421 result = {'error': True, 'data': ''}
1422 headers = {"Content-Type": "application/json",
1423 'Authorization': 'Bearer {}'.format(token['id'])}
1424 _url = "{0}/test/message/metric_request".format(self._base_path)
1425 try:
1426 r = requests.post(_url, json=metric_payload,
1427 verify=False, headers=headers)
1428 except Exception as e:
1429 log.exception(e)
1430 result['data'] = str(e)
1431 return result
1432 if r.status_code in (200, 201, 202, 204):
1433 result['error'] = False
1434 # result['data'] = Util.json_loads_byteified(r.text)
1435 result['data'] = r.text
1436 return result
1437
1438 def wim_list(self, token):
1439 result = {'error': True, 'data': ''}
1440 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1441 'Authorization': 'Bearer {}'.format(token['id'])}
1442 _url = "{0}/admin/v1/wim_accounts".format(self._base_path)
1443 try:
1444 r = requests.get(_url, params=None, verify=False,
1445 stream=True, headers=headers)
1446 except Exception as e:
1447 log.exception(e)
1448 result['data'] = str(e)
1449 return result
1450 if r.status_code in (200, 201, 202, 204):
1451 result['error'] = False
1452 result['data'] = Util.json_loads_byteified(r.text)
1453
1454 return result
1455
1456 def vim_list(self, token):
1457 result = {'error': True, 'data': ''}
1458 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1459 'Authorization': 'Bearer {}'.format(token['id'])}
1460 _url = "{0}/admin/v1/vims".format(self._base_path)
1461 try:
1462 r = requests.get(_url, params=None, verify=False,
1463 stream=True, headers=headers)
1464 except Exception as e:
1465 log.exception(e)
1466 result['data'] = str(e)
1467 return result
1468 if r.status_code in (200, 201, 202, 204):
1469 result['error'] = False
1470 result['data'] = Util.json_loads_byteified(r.text)
1471
1472 return result
1473
1474 def wim_delete(self, token, id):
1475 result = {'error': True, 'data': ''}
1476 headers = {"accept": "application/json",
1477 'Authorization': 'Bearer {}'.format(token['id'])}
1478 _url = "{0}/admin/v1/wim_accounts/{1}".format(self._base_path, id)
1479 try:
1480 r = requests.delete(_url, params=None,
1481 verify=False, headers=headers)
1482 except Exception as e:
1483 log.exception(e)
1484 result['data'] = str(e)
1485 return result
1486 if r.status_code in (200, 201, 202, 204):
1487 result['error'] = False
1488 else:
1489 result['data'] = r.text
1490 return result
1491
1492 def vim_delete(self, token, id):
1493 result = {'error': True, 'data': ''}
1494 headers = {"accept": "application/json",
1495 'Authorization': 'Bearer {}'.format(token['id'])}
1496 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
1497 try:
1498 r = requests.delete(_url, params=None,
1499 verify=False, headers=headers)
1500 except Exception as e:
1501 log.exception(e)
1502 result['data'] = str(e)
1503 return result
1504 if r.status_code in (200, 201, 202, 204):
1505 result['error'] = False
1506 else:
1507 result['data'] = r.text
1508 return result
1509
1510 def wim_get(self, token, id):
1511
1512 result = {'error': True, 'data': ''}
1513 headers = {"Content-Type": "application/json", "accept": "application/json",
1514 'Authorization': 'Bearer {}'.format(token['id'])}
1515 _url = "{0}/admin/v1/wim_accounts/{1}".format(self._base_path, id)
1516
1517 try:
1518 r = requests.get(_url, params=None, verify=False,
1519 stream=True, headers=headers)
1520 except Exception as e:
1521 log.exception(e)
1522 result['data'] = str(e)
1523 return result
1524 if r.status_code in (200, 201, 202, 204):
1525 result['error'] = False
1526 result['data'] = Util.json_loads_byteified(r.text)
1527 return result
1528
1529 def vim_get(self, token, id):
1530
1531 result = {'error': True, 'data': ''}
1532 headers = {"Content-Type": "application/json", "accept": "application/json",
1533 'Authorization': 'Bearer {}'.format(token['id'])}
1534 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
1535
1536 try:
1537 r = requests.get(_url, params=None, verify=False,
1538 stream=True, headers=headers)
1539 except Exception as e:
1540 log.exception(e)
1541 result['data'] = str(e)
1542 return result
1543 if r.status_code in (200, 201, 202, 204):
1544 result['error'] = False
1545 result['data'] = Util.json_loads_byteified(r.text)
1546 return result
1547
1548 def wim_create(self, token, wim_data):
1549 result = {'error': True, 'data': ''}
1550 headers = {"Content-Type": "application/json", "accept": "application/json",
1551 'Authorization': 'Bearer {}'.format(token['id'])}
1552
1553 _url = "{0}/admin/v1/wim_accounts".format(self._base_path)
1554
1555 try:
1556 r = requests.post(_url, json=wim_data,
1557 verify=False, headers=headers)
1558 except Exception as e:
1559 log.exception(e)
1560 result['data'] = str(e)
1561 return result
1562 if r.status_code in (200, 201, 202, 204):
1563 result['error'] = False
1564 result['data'] = Util.json_loads_byteified(r.text)
1565 return result
1566
1567 def vim_create(self, token, vim_data):
1568
1569 result = {'error': True, 'data': ''}
1570 headers = {"Content-Type": "application/json", "accept": "application/json",
1571 'Authorization': 'Bearer {}'.format(token['id'])}
1572
1573 _url = "{0}/admin/v1/vims".format(self._base_path)
1574
1575 try:
1576 r = requests.post(_url, json=vim_data,
1577 verify=False, headers=headers)
1578 except Exception as e:
1579 log.exception(e)
1580 result['data'] = str(e)
1581 return result
1582 if r.status_code in (200, 201, 202, 204):
1583 result['error'] = False
1584 result['data'] = Util.json_loads_byteified(r.text)
1585 return result
1586
1587 def sdn_list(self, token):
1588 result = {'error': True, 'data': ''}
1589 headers = {"accept": "application/json",
1590 'Authorization': 'Bearer {}'.format(token['id'])}
1591 _url = "{0}/admin/v1/sdns".format(self._base_path)
1592 try:
1593 r = requests.get(_url, params=None, verify=False,
1594 stream=True, headers=headers)
1595 except Exception as e:
1596 log.exception(e)
1597 result['data'] = str(e)
1598 return result
1599 if r.status_code in (200, 201, 202, 204):
1600 result['error'] = False
1601 result['data'] = Util.json_loads_byteified(r.text)
1602 return result
1603
1604 def sdn_delete(self, token, id):
1605 result = {'error': True, 'data': ''}
1606 headers = {"accept": "application/json",
1607 'Authorization': 'Bearer {}'.format(token['id'])}
1608 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
1609 try:
1610 r = requests.delete(_url, params=None,
1611 verify=False, headers=headers)
1612 except Exception as e:
1613 log.exception(e)
1614 result['data'] = str(e)
1615 return result
1616 if r.status_code in (200, 201, 202, 204):
1617 result['error'] = False
1618 else:
1619 result['data'] = r.text
1620 return result
1621
1622 def sdn_get(self, token, id):
1623 result = {'error': True, 'data': ''}
1624 headers = {"accept": "application/json",
1625 'Authorization': 'Bearer {}'.format(token['id'])}
1626 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
1627
1628 try:
1629 r = requests.get(_url, params=None, verify=False,
1630 stream=True, headers=headers)
1631 except Exception as e:
1632 log.exception(e)
1633 result['data'] = str(e)
1634 return result
1635 if r.status_code in (200, 201, 202, 204):
1636 result['error'] = False
1637 result['data'] = Util.json_loads_byteified(r.text)
1638 return result
1639
1640 def sdn_create(self, token, sdn_data):
1641 result = {'error': True, 'data': ''}
1642 headers = {"Content-Type": "application/json", "accept": "application/json",
1643 'Authorization': 'Bearer {}'.format(token['id'])}
1644
1645 _url = "{0}/admin/v1/sdns".format(self._base_path)
1646
1647 try:
1648 r = requests.post(_url, json=sdn_data,
1649 verify=False, headers=headers)
1650 except Exception as e:
1651 log.exception(e)
1652 result['data'] = str(e)
1653 return result
1654 if r.status_code in (200, 201, 202, 204):
1655 result['error'] = False
1656 result['data'] = Util.json_loads_byteified(r.text)
1657 return result
1658
1659 def k8sc_get(self, token, id):
1660 result = {'error': True, 'data': ''}
1661 headers = {"accept": "application/json",
1662 'Authorization': 'Bearer {}'.format(token['id'])}
1663 _url = "{0}/admin/v1/k8sclusters/{1}".format(self._base_path, id)
1664 try:
1665 r = requests.get(_url, params=None, verify=False,
1666 stream=True, headers=headers)
1667 except Exception as e:
1668 log.exception(e)
1669 result['data'] = str(e)
1670 return result
1671 if r.status_code in (200, 201, 202, 204):
1672 result['error'] = False
1673 result['data'] = Util.json_loads_byteified(r.text)
1674 return result
1675
1676 def k8sc_list(self, token):
1677 result = {'error': True, 'data': ''}
1678 headers = {"accept": "application/json",
1679 'Authorization': 'Bearer {}'.format(token['id'])}
1680 _url = "{0}/admin/v1/k8sclusters".format(self._base_path)
1681 try:
1682 r = requests.get(_url, params=None, verify=False,
1683 stream=True, headers=headers)
1684 except Exception as e:
1685 log.exception(e)
1686 result['data'] = str(e)
1687 return result
1688 if r.status_code in (200, 201, 202, 204):
1689 result['error'] = False
1690 result['data'] = Util.json_loads_byteified(r.text)
1691 return result
1692
1693 def k8sc_create(self, token, cluster_data):
1694 result = {'error': True, 'data': ''}
1695 headers = {"Content-Type": "application/json", "accept": "application/json",
1696 'Authorization': 'Bearer {}'.format(token['id'])}
1697
1698 _url = "{0}/admin/v1/k8sclusters".format(self._base_path)
1699
1700 try:
1701 r = requests.post(_url, json=cluster_data,
1702 verify=False, headers=headers)
1703 except Exception as e:
1704 log.exception(e)
1705 result['data'] = str(e)
1706 return result
1707 if r.status_code in (200, 201, 202, 204):
1708 result['error'] = False
1709 result['data'] = Util.json_loads_byteified(r.text)
1710 return result
1711
1712 def k8sc_update(self, token, id, cluster_data):
1713 result = {'error': True, 'data': ''}
1714 headers = {"Content-Type": "application/json", "accept": "application/json",
1715 'Authorization': 'Bearer {}'.format(token['id'])}
1716
1717 _url = "{0}/admin/v1/k8sclusters/{1}".format(self._base_path, id)
1718 try:
1719 r = requests.patch(_url, json=cluster_data,
1720 verify=False, headers=headers)
1721 except Exception as e:
1722 log.exception(e)
1723 result['data'] = str(e)
1724 return result
1725 if r.status_code in (200, 201, 202, 204):
1726 result['error'] = False
1727 else:
1728 result['data'] = Util.json_loads_byteified(r.text)
1729 return result
1730
1731 def k8sc_delete(self, token, id):
1732 result = {'error': True, 'data': ''}
1733 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1734 'Authorization': 'Bearer {}'.format(token['id'])}
1735
1736 _url = "{0}/admin/v1/k8sclusters/{1}".format(self._base_path, id)
1737 try:
1738 r = requests.delete(_url, params=None,
1739 verify=False, headers=headers)
1740 except Exception as e:
1741 log.exception(e)
1742 result['data'] = str(e)
1743 return result
1744 if r.status_code in (200, 201, 202, 204):
1745 result['error'] = False
1746 else:
1747 result['data'] = Util.json_loads_byteified(r.text)
1748 return result
1749
1750 def k8sr_get(self, token, id):
1751 result = {'error': True, 'data': ''}
1752 headers = {"accept": "application/json",
1753 'Authorization': 'Bearer {}'.format(token['id'])}
1754 _url = "{0}/admin/v1/k8srepos/{1}".format(self._base_path, id)
1755 try:
1756 r = requests.get(_url, params=None, verify=False,
1757 stream=True, headers=headers)
1758 except Exception as e:
1759 log.exception(e)
1760 result['data'] = str(e)
1761 return result
1762 if r.status_code in (200, 201, 202, 204):
1763 result['error'] = False
1764 result['data'] = Util.json_loads_byteified(r.text)
1765 return result
1766
1767 def k8sr_list(self, token):
1768 result = {'error': True, 'data': ''}
1769 headers = {"accept": "application/json",
1770 'Authorization': 'Bearer {}'.format(token['id'])}
1771 _url = "{0}/admin/v1/k8srepos".format(self._base_path)
1772 try:
1773 r = requests.get(_url, params=None, verify=False,
1774 stream=True, headers=headers)
1775 except Exception as e:
1776 log.exception(e)
1777 result['data'] = str(e)
1778 return result
1779 if r.status_code in (200, 201, 202, 204):
1780 result['error'] = False
1781 result['data'] = Util.json_loads_byteified(r.text)
1782 return result
1783
1784 def k8sr_create(self, token, cluster_data):
1785 result = {'error': True, 'data': ''}
1786 headers = {"Content-Type": "application/json", "accept": "application/json",
1787 'Authorization': 'Bearer {}'.format(token['id'])}
1788
1789 _url = "{0}/admin/v1/k8srepos".format(self._base_path)
1790
1791 try:
1792 r = requests.post(_url, json=cluster_data,
1793 verify=False, headers=headers)
1794 except Exception as e:
1795 log.exception(e)
1796 result['data'] = str(e)
1797 return result
1798 if r.status_code in (200, 201, 202, 204):
1799 result['error'] = False
1800 result['data'] = Util.json_loads_byteified(r.text)
1801 return result
1802
1803 def k8sr_update(self, token, id, cluster_data):
1804 result = {'error': True, 'data': ''}
1805 headers = {"Content-Type": "application/json", "accept": "application/json",
1806 'Authorization': 'Bearer {}'.format(token['id'])}
1807
1808 _url = "{0}/admin/v1/k8srepos/{1}".format(self._base_path, id)
1809 try:
1810 r = requests.patch(_url, json=cluster_data,
1811 verify=False, headers=headers)
1812 except Exception as e:
1813 log.exception(e)
1814 result['data'] = str(e)
1815 return result
1816 if r.status_code in (200, 201, 202, 204):
1817 result['error'] = False
1818 else:
1819 result['data'] = Util.json_loads_byteified(r.text)
1820 return result
1821
1822 def k8sr_delete(self, token, id):
1823 result = {'error': True, 'data': ''}
1824 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1825 'Authorization': 'Bearer {}'.format(token['id'])}
1826
1827 _url = "{0}/admin/v1/k8srepos/{1}".format(self._base_path, id)
1828 try:
1829 r = requests.delete(_url, params=None,
1830 verify=False, headers=headers)
1831 except Exception as e:
1832 log.exception(e)
1833 result['data'] = str(e)
1834 return result
1835 if r.status_code in (200, 201, 202, 204):
1836 result['error'] = False
1837 else:
1838 result['data'] = Util.json_loads_byteified(r.text)
1839 return result
1840
1841 @staticmethod
1842 def md5(f):
1843 hash_md5 = hashlib.md5()
1844 for chunk in iter(lambda: f.read(1024), b""):
1845 hash_md5.update(chunk)
1846 return hash_md5.hexdigest()