6307198488acb543f6532f5ba19d9acac79f2099
[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 from requests.packages.urllib3.exceptions import InsecureRequestWarning
26
27 requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
28
29 logging.basicConfig(level=logging.INFO)
30 log = logging.getLogger('helper.py')
31 logging.getLogger("urllib3").setLevel(logging.INFO)
32
33
34 class Client(object):
35 def __init__(self):
36 self._token_endpoint = 'admin/v1/tokens'
37 self._user_endpoint = 'admin/v1/users'
38 self._host = os.getenv('OSM_SERVER', "localhost")
39 self._so_port = 9999
40 self._base_path = 'https://{0}:{1}/osm'.format(self._host, self._so_port)
41
42 def auth(self, args):
43 result = {'error': True, 'data': ''}
44 token_url = "{0}/{1}".format(self._base_path, self._token_endpoint)
45 headers = {"Content-Type": "application/yaml", "accept": "application/json"}
46 try:
47 r = requests.post(token_url, json=args, verify=False, headers=headers)
48 except Exception as e:
49 log.exception(e)
50 result['data'] = str(e)
51 return result
52 if r.status_code == requests.codes.ok:
53 result['error'] = False
54
55 result['data'] = Util.json_loads_byteified(r.text)
56
57 return result
58
59 def switch_project(self, args):
60 result = {'error': True, 'data': ''}
61 token_url = "{0}/{1}".format(self._base_path, self._token_endpoint)
62 headers = {"Content-Type": "application/yaml", "accept": "application/json"}
63 try:
64 r = requests.post(token_url, json=args, verify=False, headers=headers)
65 except Exception as e:
66 log.exception(e)
67 result['data'] = str(e)
68 return result
69 if r.status_code == requests.codes.ok:
70 result['error'] = False
71
72 result['data'] = Util.json_loads_byteified(r.text)
73
74 return result
75
76 def user_list(self, token):
77 result = {'error': True, 'data': ''}
78 headers = {"Content-Type": "application/json", "accept": "application/json",
79 'Authorization': 'Bearer {}'.format(token['id'])}
80
81 _url = "{0}/admin/v1/users".format(self._base_path)
82 try:
83 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
84 except Exception as e:
85 log.exception(e)
86 result['data'] = str(e)
87 return result
88 if r.status_code == requests.codes.ok:
89 result['error'] = False
90 result['data'] = Util.json_loads_byteified(r.text)
91
92 return result
93
94 def user_create(self, token, user_data):
95 result = {'error': True, 'data': ''}
96 headers = {"Content-Type": "application/json", "accept": "application/json",
97 'Authorization': 'Bearer {}'.format(token['id'])}
98
99 _url = "{0}/admin/v1/users".format(self._base_path)
100
101 try:
102 r = requests.post(_url, json=user_data, verify=False, headers=headers)
103 except Exception as e:
104 log.exception(e)
105 result['data'] = str(e)
106 return result
107 if r.status_code == requests.codes.created:
108 result['error'] = False
109 result['data'] = Util.json_loads_byteified(r.text)
110 return result
111
112 def user_update(self, token, id, user_data):
113 result = {'error': True, 'data': ''}
114 headers = {"Content-Type": "application/json", "accept": "application/json",
115 'Authorization': 'Bearer {}'.format(token['id'])}
116
117 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
118 try:
119 r = requests.patch(_url, json=user_data, verify=False, headers=headers)
120 except Exception as e:
121 log.exception(e)
122 result['data'] = str(e)
123 return result
124 if r.status_code == requests.codes.no_content:
125 result['error'] = False
126 else:
127 result['data'] = Util.json_loads_byteified(r.text)
128 return result
129
130 def user_delete(self, token, id):
131 result = {'error': True, 'data': ''}
132 headers = {"Content-Type": "application/yaml", "accept": "application/json",
133 'Authorization': 'Bearer {}'.format(token['id'])}
134
135 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
136 try:
137 r = requests.delete(_url, params=None, verify=False, headers=headers)
138 except Exception as e:
139 log.exception(e)
140 result['data'] = str(e)
141 return result
142 if r.status_code == requests.codes.no_content:
143 result['error'] = False
144 else:
145 result['data'] = Util.json_loads_byteified(r.text)
146 return result
147
148 def get_user_info(self, token, id):
149 result = {'error': True, 'data': ''}
150 headers = {"Content-Type": "application/yaml", "accept": "application/json",
151 'Authorization': 'Bearer {}'.format(token['id'])}
152 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
153 try:
154 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
155 except Exception as e:
156 log.exception(e)
157 result['data'] = str(e)
158 return result
159 if r.status_code == requests.codes.ok:
160 result['error'] = False
161 result['data'] = Util.json_loads_byteified(r.text)
162 return result
163
164 def project_list(self, token):
165 result = {'error': True, 'data': ''}
166 headers = {"Content-Type": "application/yaml", "accept": "application/json",
167 'Authorization': 'Bearer {}'.format(token['id'])}
168
169 _url = "{0}/admin/v1/projects".format(self._base_path)
170 try:
171 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
172 except Exception as e:
173 log.exception(e)
174 result['data'] = str(e)
175 return result
176 if r.status_code == requests.codes.ok:
177 result['error'] = False
178 result['data'] = Util.json_loads_byteified(r.text)
179
180 return result
181
182 def project_get(self, token, id):
183 result = {'error': True, 'data': ''}
184 headers = {"Content-Type": "application/yaml", "accept": "application/json",
185 'Authorization': 'Bearer {}'.format(token['id'])}
186 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
187 try:
188 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
189 except Exception as e:
190 log.exception(e)
191 result['data'] = str(e)
192 return result
193 if r.status_code == requests.codes.ok:
194 result['error'] = False
195 result['data'] = Util.json_loads_byteified(r.text)
196 return result
197
198 def project_create(self, token, project_data):
199
200 result = {'error': True, 'data': ''}
201 headers = {"Content-Type": "application/json", "accept": "application/json",
202 'Authorization': 'Bearer {}'.format(token['id'])}
203
204 _url = "{0}/admin/v1/projects".format(self._base_path)
205
206 try:
207 r = requests.post(_url, json=project_data, verify=False, headers=headers)
208 except Exception as e:
209 log.exception(e)
210 result['data'] = str(e)
211 return result
212 if r.status_code == requests.codes.created:
213 result['error'] = False
214 result['data'] = Util.json_loads_byteified(r.text)
215 return result
216
217 def project_edit(self, token, id, project_data):
218
219 result = {'error': True, 'data': ''}
220 headers = {"Content-Type": "application/json", "accept": "application/json",
221 'Authorization': 'Bearer {}'.format(token['id'])}
222
223 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
224
225 try:
226 r = requests.put(_url, json=project_data, verify=False, headers=headers)
227 except Exception as e:
228 log.exception(e)
229 result['data'] = str(e)
230 return result
231 if r.status_code == requests.codes.no_content:
232 result['error'] = False
233 result['data'] = Util.json_loads_byteified(r.text)
234 return result
235
236 def project_delete(self, token, id):
237 result = {'error': True, 'data': ''}
238 headers = {"Content-Type": "application/yaml", "accept": "application/json",
239 'Authorization': 'Bearer {}'.format(token['id'])}
240
241 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
242 try:
243 r = requests.delete(_url, params=None, verify=False, headers=headers)
244 except Exception as e:
245 log.exception(e)
246 result['data'] = str(e)
247 return result
248 if r.status_code == requests.codes.no_content:
249 result['error'] = False
250 else:
251 result['data'] = Util.json_loads_byteified(r.text)
252 return result
253
254 def nsd_list(self, token, filter=None):
255 result = {'error': True, 'data': ''}
256 headers = {"Content-Type": "application/yaml", "accept": "application/json",
257 'Authorization': 'Bearer {}'.format(token['id'])}
258 query_path = ''
259 if filter:
260 query_path = '?_admin.type='+filter
261 _url = "{0}/nsd/v1/ns_descriptors_content{1}".format(self._base_path, query_path)
262 try:
263 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
264 except Exception as e:
265 log.exception(e)
266 result['data'] = str(e)
267 return result
268 if r.status_code == requests.codes.ok:
269 result['error'] = False
270 result['data'] = Util.json_loads_byteified(r.text)
271
272 return result
273
274 def vnfd_list(self, token, filter=None):
275 result = {'error': True, 'data': ''}
276 headers = {"Content-Type": "application/yaml", "accept": "application/json",
277 'Authorization': 'Bearer {}'.format(token['id'])}
278 query_path = ''
279 if filter:
280 query_path = '?_admin.type='+filter
281 _url = "{0}/vnfpkgm/v1/vnf_packages_content{1}".format(self._base_path, query_path)
282 try:
283 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
284 except Exception as e:
285 log.exception(e)
286 result['data'] = str(e)
287 return result
288 if r.status_code == requests.codes.ok:
289 result['error'] = False
290 result['data'] = Util.json_loads_byteified(r.text)
291
292 return result
293
294 def ns_list(self, token):
295 result = {'error': True, 'data': ''}
296 headers = {"Content-Type": "application/yaml", "accept": "application/json",
297 'Authorization': 'Bearer {}'.format(token['id'])}
298 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
299 try:
300 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
301 except Exception as e:
302 log.exception(e)
303 result['data'] = str(e)
304 return result
305 if r.status_code == requests.codes.ok:
306 result['error'] = False
307 result['data'] = Util.json_loads_byteified(r.text)
308
309 return result
310
311 def vnf_list(self, token):
312 result = {'error': True, 'data': ''}
313 headers = {"Content-Type": "application/yaml", "accept": "application/json",
314 'Authorization': 'Bearer {}'.format(token['id'])}
315 _url = "{0}/nslcm/v1/vnfrs".format(self._base_path)
316 try:
317 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
318 except Exception as e:
319 log.exception(e)
320 result['data'] = str(e)
321 return result
322 if r.status_code == requests.codes.ok:
323 result['error'] = False
324 result['data'] = Util.json_loads_byteified(r.text)
325
326 return result
327
328 def nsd_delete(self, token, id):
329 result = {'error': True, 'data': ''}
330 headers = {"Content-Type": "application/yaml", "accept": "application/json",
331 'Authorization': 'Bearer {}'.format(token['id'])}
332
333 _url = "{0}/nsd/v1/ns_descriptors_content/{1}".format(self._base_path, id)
334 try:
335 r = requests.delete(_url, params=None, verify=False, headers=headers)
336 except Exception as e:
337 log.exception(e)
338 result['data'] = str(e)
339 return result
340 if r.status_code == requests.codes.ok:
341 result['error'] = False
342 result['data'] = Util.json_loads_byteified(r.text)
343 return result
344
345 def vnfd_delete(self, token, id):
346 result = {'error': True, 'data': ''}
347 headers = {"Content-Type": "application/yaml", "accept": "application/json",
348 'Authorization': 'Bearer {}'.format(token['id'])}
349
350 _url = "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self._base_path, id)
351 try:
352 r = requests.delete(_url, params=None, verify=False, headers=headers)
353 except Exception as e:
354 log.exception(e)
355 result['data'] = str(e)
356 return result
357 if r:
358 result['error'] = False
359 if r.status_code != requests.codes.no_content:
360 result['data'] = Util.json_loads_byteified(r.text)
361 return result
362
363 def nsd_onboard(self, token, package):
364 result = {'error': True, 'data': ''}
365 headers = {"Content-Type": "application/gzip", "accept": "application/json",
366 'Authorization': 'Bearer {}'.format(token['id'])}
367 with open('/tmp/' + package.name, 'wb+') as destination:
368 for chunk in package.chunks():
369 destination.write(chunk)
370 headers['Content-File-MD5'] = self.md5(open('/tmp/' + package.name, 'rb'))
371 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
372 try:
373 r = requests.post(_url, data=open('/tmp/' + package.name, 'rb'), verify=False, headers=headers)
374 except Exception as e:
375 log.exception(e)
376 result['data'] = str(e)
377 return result
378 if r.status_code == requests.codes.created:
379 result['error'] = False
380 result['data'] = Util.json_loads_byteified(r.text)
381 return result
382
383 def vnfd_onboard(self, token, package):
384 result = {'error': True, 'data': ''}
385 headers = {"Content-Type": "application/gzip", "accept": "application/json",
386 'Authorization': 'Bearer {}'.format(token['id'])}
387 with open('/tmp/' + package.name, 'wb+') as destination:
388 for chunk in package.chunks():
389 destination.write(chunk)
390 headers['Content-File-MD5'] = self.md5(open('/tmp/' + package.name, 'rb'))
391 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
392 try:
393 r = requests.post(_url, data=open('/tmp/' + package.name, 'rb'), 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 == requests.codes.created:
399 result['error'] = False
400 result['data'] = Util.json_loads_byteified(r.text)
401 return result
402
403 def nsd_create_pkg_base(self, token, pkg_name):
404 result = {'error': True, 'data': ''}
405 headers = {"Content-Type": "application/gzip", "accept": "application/json",
406 'Authorization': 'Bearer {}'.format(token['id'])}
407
408 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
409
410 try:
411 self._create_base_pkg('nsd', pkg_name)
412 headers['Content-Filename'] = pkg_name + '.tar.gz'
413 r = requests.post(_url, data=open('/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
414 except Exception as e:
415 log.exception(e)
416 result['data'] = str(e)
417 return result
418 if r.status_code == requests.codes.created:
419 result['data'] = r.json()
420 result['error'] = False
421 if r.status_code == requests.codes.conflict:
422 result['data'] = "Invalid ID."
423 return result
424
425 def vnfd_create_pkg_base(self, token, pkg_name):
426 result = {'error': True, 'data': ''}
427 headers = {"Content-Type": "application/gzip", "accept": "application/json",
428 'Authorization': 'Bearer {}'.format(token['id'])}
429
430 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
431
432 try:
433 self._create_base_pkg('vnfd', pkg_name)
434 r = requests.post(_url, data=open('/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
435 except Exception as e:
436 log.exception(e)
437 result['data'] = str(e)
438 return result
439 if r.status_code == requests.codes.created:
440 result['data'] = r.json()
441 result['error'] = False
442 if r.status_code == requests.codes.conflict:
443 result['data'] = "Invalid ID."
444 return result
445
446 def nsd_clone(self, token, id):
447 result = {'error': True, 'data': ''}
448 headers = {"Content-Type": "application/gzip", "accept": "application/json",
449 'Authorization': 'Bearer {}'.format(token['id'])}
450
451 # get the package onboarded
452 tar_pkg = self.get_nsd_pkg(token, id)
453 tarf = tarfile.open(fileobj=tar_pkg)
454 tarf = self._descriptor_clone(tarf, 'nsd')
455 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
456
457 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
458
459 try:
460 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
461 headers=headers)
462 except Exception as e:
463 log.exception(e)
464 result['data'] = str(e)
465 return result
466 if r.status_code == requests.codes.created:
467 result['error'] = False
468 if r.status_code == requests.codes.conflict:
469 result['data'] = "Invalid ID."
470
471 return result
472
473 def vnfd_clone(self, token, id):
474 result = {'error': True, 'data': ''}
475 headers = {"Content-Type": "application/gzip", "accept": "application/json",
476 'Authorization': 'Bearer {}'.format(token['id'])}
477
478 # get the package onboarded
479 tar_pkg = self.get_vnfd_pkg(token, id)
480 tarf = tarfile.open(fileobj=tar_pkg)
481
482 tarf = self._descriptor_clone(tarf, 'vnfd')
483 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
484
485 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
486
487 try:
488 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
489 headers=headers)
490 except Exception as e:
491 log.exception(e)
492 result['data'] = str(e)
493 return result
494 if r.status_code == requests.codes.created:
495 result['error'] = False
496 if r.status_code == requests.codes.conflict:
497 result['data'] = "Invalid ID."
498
499 return result
500
501 def nsd_update(self, token, id, data):
502 result = {'error': True, 'data': ''}
503 headers = {"Content-Type": "application/gzip", "accept": "application/json",
504 'Authorization': 'Bearer {}'.format(token['id'])}
505
506 # get the package onboarded
507 tar_pkg = self.get_nsd_pkg(token, id)
508 tarf = tarfile.open(fileobj=tar_pkg)
509
510 tarf = self._descriptor_update(tarf, data)
511 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
512
513 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
514
515 try:
516 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
517 headers=headers)
518 except Exception as e:
519 log.exception(e)
520 result['data'] = str(e)
521 return result
522 if r.status_code == requests.codes.no_content:
523 result['error'] = False
524 else:
525 try:
526 result['data'] = r.json()
527 except Exception as e:
528 result['data'] = {}
529
530 return result
531
532 def vnfd_update(self, token, id, data):
533 result = {'error': True, 'data': ''}
534 headers = {"Content-Type": "application/gzip", "accept": "application/json",
535 'Authorization': 'Bearer {}'.format(token['id'])}
536
537 # get the package onboarded
538 tar_pkg = self.get_vnfd_pkg(token, id)
539 tarf = tarfile.open(fileobj=tar_pkg)
540
541 tarf = self._descriptor_update(tarf, data)
542 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
543
544 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
545
546 try:
547 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
548 headers=headers)
549 except Exception as e:
550 log.exception(e)
551 result['data'] = str(e)
552 return result
553 if r.status_code == requests.codes.no_content:
554 result['error'] = False
555 else:
556 try:
557 result['data'] = r.json()
558 except Exception as e:
559 result['data'] = {}
560
561 return result
562
563 def get_nsd_pkg(self, token, id):
564 result = {'error': True, 'data': ''}
565 headers = {"accept": "application/zip",
566 'Authorization': 'Bearer {}'.format(token['id'])}
567
568 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
569 try:
570 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
571 except Exception as e:
572 log.exception(e)
573 result['data'] = str(e)
574 return result
575 if r.status_code == requests.codes.ok:
576 result['error'] = False
577 tarf = StringIO.StringIO(r.content)
578 return tarf
579 return result
580
581 def get_vnfd_pkg(self, token, id):
582 result = {'error': True, 'data': ''}
583 headers = {"accept": "application/zip",
584 'Authorization': 'Bearer {}'.format(token['id'])}
585 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
586 try:
587 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
588 except Exception as e:
589 log.exception(e)
590 result['data'] = str(e)
591 return result
592 if r.status_code == requests.codes.ok:
593 result['error'] = False
594 tarf = StringIO.StringIO(r.content)
595 return tarf
596 return result
597
598 def _descriptor_update(self, tarf, data):
599 # extract the package on a tmp directory
600 tarf.extractall('/tmp')
601
602 for name in tarf.getnames():
603 if name.endswith(".yaml") or name.endswith(".yml"):
604 with open('/tmp/' + name, 'w') as outfile:
605 yaml.safe_dump(data, outfile, default_flow_style=False)
606 break
607
608 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + ".tar.gz", "w:gz")
609
610 for tarinfo in tarf:
611 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
612 tarf_temp.close()
613 return tarf
614
615 def _create_base_pkg(self, descriptor_type, pkg_name):
616 filename = '/tmp/'+pkg_name+'/' + pkg_name + '.yaml'
617 if descriptor_type == 'nsd':
618 descriptor = {
619 "nsd:nsd-catalog": {
620 "nsd": [
621 {
622 "short-name": str(pkg_name),
623 "vendor": "OSM Composer",
624 "description": str(pkg_name) + " descriptor",
625 "vld": [],
626 "constituent-vnfd": [],
627 "version": "1.0",
628 "id": str(pkg_name),
629 "name": str(pkg_name)
630 }
631 ]
632 }
633 }
634
635 elif descriptor_type == 'vnfd':
636 descriptor = {
637 "vnfd:vnfd-catalog": {
638 "vnfd": [
639 {
640 "short-name": str(pkg_name),
641 "vdu": [],
642 "description": "",
643 "mgmt-interface": {
644 "cp": ""
645 },
646 "id": str(pkg_name),
647 "version": "1.0",
648 "internal-vld": [],
649 "connection-point": [],
650 "name": str(pkg_name)
651 }
652 ]
653 }
654 }
655
656 if not os.path.exists(os.path.dirname(filename)):
657 try:
658 os.makedirs(os.path.dirname(filename))
659 except OSError as exc: # Guard against race condition
660 if exc.errno != errno.EEXIST:
661 raise
662
663 with open('/tmp/' + pkg_name + '/' + pkg_name + '.yaml', 'w') as yaml_file:
664 yaml_file.write(yaml.dump(descriptor, default_flow_style=False))
665
666 tarf_temp = tarfile.open('/tmp/' + pkg_name + '.tar.gz', "w:gz")
667 tarf_temp.add('/tmp/'+pkg_name+'/' + pkg_name + '.yaml', pkg_name + '/' + pkg_name + '.yaml', recursive=False)
668 tarf_temp.close()
669
670 def _descriptor_clone(self, tarf, descriptor_type):
671 # extract the package on a tmp directory
672 tarf.extractall('/tmp')
673
674 for name in tarf.getnames():
675 if name.endswith(".yaml") or name.endswith(".yml"):
676 with open('/tmp/' + name, 'r') as outfile:
677 yaml_object = yaml.load(outfile)
678
679 if descriptor_type == 'nsd':
680 nsd_list = yaml_object['nsd:nsd-catalog']['nsd']
681 for nsd in nsd_list:
682 nsd['id'] = 'clone_' + nsd['id']
683 nsd['name'] = 'clone_' + nsd['name']
684 nsd['short-name'] = 'clone_' + nsd['short-name']
685 elif descriptor_type == 'vnfd':
686 vnfd_list = yaml_object['vnfd:vnfd-catalog']['vnfd']
687 for vnfd in vnfd_list:
688 vnfd['id'] = 'clone_' + vnfd['id']
689 vnfd['name'] = 'clone_' + vnfd['name']
690 vnfd['short-name'] = 'clone_' + vnfd['short-name']
691
692 with open('/tmp/' + name, 'w') as yaml_file:
693 yaml_file.write(yaml.dump(yaml_object, default_flow_style=False))
694 break
695
696 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", "w:gz")
697
698 for tarinfo in tarf:
699 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
700 tarf_temp.close()
701 return tarf
702
703 def nsd_get(self, token, id):
704 result = {'error': True, 'data': ''}
705 headers = {'Content-Type': 'application/yaml',
706 'Authorization': 'Bearer {}'.format(token['id'])}
707 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self._base_path, id)
708 try:
709 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
710 except Exception as e:
711 log.exception(e)
712 result['data'] = str(e)
713 return result
714 if r.status_code == requests.codes.ok:
715 result['error'] = False
716 return yaml.load(r.text)
717 else:
718 try:
719 result['data'] = r.json()
720 except Exception as e:
721 result['data'] = {}
722 return result
723
724 def vnfd_get(self, token, id):
725 result = {'error': True, 'data': ''}
726 headers = {'Content-Type': 'application/yaml',
727 'Authorization': 'Bearer {}'.format(token['id'])}
728 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self._base_path, id)
729 try:
730 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
731 except Exception as e:
732 log.exception(e)
733 result['data'] = str(e)
734 return result
735 if r.status_code == requests.codes.ok:
736 result['error'] = False
737 return yaml.load(r.text)
738 else:
739 try:
740 result['data'] = r.json()
741 except Exception as e:
742 result['data'] = {}
743 return result
744
745 def nsd_artifacts(self, token, id):
746 result = {'error': True, 'data': ''}
747 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
748 'Authorization': 'Bearer {}'.format(token['id'])}
749 _url = "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self._base_path, id)
750 try:
751 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
752 except Exception as e:
753 log.exception(e)
754 result['data'] = str(e)
755 return result
756 if r.status_code == requests.codes.ok:
757 result['error'] = False
758 result['data'] = r.text
759 else:
760 try:
761 result['data'] = r.json()
762 except Exception as e:
763 result['data'] = {}
764
765 return result
766
767 def vnf_packages_artifacts(self, token, id):
768 result = {'error': True, 'data': ''}
769 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
770 'Authorization': 'Bearer {}'.format(token['id'])}
771 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self._base_path, id)
772 try:
773 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
774 except Exception as e:
775 log.exception(e)
776 result['data'] = str(e)
777 return result
778 if r.status_code == requests.codes.ok:
779 result['error'] = False
780 result['data'] = r.text
781 else:
782 try:
783 result['data'] = r.json()
784 except Exception as e:
785 result['data'] = {}
786
787 return result
788
789 def ns_create(self, token, ns_data):
790 result = {'error': True, 'data': ''}
791 headers = {"Content-Type": "application/yaml", "accept": "application/json",
792 'Authorization': 'Bearer {}'.format(token['id'])}
793
794 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
795
796 try:
797 r = requests.post(_url, json=ns_data, verify=False, headers=headers)
798 except Exception as e:
799 log.exception(e)
800 result['data'] = str(e)
801 return result
802 if r.status_code == requests.codes.ok:
803 result['error'] = False
804 result['data'] = Util.json_loads_byteified(r.text)
805 return result
806
807 def ns_op_list(self, token, id):
808 result = {'error': True, 'data': ''}
809 headers = {"Content-Type": "application/json", "accept": "application/json",
810 'Authorization': 'Bearer {}'.format(token['id'])}
811 _url = "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self._base_path, id)
812
813 try:
814 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
815 except Exception as e:
816 log.exception(e)
817 result['data'] = str(e)
818 return result
819 if r.status_code == requests.codes.ok:
820 result['error'] = False
821 result['data'] = Util.json_loads_byteified(r.text)
822
823 return result
824
825 def ns_op(self, token, id):
826 result = {'error': True, 'data': ''}
827 headers = {"Content-Type": "application/json", "accept": "application/json",
828 'Authorization': 'Bearer {}'.format(token['id'])}
829 _url = "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self._base_path, id)
830
831 try:
832 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
833 except Exception as e:
834 log.exception(e)
835 result['data'] = str(e)
836 return result
837 if r.status_code == requests.codes.ok:
838 result['error'] = False
839 result['data'] = Util.json_loads_byteified(r.text)
840
841 return result
842
843 def ns_action(self, token, id, action_payload):
844 result = {'error': True, 'data': ''}
845 headers = {"Content-Type": "application/json", "accept": "application/json",
846 'Authorization': 'Bearer {}'.format(token['id'])}
847
848 _url = "{0}/nslcm/v1/ns_instances/{1}/action".format(self._base_path, id)
849
850 try:
851 r = requests.post(_url, json=action_payload, verify=False, headers=headers)
852 except Exception as e:
853 log.exception(e)
854 result['data'] = str(e)
855 return result
856 if r.status_code == requests.codes.created:
857 result['error'] = False
858 result['data'] = Util.json_loads_byteified(r.text)
859 return result
860
861 def ns_delete(self, token, id, force=None):
862 result = {'error': True, 'data': ''}
863 headers = {"Content-Type": "application/yaml", "accept": "application/json",
864 'Authorization': 'Bearer {}'.format(token['id'])}
865 query_path = ''
866 if force:
867 query_path = '?FORCE=true'
868 _url = "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self._base_path, id, query_path)
869 try:
870 r = requests.delete(_url, params=None, verify=False, headers=headers)
871 except Exception as e:
872 log.exception(e)
873 result['data'] = str(e)
874 return result
875 if r:
876 result['error'] = False
877 if r.status_code != requests.codes.no_content:
878 result['data'] = Util.json_loads_byteified(r.text)
879 return result
880
881 def ns_get(self, token, id):
882 result = {'error': True, 'data': ''}
883 headers = {"Content-Type": "application/json", "accept": "application/json",
884 'Authorization': 'Bearer {}'.format(token['id'])}
885 _url = "{0}/nslcm/v1/ns_instances_content/{1}".format(self._base_path, id)
886
887 try:
888 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
889 except Exception as e:
890 log.exception(e)
891 result['data'] = str(e)
892 return result
893 if r.status_code == requests.codes.ok:
894 result['error'] = False
895 result['data'] = Util.json_loads_byteified(r.text)
896 return result
897
898 def vnf_get(self, token, id):
899 result = {'error': True, 'data': ''}
900 headers = {"Content-Type": "application/json", "accept": "application/json",
901 'Authorization': 'Bearer {}'.format(token['id'])}
902 _url = "{0}/nslcm/v1/vnfrs/{1}".format(self._base_path, id)
903
904 try:
905 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
906 except Exception as e:
907 log.exception(e)
908 result['data'] = str(e)
909 return result
910 if r.status_code == requests.codes.ok:
911 result['error'] = False
912 result['data'] = Util.json_loads_byteified(r.text)
913 return result
914
915 def ns_alarm_create(self, token, id, alarm_payload):
916 result = {'error': True, 'data': ''}
917 headers = {"Content-Type": "application/json",
918 'Authorization': 'Bearer {}'.format(token['id'])}
919 _url = "{0}/test/message/alarm_request".format(self._base_path)
920 try:
921 r = requests.post(_url, json=alarm_payload, verify=False, headers=headers)
922 except Exception as e:
923 log.exception(e)
924 result['data'] = str(e)
925 return result
926 if r.status_code == requests.codes.ok:
927 result['error'] = False
928 # result['data'] = Util.json_loads_byteified(r.text)
929 result['data'] = r.text
930 return result
931
932 def ns_metric_export(self, token, id, metric_payload):
933 result = {'error': True, 'data': ''}
934 headers = {"Content-Type": "application/json",
935 'Authorization': 'Bearer {}'.format(token['id'])}
936 _url = "{0}/test/message/metric_request".format(self._base_path)
937 try:
938 r = requests.post(_url, json=metric_payload, verify=False, headers=headers)
939 except Exception as e:
940 log.exception(e)
941 result['data'] = str(e)
942 return result
943 if r.status_code == requests.codes.ok:
944 result['error'] = False
945 # result['data'] = Util.json_loads_byteified(r.text)
946 result['data'] = r.text
947 return result
948
949 def vim_list(self, token):
950 result = {'error': True, 'data': ''}
951 headers = {"Content-Type": "application/yaml", "accept": "application/json",
952 'Authorization': 'Bearer {}'.format(token['id'])}
953 _url = "{0}/admin/v1/vims".format(self._base_path)
954 try:
955 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
956 except Exception as e:
957 log.exception(e)
958 result['data'] = str(e)
959 return result
960 if r.status_code == requests.codes.ok:
961 result['error'] = False
962 result['data'] = Util.json_loads_byteified(r.text)
963
964 return result
965
966 def vim_delete(self, token, id):
967 result = {'error': True, 'data': ''}
968 headers = {"accept": "application/json",
969 'Authorization': 'Bearer {}'.format(token['id'])}
970 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
971 try:
972 r = requests.delete(_url, params=None, verify=False, headers=headers)
973 except Exception as e:
974 log.exception(e)
975 result['data'] = str(e)
976 return result
977 if r.status_code == requests.codes.accepted:
978 result['error'] = False
979 else:
980 result['data'] = r.text
981 return result
982
983 def vim_get(self, token, id):
984
985 result = {'error': True, 'data': ''}
986 headers = {"Content-Type": "application/json", "accept": "application/json",
987 'Authorization': 'Bearer {}'.format(token['id'])}
988 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
989
990 try:
991 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
992 except Exception as e:
993 log.exception(e)
994 result['data'] = str(e)
995 return result
996 if r.status_code == requests.codes.ok:
997 result['error'] = False
998 result['data'] = Util.json_loads_byteified(r.text)
999 return result
1000
1001 def vim_create(self, token, vim_data):
1002
1003 result = {'error': True, 'data': ''}
1004 headers = {"Content-Type": "application/json", "accept": "application/json",
1005 'Authorization': 'Bearer {}'.format(token['id'])}
1006
1007 _url = "{0}/admin/v1/vims".format(self._base_path)
1008
1009 try:
1010 r = requests.post(_url, json=vim_data, verify=False, headers=headers)
1011 except Exception as e:
1012 log.exception(e)
1013 result['data'] = str(e)
1014 return result
1015 if r.status_code == requests.codes.created:
1016 result['error'] = False
1017 result['data'] = Util.json_loads_byteified(r.text)
1018 return result
1019
1020 def sdn_list(self, token):
1021 result = {'error': True, 'data': ''}
1022 headers = {"accept": "application/json",
1023 'Authorization': 'Bearer {}'.format(token['id'])}
1024 _url = "{0}/admin/v1/sdns".format(self._base_path)
1025 try:
1026 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1027 except Exception as e:
1028 log.exception(e)
1029 result['data'] = str(e)
1030 return result
1031 if r.status_code == requests.codes.ok:
1032 result['error'] = False
1033 result['data'] = Util.json_loads_byteified(r.text)
1034 return result
1035
1036 def sdn_delete(self, token, id):
1037 result = {'error': True, 'data': ''}
1038 headers = {"accept": "application/json",
1039 'Authorization': 'Bearer {}'.format(token['id'])}
1040 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
1041 try:
1042 r = requests.delete(_url, params=None, verify=False, headers=headers)
1043 except Exception as e:
1044 log.exception(e)
1045 result['data'] = str(e)
1046 return result
1047 if r.status_code == requests.codes.accepted:
1048 result['error'] = False
1049 else:
1050 result['data'] = r.text
1051 return result
1052
1053 def sdn_get(self, token, id):
1054 result = {'error': True, 'data': ''}
1055 headers = {"accept": "application/json",
1056 'Authorization': 'Bearer {}'.format(token['id'])}
1057 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
1058
1059 try:
1060 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1061 except Exception as e:
1062 log.exception(e)
1063 result['data'] = str(e)
1064 return result
1065 if r.status_code == requests.codes.ok:
1066 result['error'] = False
1067 result['data'] = Util.json_loads_byteified(r.text)
1068 return result
1069
1070 def sdn_create(self, token, sdn_data):
1071 result = {'error': True, 'data': ''}
1072 headers = {"Content-Type": "application/json", "accept": "application/json",
1073 'Authorization': 'Bearer {}'.format(token['id'])}
1074
1075 _url = "{0}/admin/v1/sdns".format(self._base_path)
1076
1077 try:
1078 r = requests.post(_url, json=sdn_data, verify=False, headers=headers)
1079 except Exception as e:
1080 log.exception(e)
1081 result['data'] = str(e)
1082 return result
1083 if r.status_code == requests.codes.created:
1084 result['error'] = False
1085 result['data'] = Util.json_loads_byteified(r.text)
1086 return result
1087
1088 @staticmethod
1089 def md5(f):
1090 hash_md5 = hashlib.md5()
1091 for chunk in iter(lambda: f.read(1024), b""):
1092 hash_md5.update(chunk)
1093 return hash_md5.hexdigest()