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_delete(self
, token
, id):
111 result
= {'error': True, 'data': ''}
112 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
113 'Authorization': 'Bearer {}'.format(token
['id'])}
115 _url
= "{0}/admin/v1/users/{1}".format(self
._base
_path
, id)
117 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
118 except Exception as e
:
120 result
['data'] = str(e
)
122 if r
.status_code
== requests
.codes
.no_content
:
123 result
['error'] = False
125 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
134 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
135 except Exception as e
:
137 result
['data'] = str(e
)
139 if r
.status_code
== requests
.codes
.ok
:
140 result
['error'] = False
141 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
149 _url
= "{0}/admin/v1/projects".format(self
._base
_path
)
151 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
152 except Exception as e
:
154 result
['data'] = str(e
)
156 if r
.status_code
== requests
.codes
.ok
:
157 result
['error'] = False
158 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
168 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
169 except Exception as e
:
171 result
['data'] = str(e
)
173 if r
.status_code
== requests
.codes
.ok
:
174 result
['error'] = False
175 result
['data'] = Util
.json_loads_byteified(r
.text
)
178 def project_create(self
, token
, project_data
):
180 result
= {'error': True, 'data': ''}
181 headers
= {"Content-Type": "application/json", "accept": "application/json",
182 'Authorization': 'Bearer {}'.format(token
['id'])}
184 _url
= "{0}/admin/v1/projects".format(self
._base
_path
)
187 r
= requests
.post(_url
, json
=project_data
, verify
=False, headers
=headers
)
188 except Exception as e
:
190 result
['data'] = str(e
)
192 if r
.status_code
== requests
.codes
.created
:
193 result
['error'] = False
194 result
['data'] = Util
.json_loads_byteified(r
.text
)
197 def project_edit(self
, token
, id, 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/{1}".format(self
._base
_path
, id)
206 r
= requests
.put(_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
.no_content
:
212 result
['error'] = False
213 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
221 _url
= "{0}/admin/v1/projects/{1}".format(self
._base
_path
, id)
223 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
224 except Exception as e
:
226 result
['data'] = str(e
)
228 if r
.status_code
== requests
.codes
.no_content
:
229 result
['error'] = False
231 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
239 _url
= "{0}/nsd/v1/ns_descriptors_content".format(self
._base
_path
)
241 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
242 except Exception as e
:
244 result
['data'] = str(e
)
246 if r
.status_code
== requests
.codes
.ok
:
247 result
['error'] = False
248 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
257 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
259 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
260 except Exception as e
:
262 result
['data'] = str(e
)
264 if r
.status_code
== requests
.codes
.ok
:
265 result
['error'] = False
266 result
['data'] = Util
.json_loads_byteified(r
.text
)
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
)
276 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
277 except Exception as e
:
279 result
['data'] = str(e
)
281 if r
.status_code
== requests
.codes
.ok
:
282 result
['error'] = False
283 result
['data'] = Util
.json_loads_byteified(r
.text
)
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
)
293 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
294 except Exception as e
:
296 result
['data'] = str(e
)
298 if r
.status_code
== requests
.codes
.ok
:
299 result
['error'] = False
300 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
309 _url
= "{0}/nsd/v1/ns_descriptors_content/{1}".format(self
._base
_path
, id)
311 r
= requests
.delete(_url
, params
=None, verify
=False,headers
=headers
)
312 except Exception as e
:
314 result
['data'] = str(e
)
316 if r
.status_code
== requests
.codes
.ok
:
317 result
['error'] = False
318 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
326 _url
= "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self
._base
_path
, id)
328 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
329 except Exception as e
:
331 result
['data'] = str(e
)
334 result
['error'] = False
335 if r
.status_code
!= requests
.codes
.no_content
:
336 result
['data'] = Util
.json_loads_byteified(r
.text
)
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
)
349 r
= requests
.post(_url
, data
=open('/tmp/'+package
.name
, 'rb'), verify
=False, headers
=headers
)
350 except Exception as e
:
352 result
['data'] = str(e
)
354 if r
.status_code
== requests
.codes
.created
:
355 result
['error'] = False
356 result
['data'] = Util
.json_loads_byteified(r
.text
)
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
)
369 r
= requests
.post(_url
, data
=open('/tmp/'+package
.name
, 'rb'), verify
=False, headers
=headers
)
370 except Exception as e
:
372 result
['data'] = str(e
)
374 if r
.status_code
== requests
.codes
.created
:
375 result
['error'] = False
376 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
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'))
390 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
393 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
395 except Exception as e
:
397 result
['data'] = str(e
)
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."
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'])}
411 # get the package onboarded
412 tar_pkg
= self
.get_vnfd_pkg(token
, id)
413 tarf
= tarfile
.open(fileobj
=tar_pkg
)
415 tarf
= self
._descriptor
_clone
(tarf
, 'vnfd')
416 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
418 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
421 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
423 except Exception as e
:
425 result
['data'] = str(e
)
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."
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'])}
439 # get the package onboarded
440 tar_pkg
= self
.get_nsd_pkg(token
, id)
441 tarf
= tarfile
.open(fileobj
=tar_pkg
)
443 tarf
= self
._descriptor
_update
(tarf
, data
)
444 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
446 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
449 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
451 except Exception as e
:
453 result
['data'] = str(e
)
455 if r
.status_code
== requests
.codes
.no_content
:
456 result
['error'] = False
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'])}
465 # get the package onboarded
466 tar_pkg
= self
.get_vnfd_pkg(token
, id)
467 tarf
= tarfile
.open(fileobj
=tar_pkg
)
469 tarf
= self
._descriptor
_update
(tarf
, data
)
470 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
472 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
475 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
477 except Exception as e
:
479 result
['data'] = str(e
)
481 if r
.status_code
== requests
.codes
.no_content
:
482 result
['error'] = False
486 def get_nsd_pkg(self
, token
, id):
487 result
= {'error': True, 'data': ''}
488 headers
= { "accept": "application/zip",
489 'Authorization': 'Bearer {}'.format(token
['id'])}
491 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
493 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
494 except Exception as e
:
496 result
['data'] = str(e
)
498 if r
.status_code
== requests
.codes
.ok
:
499 result
['error'] = False
500 tarf
= StringIO
.StringIO(r
.content
)
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)
510 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
511 except Exception as e
:
513 result
['data'] = str(e
)
515 if r
.status_code
== requests
.codes
.ok
:
516 result
['error'] = False
517 tarf
= StringIO
.StringIO(r
.content
)
521 def _descriptor_update(self
, tarf
, data
):
522 # extract the package on a tmp directory
523 tarf
.extractall('/tmp')
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)
531 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", "w:gz")
534 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
538 def _descriptor_clone(self
, tarf
, descriptor_type
):
539 # extract the package on a tmp directory
540 tarf
.extractall('/tmp')
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
)
547 if descriptor_type
== 'nsd':
548 nsd_list
= yaml_object
['nsd:nsd-catalog']['nsd']
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']
561 with
open('/tmp/' + name
, 'w') as yaml_file
:
562 yaml_file
.write(yaml
.dump(yaml_object
, default_flow_style
=False))
565 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", "w:gz")
568 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
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)
578 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
579 except Exception as e
:
581 result
['data'] = str(e
)
583 if r
.status_code
== requests
.codes
.ok
:
584 result
['error'] = False
585 return yaml
.load(r
.text
)
588 result
['data'] = r
.json()
589 except Exception as e
:
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)
599 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
600 except Exception as e
:
602 result
['data'] = str(e
)
604 if r
.status_code
== requests
.codes
.ok
:
605 result
['error'] = False
606 return yaml
.load(r
.text
)
609 result
['data'] = r
.json()
610 except Exception as e
:
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)
620 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
621 except Exception as e
:
623 result
['data'] = str(e
)
625 if r
.status_code
== requests
.codes
.ok
:
626 result
['error'] = False
627 result
['data'] = r
.text
630 result
['data'] = r
.json()
631 except Exception as e
:
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)
642 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
643 except Exception as e
:
645 result
['data'] = str(e
)
647 if r
.status_code
== requests
.codes
.ok
:
648 result
['error'] = False
649 result
['data'] = r
.text
652 result
['data'] = r
.json()
653 except Exception as e
:
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'])}
663 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
666 r
= requests
.post(_url
, json
=ns_data
, verify
=False, headers
=headers
)
667 except Exception as e
:
669 result
['data'] = str(e
)
671 if r
.status_code
== requests
.codes
.ok
:
672 result
['error'] = False
673 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
683 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
684 except Exception as e
:
686 result
['data'] = str(e
)
688 if r
.status_code
== requests
.codes
.ok
:
689 result
['error'] = False
690 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
701 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
702 except Exception as e
:
704 result
['data'] = str(e
)
706 if r
.status_code
== requests
.codes
.ok
:
707 result
['error'] = False
708 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
717 _url
= "{0}/nslcm/v1/ns_instances/{1}/action".format(self
._base
_path
, id)
720 r
= requests
.post(_url
, json
=action_payload
, verify
=False, headers
=headers
)
721 except Exception as e
:
723 result
['data'] = str(e
)
725 if r
.status_code
== requests
.codes
.created
:
726 result
['error'] = False
727 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
736 query_path
= '?FORCE=true'
737 _url
= "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self
._base
_path
, id, query_path
)
739 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
740 except Exception as e
:
742 result
['data'] = str(e
)
745 result
['error'] = False
746 if r
.status_code
!= requests
.codes
.no_content
:
747 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
757 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
758 except Exception as e
:
760 result
['data'] = str(e
)
762 if r
.status_code
== requests
.codes
.ok
:
763 result
['error'] = False
764 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
774 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
775 except Exception as e
:
777 result
['data'] = str(e
)
779 if r
.status_code
== requests
.codes
.ok
:
780 result
['error'] = False
781 result
['data'] = Util
.json_loads_byteified(r
.text
)
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
)
790 r
= requests
.post(_url
, json
=alarm_payload
, verify
=False, headers
=headers
)
791 except Exception as e
:
793 result
['data'] = str(e
)
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
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
)
807 r
= requests
.post(_url
, json
=metric_payload
, verify
=False, headers
=headers
)
808 except Exception as e
:
810 result
['data'] = str(e
)
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
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
)
824 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
825 except Exception as e
:
827 result
['data'] = str(e
)
829 if r
.status_code
== requests
.codes
.ok
:
830 result
['error'] = False
831 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
841 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
842 except Exception as e
:
844 result
['data'] = str(e
)
846 if r
.status_code
== requests
.codes
.accepted
:
847 result
['error'] = False
849 result
['data'] = r
.text
852 def vim_get(self
, token
, id):
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)
860 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
861 except Exception as e
:
863 result
['data'] = str(e
)
865 if r
.status_code
== requests
.codes
.ok
:
866 result
['error'] = False
867 result
['data'] = Util
.json_loads_byteified(r
.text
)
870 def vim_create(self
, token
, vim_data
):
872 result
= {'error': True, 'data': ''}
873 headers
= {"Content-Type": "application/json", "accept": "application/json",
874 'Authorization': 'Bearer {}'.format(token
['id'])}
876 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
879 r
= requests
.post(_url
, json
=vim_data
, verify
=False, headers
=headers
)
880 except Exception as e
:
882 result
['data'] = str(e
)
884 if r
.status_code
== requests
.codes
.created
:
885 result
['error'] = False
886 result
['data'] = Util
.json_loads_byteified(r
.text
)
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
)
895 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
896 except Exception as e
:
898 result
['data'] = str(e
)
900 if r
.status_code
== requests
.codes
.ok
:
901 result
['error'] = False
902 result
['data'] = Util
.json_loads_byteified(r
.text
)
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)
911 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
912 except Exception as e
:
914 result
['data'] = str(e
)
916 if r
.status_code
== requests
.codes
.accepted
:
917 result
['error'] = False
919 result
['data'] = r
.text
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)
929 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
930 except Exception as e
:
932 result
['data'] = str(e
)
934 if r
.status_code
== requests
.codes
.ok
:
935 result
['error'] = False
936 result
['data'] = Util
.json_loads_byteified(r
.text
)
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'])}
945 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
948 r
= requests
.post(_url
, json
=sdn_data
, verify
=False, headers
=headers
)
949 except Exception as e
:
951 result
['data'] = str(e
)
953 if r
.status_code
== requests
.codes
.created
:
954 result
['error'] = False
955 result
['data'] = Util
.json_loads_byteified(r
.text
)
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()