bug fix on NSD composer; enabling button compose new descriptor
[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):
255 result = {'error': True, 'data': ''}
256 headers = {"Content-Type": "application/yaml", "accept": "application/json",
257 'Authorization': 'Bearer {}'.format(token['id'])}
258
259 _url = "{0}/nsd/v1/ns_descriptors_content".format(self._base_path)
260 try:
261 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
262 except Exception as e:
263 log.exception(e)
264 result['data'] = str(e)
265 return result
266 if r.status_code == requests.codes.ok:
267 result['error'] = False
268 result['data'] = Util.json_loads_byteified(r.text)
269
270 return result
271
272 def vnfd_list(self, token):
273 result = {'error': True, 'data': ''}
274 headers = {"Content-Type": "application/yaml", "accept": "application/json",
275 'Authorization': 'Bearer {}'.format(token['id'])}
276
277 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
278 try:
279 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
280 except Exception as e:
281 log.exception(e)
282 result['data'] = str(e)
283 return result
284 if r.status_code == requests.codes.ok:
285 result['error'] = False
286 result['data'] = Util.json_loads_byteified(r.text)
287
288 return result
289
290 def ns_list(self, token):
291 result = {'error': True, 'data': ''}
292 headers = {"Content-Type": "application/yaml", "accept": "application/json",
293 'Authorization': 'Bearer {}'.format(token['id'])}
294 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
295 try:
296 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
297 except Exception as e:
298 log.exception(e)
299 result['data'] = str(e)
300 return result
301 if r.status_code == requests.codes.ok:
302 result['error'] = False
303 result['data'] = Util.json_loads_byteified(r.text)
304
305 return result
306
307 def vnf_list(self, token):
308 result = {'error': True, 'data': ''}
309 headers = {"Content-Type": "application/yaml", "accept": "application/json",
310 'Authorization': 'Bearer {}'.format(token['id'])}
311 _url = "{0}/nslcm/v1/vnfrs".format(self._base_path)
312 try:
313 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
314 except Exception as e:
315 log.exception(e)
316 result['data'] = str(e)
317 return result
318 if r.status_code == requests.codes.ok:
319 result['error'] = False
320 result['data'] = Util.json_loads_byteified(r.text)
321
322 return result
323
324 def nsd_delete(self, token, id):
325 result = {'error': True, 'data': ''}
326 headers = {"Content-Type": "application/yaml", "accept": "application/json",
327 'Authorization': 'Bearer {}'.format(token['id'])}
328
329 _url = "{0}/nsd/v1/ns_descriptors_content/{1}".format(self._base_path, id)
330 try:
331 r = requests.delete(_url, params=None, verify=False, headers=headers)
332 except Exception as e:
333 log.exception(e)
334 result['data'] = str(e)
335 return result
336 if r.status_code == requests.codes.ok:
337 result['error'] = False
338 result['data'] = Util.json_loads_byteified(r.text)
339 return result
340
341 def vnfd_delete(self, token, id):
342 result = {'error': True, 'data': ''}
343 headers = {"Content-Type": "application/yaml", "accept": "application/json",
344 'Authorization': 'Bearer {}'.format(token['id'])}
345
346 _url = "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self._base_path, id)
347 try:
348 r = requests.delete(_url, params=None, verify=False, headers=headers)
349 except Exception as e:
350 log.exception(e)
351 result['data'] = str(e)
352 return result
353 if r:
354 result['error'] = False
355 if r.status_code != requests.codes.no_content:
356 result['data'] = Util.json_loads_byteified(r.text)
357 return result
358
359 def nsd_onboard(self, token, package):
360 result = {'error': True, 'data': ''}
361 headers = {"Content-Type": "application/gzip", "accept": "application/json",
362 'Authorization': 'Bearer {}'.format(token['id'])}
363 with open('/tmp/' + package.name, 'wb+') as destination:
364 for chunk in package.chunks():
365 destination.write(chunk)
366 headers['Content-File-MD5'] = self.md5(open('/tmp/' + package.name, 'rb'))
367 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
368 try:
369 r = requests.post(_url, data=open('/tmp/' + package.name, 'rb'), verify=False, headers=headers)
370 except Exception as e:
371 log.exception(e)
372 result['data'] = str(e)
373 return result
374 if r.status_code == requests.codes.created:
375 result['error'] = False
376 result['data'] = Util.json_loads_byteified(r.text)
377 return result
378
379 def vnfd_onboard(self, token, package):
380 result = {'error': True, 'data': ''}
381 headers = {"Content-Type": "application/gzip", "accept": "application/json",
382 'Authorization': 'Bearer {}'.format(token['id'])}
383 with open('/tmp/' + package.name, 'wb+') as destination:
384 for chunk in package.chunks():
385 destination.write(chunk)
386 headers['Content-File-MD5'] = self.md5(open('/tmp/' + package.name, 'rb'))
387 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
388 try:
389 r = requests.post(_url, data=open('/tmp/' + package.name, 'rb'), verify=False, headers=headers)
390 except Exception as e:
391 log.exception(e)
392 result['data'] = str(e)
393 return result
394 if r.status_code == requests.codes.created:
395 result['error'] = False
396 result['data'] = Util.json_loads_byteified(r.text)
397 return result
398
399 def nsd_create_pkg_base(self, token, pkg_name):
400 result = {'error': True, 'data': ''}
401 headers = {"Content-Type": "application/gzip", "accept": "application/json",
402 'Authorization': 'Bearer {}'.format(token['id'])}
403
404 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
405
406 try:
407 self._create_base_pkg('nsd', pkg_name)
408 r = requests.post(_url, data=open('/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
409 except Exception as e:
410 log.exception(e)
411 result['data'] = str(e)
412 return result
413 if r.status_code == requests.codes.created:
414 result['data'] = r.json()
415 result['error'] = False
416 if r.status_code == requests.codes.conflict:
417 result['data'] = "Invalid ID."
418 return result
419
420 def vnfd_create_pkg_base(self, token, pkg_name):
421 result = {'error': True, 'data': ''}
422 headers = {"Content-Type": "application/gzip", "accept": "application/json",
423 'Authorization': 'Bearer {}'.format(token['id'])}
424
425 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
426
427 try:
428 self._create_base_pkg('vnfd', pkg_name)
429 r = requests.post(_url, data=open('/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
430 except Exception as e:
431 log.exception(e)
432 result['data'] = str(e)
433 return result
434 if r.status_code == requests.codes.created:
435 result['data'] = r.json()
436 result['error'] = False
437 if r.status_code == requests.codes.conflict:
438 result['data'] = "Invalid ID."
439 return result
440
441 def nsd_clone(self, token, id):
442 result = {'error': True, 'data': ''}
443 headers = {"Content-Type": "application/gzip", "accept": "application/json",
444 'Authorization': 'Bearer {}'.format(token['id'])}
445
446 # get the package onboarded
447 tar_pkg = self.get_nsd_pkg(token, id)
448 tarf = tarfile.open(fileobj=tar_pkg)
449 tarf = self._descriptor_clone(tarf, 'nsd')
450 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
451
452 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
453
454 try:
455 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
456 headers=headers)
457 except Exception as e:
458 log.exception(e)
459 result['data'] = str(e)
460 return result
461 if r.status_code == requests.codes.created:
462 result['error'] = False
463 if r.status_code == requests.codes.conflict:
464 result['data'] = "Invalid ID."
465
466 return result
467
468 def vnfd_clone(self, token, id):
469 result = {'error': True, 'data': ''}
470 headers = {"Content-Type": "application/gzip", "accept": "application/json",
471 'Authorization': 'Bearer {}'.format(token['id'])}
472
473 # get the package onboarded
474 tar_pkg = self.get_vnfd_pkg(token, id)
475 tarf = tarfile.open(fileobj=tar_pkg)
476
477 tarf = self._descriptor_clone(tarf, 'vnfd')
478 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
479
480 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
481
482 try:
483 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
484 headers=headers)
485 except Exception as e:
486 log.exception(e)
487 result['data'] = str(e)
488 return result
489 if r.status_code == requests.codes.created:
490 result['error'] = False
491 if r.status_code == requests.codes.conflict:
492 result['data'] = "Invalid ID."
493
494 return result
495
496 def nsd_update(self, token, id, data):
497 result = {'error': True, 'data': ''}
498 headers = {"Content-Type": "application/gzip", "accept": "application/json",
499 'Authorization': 'Bearer {}'.format(token['id'])}
500
501 # get the package onboarded
502 tar_pkg = self.get_nsd_pkg(token, id)
503 tarf = tarfile.open(fileobj=tar_pkg)
504
505 tarf = self._descriptor_update(tarf, data)
506 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
507
508 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
509
510 try:
511 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
512 headers=headers)
513 except Exception as e:
514 log.exception(e)
515 result['data'] = str(e)
516 return result
517 if r.status_code == requests.codes.no_content:
518 result['error'] = False
519
520 return result
521
522 def vnfd_update(self, token, id, data):
523 result = {'error': True, 'data': ''}
524 headers = {"Content-Type": "application/gzip", "accept": "application/json",
525 'Authorization': 'Bearer {}'.format(token['id'])}
526
527 # get the package onboarded
528 tar_pkg = self.get_vnfd_pkg(token, id)
529 tarf = tarfile.open(fileobj=tar_pkg)
530
531 tarf = self._descriptor_update(tarf, data)
532 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
533
534 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
535
536 try:
537 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
538 headers=headers)
539 except Exception as e:
540 log.exception(e)
541 result['data'] = str(e)
542 return result
543 if r.status_code == requests.codes.no_content:
544 result['error'] = False
545
546 return result
547
548 def get_nsd_pkg(self, token, id):
549 result = {'error': True, 'data': ''}
550 headers = {"accept": "application/zip",
551 'Authorization': 'Bearer {}'.format(token['id'])}
552
553 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
554 try:
555 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
556 except Exception as e:
557 log.exception(e)
558 result['data'] = str(e)
559 return result
560 if r.status_code == requests.codes.ok:
561 result['error'] = False
562 tarf = StringIO.StringIO(r.content)
563 return tarf
564 return result
565
566 def get_vnfd_pkg(self, token, id):
567 result = {'error': True, 'data': ''}
568 headers = {"accept": "application/zip",
569 'Authorization': 'Bearer {}'.format(token['id'])}
570 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
571 try:
572 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
573 except Exception as e:
574 log.exception(e)
575 result['data'] = str(e)
576 return result
577 if r.status_code == requests.codes.ok:
578 result['error'] = False
579 tarf = StringIO.StringIO(r.content)
580 return tarf
581 return result
582
583 def _descriptor_update(self, tarf, data):
584 # extract the package on a tmp directory
585 tarf.extractall('/tmp')
586
587 for name in tarf.getnames():
588 if name.endswith(".yaml") or name.endswith(".yml"):
589 with open('/tmp/' + name, 'w') as outfile:
590 yaml.safe_dump(data, outfile, default_flow_style=False)
591 break
592
593 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + ".tar.gz", "w:gz")
594
595 for tarinfo in tarf:
596 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
597 tarf_temp.close()
598 return tarf
599
600 def _create_base_pkg(self, descriptor_type, pkg_name):
601 filename = '/tmp/'+pkg_name+'/' + pkg_name + '.yaml'
602 if descriptor_type == 'nsd':
603 descriptor = {
604 "nsd:nsd-catalog": {
605 "nsd": [
606 {
607 "short-name": str(pkg_name),
608 "vendor": "OSM Composer",
609 "description": str(pkg_name) + " descriptor",
610 "vld": [],
611 "constituent-vnfd": [],
612 "version": "1.0",
613 "id": str(pkg_name),
614 "name": str(pkg_name)
615 }
616 ]
617 }
618 }
619
620 elif descriptor_type == 'vnfd':
621 descriptor = {
622 "vnfd:vnfd-catalog": {
623 "vnfd": [
624 {
625 "short-name": str(pkg_name),
626 "vdu": [],
627 "description": "",
628 "mgmt-interface": {},
629 "id": str(pkg_name),
630 "version": "1.0",
631 "internal-vld": [],
632 "connection-point": [],
633 "name": str(pkg_name)
634 }
635 ]
636 }
637 }
638
639 if not os.path.exists(os.path.dirname(filename)):
640 try:
641 os.makedirs(os.path.dirname(filename))
642 except OSError as exc: # Guard against race condition
643 if exc.errno != errno.EEXIST:
644 raise
645
646 with open('/tmp/' + pkg_name + '/' + pkg_name + '.yaml', 'w') as yaml_file:
647 yaml_file.write(yaml.dump(descriptor, default_flow_style=False))
648
649 tarf_temp = tarfile.open('/tmp/' + pkg_name + '.tar.gz', "w:gz")
650 tarf_temp.add('/tmp/'+pkg_name+'/' + pkg_name + '.yaml', pkg_name + '/' + pkg_name + '.yaml', recursive=False)
651 tarf_temp.close()
652
653 def _descriptor_clone(self, tarf, descriptor_type):
654 # extract the package on a tmp directory
655 tarf.extractall('/tmp')
656
657 for name in tarf.getnames():
658 if name.endswith(".yaml") or name.endswith(".yml"):
659 with open('/tmp/' + name, 'r') as outfile:
660 yaml_object = yaml.load(outfile)
661
662 if descriptor_type == 'nsd':
663 nsd_list = yaml_object['nsd:nsd-catalog']['nsd']
664 for nsd in nsd_list:
665 nsd['id'] = 'clone_' + nsd['id']
666 nsd['name'] = 'clone_' + nsd['name']
667 nsd['short-name'] = 'clone_' + nsd['short-name']
668 elif descriptor_type == 'vnfd':
669 vnfd_list = yaml_object['vnfd:vnfd-catalog']['vnfd']
670 for vnfd in vnfd_list:
671 vnfd['id'] = 'clone_' + vnfd['id']
672 vnfd['name'] = 'clone_' + vnfd['name']
673 vnfd['short-name'] = 'clone_' + vnfd['short-name']
674
675 with open('/tmp/' + name, 'w') as yaml_file:
676 yaml_file.write(yaml.dump(yaml_object, default_flow_style=False))
677 break
678
679 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", "w:gz")
680
681 for tarinfo in tarf:
682 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
683 tarf_temp.close()
684 return tarf
685
686 def nsd_get(self, token, id):
687 result = {'error': True, 'data': ''}
688 headers = {'Content-Type': 'application/yaml',
689 'Authorization': 'Bearer {}'.format(token['id'])}
690 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self._base_path, id)
691 try:
692 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
693 except Exception as e:
694 log.exception(e)
695 result['data'] = str(e)
696 return result
697 if r.status_code == requests.codes.ok:
698 result['error'] = False
699 return yaml.load(r.text)
700 else:
701 try:
702 result['data'] = r.json()
703 except Exception as e:
704 result['data'] = {}
705 return result
706
707 def vnfd_get(self, token, id):
708 result = {'error': True, 'data': ''}
709 headers = {'Content-Type': 'application/yaml',
710 'Authorization': 'Bearer {}'.format(token['id'])}
711 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self._base_path, id)
712 try:
713 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
714 except Exception as e:
715 log.exception(e)
716 result['data'] = str(e)
717 return result
718 if r.status_code == requests.codes.ok:
719 result['error'] = False
720 return yaml.load(r.text)
721 else:
722 try:
723 result['data'] = r.json()
724 except Exception as e:
725 result['data'] = {}
726 return result
727
728 def nsd_artifacts(self, token, id):
729 result = {'error': True, 'data': ''}
730 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
731 'Authorization': 'Bearer {}'.format(token['id'])}
732 _url = "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self._base_path, id)
733 try:
734 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
735 except Exception as e:
736 log.exception(e)
737 result['data'] = str(e)
738 return result
739 if r.status_code == requests.codes.ok:
740 result['error'] = False
741 result['data'] = r.text
742 else:
743 try:
744 result['data'] = r.json()
745 except Exception as e:
746 result['data'] = {}
747
748 return result
749
750 def vnf_packages_artifacts(self, token, id):
751 result = {'error': True, 'data': ''}
752 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
753 'Authorization': 'Bearer {}'.format(token['id'])}
754 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self._base_path, id)
755 try:
756 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
757 except Exception as e:
758 log.exception(e)
759 result['data'] = str(e)
760 return result
761 if r.status_code == requests.codes.ok:
762 result['error'] = False
763 result['data'] = r.text
764 else:
765 try:
766 result['data'] = r.json()
767 except Exception as e:
768 result['data'] = {}
769
770 return result
771
772 def ns_create(self, token, ns_data):
773 result = {'error': True, 'data': ''}
774 headers = {"Content-Type": "application/yaml", "accept": "application/json",
775 'Authorization': 'Bearer {}'.format(token['id'])}
776
777 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
778
779 try:
780 r = requests.post(_url, json=ns_data, verify=False, headers=headers)
781 except Exception as e:
782 log.exception(e)
783 result['data'] = str(e)
784 return result
785 if r.status_code == requests.codes.ok:
786 result['error'] = False
787 result['data'] = Util.json_loads_byteified(r.text)
788 return result
789
790 def ns_op_list(self, token, id):
791 result = {'error': True, 'data': ''}
792 headers = {"Content-Type": "application/json", "accept": "application/json",
793 'Authorization': 'Bearer {}'.format(token['id'])}
794 _url = "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self._base_path, id)
795
796 try:
797 r = requests.get(_url, params=None, verify=False, stream=True, 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
806 return result
807
808 def ns_op(self, token, id):
809 result = {'error': True, 'data': ''}
810 headers = {"Content-Type": "application/json", "accept": "application/json",
811 'Authorization': 'Bearer {}'.format(token['id'])}
812 _url = "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self._base_path, id)
813
814 try:
815 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
816 except Exception as e:
817 log.exception(e)
818 result['data'] = str(e)
819 return result
820 if r.status_code == requests.codes.ok:
821 result['error'] = False
822 result['data'] = Util.json_loads_byteified(r.text)
823
824 return result
825
826 def ns_action(self, token, id, action_payload):
827 result = {'error': True, 'data': ''}
828 headers = {"Content-Type": "application/json", "accept": "application/json",
829 'Authorization': 'Bearer {}'.format(token['id'])}
830
831 _url = "{0}/nslcm/v1/ns_instances/{1}/action".format(self._base_path, id)
832
833 try:
834 r = requests.post(_url, json=action_payload, verify=False, headers=headers)
835 except Exception as e:
836 log.exception(e)
837 result['data'] = str(e)
838 return result
839 if r.status_code == requests.codes.created:
840 result['error'] = False
841 result['data'] = Util.json_loads_byteified(r.text)
842 return result
843
844 def ns_delete(self, token, id, force=None):
845 result = {'error': True, 'data': ''}
846 headers = {"Content-Type": "application/yaml", "accept": "application/json",
847 'Authorization': 'Bearer {}'.format(token['id'])}
848 query_path = ''
849 if force:
850 query_path = '?FORCE=true'
851 _url = "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self._base_path, id, query_path)
852 try:
853 r = requests.delete(_url, params=None, verify=False, headers=headers)
854 except Exception as e:
855 log.exception(e)
856 result['data'] = str(e)
857 return result
858 if r:
859 result['error'] = False
860 if r.status_code != requests.codes.no_content:
861 result['data'] = Util.json_loads_byteified(r.text)
862 return result
863
864 def ns_get(self, token, id):
865 result = {'error': True, 'data': ''}
866 headers = {"Content-Type": "application/json", "accept": "application/json",
867 'Authorization': 'Bearer {}'.format(token['id'])}
868 _url = "{0}/nslcm/v1/ns_instances_content/{1}".format(self._base_path, id)
869
870 try:
871 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
872 except Exception as e:
873 log.exception(e)
874 result['data'] = str(e)
875 return result
876 if r.status_code == requests.codes.ok:
877 result['error'] = False
878 result['data'] = Util.json_loads_byteified(r.text)
879 return result
880
881 def vnf_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/vnfrs/{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 ns_alarm_create(self, token, id, alarm_payload):
899 result = {'error': True, 'data': ''}
900 headers = {"Content-Type": "application/json",
901 'Authorization': 'Bearer {}'.format(token['id'])}
902 _url = "{0}/test/message/alarm_request".format(self._base_path)
903 try:
904 r = requests.post(_url, json=alarm_payload, verify=False, headers=headers)
905 except Exception as e:
906 log.exception(e)
907 result['data'] = str(e)
908 return result
909 if r.status_code == requests.codes.ok:
910 result['error'] = False
911 # result['data'] = Util.json_loads_byteified(r.text)
912 result['data'] = r.text
913 return result
914
915 def ns_metric_export(self, token, id, metric_payload):
916 result = {'error': True, 'data': ''}
917 headers = {"Content-Type": "application/json",
918 'Authorization': 'Bearer {}'.format(token['id'])}
919 _url = "{0}/test/message/metric_request".format(self._base_path)
920 try:
921 r = requests.post(_url, json=metric_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 vim_list(self, token):
933 result = {'error': True, 'data': ''}
934 headers = {"Content-Type": "application/yaml", "accept": "application/json",
935 'Authorization': 'Bearer {}'.format(token['id'])}
936 _url = "{0}/admin/v1/vims".format(self._base_path)
937 try:
938 r = requests.get(_url, params=None, verify=False, stream=True, 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
947 return result
948
949 def vim_delete(self, token, id):
950 result = {'error': True, 'data': ''}
951 headers = {"accept": "application/json",
952 'Authorization': 'Bearer {}'.format(token['id'])}
953 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
954 try:
955 r = requests.delete(_url, params=None, verify=False, 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.accepted:
961 result['error'] = False
962 else:
963 result['data'] = r.text
964 return result
965
966 def vim_get(self, token, id):
967
968 result = {'error': True, 'data': ''}
969 headers = {"Content-Type": "application/json", "accept": "application/json",
970 'Authorization': 'Bearer {}'.format(token['id'])}
971 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
972
973 try:
974 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
975 except Exception as e:
976 log.exception(e)
977 result['data'] = str(e)
978 return result
979 if r.status_code == requests.codes.ok:
980 result['error'] = False
981 result['data'] = Util.json_loads_byteified(r.text)
982 return result
983
984 def vim_create(self, token, vim_data):
985
986 result = {'error': True, 'data': ''}
987 headers = {"Content-Type": "application/json", "accept": "application/json",
988 'Authorization': 'Bearer {}'.format(token['id'])}
989
990 _url = "{0}/admin/v1/vims".format(self._base_path)
991
992 try:
993 r = requests.post(_url, json=vim_data, verify=False, headers=headers)
994 except Exception as e:
995 log.exception(e)
996 result['data'] = str(e)
997 return result
998 if r.status_code == requests.codes.created:
999 result['error'] = False
1000 result['data'] = Util.json_loads_byteified(r.text)
1001 return result
1002
1003 def sdn_list(self, token):
1004 result = {'error': True, 'data': ''}
1005 headers = {"accept": "application/json",
1006 'Authorization': 'Bearer {}'.format(token['id'])}
1007 _url = "{0}/admin/v1/sdns".format(self._base_path)
1008 try:
1009 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1010 except Exception as e:
1011 log.exception(e)
1012 result['data'] = str(e)
1013 return result
1014 if r.status_code == requests.codes.ok:
1015 result['error'] = False
1016 result['data'] = Util.json_loads_byteified(r.text)
1017 return result
1018
1019 def sdn_delete(self, token, id):
1020 result = {'error': True, 'data': ''}
1021 headers = {"accept": "application/json",
1022 'Authorization': 'Bearer {}'.format(token['id'])}
1023 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
1024 try:
1025 r = requests.delete(_url, params=None, verify=False, headers=headers)
1026 except Exception as e:
1027 log.exception(e)
1028 result['data'] = str(e)
1029 return result
1030 if r.status_code == requests.codes.accepted:
1031 result['error'] = False
1032 else:
1033 result['data'] = r.text
1034 return result
1035
1036 def sdn_get(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
1042 try:
1043 r = requests.get(_url, params=None, verify=False, 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 == requests.codes.ok:
1049 result['error'] = False
1050 result['data'] = Util.json_loads_byteified(r.text)
1051 return result
1052
1053 def sdn_create(self, token, sdn_data):
1054 result = {'error': True, 'data': ''}
1055 headers = {"Content-Type": "application/json", "accept": "application/json",
1056 'Authorization': 'Bearer {}'.format(token['id'])}
1057
1058 _url = "{0}/admin/v1/sdns".format(self._base_path)
1059
1060 try:
1061 r = requests.post(_url, json=sdn_data, verify=False, headers=headers)
1062 except Exception as e:
1063 log.exception(e)
1064 result['data'] = str(e)
1065 return result
1066 if r.status_code == requests.codes.created:
1067 result['error'] = False
1068 result['data'] = Util.json_loads_byteified(r.text)
1069 return result
1070
1071 @staticmethod
1072 def md5(f):
1073 hash_md5 = hashlib.md5()
1074 for chunk in iter(lambda: f.read(1024), b""):
1075 hash_md5.update(chunk)
1076 return hash_md5.hexdigest()