2 # Copyright 2018 EveryUP Srl
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
24 from lib
.util
import Util
28 logging
.basicConfig(level
=logging
.DEBUG
)
29 log
= logging
.getLogger('helper.py')
34 self
._token
_endpoint
= 'admin/v1/tokens'
35 self
._user
_endpoint
= 'admin/v1/users'
36 self
._host
= os
.getenv('OSM_SERVER', "localhost")
38 self
._base
_path
= "https://{0}:{1}/osm".format(self
._host
, self
._so
_port
)
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"}
45 r
= requests
.post(token_url
, json
=args
, verify
=False, headers
=headers
)
46 except Exception as e
:
48 result
['data'] = str(e
)
50 if r
.status_code
== requests
.codes
.ok
:
51 result
['error'] = False
53 result
['data'] = Util
.json_loads_byteified(r
.text
)
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"}
62 r
= requests
.post(token_url
, json
=args
, verify
=False, headers
=headers
)
63 except Exception as e
:
65 result
['data'] = str(e
)
67 if r
.status_code
== requests
.codes
.ok
:
68 result
['error'] = False
70 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
79 _url
= "{0}/admin/v1/users".format(self
._base
_path
)
81 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
82 except Exception as e
:
84 result
['data'] = str(e
)
86 if r
.status_code
== requests
.codes
.ok
:
87 result
['error'] = False
88 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
97 _url
= "{0}/admin/v1/users".format(self
._base
_path
)
100 r
= requests
.post(_url
, json
=user_data
, verify
=False, headers
=headers
)
101 except Exception as e
:
103 result
['data'] = str(e
)
105 if r
.status_code
== requests
.codes
.created
:
106 result
['error'] = False
107 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
115 _url
= "{0}/admin/v1/users/{1}".format(self
._base
_path
, id)
119 r
= requests
.post(_url
, json
=user_data
, verify
=False, headers
=headers
)
120 except Exception as e
:
122 result
['data'] = str(e
)
124 if r
.status_code
== requests
.codes
.created
:
125 result
['error'] = False
126 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
134 _url
= "{0}/admin/v1/users/{1}".format(self
._base
_path
, id)
136 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
137 except Exception as e
:
139 result
['data'] = str(e
)
141 if r
.status_code
== requests
.codes
.no_content
:
142 result
['error'] = False
144 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
153 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
154 except Exception as e
:
156 result
['data'] = str(e
)
158 if r
.status_code
== requests
.codes
.ok
:
159 result
['error'] = False
160 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
168 _url
= "{0}/admin/v1/projects".format(self
._base
_path
)
170 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
171 except Exception as e
:
173 result
['data'] = str(e
)
175 if r
.status_code
== requests
.codes
.ok
:
176 result
['error'] = False
177 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
187 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
188 except Exception as e
:
190 result
['data'] = str(e
)
192 if r
.status_code
== requests
.codes
.ok
:
193 result
['error'] = False
194 result
['data'] = Util
.json_loads_byteified(r
.text
)
197 def project_create(self
, token
, project_data
):
199 result
= {'error': True, 'data': ''}
200 headers
= {"Content-Type": "application/json", "accept": "application/json",
201 'Authorization': 'Bearer {}'.format(token
['id'])}
203 _url
= "{0}/admin/v1/projects".format(self
._base
_path
)
206 r
= requests
.post(_url
, json
=project_data
, verify
=False, headers
=headers
)
207 except Exception as e
:
209 result
['data'] = str(e
)
211 if r
.status_code
== requests
.codes
.created
:
212 result
['error'] = False
213 result
['data'] = Util
.json_loads_byteified(r
.text
)
216 def project_edit(self
, token
, id, project_data
):
218 result
= {'error': True, 'data': ''}
219 headers
= {"Content-Type": "application/json", "accept": "application/json",
220 'Authorization': 'Bearer {}'.format(token
['id'])}
222 _url
= "{0}/admin/v1/projects/{1}".format(self
._base
_path
, id)
225 r
= requests
.put(_url
, json
=project_data
, verify
=False, headers
=headers
)
226 except Exception as e
:
228 result
['data'] = str(e
)
230 if r
.status_code
== requests
.codes
.no_content
:
231 result
['error'] = False
232 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
240 _url
= "{0}/admin/v1/projects/{1}".format(self
._base
_path
, id)
242 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
243 except Exception as e
:
245 result
['data'] = str(e
)
247 if r
.status_code
== requests
.codes
.no_content
:
248 result
['error'] = False
250 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
258 _url
= "{0}/nsd/v1/ns_descriptors_content".format(self
._base
_path
)
260 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
261 except Exception as e
:
263 result
['data'] = str(e
)
265 if r
.status_code
== requests
.codes
.ok
:
266 result
['error'] = False
267 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
276 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
278 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
279 except Exception as e
:
281 result
['data'] = str(e
)
283 if r
.status_code
== requests
.codes
.ok
:
284 result
['error'] = False
285 result
['data'] = Util
.json_loads_byteified(r
.text
)
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
)
295 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
296 except Exception as e
:
298 result
['data'] = str(e
)
300 if r
.status_code
== requests
.codes
.ok
:
301 result
['error'] = False
302 result
['data'] = Util
.json_loads_byteified(r
.text
)
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
)
312 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
313 except Exception as e
:
315 result
['data'] = str(e
)
317 if r
.status_code
== requests
.codes
.ok
:
318 result
['error'] = False
319 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
328 _url
= "{0}/nsd/v1/ns_descriptors_content/{1}".format(self
._base
_path
, id)
330 r
= requests
.delete(_url
, params
=None, verify
=False,headers
=headers
)
331 except Exception as e
:
333 result
['data'] = str(e
)
335 if r
.status_code
== requests
.codes
.ok
:
336 result
['error'] = False
337 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
345 _url
= "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self
._base
_path
, id)
347 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
348 except Exception as e
:
350 result
['data'] = str(e
)
353 result
['error'] = False
354 if r
.status_code
!= requests
.codes
.no_content
:
355 result
['data'] = Util
.json_loads_byteified(r
.text
)
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
)
368 r
= requests
.post(_url
, data
=open('/tmp/'+package
.name
, 'rb'), verify
=False, headers
=headers
)
369 except Exception as e
:
371 result
['data'] = str(e
)
373 if r
.status_code
== requests
.codes
.created
:
374 result
['error'] = False
375 result
['data'] = Util
.json_loads_byteified(r
.text
)
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
)
388 r
= requests
.post(_url
, data
=open('/tmp/'+package
.name
, 'rb'), verify
=False, headers
=headers
)
389 except Exception as e
:
391 result
['data'] = str(e
)
393 if r
.status_code
== requests
.codes
.created
:
394 result
['error'] = False
395 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
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'))
409 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
412 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
414 except Exception as e
:
416 result
['data'] = str(e
)
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."
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'])}
430 # get the package onboarded
431 tar_pkg
= self
.get_vnfd_pkg(token
, id)
432 tarf
= tarfile
.open(fileobj
=tar_pkg
)
434 tarf
= self
._descriptor
_clone
(tarf
, 'vnfd')
435 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
437 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
440 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
442 except Exception as e
:
444 result
['data'] = str(e
)
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."
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'])}
458 # get the package onboarded
459 tar_pkg
= self
.get_nsd_pkg(token
, id)
460 tarf
= tarfile
.open(fileobj
=tar_pkg
)
462 tarf
= self
._descriptor
_update
(tarf
, data
)
463 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
465 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
468 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
470 except Exception as e
:
472 result
['data'] = str(e
)
474 if r
.status_code
== requests
.codes
.no_content
:
475 result
['error'] = False
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'])}
484 # get the package onboarded
485 tar_pkg
= self
.get_vnfd_pkg(token
, id)
486 tarf
= tarfile
.open(fileobj
=tar_pkg
)
488 tarf
= self
._descriptor
_update
(tarf
, data
)
489 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
491 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
494 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
496 except Exception as e
:
498 result
['data'] = str(e
)
500 if r
.status_code
== requests
.codes
.no_content
:
501 result
['error'] = False
505 def get_nsd_pkg(self
, token
, id):
506 result
= {'error': True, 'data': ''}
507 headers
= { "accept": "application/zip",
508 'Authorization': 'Bearer {}'.format(token
['id'])}
510 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
512 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
513 except Exception as e
:
515 result
['data'] = str(e
)
517 if r
.status_code
== requests
.codes
.ok
:
518 result
['error'] = False
519 tarf
= StringIO
.StringIO(r
.content
)
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)
529 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
530 except Exception as e
:
532 result
['data'] = str(e
)
534 if r
.status_code
== requests
.codes
.ok
:
535 result
['error'] = False
536 tarf
= StringIO
.StringIO(r
.content
)
540 def _descriptor_update(self
, tarf
, data
):
541 # extract the package on a tmp directory
542 tarf
.extractall('/tmp')
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)
550 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", "w:gz")
553 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
557 def _descriptor_clone(self
, tarf
, descriptor_type
):
558 # extract the package on a tmp directory
559 tarf
.extractall('/tmp')
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
)
566 if descriptor_type
== 'nsd':
567 nsd_list
= yaml_object
['nsd:nsd-catalog']['nsd']
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']
580 with
open('/tmp/' + name
, 'w') as yaml_file
:
581 yaml_file
.write(yaml
.dump(yaml_object
, default_flow_style
=False))
584 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", "w:gz")
587 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
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)
597 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
598 except Exception as e
:
600 result
['data'] = str(e
)
602 if r
.status_code
== requests
.codes
.ok
:
603 result
['error'] = False
604 return yaml
.load(r
.text
)
607 result
['data'] = r
.json()
608 except Exception as e
:
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)
618 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
619 except Exception as e
:
621 result
['data'] = str(e
)
623 if r
.status_code
== requests
.codes
.ok
:
624 result
['error'] = False
625 return yaml
.load(r
.text
)
628 result
['data'] = r
.json()
629 except Exception as e
:
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)
639 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
640 except Exception as e
:
642 result
['data'] = str(e
)
644 if r
.status_code
== requests
.codes
.ok
:
645 result
['error'] = False
646 result
['data'] = r
.text
649 result
['data'] = r
.json()
650 except Exception as e
:
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)
661 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
662 except Exception as e
:
664 result
['data'] = str(e
)
666 if r
.status_code
== requests
.codes
.ok
:
667 result
['error'] = False
668 result
['data'] = r
.text
671 result
['data'] = r
.json()
672 except Exception as e
:
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'])}
682 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
685 r
= requests
.post(_url
, json
=ns_data
, verify
=False, headers
=headers
)
686 except Exception as e
:
688 result
['data'] = str(e
)
690 if r
.status_code
== requests
.codes
.ok
:
691 result
['error'] = False
692 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
702 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
703 except Exception as e
:
705 result
['data'] = str(e
)
707 if r
.status_code
== requests
.codes
.ok
:
708 result
['error'] = False
709 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
720 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
721 except Exception as e
:
723 result
['data'] = str(e
)
725 if r
.status_code
== requests
.codes
.ok
:
726 result
['error'] = False
727 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
736 _url
= "{0}/nslcm/v1/ns_instances/{1}/action".format(self
._base
_path
, id)
739 r
= requests
.post(_url
, json
=action_payload
, verify
=False, headers
=headers
)
740 except Exception as e
:
742 result
['data'] = str(e
)
744 if r
.status_code
== requests
.codes
.created
:
745 result
['error'] = False
746 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
755 query_path
= '?FORCE=true'
756 _url
= "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self
._base
_path
, id, query_path
)
758 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
759 except Exception as e
:
761 result
['data'] = str(e
)
764 result
['error'] = False
765 if r
.status_code
!= requests
.codes
.no_content
:
766 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
776 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
777 except Exception as e
:
779 result
['data'] = str(e
)
781 if r
.status_code
== requests
.codes
.ok
:
782 result
['error'] = False
783 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
793 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
794 except Exception as e
:
796 result
['data'] = str(e
)
798 if r
.status_code
== requests
.codes
.ok
:
799 result
['error'] = False
800 result
['data'] = Util
.json_loads_byteified(r
.text
)
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
)
809 r
= requests
.post(_url
, json
=alarm_payload
, verify
=False, headers
=headers
)
810 except Exception as e
:
812 result
['data'] = str(e
)
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
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
)
826 r
= requests
.post(_url
, json
=metric_payload
, verify
=False, headers
=headers
)
827 except Exception as e
:
829 result
['data'] = str(e
)
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
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
)
843 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
844 except Exception as e
:
846 result
['data'] = str(e
)
848 if r
.status_code
== requests
.codes
.ok
:
849 result
['error'] = False
850 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
860 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
861 except Exception as e
:
863 result
['data'] = str(e
)
865 if r
.status_code
== requests
.codes
.accepted
:
866 result
['error'] = False
868 result
['data'] = r
.text
871 def vim_get(self
, token
, id):
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)
879 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
880 except Exception as e
:
882 result
['data'] = str(e
)
884 if r
.status_code
== requests
.codes
.ok
:
885 result
['error'] = False
886 result
['data'] = Util
.json_loads_byteified(r
.text
)
889 def vim_create(self
, token
, vim_data
):
891 result
= {'error': True, 'data': ''}
892 headers
= {"Content-Type": "application/json", "accept": "application/json",
893 'Authorization': 'Bearer {}'.format(token
['id'])}
895 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
898 r
= requests
.post(_url
, json
=vim_data
, verify
=False, headers
=headers
)
899 except Exception as e
:
901 result
['data'] = str(e
)
903 if r
.status_code
== requests
.codes
.created
:
904 result
['error'] = False
905 result
['data'] = Util
.json_loads_byteified(r
.text
)
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
)
914 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
915 except Exception as e
:
917 result
['data'] = str(e
)
919 if r
.status_code
== requests
.codes
.ok
:
920 result
['error'] = False
921 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
930 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
931 except Exception as e
:
933 result
['data'] = str(e
)
935 if r
.status_code
== requests
.codes
.accepted
:
936 result
['error'] = False
938 result
['data'] = r
.text
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)
948 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
949 except Exception as e
:
951 result
['data'] = str(e
)
953 if r
.status_code
== requests
.codes
.ok
:
954 result
['error'] = False
955 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
964 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
967 r
= requests
.post(_url
, json
=sdn_data
, verify
=False, headers
=headers
)
968 except Exception as e
:
970 result
['data'] = str(e
)
972 if r
.status_code
== requests
.codes
.created
:
973 result
['error'] = False
974 result
['data'] = Util
.json_loads_byteified(r
.text
)
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()