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