fix parameter on instance creation
[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
17 import requests
18 import logging
19 import json
20 import tarfile
21 import yaml
22 import pyaml
23 import StringIO
24 from lib.util import Util
25 import hashlib
26 import os
27 from requests.packages.urllib3.exceptions import InsecureRequestWarning
28
29 requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
30
31 logging.basicConfig(level=logging.INFO)
32 log = logging.getLogger('helper.py')
33 logging.getLogger("urllib3").setLevel(logging.INFO)
34
35 class Client(object):
36 def __init__(self):
37 self._token_endpoint = 'admin/v1/tokens'
38 self._user_endpoint = 'admin/v1/users'
39 self._host = os.getenv('OSM_SERVER', "localhost")
40 self._so_port = 9999
41 self._base_path = "https://{0}:{1}/osm".format(self._host, self._so_port)
42
43 def auth(self, args):
44 result = {'error': True, 'data': ''}
45 token_url = "{0}/{1}".format(self._base_path, self._token_endpoint)
46 headers = {"Content-Type": "application/yaml", "accept": "application/json"}
47 try:
48 r = requests.post(token_url, json=args, verify=False, headers=headers)
49 except Exception as e:
50 log.exception(e)
51 result['data'] = str(e)
52 return result
53 if r.status_code == requests.codes.ok:
54 result['error'] = False
55
56 result['data'] = Util.json_loads_byteified(r.text)
57
58 return result
59
60 def switch_project(self, args):
61 result = {'error': True, 'data': ''}
62 token_url = "{0}/{1}".format(self._base_path, self._token_endpoint)
63 headers = {"Content-Type": "application/yaml", "accept": "application/json"}
64 try:
65 r = requests.post(token_url, json=args, verify=False, headers=headers)
66 except Exception as e:
67 log.exception(e)
68 result['data'] = str(e)
69 return result
70 if r.status_code == requests.codes.ok:
71 result['error'] = False
72
73 result['data'] = Util.json_loads_byteified(r.text)
74
75 return result
76
77 def user_list(self, token):
78 result = {'error': True, 'data': ''}
79 headers = {"Content-Type": "application/json", "accept": "application/json",
80 'Authorization': 'Bearer {}'.format(token['id'])}
81
82 _url = "{0}/admin/v1/users".format(self._base_path)
83 try:
84 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
85 except Exception as e:
86 log.exception(e)
87 result['data'] = str(e)
88 return result
89 if r.status_code == requests.codes.ok:
90 result['error'] = False
91 result['data'] = Util.json_loads_byteified(r.text)
92
93 return result
94
95 def user_create(self, token, user_data):
96 result = {'error': True, 'data': ''}
97 headers = {"Content-Type": "application/json", "accept": "application/json",
98 'Authorization': 'Bearer {}'.format(token['id'])}
99
100 _url = "{0}/admin/v1/users".format(self._base_path)
101
102 try:
103 r = requests.post(_url, json=user_data, verify=False, headers=headers)
104 except Exception as e:
105 log.exception(e)
106 result['data'] = str(e)
107 return result
108 if r.status_code == requests.codes.created:
109 result['error'] = False
110 result['data'] = Util.json_loads_byteified(r.text)
111 return result
112
113 def user_update(self, token, id, user_data):
114 result = {'error': True, 'data': ''}
115 headers = {"Content-Type": "application/json", "accept": "application/json",
116 'Authorization': 'Bearer {}'.format(token['id'])}
117
118 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
119 try:
120 r = requests.post(_url, json=user_data, verify=False, headers=headers)
121 except Exception as e:
122 log.exception(e)
123 result['data'] = str(e)
124 return result
125 if r.status_code == requests.codes.created:
126 result['error'] = False
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_clone(self, token, id):
400 result = {'error': True, 'data': ''}
401 headers = {"Content-Type": "application/gzip", "accept": "application/json",
402 'Authorization': 'Bearer {}'.format(token['id'])}
403
404 # get the package onboarded
405 tar_pkg = self.get_nsd_pkg(token, id)
406 tarf = tarfile.open(fileobj=tar_pkg)
407 tarf = self._descriptor_clone(tarf, 'nsd')
408 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
409
410 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
411
412 try:
413 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
414 headers=headers)
415 except Exception as e:
416 log.exception(e)
417 result['data'] = str(e)
418 return result
419 if r.status_code == requests.codes.created:
420 result['error'] = False
421 if r.status_code == requests.codes.conflict:
422 result['data'] = "Invalid ID."
423
424 return result
425
426 def vnfd_clone(self, token, id):
427 result = {'error': True, 'data': ''}
428 headers = {"Content-Type": "application/gzip", "accept": "application/json",
429 'Authorization': 'Bearer {}'.format(token['id'])}
430
431 # get the package onboarded
432 tar_pkg = self.get_vnfd_pkg(token, id)
433 tarf = tarfile.open(fileobj=tar_pkg)
434
435 tarf = self._descriptor_clone(tarf, 'vnfd')
436 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
437
438 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
439
440 try:
441 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
442 headers=headers)
443 except Exception as e:
444 log.exception(e)
445 result['data'] = str(e)
446 return result
447 if r.status_code == requests.codes.created:
448 result['error'] = False
449 if r.status_code == requests.codes.conflict:
450 result['data'] = "Invalid ID."
451
452 return result
453
454 def nsd_update(self, token, id, data):
455 result = {'error': True, 'data': ''}
456 headers = {"Content-Type": "application/gzip", "accept": "application/json",
457 'Authorization': 'Bearer {}'.format(token['id'])}
458
459 # get the package onboarded
460 tar_pkg = self.get_nsd_pkg(token, id)
461 tarf = tarfile.open(fileobj=tar_pkg)
462
463 tarf = self._descriptor_update(tarf, data)
464 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
465
466 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
467
468 try:
469 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
470 headers=headers)
471 except Exception as e:
472 log.exception(e)
473 result['data'] = str(e)
474 return result
475 if r.status_code == requests.codes.no_content:
476 result['error'] = False
477
478 return result
479
480 def vnfd_update(self, token, id, data):
481 result = {'error': True, 'data': ''}
482 headers = {"Content-Type": "application/gzip", "accept": "application/json",
483 'Authorization': 'Bearer {}'.format(token['id'])}
484
485 # get the package onboarded
486 tar_pkg = self.get_vnfd_pkg(token, id)
487 tarf = tarfile.open(fileobj=tar_pkg)
488
489 tarf = self._descriptor_update(tarf, data)
490 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
491
492 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
493
494 try:
495 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
496 headers=headers)
497 except Exception as e:
498 log.exception(e)
499 result['data'] = str(e)
500 return result
501 if r.status_code == requests.codes.no_content:
502 result['error'] = False
503
504 return result
505
506 def get_nsd_pkg(self, token, id):
507 result = {'error': True, 'data': ''}
508 headers = { "accept": "application/zip",
509 'Authorization': 'Bearer {}'.format(token['id'])}
510
511 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
512 try:
513 r = requests.get(_url, params=None, verify=False, stream=True, 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.ok:
519 result['error'] = False
520 tarf = StringIO.StringIO(r.content)
521 return tarf
522 return result
523
524 def get_vnfd_pkg(self, token, id):
525 result = {'error': True, 'data': ''}
526 headers = {"accept": "application/zip",
527 'Authorization': 'Bearer {}'.format(token['id'])}
528 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
529 try:
530 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
531 except Exception as e:
532 log.exception(e)
533 result['data'] = str(e)
534 return result
535 if r.status_code == requests.codes.ok:
536 result['error'] = False
537 tarf = StringIO.StringIO(r.content)
538 return tarf
539 return result
540
541 def _descriptor_update(self, tarf, data):
542 # extract the package on a tmp directory
543 tarf.extractall('/tmp')
544
545 for name in tarf.getnames():
546 if name.endswith(".yaml") or name.endswith(".yml"):
547 with open('/tmp/' + name, 'w') as outfile:
548 yaml.safe_dump(data, outfile, default_flow_style=False)
549 break
550
551 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + ".tar.gz", "w:gz")
552
553 for tarinfo in tarf:
554 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
555 tarf_temp.close()
556 return tarf
557
558 def _descriptor_clone(self, tarf, descriptor_type):
559 # extract the package on a tmp directory
560 tarf.extractall('/tmp')
561
562 for name in tarf.getnames():
563 if name.endswith(".yaml") or name.endswith(".yml"):
564 with open('/tmp/' + name, 'r') as outfile:
565 yaml_object = yaml.load(outfile)
566
567 if descriptor_type == 'nsd':
568 nsd_list = yaml_object['nsd:nsd-catalog']['nsd']
569 for nsd in nsd_list:
570 nsd['id'] = 'clone_' + nsd['id']
571 nsd['name'] = 'clone_' +nsd['name']
572 nsd['short-name'] = 'clone_' +nsd['short-name']
573 elif descriptor_type == 'vnfd':
574 vnfd_list = yaml_object['vnfd:vnfd-catalog']['vnfd']
575 for vnfd in vnfd_list:
576 vnfd['id'] = 'clone_' + vnfd['id']
577 vnfd['name'] = 'clone_' + vnfd['name']
578 vnfd['short-name'] = 'clone_' + vnfd['short-name']
579
580
581 with open('/tmp/' + name, 'w') as yaml_file:
582 yaml_file.write(yaml.dump(yaml_object, default_flow_style=False))
583 break
584
585 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", "w:gz")
586
587 for tarinfo in tarf:
588 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
589 tarf_temp.close()
590 return tarf
591
592 def nsd_get(self, token, id):
593 result = {'error': True, 'data': ''}
594 headers = {'Content-Type': 'application/yaml',
595 'Authorization': 'Bearer {}'.format(token['id'])}
596 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self._base_path, id)
597 try:
598 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
599 except Exception as e:
600 log.exception(e)
601 result['data'] = str(e)
602 return result
603 if r.status_code == requests.codes.ok:
604 result['error'] = False
605 return yaml.load(r.text)
606 else:
607 try:
608 result['data'] = r.json()
609 except Exception as e:
610 result['data'] = {}
611 return result
612
613 def vnfd_get(self, token, id):
614 result = {'error': True, 'data': ''}
615 headers = {'Content-Type': 'application/yaml',
616 'Authorization': 'Bearer {}'.format(token['id'])}
617 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self._base_path, id)
618 try:
619 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
620 except Exception as e:
621 log.exception(e)
622 result['data'] = str(e)
623 return result
624 if r.status_code == requests.codes.ok:
625 result['error'] = False
626 return yaml.load(r.text)
627 else:
628 try:
629 result['data'] = r.json()
630 except Exception as e:
631 result['data'] = {}
632 return result
633
634 def nsd_artifacts(self, token, id):
635 result = {'error': True, 'data': ''}
636 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
637 'Authorization': 'Bearer {}'.format(token['id'])}
638 _url = "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self._base_path, id)
639 try:
640 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
641 except Exception as e:
642 log.exception(e)
643 result['data'] = str(e)
644 return result
645 if r.status_code == requests.codes.ok:
646 result['error'] = False
647 result['data'] = r.text
648 else:
649 try:
650 result['data'] = r.json()
651 except Exception as e:
652 result['data'] = {}
653
654 return result
655
656 def vnf_packages_artifacts(self, token, id):
657 result = {'error': True, 'data': ''}
658 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
659 'Authorization': 'Bearer {}'.format(token['id'])}
660 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self._base_path, id)
661 try:
662 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
663 except Exception as e:
664 log.exception(e)
665 result['data'] = str(e)
666 return result
667 if r.status_code == requests.codes.ok:
668 result['error'] = False
669 result['data'] = r.text
670 else:
671 try:
672 result['data'] = r.json()
673 except Exception as e:
674 result['data'] = {}
675
676 return result
677
678 def ns_create(self, token, ns_data):
679 result = {'error': True, 'data': ''}
680 headers = {"Content-Type": "application/yaml", "accept": "application/json",
681 'Authorization': 'Bearer {}'.format(token['id'])}
682
683 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
684
685 try:
686 r = requests.post(_url, json=ns_data, verify=False, headers=headers)
687 except Exception as e:
688 log.exception(e)
689 result['data'] = str(e)
690 return result
691 if r.status_code == requests.codes.ok:
692 result['error'] = False
693 result['data'] = Util.json_loads_byteified(r.text)
694 return result
695
696 def ns_op_list(self, token, id):
697 result = {'error': True, 'data': ''}
698 headers = {"Content-Type": "application/json", "accept": "application/json",
699 'Authorization': 'Bearer {}'.format(token['id'])}
700 _url = "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self._base_path, id)
701
702 try:
703 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
704 except Exception as e:
705 log.exception(e)
706 result['data'] = str(e)
707 return result
708 if r.status_code == requests.codes.ok:
709 result['error'] = False
710 result['data'] = Util.json_loads_byteified(r.text)
711
712 return result
713
714 def ns_op(self, token, id):
715 result = {'error': True, 'data': ''}
716 headers = {"Content-Type": "application/json", "accept": "application/json",
717 'Authorization': 'Bearer {}'.format(token['id'])}
718 _url = "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self._base_path, id)
719
720 try:
721 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
722 except Exception as e:
723 log.exception(e)
724 result['data'] = str(e)
725 return result
726 if r.status_code == requests.codes.ok:
727 result['error'] = False
728 result['data'] = Util.json_loads_byteified(r.text)
729
730 return result
731
732 def ns_action(self, token, id, action_payload):
733 result = {'error': True, 'data': ''}
734 headers = {"Content-Type": "application/json", "accept": "application/json",
735 'Authorization': 'Bearer {}'.format(token['id'])}
736
737 _url = "{0}/nslcm/v1/ns_instances/{1}/action".format(self._base_path, id)
738
739 try:
740 r = requests.post(_url, json=action_payload, verify=False, headers=headers)
741 except Exception as e:
742 log.exception(e)
743 result['data'] = str(e)
744 return result
745 if r.status_code == requests.codes.created:
746 result['error'] = False
747 result['data'] = Util.json_loads_byteified(r.text)
748 return result
749
750 def ns_delete(self, token, id, force=None):
751 result = {'error': True, 'data': ''}
752 headers = {"Content-Type": "application/yaml", "accept": "application/json",
753 'Authorization': 'Bearer {}'.format(token['id'])}
754 query_path = ''
755 if force:
756 query_path = '?FORCE=true'
757 _url = "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self._base_path, id, query_path)
758 try:
759 r = requests.delete(_url, params=None, verify=False, headers=headers)
760 except Exception as e:
761 log.exception(e)
762 result['data'] = str(e)
763 return result
764 if r:
765 result['error'] = False
766 if r.status_code != requests.codes.no_content:
767 result['data'] = Util.json_loads_byteified(r.text)
768 return result
769
770 def ns_get(self, token, id):
771 result = {'error': True, 'data': ''}
772 headers = {"Content-Type": "application/json", "accept": "application/json",
773 'Authorization': 'Bearer {}'.format(token['id'])}
774 _url = "{0}/nslcm/v1/ns_instances_content/{1}".format(self._base_path, id)
775
776 try:
777 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
778 except Exception as e:
779 log.exception(e)
780 result['data'] = str(e)
781 return result
782 if r.status_code == requests.codes.ok:
783 result['error'] = False
784 result['data'] = Util.json_loads_byteified(r.text)
785 return result
786
787 def vnf_get(self, token, id):
788 result = {'error': True, 'data': ''}
789 headers = {"Content-Type": "application/json", "accept": "application/json",
790 'Authorization': 'Bearer {}'.format(token['id'])}
791 _url = "{0}/nslcm/v1/vnfrs/{1}".format(self._base_path, id)
792
793 try:
794 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
795 except Exception as e:
796 log.exception(e)
797 result['data'] = str(e)
798 return result
799 if r.status_code == requests.codes.ok:
800 result['error'] = False
801 result['data'] = Util.json_loads_byteified(r.text)
802 return result
803
804 def ns_alarm_create(self, token, id, alarm_payload):
805 result = {'error': True, 'data': ''}
806 headers = {"Content-Type": "application/json",
807 'Authorization': 'Bearer {}'.format(token['id'])}
808 _url = "{0}/test/message/alarm_request".format(self._base_path)
809 try:
810 r = requests.post(_url, json=alarm_payload, verify=False, 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 result['data'] = r.text
819 return result
820
821 def ns_metric_export(self, token, id, metric_payload):
822 result = {'error': True, 'data': ''}
823 headers = {"Content-Type": "application/json",
824 'Authorization': 'Bearer {}'.format(token['id'])}
825 _url = "{0}/test/message/metric_request".format(self._base_path)
826 try:
827 r = requests.post(_url, json=metric_payload, verify=False, headers=headers)
828 except Exception as e:
829 log.exception(e)
830 result['data'] = str(e)
831 return result
832 if r.status_code == requests.codes.ok:
833 result['error'] = False
834 #result['data'] = Util.json_loads_byteified(r.text)
835 result['data'] = r.text
836 return result
837
838 def vim_list(self, token):
839 result = {'error': True, 'data': ''}
840 headers = {"Content-Type": "application/yaml", "accept": "application/json",
841 'Authorization': 'Bearer {}'.format(token['id'])}
842 _url = "{0}/admin/v1/vims".format(self._base_path)
843 try:
844 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
845 except Exception as e:
846 log.exception(e)
847 result['data'] = str(e)
848 return result
849 if r.status_code == requests.codes.ok:
850 result['error'] = False
851 result['data'] = Util.json_loads_byteified(r.text)
852
853 return result
854
855 def vim_delete(self, token, id):
856 result = {'error': True, 'data': ''}
857 headers = { "accept": "application/json",
858 'Authorization': 'Bearer {}'.format(token['id'])}
859 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
860 try:
861 r = requests.delete(_url, params=None, verify=False, headers=headers)
862 except Exception as e:
863 log.exception(e)
864 result['data'] = str(e)
865 return result
866 if r.status_code == requests.codes.accepted:
867 result['error'] = False
868 else:
869 result['data'] = r.text
870 return result
871
872 def vim_get(self, token, id):
873
874 result = {'error': True, 'data': ''}
875 headers = {"Content-Type": "application/json", "accept": "application/json",
876 'Authorization': 'Bearer {}'.format(token['id'])}
877 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
878
879 try:
880 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
881 except Exception as e:
882 log.exception(e)
883 result['data'] = str(e)
884 return result
885 if r.status_code == requests.codes.ok:
886 result['error'] = False
887 result['data'] = Util.json_loads_byteified(r.text)
888 return result
889
890 def vim_create(self, token, vim_data):
891
892 result = {'error': True, 'data': ''}
893 headers = {"Content-Type": "application/json", "accept": "application/json",
894 'Authorization': 'Bearer {}'.format(token['id'])}
895
896 _url = "{0}/admin/v1/vims".format(self._base_path)
897
898 try:
899 r = requests.post(_url, json=vim_data, verify=False, headers=headers)
900 except Exception as e:
901 log.exception(e)
902 result['data'] = str(e)
903 return result
904 if r.status_code == requests.codes.created:
905 result['error'] = False
906 result['data'] = Util.json_loads_byteified(r.text)
907 return result
908
909 def sdn_list(self, token):
910 result = {'error': True, 'data': ''}
911 headers = {"accept": "application/json",
912 'Authorization': 'Bearer {}'.format(token['id'])}
913 _url = "{0}/admin/v1/sdns".format(self._base_path)
914 try:
915 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
916 except Exception as e:
917 log.exception(e)
918 result['data'] = str(e)
919 return result
920 if r.status_code == requests.codes.ok:
921 result['error'] = False
922 result['data'] = Util.json_loads_byteified(r.text)
923 return result
924
925 def sdn_delete(self, token, id):
926 result = {'error': True, 'data': ''}
927 headers = {"accept": "application/json",
928 'Authorization': 'Bearer {}'.format(token['id'])}
929 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
930 try:
931 r = requests.delete(_url, params=None, verify=False, headers=headers)
932 except Exception as e:
933 log.exception(e)
934 result['data'] = str(e)
935 return result
936 if r.status_code == requests.codes.accepted:
937 result['error'] = False
938 else:
939 result['data'] = r.text
940 return result
941
942 def sdn_get(self, token, id):
943 result = {'error': True, 'data': ''}
944 headers = {"accept": "application/json",
945 'Authorization': 'Bearer {}'.format(token['id'])}
946 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
947
948 try:
949 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
950 except Exception as e:
951 log.exception(e)
952 result['data'] = str(e)
953 return result
954 if r.status_code == requests.codes.ok:
955 result['error'] = False
956 result['data'] = Util.json_loads_byteified(r.text)
957 return result
958
959
960 def sdn_create(self, token, sdn_data):
961 result = {'error': True, 'data': ''}
962 headers = {"Content-Type": "application/json", "accept": "application/json",
963 'Authorization': 'Bearer {}'.format(token['id'])}
964
965 _url = "{0}/admin/v1/sdns".format(self._base_path)
966
967 try:
968 r = requests.post(_url, json=sdn_data, 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.created:
974 result['error'] = False
975 result['data'] = Util.json_loads_byteified(r.text)
976 return result
977
978
979 @staticmethod
980 def md5(f):
981 hash_md5 = hashlib.md5()
982 for chunk in iter(lambda: f.read(1024), b""):
983 hash_md5.update(chunk)
984 return hash_md5.hexdigest()