fix error handling nsd/vnd composer; promt for cp-ref
[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 headers['Content-Filename'] = pkg_name + '.tar.gz'
409 r = requests.post(_url, data=open('/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
410 except Exception as e:
411 log.exception(e)
412 result['data'] = str(e)
413 return result
414 if r.status_code == requests.codes.created:
415 result['data'] = r.json()
416 result['error'] = False
417 if r.status_code == requests.codes.conflict:
418 result['data'] = "Invalid ID."
419 return result
420
421 def vnfd_create_pkg_base(self, token, pkg_name):
422 result = {'error': True, 'data': ''}
423 headers = {"Content-Type": "application/gzip", "accept": "application/json",
424 'Authorization': 'Bearer {}'.format(token['id'])}
425
426 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
427
428 try:
429 self._create_base_pkg('vnfd', pkg_name)
430 r = requests.post(_url, data=open('/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
431 except Exception as e:
432 log.exception(e)
433 result['data'] = str(e)
434 return result
435 if r.status_code == requests.codes.created:
436 result['data'] = r.json()
437 result['error'] = False
438 if r.status_code == requests.codes.conflict:
439 result['data'] = "Invalid ID."
440 return result
441
442 def nsd_clone(self, token, id):
443 result = {'error': True, 'data': ''}
444 headers = {"Content-Type": "application/gzip", "accept": "application/json",
445 'Authorization': 'Bearer {}'.format(token['id'])}
446
447 # get the package onboarded
448 tar_pkg = self.get_nsd_pkg(token, id)
449 tarf = tarfile.open(fileobj=tar_pkg)
450 tarf = self._descriptor_clone(tarf, 'nsd')
451 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
452
453 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
454
455 try:
456 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
457 headers=headers)
458 except Exception as e:
459 log.exception(e)
460 result['data'] = str(e)
461 return result
462 if r.status_code == requests.codes.created:
463 result['error'] = False
464 if r.status_code == requests.codes.conflict:
465 result['data'] = "Invalid ID."
466
467 return result
468
469 def vnfd_clone(self, token, id):
470 result = {'error': True, 'data': ''}
471 headers = {"Content-Type": "application/gzip", "accept": "application/json",
472 'Authorization': 'Bearer {}'.format(token['id'])}
473
474 # get the package onboarded
475 tar_pkg = self.get_vnfd_pkg(token, id)
476 tarf = tarfile.open(fileobj=tar_pkg)
477
478 tarf = self._descriptor_clone(tarf, 'vnfd')
479 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
480
481 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
482
483 try:
484 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
485 headers=headers)
486 except Exception as e:
487 log.exception(e)
488 result['data'] = str(e)
489 return result
490 if r.status_code == requests.codes.created:
491 result['error'] = False
492 if r.status_code == requests.codes.conflict:
493 result['data'] = "Invalid ID."
494
495 return result
496
497 def nsd_update(self, token, id, data):
498 result = {'error': True, 'data': ''}
499 headers = {"Content-Type": "application/gzip", "accept": "application/json",
500 'Authorization': 'Bearer {}'.format(token['id'])}
501
502 # get the package onboarded
503 tar_pkg = self.get_nsd_pkg(token, id)
504 tarf = tarfile.open(fileobj=tar_pkg)
505
506 tarf = self._descriptor_update(tarf, data)
507 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
508
509 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
510
511 try:
512 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
513 headers=headers)
514 except Exception as e:
515 log.exception(e)
516 result['data'] = str(e)
517 return result
518 if r.status_code == requests.codes.no_content:
519 result['error'] = False
520 else:
521 try:
522 result['data'] = r.json()
523 except Exception as e:
524 result['data'] = {}
525
526 return result
527
528 def vnfd_update(self, token, id, data):
529 result = {'error': True, 'data': ''}
530 headers = {"Content-Type": "application/gzip", "accept": "application/json",
531 'Authorization': 'Bearer {}'.format(token['id'])}
532
533 # get the package onboarded
534 tar_pkg = self.get_vnfd_pkg(token, id)
535 tarf = tarfile.open(fileobj=tar_pkg)
536
537 tarf = self._descriptor_update(tarf, data)
538 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
539
540 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
541
542 try:
543 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
544 headers=headers)
545 except Exception as e:
546 log.exception(e)
547 result['data'] = str(e)
548 return result
549 if r.status_code == requests.codes.no_content:
550 result['error'] = False
551 else:
552 try:
553 result['data'] = r.json()
554 except Exception as e:
555 result['data'] = {}
556
557 return result
558
559 def get_nsd_pkg(self, token, id):
560 result = {'error': True, 'data': ''}
561 headers = {"accept": "application/zip",
562 'Authorization': 'Bearer {}'.format(token['id'])}
563
564 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
565 try:
566 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
567 except Exception as e:
568 log.exception(e)
569 result['data'] = str(e)
570 return result
571 if r.status_code == requests.codes.ok:
572 result['error'] = False
573 tarf = StringIO.StringIO(r.content)
574 return tarf
575 return result
576
577 def get_vnfd_pkg(self, token, id):
578 result = {'error': True, 'data': ''}
579 headers = {"accept": "application/zip",
580 'Authorization': 'Bearer {}'.format(token['id'])}
581 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
582 try:
583 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
584 except Exception as e:
585 log.exception(e)
586 result['data'] = str(e)
587 return result
588 if r.status_code == requests.codes.ok:
589 result['error'] = False
590 tarf = StringIO.StringIO(r.content)
591 return tarf
592 return result
593
594 def _descriptor_update(self, tarf, data):
595 # extract the package on a tmp directory
596 tarf.extractall('/tmp')
597
598 for name in tarf.getnames():
599 if name.endswith(".yaml") or name.endswith(".yml"):
600 with open('/tmp/' + name, 'w') as outfile:
601 yaml.safe_dump(data, outfile, default_flow_style=False)
602 break
603
604 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + ".tar.gz", "w:gz")
605
606 for tarinfo in tarf:
607 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
608 tarf_temp.close()
609 return tarf
610
611 def _create_base_pkg(self, descriptor_type, pkg_name):
612 filename = '/tmp/'+pkg_name+'/' + pkg_name + '.yaml'
613 if descriptor_type == 'nsd':
614 descriptor = {
615 "nsd:nsd-catalog": {
616 "nsd": [
617 {
618 "short-name": str(pkg_name),
619 "vendor": "OSM Composer",
620 "description": str(pkg_name) + " descriptor",
621 "vld": [],
622 "constituent-vnfd": [],
623 "version": "1.0",
624 "id": str(pkg_name),
625 "name": str(pkg_name)
626 }
627 ]
628 }
629 }
630
631 elif descriptor_type == 'vnfd':
632 descriptor = {
633 "vnfd:vnfd-catalog": {
634 "vnfd": [
635 {
636 "short-name": str(pkg_name),
637 "vdu": [],
638 "description": "",
639 "mgmt-interface": {
640 "cp": ""
641 },
642 "id": str(pkg_name),
643 "version": "1.0",
644 "internal-vld": [],
645 "connection-point": [],
646 "name": str(pkg_name)
647 }
648 ]
649 }
650 }
651
652 if not os.path.exists(os.path.dirname(filename)):
653 try:
654 os.makedirs(os.path.dirname(filename))
655 except OSError as exc: # Guard against race condition
656 if exc.errno != errno.EEXIST:
657 raise
658
659 with open('/tmp/' + pkg_name + '/' + pkg_name + '.yaml', 'w') as yaml_file:
660 yaml_file.write(yaml.dump(descriptor, default_flow_style=False))
661
662 tarf_temp = tarfile.open('/tmp/' + pkg_name + '.tar.gz', "w:gz")
663 tarf_temp.add('/tmp/'+pkg_name+'/' + pkg_name + '.yaml', pkg_name + '/' + pkg_name + '.yaml', recursive=False)
664 tarf_temp.close()
665
666 def _descriptor_clone(self, tarf, descriptor_type):
667 # extract the package on a tmp directory
668 tarf.extractall('/tmp')
669
670 for name in tarf.getnames():
671 if name.endswith(".yaml") or name.endswith(".yml"):
672 with open('/tmp/' + name, 'r') as outfile:
673 yaml_object = yaml.load(outfile)
674
675 if descriptor_type == 'nsd':
676 nsd_list = yaml_object['nsd:nsd-catalog']['nsd']
677 for nsd in nsd_list:
678 nsd['id'] = 'clone_' + nsd['id']
679 nsd['name'] = 'clone_' + nsd['name']
680 nsd['short-name'] = 'clone_' + nsd['short-name']
681 elif descriptor_type == 'vnfd':
682 vnfd_list = yaml_object['vnfd:vnfd-catalog']['vnfd']
683 for vnfd in vnfd_list:
684 vnfd['id'] = 'clone_' + vnfd['id']
685 vnfd['name'] = 'clone_' + vnfd['name']
686 vnfd['short-name'] = 'clone_' + vnfd['short-name']
687
688 with open('/tmp/' + name, 'w') as yaml_file:
689 yaml_file.write(yaml.dump(yaml_object, default_flow_style=False))
690 break
691
692 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", "w:gz")
693
694 for tarinfo in tarf:
695 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
696 tarf_temp.close()
697 return tarf
698
699 def nsd_get(self, token, id):
700 result = {'error': True, 'data': ''}
701 headers = {'Content-Type': 'application/yaml',
702 'Authorization': 'Bearer {}'.format(token['id'])}
703 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self._base_path, id)
704 try:
705 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
706 except Exception as e:
707 log.exception(e)
708 result['data'] = str(e)
709 return result
710 if r.status_code == requests.codes.ok:
711 result['error'] = False
712 return yaml.load(r.text)
713 else:
714 try:
715 result['data'] = r.json()
716 except Exception as e:
717 result['data'] = {}
718 return result
719
720 def vnfd_get(self, token, id):
721 result = {'error': True, 'data': ''}
722 headers = {'Content-Type': 'application/yaml',
723 'Authorization': 'Bearer {}'.format(token['id'])}
724 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self._base_path, id)
725 try:
726 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
727 except Exception as e:
728 log.exception(e)
729 result['data'] = str(e)
730 return result
731 if r.status_code == requests.codes.ok:
732 result['error'] = False
733 return yaml.load(r.text)
734 else:
735 try:
736 result['data'] = r.json()
737 except Exception as e:
738 result['data'] = {}
739 return result
740
741 def nsd_artifacts(self, token, id):
742 result = {'error': True, 'data': ''}
743 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
744 'Authorization': 'Bearer {}'.format(token['id'])}
745 _url = "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self._base_path, id)
746 try:
747 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
748 except Exception as e:
749 log.exception(e)
750 result['data'] = str(e)
751 return result
752 if r.status_code == requests.codes.ok:
753 result['error'] = False
754 result['data'] = r.text
755 else:
756 try:
757 result['data'] = r.json()
758 except Exception as e:
759 result['data'] = {}
760
761 return result
762
763 def vnf_packages_artifacts(self, token, id):
764 result = {'error': True, 'data': ''}
765 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
766 'Authorization': 'Bearer {}'.format(token['id'])}
767 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self._base_path, id)
768 try:
769 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
770 except Exception as e:
771 log.exception(e)
772 result['data'] = str(e)
773 return result
774 if r.status_code == requests.codes.ok:
775 result['error'] = False
776 result['data'] = r.text
777 else:
778 try:
779 result['data'] = r.json()
780 except Exception as e:
781 result['data'] = {}
782
783 return result
784
785 def ns_create(self, token, ns_data):
786 result = {'error': True, 'data': ''}
787 headers = {"Content-Type": "application/yaml", "accept": "application/json",
788 'Authorization': 'Bearer {}'.format(token['id'])}
789
790 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
791
792 try:
793 r = requests.post(_url, json=ns_data, verify=False, headers=headers)
794 except Exception as e:
795 log.exception(e)
796 result['data'] = str(e)
797 return result
798 if r.status_code == requests.codes.ok:
799 result['error'] = False
800 result['data'] = Util.json_loads_byteified(r.text)
801 return result
802
803 def ns_op_list(self, token, id):
804 result = {'error': True, 'data': ''}
805 headers = {"Content-Type": "application/json", "accept": "application/json",
806 'Authorization': 'Bearer {}'.format(token['id'])}
807 _url = "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self._base_path, id)
808
809 try:
810 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
811 except Exception as e:
812 log.exception(e)
813 result['data'] = str(e)
814 return result
815 if r.status_code == requests.codes.ok:
816 result['error'] = False
817 result['data'] = Util.json_loads_byteified(r.text)
818
819 return result
820
821 def ns_op(self, token, id):
822 result = {'error': True, 'data': ''}
823 headers = {"Content-Type": "application/json", "accept": "application/json",
824 'Authorization': 'Bearer {}'.format(token['id'])}
825 _url = "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self._base_path, id)
826
827 try:
828 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
829 except Exception as e:
830 log.exception(e)
831 result['data'] = str(e)
832 return result
833 if r.status_code == requests.codes.ok:
834 result['error'] = False
835 result['data'] = Util.json_loads_byteified(r.text)
836
837 return result
838
839 def ns_action(self, token, id, action_payload):
840 result = {'error': True, 'data': ''}
841 headers = {"Content-Type": "application/json", "accept": "application/json",
842 'Authorization': 'Bearer {}'.format(token['id'])}
843
844 _url = "{0}/nslcm/v1/ns_instances/{1}/action".format(self._base_path, id)
845
846 try:
847 r = requests.post(_url, json=action_payload, verify=False, headers=headers)
848 except Exception as e:
849 log.exception(e)
850 result['data'] = str(e)
851 return result
852 if r.status_code == requests.codes.created:
853 result['error'] = False
854 result['data'] = Util.json_loads_byteified(r.text)
855 return result
856
857 def ns_delete(self, token, id, force=None):
858 result = {'error': True, 'data': ''}
859 headers = {"Content-Type": "application/yaml", "accept": "application/json",
860 'Authorization': 'Bearer {}'.format(token['id'])}
861 query_path = ''
862 if force:
863 query_path = '?FORCE=true'
864 _url = "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self._base_path, id, query_path)
865 try:
866 r = requests.delete(_url, params=None, verify=False, headers=headers)
867 except Exception as e:
868 log.exception(e)
869 result['data'] = str(e)
870 return result
871 if r:
872 result['error'] = False
873 if r.status_code != requests.codes.no_content:
874 result['data'] = Util.json_loads_byteified(r.text)
875 return result
876
877 def ns_get(self, token, id):
878 result = {'error': True, 'data': ''}
879 headers = {"Content-Type": "application/json", "accept": "application/json",
880 'Authorization': 'Bearer {}'.format(token['id'])}
881 _url = "{0}/nslcm/v1/ns_instances_content/{1}".format(self._base_path, id)
882
883 try:
884 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
885 except Exception as e:
886 log.exception(e)
887 result['data'] = str(e)
888 return result
889 if r.status_code == requests.codes.ok:
890 result['error'] = False
891 result['data'] = Util.json_loads_byteified(r.text)
892 return result
893
894 def vnf_get(self, token, id):
895 result = {'error': True, 'data': ''}
896 headers = {"Content-Type": "application/json", "accept": "application/json",
897 'Authorization': 'Bearer {}'.format(token['id'])}
898 _url = "{0}/nslcm/v1/vnfrs/{1}".format(self._base_path, id)
899
900 try:
901 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
902 except Exception as e:
903 log.exception(e)
904 result['data'] = str(e)
905 return result
906 if r.status_code == requests.codes.ok:
907 result['error'] = False
908 result['data'] = Util.json_loads_byteified(r.text)
909 return result
910
911 def ns_alarm_create(self, token, id, alarm_payload):
912 result = {'error': True, 'data': ''}
913 headers = {"Content-Type": "application/json",
914 'Authorization': 'Bearer {}'.format(token['id'])}
915 _url = "{0}/test/message/alarm_request".format(self._base_path)
916 try:
917 r = requests.post(_url, json=alarm_payload, verify=False, headers=headers)
918 except Exception as e:
919 log.exception(e)
920 result['data'] = str(e)
921 return result
922 if r.status_code == requests.codes.ok:
923 result['error'] = False
924 # result['data'] = Util.json_loads_byteified(r.text)
925 result['data'] = r.text
926 return result
927
928 def ns_metric_export(self, token, id, metric_payload):
929 result = {'error': True, 'data': ''}
930 headers = {"Content-Type": "application/json",
931 'Authorization': 'Bearer {}'.format(token['id'])}
932 _url = "{0}/test/message/metric_request".format(self._base_path)
933 try:
934 r = requests.post(_url, json=metric_payload, verify=False, headers=headers)
935 except Exception as e:
936 log.exception(e)
937 result['data'] = str(e)
938 return result
939 if r.status_code == requests.codes.ok:
940 result['error'] = False
941 # result['data'] = Util.json_loads_byteified(r.text)
942 result['data'] = r.text
943 return result
944
945 def vim_list(self, token):
946 result = {'error': True, 'data': ''}
947 headers = {"Content-Type": "application/yaml", "accept": "application/json",
948 'Authorization': 'Bearer {}'.format(token['id'])}
949 _url = "{0}/admin/v1/vims".format(self._base_path)
950 try:
951 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
952 except Exception as e:
953 log.exception(e)
954 result['data'] = str(e)
955 return result
956 if r.status_code == requests.codes.ok:
957 result['error'] = False
958 result['data'] = Util.json_loads_byteified(r.text)
959
960 return result
961
962 def vim_delete(self, token, id):
963 result = {'error': True, 'data': ''}
964 headers = {"accept": "application/json",
965 'Authorization': 'Bearer {}'.format(token['id'])}
966 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
967 try:
968 r = requests.delete(_url, params=None, verify=False, headers=headers)
969 except Exception as e:
970 log.exception(e)
971 result['data'] = str(e)
972 return result
973 if r.status_code == requests.codes.accepted:
974 result['error'] = False
975 else:
976 result['data'] = r.text
977 return result
978
979 def vim_get(self, token, id):
980
981 result = {'error': True, 'data': ''}
982 headers = {"Content-Type": "application/json", "accept": "application/json",
983 'Authorization': 'Bearer {}'.format(token['id'])}
984 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
985
986 try:
987 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
988 except Exception as e:
989 log.exception(e)
990 result['data'] = str(e)
991 return result
992 if r.status_code == requests.codes.ok:
993 result['error'] = False
994 result['data'] = Util.json_loads_byteified(r.text)
995 return result
996
997 def vim_create(self, token, vim_data):
998
999 result = {'error': True, 'data': ''}
1000 headers = {"Content-Type": "application/json", "accept": "application/json",
1001 'Authorization': 'Bearer {}'.format(token['id'])}
1002
1003 _url = "{0}/admin/v1/vims".format(self._base_path)
1004
1005 try:
1006 r = requests.post(_url, json=vim_data, verify=False, headers=headers)
1007 except Exception as e:
1008 log.exception(e)
1009 result['data'] = str(e)
1010 return result
1011 if r.status_code == requests.codes.created:
1012 result['error'] = False
1013 result['data'] = Util.json_loads_byteified(r.text)
1014 return result
1015
1016 def sdn_list(self, token):
1017 result = {'error': True, 'data': ''}
1018 headers = {"accept": "application/json",
1019 'Authorization': 'Bearer {}'.format(token['id'])}
1020 _url = "{0}/admin/v1/sdns".format(self._base_path)
1021 try:
1022 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1023 except Exception as e:
1024 log.exception(e)
1025 result['data'] = str(e)
1026 return result
1027 if r.status_code == requests.codes.ok:
1028 result['error'] = False
1029 result['data'] = Util.json_loads_byteified(r.text)
1030 return result
1031
1032 def sdn_delete(self, token, id):
1033 result = {'error': True, 'data': ''}
1034 headers = {"accept": "application/json",
1035 'Authorization': 'Bearer {}'.format(token['id'])}
1036 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
1037 try:
1038 r = requests.delete(_url, params=None, verify=False, headers=headers)
1039 except Exception as e:
1040 log.exception(e)
1041 result['data'] = str(e)
1042 return result
1043 if r.status_code == requests.codes.accepted:
1044 result['error'] = False
1045 else:
1046 result['data'] = r.text
1047 return result
1048
1049 def sdn_get(self, token, id):
1050 result = {'error': True, 'data': ''}
1051 headers = {"accept": "application/json",
1052 'Authorization': 'Bearer {}'.format(token['id'])}
1053 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
1054
1055 try:
1056 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1057 except Exception as e:
1058 log.exception(e)
1059 result['data'] = str(e)
1060 return result
1061 if r.status_code == requests.codes.ok:
1062 result['error'] = False
1063 result['data'] = Util.json_loads_byteified(r.text)
1064 return result
1065
1066 def sdn_create(self, token, sdn_data):
1067 result = {'error': True, 'data': ''}
1068 headers = {"Content-Type": "application/json", "accept": "application/json",
1069 'Authorization': 'Bearer {}'.format(token['id'])}
1070
1071 _url = "{0}/admin/v1/sdns".format(self._base_path)
1072
1073 try:
1074 r = requests.post(_url, json=sdn_data, verify=False, headers=headers)
1075 except Exception as e:
1076 log.exception(e)
1077 result['data'] = str(e)
1078 return result
1079 if r.status_code == requests.codes.created:
1080 result['error'] = False
1081 result['data'] = Util.json_loads_byteified(r.text)
1082 return result
1083
1084 @staticmethod
1085 def md5(f):
1086 hash_md5 = hashlib.md5()
1087 for chunk in iter(lambda: f.read(1024), b""):
1088 hash_md5.update(chunk)
1089 return hash_md5.hexdigest()