feb710cf494c778a49bc306c56332d5ec6c234e6
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
)
106 if r
.status_code
== requests
.codes
.created
:
107 result
['error'] = False
108 result
['data'] = Util
.json_loads_byteified(r
.text
)
111 def user_delete(self
, token
, id):
112 result
= {'error': True, 'data': ''}
113 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
114 'Authorization': 'Bearer {}'.format(token
['id'])}
116 _url
= "{0}/admin/v1/users/{1}".format(self
._base
_path
, id)
118 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
119 except Exception as e
:
121 result
['data'] = str(e
)
123 if r
.status_code
== requests
.codes
.no_content
:
124 result
['error'] = False
126 result
['data'] = Util
.json_loads_byteified(r
.text
)
129 def get_user_info(self
, token
, id):
130 result
= {'error': True, 'data': ''}
131 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
132 'Authorization': 'Bearer {}'.format(token
['id'])}
133 _url
= "{0}/admin/v1/users/{1}".format(self
._base
_path
, id)
135 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
136 except Exception as e
:
138 result
['data'] = str(e
)
140 if r
.status_code
== requests
.codes
.ok
:
141 result
['error'] = False
142 result
['data'] = Util
.json_loads_byteified(r
.text
)
145 def project_list(self
, token
):
146 result
= {'error': True, 'data': ''}
147 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
148 'Authorization': 'Bearer {}'.format(token
['id'])}
150 _url
= "{0}/admin/v1/projects".format(self
._base
_path
)
152 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
153 except Exception as e
:
155 result
['data'] = str(e
)
157 if r
.status_code
== requests
.codes
.ok
:
158 result
['error'] = False
159 result
['data'] = Util
.json_loads_byteified(r
.text
)
163 def project_get(self
, token
, id):
164 result
= {'error': True, 'data': ''}
165 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
166 'Authorization': 'Bearer {}'.format(token
['id'])}
167 _url
= "{0}/admin/v1/projects/{1}".format(self
._base
_path
, id)
169 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
170 except Exception as e
:
172 result
['data'] = str(e
)
174 if r
.status_code
== requests
.codes
.ok
:
175 result
['error'] = False
176 result
['data'] = Util
.json_loads_byteified(r
.text
)
179 def project_create(self
, token
, project_data
):
181 result
= {'error': True, 'data': ''}
182 headers
= {"Content-Type": "application/json", "accept": "application/json",
183 'Authorization': 'Bearer {}'.format(token
['id'])}
185 _url
= "{0}/admin/v1/projects".format(self
._base
_path
)
188 r
= requests
.post(_url
, json
=project_data
, verify
=False, headers
=headers
)
189 except Exception as e
:
191 result
['data'] = str(e
)
194 if r
.status_code
== requests
.codes
.created
:
195 result
['error'] = False
196 result
['data'] = Util
.json_loads_byteified(r
.text
)
199 def project_edit(self
, token
, id, project_data
):
201 result
= {'error': True, 'data': ''}
202 headers
= {"Content-Type": "application/json", "accept": "application/json",
203 'Authorization': 'Bearer {}'.format(token
['id'])}
205 _url
= "{0}/admin/v1/projects/{1}".format(self
._base
_path
, id)
208 r
= requests
.put(_url
, json
=project_data
, verify
=False, headers
=headers
)
209 except Exception as e
:
211 result
['data'] = str(e
)
214 if r
.status_code
== requests
.codes
.no_content
:
215 result
['error'] = False
216 result
['data'] = Util
.json_loads_byteified(r
.text
)
219 def project_delete(self
, token
, id):
220 result
= {'error': True, 'data': ''}
221 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
222 'Authorization': 'Bearer {}'.format(token
['id'])}
224 _url
= "{0}/admin/v1/projects/{1}".format(self
._base
_path
, id)
226 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
227 except Exception as e
:
229 result
['data'] = str(e
)
231 if r
.status_code
== requests
.codes
.no_content
:
232 result
['error'] = False
234 result
['data'] = Util
.json_loads_byteified(r
.text
)
237 def nsd_list(self
, token
):
238 result
= {'error': True, 'data': ''}
239 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
240 'Authorization': 'Bearer {}'.format(token
['id'])}
242 _url
= "{0}/nsd/v1/ns_descriptors_content".format(self
._base
_path
)
244 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
245 except Exception as e
:
247 result
['data'] = str(e
)
249 if r
.status_code
== requests
.codes
.ok
:
250 result
['error'] = False
251 result
['data'] = Util
.json_loads_byteified(r
.text
)
255 def vnfd_list(self
, token
):
256 result
= {'error': True, 'data': ''}
257 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
258 'Authorization': 'Bearer {}'.format(token
['id'])}
260 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
262 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
263 except Exception as e
:
265 result
['data'] = str(e
)
267 if r
.status_code
== requests
.codes
.ok
:
268 result
['error'] = False
269 result
['data'] = Util
.json_loads_byteified(r
.text
)
273 def ns_list(self
, token
):
274 result
= {'error': True, 'data': ''}
275 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
276 'Authorization': 'Bearer {}'.format(token
['id'])}
277 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
279 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
280 except Exception as e
:
282 result
['data'] = str(e
)
284 if r
.status_code
== requests
.codes
.ok
:
285 result
['error'] = False
286 result
['data'] = Util
.json_loads_byteified(r
.text
)
290 def vnf_list(self
, token
):
291 result
= {'error': True, 'data': ''}
292 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
293 'Authorization': 'Bearer {}'.format(token
['id'])}
294 _url
= "{0}/nslcm/v1/vnfrs".format(self
._base
_path
)
296 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
297 except Exception as e
:
299 result
['data'] = str(e
)
301 if r
.status_code
== requests
.codes
.ok
:
302 result
['error'] = False
303 result
['data'] = Util
.json_loads_byteified(r
.text
)
307 def nsd_delete(self
, token
, id):
308 result
= {'error': True, 'data': ''}
309 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
310 'Authorization': 'Bearer {}'.format(token
['id'])}
312 _url
= "{0}/nsd/v1/ns_descriptors_content/{1}".format(self
._base
_path
, id)
314 r
= requests
.delete(_url
, params
=None, verify
=False,headers
=headers
)
315 except Exception as e
:
317 result
['data'] = str(e
)
319 if r
.status_code
== requests
.codes
.ok
:
320 result
['error'] = False
321 result
['data'] = Util
.json_loads_byteified(r
.text
)
324 def vnfd_delete(self
, token
, id):
325 result
= {'error': True, 'data': ''}
326 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
327 'Authorization': 'Bearer {}'.format(token
['id'])}
329 _url
= "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self
._base
_path
, id)
331 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
332 except Exception as e
:
334 result
['data'] = str(e
)
337 result
['error'] = False
338 if r
.status_code
!= requests
.codes
.no_content
:
339 result
['data'] = Util
.json_loads_byteified(r
.text
)
342 def nsd_onboard(self
, token
, package
):
343 result
= {'error': True, 'data': ''}
344 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
345 'Authorization': 'Bearer {}'.format(token
['id'])}
346 with
open('/tmp/'+package
.name
, 'wb+') as destination
:
347 for chunk
in package
.chunks():
348 destination
.write(chunk
)
349 headers
['Content-File-MD5'] = self
.md5(open('/tmp/'+package
.name
, 'rb'))
350 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
352 r
= requests
.post(_url
, data
=open('/tmp/'+package
.name
, 'rb'), verify
=False, headers
=headers
)
353 except Exception as e
:
355 result
['data'] = str(e
)
357 if r
.status_code
== requests
.codes
.created
:
358 result
['error'] = False
359 result
['data'] = Util
.json_loads_byteified(r
.text
)
362 def vnfd_onboard(self
, token
, package
):
363 result
= {'error': True, 'data': ''}
364 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
365 'Authorization': 'Bearer {}'.format(token
['id'])}
366 with
open('/tmp/'+package
.name
, 'wb+') as destination
:
367 for chunk
in package
.chunks():
368 destination
.write(chunk
)
369 headers
['Content-File-MD5'] = self
.md5(open('/tmp/'+package
.name
, 'rb'))
370 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
372 r
= requests
.post(_url
, data
=open('/tmp/'+package
.name
, 'rb'), verify
=False, headers
=headers
)
373 except Exception as e
:
375 result
['data'] = str(e
)
377 if r
.status_code
== requests
.codes
.created
:
378 result
['error'] = False
379 result
['data'] = Util
.json_loads_byteified(r
.text
)
382 def nsd_update(self
, token
, id, data
):
383 result
= {'error': True, 'data': ''}
384 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
385 'Authorization': 'Bearer {}'.format(token
['id'])}
387 # get the package onboarded
388 tar_pkg
= self
.get_nsd_pkg(token
, id)
389 tarf
= tarfile
.open(fileobj
=tar_pkg
)
391 tarf
= self
._descriptor
_update
(tarf
, data
)
392 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
394 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
397 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
399 except Exception as e
:
401 result
['data'] = str(e
)
403 if r
.status_code
== requests
.codes
.no_content
:
404 result
['error'] = False
408 def vnfd_update(self
, token
, id, data
):
409 result
= {'error': True, 'data': ''}
410 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
411 'Authorization': 'Bearer {}'.format(token
['id'])}
413 # get the package onboarded
414 tar_pkg
= self
.get_vnfd_pkg(token
, id)
415 tarf
= tarfile
.open(fileobj
=tar_pkg
)
417 tarf
= self
._descriptor
_update
(tarf
, data
)
418 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
420 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
423 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
425 except Exception as e
:
427 result
['data'] = str(e
)
429 if r
.status_code
== requests
.codes
.no_content
:
430 result
['error'] = False
434 def get_nsd_pkg(self
, token
, id):
435 result
= {'error': True, 'data': ''}
436 headers
= { "accept": "application/zip",
437 'Authorization': 'Bearer {}'.format(token
['id'])}
439 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
441 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
442 except Exception as e
:
444 result
['data'] = str(e
)
446 if r
.status_code
== requests
.codes
.ok
:
447 result
['error'] = False
448 tarf
= StringIO
.StringIO(r
.content
)
452 def get_vnfd_pkg(self
, token
, id):
453 result
= {'error': True, 'data': ''}
454 headers
= {"accept": "application/zip",
455 'Authorization': 'Bearer {}'.format(token
['id'])}
456 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
458 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
460 except Exception as e
:
462 result
['data'] = str(e
)
464 if r
.status_code
== requests
.codes
.ok
:
465 result
['error'] = False
466 tarf
= StringIO
.StringIO(r
.content
)
470 def _descriptor_update(self
, tarf
, data
):
471 print tarf
.getnames()
472 # extract the package on a tmp directory
473 tarf
.extractall('/tmp')
475 for name
in tarf
.getnames():
476 if name
.endswith(".yaml") or name
.endswith(".yml"):
477 with
open('/tmp/' + name
, 'w') as outfile
:
478 yaml
.safe_dump(data
, outfile
, default_flow_style
=False)
481 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", "w:gz")
482 # tarf_temp = tarfile.open("pippo.tar.gz", "w:gz")
483 print tarf_temp
.getnames()
484 # tarf_temp.add('/tmp/'+tarf.getnames()[0])
486 # if tarinfo.name.startswith(tarf.getnames()[0]):
487 # new_name = tarinfo.name[len(tarf.getnames()[0]):]
488 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
489 print tarf_temp
.getnames()
493 def nsd_get(self
, token
, id):
494 result
= {'error': True, 'data': ''}
495 headers
= {'Content-Type': 'application/yaml',
496 'Authorization': 'Bearer {}'.format(token
['id'])}
497 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self
._base
_path
, id)
499 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
500 except Exception as e
:
502 result
['data'] = str(e
)
504 if r
.status_code
== requests
.codes
.ok
:
505 result
['error'] = False
506 return yaml
.load(r
.text
)
509 result
['data'] = r
.json()
510 except Exception as e
:
514 def vnfd_get(self
, token
, id):
515 result
= {'error': True, 'data': ''}
516 headers
= {'Content-Type': 'application/yaml',
517 'Authorization': 'Bearer {}'.format(token
['id'])}
518 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self
._base
_path
, id)
520 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
521 except Exception as e
:
523 result
['data'] = str(e
)
525 if r
.status_code
== requests
.codes
.ok
:
526 result
['error'] = False
527 return yaml
.load(r
.text
)
530 result
['data'] = r
.json()
531 except Exception as e
:
535 def nsd_artifacts(self
, token
, id):
536 result
= {'error': True, 'data': ''}
537 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
538 'Authorization': 'Bearer {}'.format(token
['id'])}
539 _url
= "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self
._base
_path
, id)
541 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
542 except Exception as e
:
544 result
['data'] = str(e
)
546 if r
.status_code
== requests
.codes
.ok
:
547 result
['error'] = False
548 result
['data'] = r
.text
551 result
['data'] = r
.json()
552 except Exception as e
:
557 def vnf_packages_artifacts(self
, token
, id):
558 result
= {'error': True, 'data': ''}
559 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
560 'Authorization': 'Bearer {}'.format(token
['id'])}
561 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self
._base
_path
, id)
563 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
564 except Exception as e
:
566 result
['data'] = str(e
)
568 if r
.status_code
== requests
.codes
.ok
:
569 result
['error'] = False
570 result
['data'] = r
.text
573 result
['data'] = r
.json()
574 except Exception as e
:
579 def ns_create(self
, token
, ns_data
):
580 result
= {'error': True, 'data': ''}
581 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
582 'Authorization': 'Bearer {}'.format(token
['id'])}
584 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
587 r
= requests
.post(_url
, json
=ns_data
, verify
=False, headers
=headers
)
588 except Exception as e
:
590 result
['data'] = str(e
)
592 if r
.status_code
== requests
.codes
.ok
:
593 result
['error'] = False
594 result
['data'] = Util
.json_loads_byteified(r
.text
)
597 def ns_op_list(self
, token
, id):
598 result
= {'error': True, 'data': ''}
599 headers
= {"Content-Type": "application/json", "accept": "application/json",
600 'Authorization': 'Bearer {}'.format(token
['id'])}
601 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self
._base
_path
, id)
604 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
605 except Exception as e
:
607 result
['data'] = str(e
)
609 if r
.status_code
== requests
.codes
.ok
:
610 result
['error'] = False
611 result
['data'] = Util
.json_loads_byteified(r
.text
)
615 def ns_op(self
, token
, id):
616 result
= {'error': True, 'data': ''}
617 headers
= {"Content-Type": "application/json", "accept": "application/json",
618 'Authorization': 'Bearer {}'.format(token
['id'])}
619 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self
._base
_path
, id)
622 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
623 except Exception as e
:
625 result
['data'] = str(e
)
627 if r
.status_code
== requests
.codes
.ok
:
628 result
['error'] = False
629 result
['data'] = Util
.json_loads_byteified(r
.text
)
633 def ns_action(self
, token
, id, action_payload
):
634 result
= {'error': True, 'data': ''}
635 headers
= {"Content-Type": "application/json", "accept": "application/json",
636 'Authorization': 'Bearer {}'.format(token
['id'])}
638 _url
= "{0}/nslcm/v1/ns_instances/{1}/action".format(self
._base
_path
, id)
641 r
= requests
.post(_url
, json
=action_payload
, verify
=False, headers
=headers
)
642 except Exception as e
:
644 result
['data'] = str(e
)
647 if r
.status_code
== requests
.codes
.created
:
648 result
['error'] = False
649 result
['data'] = Util
.json_loads_byteified(r
.text
)
652 def ns_delete(self
, token
, id, force
=None):
653 result
= {'error': True, 'data': ''}
654 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
655 'Authorization': 'Bearer {}'.format(token
['id'])}
658 query_path
= '?FORCE=true'
659 _url
= "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self
._base
_path
, id, query_path
)
661 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
662 except Exception as e
:
664 result
['data'] = str(e
)
667 result
['error'] = False
668 if r
.status_code
!= requests
.codes
.no_content
:
669 result
['data'] = Util
.json_loads_byteified(r
.text
)
672 def ns_get(self
, token
, id):
673 result
= {'error': True, 'data': ''}
674 headers
= {"Content-Type": "application/json", "accept": "application/json",
675 'Authorization': 'Bearer {}'.format(token
['id'])}
676 _url
= "{0}/nslcm/v1/ns_instances_content/{1}".format(self
._base
_path
, id)
679 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
680 except Exception as e
:
682 result
['data'] = str(e
)
684 if r
.status_code
== requests
.codes
.ok
:
685 result
['error'] = False
686 result
['data'] = Util
.json_loads_byteified(r
.text
)
689 def vnf_get(self
, token
, id):
690 result
= {'error': True, 'data': ''}
691 headers
= {"Content-Type": "application/json", "accept": "application/json",
692 'Authorization': 'Bearer {}'.format(token
['id'])}
693 _url
= "{0}/nslcm/v1/vnfrs/{1}".format(self
._base
_path
, id)
696 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
697 except Exception as e
:
699 result
['data'] = str(e
)
701 if r
.status_code
== requests
.codes
.ok
:
702 result
['error'] = False
703 result
['data'] = Util
.json_loads_byteified(r
.text
)
706 def ns_alarm_create(self
, token
, id, alarm_payload
):
707 result
= {'error': True, 'data': ''}
708 headers
= {"Content-Type": "application/json",
709 'Authorization': 'Bearer {}'.format(token
['id'])}
710 _url
= "{0}/test/message/alarm_request".format(self
._base
_path
)
712 r
= requests
.post(_url
, json
=alarm_payload
, verify
=False, headers
=headers
)
713 except Exception as e
:
715 result
['data'] = str(e
)
718 if r
.status_code
== requests
.codes
.ok
:
719 result
['error'] = False
720 #result['data'] = Util.json_loads_byteified(r.text)
721 result
['data'] = r
.text
724 def ns_metric_export(self
, token
, id, metric_payload
):
725 result
= {'error': True, 'data': ''}
726 headers
= {"Content-Type": "application/json",
727 'Authorization': 'Bearer {}'.format(token
['id'])}
728 _url
= "{0}/test/message/metric_request".format(self
._base
_path
)
730 r
= requests
.post(_url
, json
=metric_payload
, verify
=False, headers
=headers
)
731 except Exception as e
:
733 result
['data'] = str(e
)
736 if r
.status_code
== requests
.codes
.ok
:
737 result
['error'] = False
738 #result['data'] = Util.json_loads_byteified(r.text)
739 result
['data'] = r
.text
742 def vim_list(self
, token
):
743 result
= {'error': True, 'data': ''}
744 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
745 'Authorization': 'Bearer {}'.format(token
['id'])}
746 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
748 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
749 except Exception as e
:
751 result
['data'] = str(e
)
753 if r
.status_code
== requests
.codes
.ok
:
754 result
['error'] = False
755 result
['data'] = Util
.json_loads_byteified(r
.text
)
759 def vim_delete(self
, token
, id):
760 result
= {'error': True, 'data': ''}
761 headers
= { "accept": "application/json",
762 'Authorization': 'Bearer {}'.format(token
['id'])}
763 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
765 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
766 except Exception as e
:
768 result
['data'] = str(e
)
771 if r
.status_code
== requests
.codes
.accepted
:
772 result
['error'] = False
774 result
['data'] = r
.text
777 def vim_get(self
, token
, id):
779 result
= {'error': True, 'data': ''}
780 headers
= {"Content-Type": "application/json", "accept": "application/json",
781 'Authorization': 'Bearer {}'.format(token
['id'])}
782 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
785 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
786 except Exception as e
:
788 result
['data'] = str(e
)
790 if r
.status_code
== requests
.codes
.ok
:
791 result
['error'] = False
792 result
['data'] = Util
.json_loads_byteified(r
.text
)
795 def vim_create(self
, token
, vim_data
):
797 result
= {'error': True, 'data': ''}
798 headers
= {"Content-Type": "application/json", "accept": "application/json",
799 'Authorization': 'Bearer {}'.format(token
['id'])}
801 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
804 r
= requests
.post(_url
, json
=vim_data
, verify
=False, headers
=headers
)
805 except Exception as e
:
807 result
['data'] = str(e
)
810 if r
.status_code
== requests
.codes
.created
:
811 result
['error'] = False
812 result
['data'] = Util
.json_loads_byteified(r
.text
)
815 def sdn_list(self
, token
):
816 result
= {'error': True, 'data': ''}
817 headers
= {"accept": "application/json",
818 'Authorization': 'Bearer {}'.format(token
['id'])}
819 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
821 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
822 except Exception as e
:
824 result
['data'] = str(e
)
826 if r
.status_code
== requests
.codes
.ok
:
827 result
['error'] = False
828 result
['data'] = Util
.json_loads_byteified(r
.text
)
831 def sdn_delete(self
, token
, id):
832 result
= {'error': True, 'data': ''}
833 headers
= {"accept": "application/json",
834 'Authorization': 'Bearer {}'.format(token
['id'])}
835 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
837 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
838 except Exception as e
:
840 result
['data'] = str(e
)
843 if r
.status_code
== requests
.codes
.accepted
:
844 result
['error'] = False
846 result
['data'] = r
.text
849 def sdn_get(self
, token
, id):
850 result
= {'error': True, 'data': ''}
851 headers
= {"accept": "application/json",
852 'Authorization': 'Bearer {}'.format(token
['id'])}
853 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
856 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
857 except Exception as e
:
859 result
['data'] = str(e
)
861 if r
.status_code
== requests
.codes
.ok
:
862 result
['error'] = False
863 result
['data'] = Util
.json_loads_byteified(r
.text
)
867 def sdn_create(self
, token
, sdn_data
):
868 result
= {'error': True, 'data': ''}
869 headers
= {"Content-Type": "application/json", "accept": "application/json",
870 'Authorization': 'Bearer {}'.format(token
['id'])}
872 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
875 r
= requests
.post(_url
, json
=sdn_data
, verify
=False, headers
=headers
)
876 except Exception as e
:
878 result
['data'] = str(e
)
881 if r
.status_code
== requests
.codes
.created
:
882 result
['error'] = False
883 result
['data'] = Util
.json_loads_byteified(r
.text
)
889 hash_md5
= hashlib
.md5()
890 for chunk
in iter(lambda: f
.read(1024), b
""):
891 hash_md5
.update(chunk
)
892 return hash_md5
.hexdigest()