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