6307198488acb543f6532f5ba19d9acac79f2099
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.
22 from lib
.util
import Util
25 from requests
.packages
.urllib3
.exceptions
import InsecureRequestWarning
27 requests
.packages
.urllib3
.disable_warnings(InsecureRequestWarning
)
29 logging
.basicConfig(level
=logging
.INFO
)
30 log
= logging
.getLogger('helper.py')
31 logging
.getLogger("urllib3").setLevel(logging
.INFO
)
36 self
._token
_endpoint
= 'admin/v1/tokens'
37 self
._user
_endpoint
= 'admin/v1/users'
38 self
._host
= os
.getenv('OSM_SERVER', "localhost")
40 self
._base
_path
= 'https://{0}:{1}/osm'.format(self
._host
, self
._so
_port
)
43 result
= {'error': True, 'data': ''}
44 token_url
= "{0}/{1}".format(self
._base
_path
, self
._token
_endpoint
)
45 headers
= {"Content-Type": "application/yaml", "accept": "application/json"}
47 r
= requests
.post(token_url
, json
=args
, verify
=False, headers
=headers
)
48 except Exception as e
:
50 result
['data'] = str(e
)
52 if r
.status_code
== requests
.codes
.ok
:
53 result
['error'] = False
55 result
['data'] = Util
.json_loads_byteified(r
.text
)
59 def switch_project(self
, args
):
60 result
= {'error': True, 'data': ''}
61 token_url
= "{0}/{1}".format(self
._base
_path
, self
._token
_endpoint
)
62 headers
= {"Content-Type": "application/yaml", "accept": "application/json"}
64 r
= requests
.post(token_url
, json
=args
, verify
=False, headers
=headers
)
65 except Exception as e
:
67 result
['data'] = str(e
)
69 if r
.status_code
== requests
.codes
.ok
:
70 result
['error'] = False
72 result
['data'] = Util
.json_loads_byteified(r
.text
)
76 def user_list(self
, token
):
77 result
= {'error': True, 'data': ''}
78 headers
= {"Content-Type": "application/json", "accept": "application/json",
79 'Authorization': 'Bearer {}'.format(token
['id'])}
81 _url
= "{0}/admin/v1/users".format(self
._base
_path
)
83 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
84 except Exception as e
:
86 result
['data'] = str(e
)
88 if r
.status_code
== requests
.codes
.ok
:
89 result
['error'] = False
90 result
['data'] = Util
.json_loads_byteified(r
.text
)
94 def user_create(self
, token
, user_data
):
95 result
= {'error': True, 'data': ''}
96 headers
= {"Content-Type": "application/json", "accept": "application/json",
97 'Authorization': 'Bearer {}'.format(token
['id'])}
99 _url
= "{0}/admin/v1/users".format(self
._base
_path
)
102 r
= requests
.post(_url
, json
=user_data
, verify
=False, headers
=headers
)
103 except Exception as e
:
105 result
['data'] = str(e
)
107 if r
.status_code
== requests
.codes
.created
:
108 result
['error'] = False
109 result
['data'] = Util
.json_loads_byteified(r
.text
)
112 def user_update(self
, token
, id, user_data
):
113 result
= {'error': True, 'data': ''}
114 headers
= {"Content-Type": "application/json", "accept": "application/json",
115 'Authorization': 'Bearer {}'.format(token
['id'])}
117 _url
= "{0}/admin/v1/users/{1}".format(self
._base
_path
, id)
119 r
= requests
.patch(_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
.no_content
:
125 result
['error'] = False
127 result
['data'] = Util
.json_loads_byteified(r
.text
)
130 def user_delete(self
, token
, id):
131 result
= {'error': True, 'data': ''}
132 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
133 'Authorization': 'Bearer {}'.format(token
['id'])}
135 _url
= "{0}/admin/v1/users/{1}".format(self
._base
_path
, id)
137 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
138 except Exception as e
:
140 result
['data'] = str(e
)
142 if r
.status_code
== requests
.codes
.no_content
:
143 result
['error'] = False
145 result
['data'] = Util
.json_loads_byteified(r
.text
)
148 def get_user_info(self
, token
, id):
149 result
= {'error': True, 'data': ''}
150 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
151 'Authorization': 'Bearer {}'.format(token
['id'])}
152 _url
= "{0}/admin/v1/users/{1}".format(self
._base
_path
, id)
154 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
155 except Exception as e
:
157 result
['data'] = str(e
)
159 if r
.status_code
== requests
.codes
.ok
:
160 result
['error'] = False
161 result
['data'] = Util
.json_loads_byteified(r
.text
)
164 def project_list(self
, token
):
165 result
= {'error': True, 'data': ''}
166 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
167 'Authorization': 'Bearer {}'.format(token
['id'])}
169 _url
= "{0}/admin/v1/projects".format(self
._base
_path
)
171 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
172 except Exception as e
:
174 result
['data'] = str(e
)
176 if r
.status_code
== requests
.codes
.ok
:
177 result
['error'] = False
178 result
['data'] = Util
.json_loads_byteified(r
.text
)
182 def project_get(self
, token
, id):
183 result
= {'error': True, 'data': ''}
184 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
185 'Authorization': 'Bearer {}'.format(token
['id'])}
186 _url
= "{0}/admin/v1/projects/{1}".format(self
._base
_path
, id)
188 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
189 except Exception as e
:
191 result
['data'] = str(e
)
193 if r
.status_code
== requests
.codes
.ok
:
194 result
['error'] = False
195 result
['data'] = Util
.json_loads_byteified(r
.text
)
198 def project_create(self
, token
, project_data
):
200 result
= {'error': True, 'data': ''}
201 headers
= {"Content-Type": "application/json", "accept": "application/json",
202 'Authorization': 'Bearer {}'.format(token
['id'])}
204 _url
= "{0}/admin/v1/projects".format(self
._base
_path
)
207 r
= requests
.post(_url
, json
=project_data
, verify
=False, headers
=headers
)
208 except Exception as e
:
210 result
['data'] = str(e
)
212 if r
.status_code
== requests
.codes
.created
:
213 result
['error'] = False
214 result
['data'] = Util
.json_loads_byteified(r
.text
)
217 def project_edit(self
, token
, id, project_data
):
219 result
= {'error': True, 'data': ''}
220 headers
= {"Content-Type": "application/json", "accept": "application/json",
221 'Authorization': 'Bearer {}'.format(token
['id'])}
223 _url
= "{0}/admin/v1/projects/{1}".format(self
._base
_path
, id)
226 r
= requests
.put(_url
, json
=project_data
, 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
233 result
['data'] = Util
.json_loads_byteified(r
.text
)
236 def project_delete(self
, token
, id):
237 result
= {'error': True, 'data': ''}
238 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
239 'Authorization': 'Bearer {}'.format(token
['id'])}
241 _url
= "{0}/admin/v1/projects/{1}".format(self
._base
_path
, id)
243 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
244 except Exception as e
:
246 result
['data'] = str(e
)
248 if r
.status_code
== requests
.codes
.no_content
:
249 result
['error'] = False
251 result
['data'] = Util
.json_loads_byteified(r
.text
)
254 def nsd_list(self
, token
, filter=None):
255 result
= {'error': True, 'data': ''}
256 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
257 'Authorization': 'Bearer {}'.format(token
['id'])}
260 query_path
= '?_admin.type='+filter
261 _url
= "{0}/nsd/v1/ns_descriptors_content{1}".format(self
._base
_path
, query_path
)
263 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
264 except Exception as e
:
266 result
['data'] = str(e
)
268 if r
.status_code
== requests
.codes
.ok
:
269 result
['error'] = False
270 result
['data'] = Util
.json_loads_byteified(r
.text
)
274 def vnfd_list(self
, token
, filter=None):
275 result
= {'error': True, 'data': ''}
276 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
277 'Authorization': 'Bearer {}'.format(token
['id'])}
280 query_path
= '?_admin.type='+filter
281 _url
= "{0}/vnfpkgm/v1/vnf_packages_content{1}".format(self
._base
_path
, query_path
)
283 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
284 except Exception as e
:
286 result
['data'] = str(e
)
288 if r
.status_code
== requests
.codes
.ok
:
289 result
['error'] = False
290 result
['data'] = Util
.json_loads_byteified(r
.text
)
294 def ns_list(self
, token
):
295 result
= {'error': True, 'data': ''}
296 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
297 'Authorization': 'Bearer {}'.format(token
['id'])}
298 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
300 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
301 except Exception as e
:
303 result
['data'] = str(e
)
305 if r
.status_code
== requests
.codes
.ok
:
306 result
['error'] = False
307 result
['data'] = Util
.json_loads_byteified(r
.text
)
311 def vnf_list(self
, token
):
312 result
= {'error': True, 'data': ''}
313 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
314 'Authorization': 'Bearer {}'.format(token
['id'])}
315 _url
= "{0}/nslcm/v1/vnfrs".format(self
._base
_path
)
317 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
318 except Exception as e
:
320 result
['data'] = str(e
)
322 if r
.status_code
== requests
.codes
.ok
:
323 result
['error'] = False
324 result
['data'] = Util
.json_loads_byteified(r
.text
)
328 def nsd_delete(self
, token
, id):
329 result
= {'error': True, 'data': ''}
330 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
331 'Authorization': 'Bearer {}'.format(token
['id'])}
333 _url
= "{0}/nsd/v1/ns_descriptors_content/{1}".format(self
._base
_path
, id)
335 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
336 except Exception as e
:
338 result
['data'] = str(e
)
340 if r
.status_code
== requests
.codes
.ok
:
341 result
['error'] = False
342 result
['data'] = Util
.json_loads_byteified(r
.text
)
345 def vnfd_delete(self
, token
, id):
346 result
= {'error': True, 'data': ''}
347 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
348 'Authorization': 'Bearer {}'.format(token
['id'])}
350 _url
= "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self
._base
_path
, id)
352 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
353 except Exception as e
:
355 result
['data'] = str(e
)
358 result
['error'] = False
359 if r
.status_code
!= requests
.codes
.no_content
:
360 result
['data'] = Util
.json_loads_byteified(r
.text
)
363 def nsd_onboard(self
, token
, package
):
364 result
= {'error': True, 'data': ''}
365 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
366 'Authorization': 'Bearer {}'.format(token
['id'])}
367 with
open('/tmp/' + package
.name
, 'wb+') as destination
:
368 for chunk
in package
.chunks():
369 destination
.write(chunk
)
370 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + package
.name
, 'rb'))
371 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
373 r
= requests
.post(_url
, data
=open('/tmp/' + package
.name
, 'rb'), verify
=False, headers
=headers
)
374 except Exception as e
:
376 result
['data'] = str(e
)
378 if r
.status_code
== requests
.codes
.created
:
379 result
['error'] = False
380 result
['data'] = Util
.json_loads_byteified(r
.text
)
383 def vnfd_onboard(self
, token
, package
):
384 result
= {'error': True, 'data': ''}
385 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
386 'Authorization': 'Bearer {}'.format(token
['id'])}
387 with
open('/tmp/' + package
.name
, 'wb+') as destination
:
388 for chunk
in package
.chunks():
389 destination
.write(chunk
)
390 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + package
.name
, 'rb'))
391 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
393 r
= requests
.post(_url
, data
=open('/tmp/' + package
.name
, 'rb'), verify
=False, headers
=headers
)
394 except Exception as e
:
396 result
['data'] = str(e
)
398 if r
.status_code
== requests
.codes
.created
:
399 result
['error'] = False
400 result
['data'] = Util
.json_loads_byteified(r
.text
)
403 def nsd_create_pkg_base(self
, token
, pkg_name
):
404 result
= {'error': True, 'data': ''}
405 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
406 'Authorization': 'Bearer {}'.format(token
['id'])}
408 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
411 self
._create
_base
_pkg
('nsd', pkg_name
)
412 headers
['Content-Filename'] = pkg_name
+ '.tar.gz'
413 r
= requests
.post(_url
, data
=open('/tmp/' + pkg_name
+ '.tar.gz', 'rb'), verify
=False, headers
=headers
)
414 except Exception as e
:
416 result
['data'] = str(e
)
418 if r
.status_code
== requests
.codes
.created
:
419 result
['data'] = r
.json()
420 result
['error'] = False
421 if r
.status_code
== requests
.codes
.conflict
:
422 result
['data'] = "Invalid ID."
425 def vnfd_create_pkg_base(self
, token
, pkg_name
):
426 result
= {'error': True, 'data': ''}
427 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
428 'Authorization': 'Bearer {}'.format(token
['id'])}
430 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
433 self
._create
_base
_pkg
('vnfd', pkg_name
)
434 r
= requests
.post(_url
, data
=open('/tmp/' + pkg_name
+ '.tar.gz', 'rb'), verify
=False, headers
=headers
)
435 except Exception as e
:
437 result
['data'] = str(e
)
439 if r
.status_code
== requests
.codes
.created
:
440 result
['data'] = r
.json()
441 result
['error'] = False
442 if r
.status_code
== requests
.codes
.conflict
:
443 result
['data'] = "Invalid ID."
446 def nsd_clone(self
, token
, id):
447 result
= {'error': True, 'data': ''}
448 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
449 'Authorization': 'Bearer {}'.format(token
['id'])}
451 # get the package onboarded
452 tar_pkg
= self
.get_nsd_pkg(token
, id)
453 tarf
= tarfile
.open(fileobj
=tar_pkg
)
454 tarf
= self
._descriptor
_clone
(tarf
, 'nsd')
455 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
457 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
460 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
462 except Exception as e
:
464 result
['data'] = str(e
)
466 if r
.status_code
== requests
.codes
.created
:
467 result
['error'] = False
468 if r
.status_code
== requests
.codes
.conflict
:
469 result
['data'] = "Invalid ID."
473 def vnfd_clone(self
, token
, id):
474 result
= {'error': True, 'data': ''}
475 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
476 'Authorization': 'Bearer {}'.format(token
['id'])}
478 # get the package onboarded
479 tar_pkg
= self
.get_vnfd_pkg(token
, id)
480 tarf
= tarfile
.open(fileobj
=tar_pkg
)
482 tarf
= self
._descriptor
_clone
(tarf
, 'vnfd')
483 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
485 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
488 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
490 except Exception as e
:
492 result
['data'] = str(e
)
494 if r
.status_code
== requests
.codes
.created
:
495 result
['error'] = False
496 if r
.status_code
== requests
.codes
.conflict
:
497 result
['data'] = "Invalid ID."
501 def nsd_update(self
, token
, id, data
):
502 result
= {'error': True, 'data': ''}
503 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
504 'Authorization': 'Bearer {}'.format(token
['id'])}
506 # get the package onboarded
507 tar_pkg
= self
.get_nsd_pkg(token
, id)
508 tarf
= tarfile
.open(fileobj
=tar_pkg
)
510 tarf
= self
._descriptor
_update
(tarf
, data
)
511 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
513 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
516 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
518 except Exception as e
:
520 result
['data'] = str(e
)
522 if r
.status_code
== requests
.codes
.no_content
:
523 result
['error'] = False
526 result
['data'] = r
.json()
527 except Exception as e
:
532 def vnfd_update(self
, token
, id, data
):
533 result
= {'error': True, 'data': ''}
534 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
535 'Authorization': 'Bearer {}'.format(token
['id'])}
537 # get the package onboarded
538 tar_pkg
= self
.get_vnfd_pkg(token
, id)
539 tarf
= tarfile
.open(fileobj
=tar_pkg
)
541 tarf
= self
._descriptor
_update
(tarf
, data
)
542 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
544 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
547 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
549 except Exception as e
:
551 result
['data'] = str(e
)
553 if r
.status_code
== requests
.codes
.no_content
:
554 result
['error'] = False
557 result
['data'] = r
.json()
558 except Exception as e
:
563 def get_nsd_pkg(self
, token
, id):
564 result
= {'error': True, 'data': ''}
565 headers
= {"accept": "application/zip",
566 'Authorization': 'Bearer {}'.format(token
['id'])}
568 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
570 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
571 except Exception as e
:
573 result
['data'] = str(e
)
575 if r
.status_code
== requests
.codes
.ok
:
576 result
['error'] = False
577 tarf
= StringIO
.StringIO(r
.content
)
581 def get_vnfd_pkg(self
, token
, id):
582 result
= {'error': True, 'data': ''}
583 headers
= {"accept": "application/zip",
584 'Authorization': 'Bearer {}'.format(token
['id'])}
585 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
587 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, 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 tarf
= StringIO
.StringIO(r
.content
)
598 def _descriptor_update(self
, tarf
, data
):
599 # extract the package on a tmp directory
600 tarf
.extractall('/tmp')
602 for name
in tarf
.getnames():
603 if name
.endswith(".yaml") or name
.endswith(".yml"):
604 with
open('/tmp/' + name
, 'w') as outfile
:
605 yaml
.safe_dump(data
, outfile
, default_flow_style
=False)
608 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", "w:gz")
611 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
615 def _create_base_pkg(self
, descriptor_type
, pkg_name
):
616 filename
= '/tmp/'+pkg_name
+'/' + pkg_name
+ '.yaml'
617 if descriptor_type
== 'nsd':
622 "short-name": str(pkg_name
),
623 "vendor": "OSM Composer",
624 "description": str(pkg_name
) + " descriptor",
626 "constituent-vnfd": [],
629 "name": str(pkg_name
)
635 elif descriptor_type
== 'vnfd':
637 "vnfd:vnfd-catalog": {
640 "short-name": str(pkg_name
),
649 "connection-point": [],
650 "name": str(pkg_name
)
656 if not os
.path
.exists(os
.path
.dirname(filename
)):
658 os
.makedirs(os
.path
.dirname(filename
))
659 except OSError as exc
: # Guard against race condition
660 if exc
.errno
!= errno
.EEXIST
:
663 with
open('/tmp/' + pkg_name
+ '/' + pkg_name
+ '.yaml', 'w') as yaml_file
:
664 yaml_file
.write(yaml
.dump(descriptor
, default_flow_style
=False))
666 tarf_temp
= tarfile
.open('/tmp/' + pkg_name
+ '.tar.gz', "w:gz")
667 tarf_temp
.add('/tmp/'+pkg_name
+'/' + pkg_name
+ '.yaml', pkg_name
+ '/' + pkg_name
+ '.yaml', recursive
=False)
670 def _descriptor_clone(self
, tarf
, descriptor_type
):
671 # extract the package on a tmp directory
672 tarf
.extractall('/tmp')
674 for name
in tarf
.getnames():
675 if name
.endswith(".yaml") or name
.endswith(".yml"):
676 with
open('/tmp/' + name
, 'r') as outfile
:
677 yaml_object
= yaml
.load(outfile
)
679 if descriptor_type
== 'nsd':
680 nsd_list
= yaml_object
['nsd:nsd-catalog']['nsd']
682 nsd
['id'] = 'clone_' + nsd
['id']
683 nsd
['name'] = 'clone_' + nsd
['name']
684 nsd
['short-name'] = 'clone_' + nsd
['short-name']
685 elif descriptor_type
== 'vnfd':
686 vnfd_list
= yaml_object
['vnfd:vnfd-catalog']['vnfd']
687 for vnfd
in vnfd_list
:
688 vnfd
['id'] = 'clone_' + vnfd
['id']
689 vnfd
['name'] = 'clone_' + vnfd
['name']
690 vnfd
['short-name'] = 'clone_' + vnfd
['short-name']
692 with
open('/tmp/' + name
, 'w') as yaml_file
:
693 yaml_file
.write(yaml
.dump(yaml_object
, default_flow_style
=False))
696 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", "w:gz")
699 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
703 def nsd_get(self
, token
, id):
704 result
= {'error': True, 'data': ''}
705 headers
= {'Content-Type': 'application/yaml',
706 'Authorization': 'Bearer {}'.format(token
['id'])}
707 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self
._base
_path
, id)
709 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
710 except Exception as e
:
712 result
['data'] = str(e
)
714 if r
.status_code
== requests
.codes
.ok
:
715 result
['error'] = False
716 return yaml
.load(r
.text
)
719 result
['data'] = r
.json()
720 except Exception as e
:
724 def vnfd_get(self
, token
, id):
725 result
= {'error': True, 'data': ''}
726 headers
= {'Content-Type': 'application/yaml',
727 'Authorization': 'Bearer {}'.format(token
['id'])}
728 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self
._base
_path
, id)
730 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
731 except Exception as e
:
733 result
['data'] = str(e
)
735 if r
.status_code
== requests
.codes
.ok
:
736 result
['error'] = False
737 return yaml
.load(r
.text
)
740 result
['data'] = r
.json()
741 except Exception as e
:
745 def nsd_artifacts(self
, token
, id):
746 result
= {'error': True, 'data': ''}
747 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
748 'Authorization': 'Bearer {}'.format(token
['id'])}
749 _url
= "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self
._base
_path
, id)
751 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
752 except Exception as e
:
754 result
['data'] = str(e
)
756 if r
.status_code
== requests
.codes
.ok
:
757 result
['error'] = False
758 result
['data'] = r
.text
761 result
['data'] = r
.json()
762 except Exception as e
:
767 def vnf_packages_artifacts(self
, token
, id):
768 result
= {'error': True, 'data': ''}
769 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
770 'Authorization': 'Bearer {}'.format(token
['id'])}
771 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self
._base
_path
, id)
773 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
774 except Exception as e
:
776 result
['data'] = str(e
)
778 if r
.status_code
== requests
.codes
.ok
:
779 result
['error'] = False
780 result
['data'] = r
.text
783 result
['data'] = r
.json()
784 except Exception as e
:
789 def ns_create(self
, token
, ns_data
):
790 result
= {'error': True, 'data': ''}
791 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
792 'Authorization': 'Bearer {}'.format(token
['id'])}
794 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
797 r
= requests
.post(_url
, json
=ns_data
, verify
=False, headers
=headers
)
798 except Exception as e
:
800 result
['data'] = str(e
)
802 if r
.status_code
== requests
.codes
.ok
:
803 result
['error'] = False
804 result
['data'] = Util
.json_loads_byteified(r
.text
)
807 def ns_op_list(self
, token
, id):
808 result
= {'error': True, 'data': ''}
809 headers
= {"Content-Type": "application/json", "accept": "application/json",
810 'Authorization': 'Bearer {}'.format(token
['id'])}
811 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self
._base
_path
, id)
814 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
815 except Exception as e
:
817 result
['data'] = str(e
)
819 if r
.status_code
== requests
.codes
.ok
:
820 result
['error'] = False
821 result
['data'] = Util
.json_loads_byteified(r
.text
)
825 def ns_op(self
, token
, id):
826 result
= {'error': True, 'data': ''}
827 headers
= {"Content-Type": "application/json", "accept": "application/json",
828 'Authorization': 'Bearer {}'.format(token
['id'])}
829 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self
._base
_path
, id)
832 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
833 except Exception as e
:
835 result
['data'] = str(e
)
837 if r
.status_code
== requests
.codes
.ok
:
838 result
['error'] = False
839 result
['data'] = Util
.json_loads_byteified(r
.text
)
843 def ns_action(self
, token
, id, action_payload
):
844 result
= {'error': True, 'data': ''}
845 headers
= {"Content-Type": "application/json", "accept": "application/json",
846 'Authorization': 'Bearer {}'.format(token
['id'])}
848 _url
= "{0}/nslcm/v1/ns_instances/{1}/action".format(self
._base
_path
, id)
851 r
= requests
.post(_url
, json
=action_payload
, verify
=False, headers
=headers
)
852 except Exception as e
:
854 result
['data'] = str(e
)
856 if r
.status_code
== requests
.codes
.created
:
857 result
['error'] = False
858 result
['data'] = Util
.json_loads_byteified(r
.text
)
861 def ns_delete(self
, token
, id, force
=None):
862 result
= {'error': True, 'data': ''}
863 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
864 'Authorization': 'Bearer {}'.format(token
['id'])}
867 query_path
= '?FORCE=true'
868 _url
= "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self
._base
_path
, id, query_path
)
870 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
871 except Exception as e
:
873 result
['data'] = str(e
)
876 result
['error'] = False
877 if r
.status_code
!= requests
.codes
.no_content
:
878 result
['data'] = Util
.json_loads_byteified(r
.text
)
881 def ns_get(self
, token
, id):
882 result
= {'error': True, 'data': ''}
883 headers
= {"Content-Type": "application/json", "accept": "application/json",
884 'Authorization': 'Bearer {}'.format(token
['id'])}
885 _url
= "{0}/nslcm/v1/ns_instances_content/{1}".format(self
._base
_path
, id)
888 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
889 except Exception as e
:
891 result
['data'] = str(e
)
893 if r
.status_code
== requests
.codes
.ok
:
894 result
['error'] = False
895 result
['data'] = Util
.json_loads_byteified(r
.text
)
898 def vnf_get(self
, token
, id):
899 result
= {'error': True, 'data': ''}
900 headers
= {"Content-Type": "application/json", "accept": "application/json",
901 'Authorization': 'Bearer {}'.format(token
['id'])}
902 _url
= "{0}/nslcm/v1/vnfrs/{1}".format(self
._base
_path
, id)
905 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
906 except Exception as e
:
908 result
['data'] = str(e
)
910 if r
.status_code
== requests
.codes
.ok
:
911 result
['error'] = False
912 result
['data'] = Util
.json_loads_byteified(r
.text
)
915 def ns_alarm_create(self
, token
, id, alarm_payload
):
916 result
= {'error': True, 'data': ''}
917 headers
= {"Content-Type": "application/json",
918 'Authorization': 'Bearer {}'.format(token
['id'])}
919 _url
= "{0}/test/message/alarm_request".format(self
._base
_path
)
921 r
= requests
.post(_url
, json
=alarm_payload
, verify
=False, headers
=headers
)
922 except Exception as e
:
924 result
['data'] = str(e
)
926 if r
.status_code
== requests
.codes
.ok
:
927 result
['error'] = False
928 # result['data'] = Util.json_loads_byteified(r.text)
929 result
['data'] = r
.text
932 def ns_metric_export(self
, token
, id, metric_payload
):
933 result
= {'error': True, 'data': ''}
934 headers
= {"Content-Type": "application/json",
935 'Authorization': 'Bearer {}'.format(token
['id'])}
936 _url
= "{0}/test/message/metric_request".format(self
._base
_path
)
938 r
= requests
.post(_url
, json
=metric_payload
, verify
=False, headers
=headers
)
939 except Exception as e
:
941 result
['data'] = str(e
)
943 if r
.status_code
== requests
.codes
.ok
:
944 result
['error'] = False
945 # result['data'] = Util.json_loads_byteified(r.text)
946 result
['data'] = r
.text
949 def vim_list(self
, token
):
950 result
= {'error': True, 'data': ''}
951 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
952 'Authorization': 'Bearer {}'.format(token
['id'])}
953 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
955 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
956 except Exception as e
:
958 result
['data'] = str(e
)
960 if r
.status_code
== requests
.codes
.ok
:
961 result
['error'] = False
962 result
['data'] = Util
.json_loads_byteified(r
.text
)
966 def vim_delete(self
, token
, id):
967 result
= {'error': True, 'data': ''}
968 headers
= {"accept": "application/json",
969 'Authorization': 'Bearer {}'.format(token
['id'])}
970 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
972 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
973 except Exception as e
:
975 result
['data'] = str(e
)
977 if r
.status_code
== requests
.codes
.accepted
:
978 result
['error'] = False
980 result
['data'] = r
.text
983 def vim_get(self
, token
, id):
985 result
= {'error': True, 'data': ''}
986 headers
= {"Content-Type": "application/json", "accept": "application/json",
987 'Authorization': 'Bearer {}'.format(token
['id'])}
988 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
991 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
992 except Exception as e
:
994 result
['data'] = str(e
)
996 if r
.status_code
== requests
.codes
.ok
:
997 result
['error'] = False
998 result
['data'] = Util
.json_loads_byteified(r
.text
)
1001 def vim_create(self
, token
, vim_data
):
1003 result
= {'error': True, 'data': ''}
1004 headers
= {"Content-Type": "application/json", "accept": "application/json",
1005 'Authorization': 'Bearer {}'.format(token
['id'])}
1007 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
1010 r
= requests
.post(_url
, json
=vim_data
, verify
=False, headers
=headers
)
1011 except Exception as e
:
1013 result
['data'] = str(e
)
1015 if r
.status_code
== requests
.codes
.created
:
1016 result
['error'] = False
1017 result
['data'] = Util
.json_loads_byteified(r
.text
)
1020 def sdn_list(self
, token
):
1021 result
= {'error': True, 'data': ''}
1022 headers
= {"accept": "application/json",
1023 'Authorization': 'Bearer {}'.format(token
['id'])}
1024 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
1026 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1027 except Exception as e
:
1029 result
['data'] = str(e
)
1031 if r
.status_code
== requests
.codes
.ok
:
1032 result
['error'] = False
1033 result
['data'] = Util
.json_loads_byteified(r
.text
)
1036 def sdn_delete(self
, token
, id):
1037 result
= {'error': True, 'data': ''}
1038 headers
= {"accept": "application/json",
1039 'Authorization': 'Bearer {}'.format(token
['id'])}
1040 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
1042 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
1043 except Exception as e
:
1045 result
['data'] = str(e
)
1047 if r
.status_code
== requests
.codes
.accepted
:
1048 result
['error'] = False
1050 result
['data'] = r
.text
1053 def sdn_get(self
, token
, id):
1054 result
= {'error': True, 'data': ''}
1055 headers
= {"accept": "application/json",
1056 'Authorization': 'Bearer {}'.format(token
['id'])}
1057 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
1060 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1061 except Exception as e
:
1063 result
['data'] = str(e
)
1065 if r
.status_code
== requests
.codes
.ok
:
1066 result
['error'] = False
1067 result
['data'] = Util
.json_loads_byteified(r
.text
)
1070 def sdn_create(self
, token
, sdn_data
):
1071 result
= {'error': True, 'data': ''}
1072 headers
= {"Content-Type": "application/json", "accept": "application/json",
1073 'Authorization': 'Bearer {}'.format(token
['id'])}
1075 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
1078 r
= requests
.post(_url
, json
=sdn_data
, verify
=False, headers
=headers
)
1079 except Exception as e
:
1081 result
['data'] = str(e
)
1083 if r
.status_code
== requests
.codes
.created
:
1084 result
['error'] = False
1085 result
['data'] = Util
.json_loads_byteified(r
.text
)
1090 hash_md5
= hashlib
.md5()
1091 for chunk
in iter(lambda: f
.read(1024), b
""):
1092 hash_md5
.update(chunk
)
1093 return hash_md5
.hexdigest()