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
):
255 result
= {'error': True, 'data': ''}
256 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
257 'Authorization': 'Bearer {}'.format(token
['id'])}
259 _url
= "{0}/nsd/v1/ns_descriptors_content".format(self
._base
_path
)
261 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
262 except Exception as e
:
264 result
['data'] = str(e
)
266 if r
.status_code
== requests
.codes
.ok
:
267 result
['error'] = False
268 result
['data'] = Util
.json_loads_byteified(r
.text
)
272 def vnfd_list(self
, token
):
273 result
= {'error': True, 'data': ''}
274 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
275 'Authorization': 'Bearer {}'.format(token
['id'])}
277 _url
= "{0}/vnfpkgm/v1/vnf_packages_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 ns_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/ns_instances_content".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 vnf_list(self
, token
):
308 result
= {'error': True, 'data': ''}
309 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
310 'Authorization': 'Bearer {}'.format(token
['id'])}
311 _url
= "{0}/nslcm/v1/vnfrs".format(self
._base
_path
)
313 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
314 except Exception as e
:
316 result
['data'] = str(e
)
318 if r
.status_code
== requests
.codes
.ok
:
319 result
['error'] = False
320 result
['data'] = Util
.json_loads_byteified(r
.text
)
324 def nsd_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}/nsd/v1/ns_descriptors_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
)
336 if r
.status_code
== requests
.codes
.ok
:
337 result
['error'] = False
338 result
['data'] = Util
.json_loads_byteified(r
.text
)
341 def vnfd_delete(self
, token
, id):
342 result
= {'error': True, 'data': ''}
343 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
344 'Authorization': 'Bearer {}'.format(token
['id'])}
346 _url
= "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self
._base
_path
, id)
348 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
349 except Exception as e
:
351 result
['data'] = str(e
)
354 result
['error'] = False
355 if r
.status_code
!= requests
.codes
.no_content
:
356 result
['data'] = Util
.json_loads_byteified(r
.text
)
359 def nsd_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}/nsd/v1/ns_descriptors_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 vnfd_onboard(self
, token
, package
):
380 result
= {'error': True, 'data': ''}
381 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
382 'Authorization': 'Bearer {}'.format(token
['id'])}
383 with
open('/tmp/' + package
.name
, 'wb+') as destination
:
384 for chunk
in package
.chunks():
385 destination
.write(chunk
)
386 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + package
.name
, 'rb'))
387 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
389 r
= requests
.post(_url
, data
=open('/tmp/' + package
.name
, 'rb'), verify
=False, headers
=headers
)
390 except Exception as e
:
392 result
['data'] = str(e
)
394 if r
.status_code
== requests
.codes
.created
:
395 result
['error'] = False
396 result
['data'] = Util
.json_loads_byteified(r
.text
)
399 def nsd_create_pkg_base(self
, token
, pkg_name
):
400 result
= {'error': True, 'data': ''}
401 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
402 'Authorization': 'Bearer {}'.format(token
['id'])}
404 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
407 self
._create
_base
_pkg
('nsd', pkg_name
)
408 headers
['Content-Filename'] = pkg_name
+ '.tar.gz'
409 r
= requests
.post(_url
, data
=open('/tmp/' + pkg_name
+ '.tar.gz', 'rb'), verify
=False, headers
=headers
)
410 except Exception as e
:
412 result
['data'] = str(e
)
414 if r
.status_code
== requests
.codes
.created
:
415 result
['data'] = r
.json()
416 result
['error'] = False
417 if r
.status_code
== requests
.codes
.conflict
:
418 result
['data'] = "Invalid ID."
421 def vnfd_create_pkg_base(self
, token
, pkg_name
):
422 result
= {'error': True, 'data': ''}
423 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
424 'Authorization': 'Bearer {}'.format(token
['id'])}
426 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
429 self
._create
_base
_pkg
('vnfd', pkg_name
)
430 r
= requests
.post(_url
, data
=open('/tmp/' + pkg_name
+ '.tar.gz', 'rb'), verify
=False, headers
=headers
)
431 except Exception as e
:
433 result
['data'] = str(e
)
435 if r
.status_code
== requests
.codes
.created
:
436 result
['data'] = r
.json()
437 result
['error'] = False
438 if r
.status_code
== requests
.codes
.conflict
:
439 result
['data'] = "Invalid ID."
442 def nsd_clone(self
, token
, id):
443 result
= {'error': True, 'data': ''}
444 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
445 'Authorization': 'Bearer {}'.format(token
['id'])}
447 # get the package onboarded
448 tar_pkg
= self
.get_nsd_pkg(token
, id)
449 tarf
= tarfile
.open(fileobj
=tar_pkg
)
450 tarf
= self
._descriptor
_clone
(tarf
, 'nsd')
451 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
453 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
456 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
458 except Exception as e
:
460 result
['data'] = str(e
)
462 if r
.status_code
== requests
.codes
.created
:
463 result
['error'] = False
464 if r
.status_code
== requests
.codes
.conflict
:
465 result
['data'] = "Invalid ID."
469 def vnfd_clone(self
, token
, id):
470 result
= {'error': True, 'data': ''}
471 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
472 'Authorization': 'Bearer {}'.format(token
['id'])}
474 # get the package onboarded
475 tar_pkg
= self
.get_vnfd_pkg(token
, id)
476 tarf
= tarfile
.open(fileobj
=tar_pkg
)
478 tarf
= self
._descriptor
_clone
(tarf
, 'vnfd')
479 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
481 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
484 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
486 except Exception as e
:
488 result
['data'] = str(e
)
490 if r
.status_code
== requests
.codes
.created
:
491 result
['error'] = False
492 if r
.status_code
== requests
.codes
.conflict
:
493 result
['data'] = "Invalid ID."
497 def nsd_update(self
, token
, id, data
):
498 result
= {'error': True, 'data': ''}
499 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
500 'Authorization': 'Bearer {}'.format(token
['id'])}
502 # get the package onboarded
503 tar_pkg
= self
.get_nsd_pkg(token
, id)
504 tarf
= tarfile
.open(fileobj
=tar_pkg
)
506 tarf
= self
._descriptor
_update
(tarf
, data
)
507 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
509 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
512 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
514 except Exception as e
:
516 result
['data'] = str(e
)
518 if r
.status_code
== requests
.codes
.no_content
:
519 result
['error'] = False
522 result
['data'] = r
.json()
523 except Exception as e
:
528 def vnfd_update(self
, token
, id, data
):
529 result
= {'error': True, 'data': ''}
530 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
531 'Authorization': 'Bearer {}'.format(token
['id'])}
533 # get the package onboarded
534 tar_pkg
= self
.get_vnfd_pkg(token
, id)
535 tarf
= tarfile
.open(fileobj
=tar_pkg
)
537 tarf
= self
._descriptor
_update
(tarf
, data
)
538 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
540 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
543 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
545 except Exception as e
:
547 result
['data'] = str(e
)
549 if r
.status_code
== requests
.codes
.no_content
:
550 result
['error'] = False
553 result
['data'] = r
.json()
554 except Exception as e
:
559 def get_nsd_pkg(self
, token
, id):
560 result
= {'error': True, 'data': ''}
561 headers
= {"accept": "application/zip",
562 'Authorization': 'Bearer {}'.format(token
['id'])}
564 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
566 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
567 except Exception as e
:
569 result
['data'] = str(e
)
571 if r
.status_code
== requests
.codes
.ok
:
572 result
['error'] = False
573 tarf
= StringIO
.StringIO(r
.content
)
577 def get_vnfd_pkg(self
, token
, id):
578 result
= {'error': True, 'data': ''}
579 headers
= {"accept": "application/zip",
580 'Authorization': 'Bearer {}'.format(token
['id'])}
581 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
583 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
584 except Exception as e
:
586 result
['data'] = str(e
)
588 if r
.status_code
== requests
.codes
.ok
:
589 result
['error'] = False
590 tarf
= StringIO
.StringIO(r
.content
)
594 def _descriptor_update(self
, tarf
, data
):
595 # extract the package on a tmp directory
596 tarf
.extractall('/tmp')
598 for name
in tarf
.getnames():
599 if name
.endswith(".yaml") or name
.endswith(".yml"):
600 with
open('/tmp/' + name
, 'w') as outfile
:
601 yaml
.safe_dump(data
, outfile
, default_flow_style
=False)
604 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", "w:gz")
607 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
611 def _create_base_pkg(self
, descriptor_type
, pkg_name
):
612 filename
= '/tmp/'+pkg_name
+'/' + pkg_name
+ '.yaml'
613 if descriptor_type
== 'nsd':
618 "short-name": str(pkg_name
),
619 "vendor": "OSM Composer",
620 "description": str(pkg_name
) + " descriptor",
622 "constituent-vnfd": [],
625 "name": str(pkg_name
)
631 elif descriptor_type
== 'vnfd':
633 "vnfd:vnfd-catalog": {
636 "short-name": str(pkg_name
),
645 "connection-point": [],
646 "name": str(pkg_name
)
652 if not os
.path
.exists(os
.path
.dirname(filename
)):
654 os
.makedirs(os
.path
.dirname(filename
))
655 except OSError as exc
: # Guard against race condition
656 if exc
.errno
!= errno
.EEXIST
:
659 with
open('/tmp/' + pkg_name
+ '/' + pkg_name
+ '.yaml', 'w') as yaml_file
:
660 yaml_file
.write(yaml
.dump(descriptor
, default_flow_style
=False))
662 tarf_temp
= tarfile
.open('/tmp/' + pkg_name
+ '.tar.gz', "w:gz")
663 tarf_temp
.add('/tmp/'+pkg_name
+'/' + pkg_name
+ '.yaml', pkg_name
+ '/' + pkg_name
+ '.yaml', recursive
=False)
666 def _descriptor_clone(self
, tarf
, descriptor_type
):
667 # extract the package on a tmp directory
668 tarf
.extractall('/tmp')
670 for name
in tarf
.getnames():
671 if name
.endswith(".yaml") or name
.endswith(".yml"):
672 with
open('/tmp/' + name
, 'r') as outfile
:
673 yaml_object
= yaml
.load(outfile
)
675 if descriptor_type
== 'nsd':
676 nsd_list
= yaml_object
['nsd:nsd-catalog']['nsd']
678 nsd
['id'] = 'clone_' + nsd
['id']
679 nsd
['name'] = 'clone_' + nsd
['name']
680 nsd
['short-name'] = 'clone_' + nsd
['short-name']
681 elif descriptor_type
== 'vnfd':
682 vnfd_list
= yaml_object
['vnfd:vnfd-catalog']['vnfd']
683 for vnfd
in vnfd_list
:
684 vnfd
['id'] = 'clone_' + vnfd
['id']
685 vnfd
['name'] = 'clone_' + vnfd
['name']
686 vnfd
['short-name'] = 'clone_' + vnfd
['short-name']
688 with
open('/tmp/' + name
, 'w') as yaml_file
:
689 yaml_file
.write(yaml
.dump(yaml_object
, default_flow_style
=False))
692 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", "w:gz")
695 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
699 def nsd_get(self
, token
, id):
700 result
= {'error': True, 'data': ''}
701 headers
= {'Content-Type': 'application/yaml',
702 'Authorization': 'Bearer {}'.format(token
['id'])}
703 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self
._base
_path
, id)
705 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
706 except Exception as e
:
708 result
['data'] = str(e
)
710 if r
.status_code
== requests
.codes
.ok
:
711 result
['error'] = False
712 return yaml
.load(r
.text
)
715 result
['data'] = r
.json()
716 except Exception as e
:
720 def vnfd_get(self
, token
, id):
721 result
= {'error': True, 'data': ''}
722 headers
= {'Content-Type': 'application/yaml',
723 'Authorization': 'Bearer {}'.format(token
['id'])}
724 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self
._base
_path
, id)
726 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
727 except Exception as e
:
729 result
['data'] = str(e
)
731 if r
.status_code
== requests
.codes
.ok
:
732 result
['error'] = False
733 return yaml
.load(r
.text
)
736 result
['data'] = r
.json()
737 except Exception as e
:
741 def nsd_artifacts(self
, token
, id):
742 result
= {'error': True, 'data': ''}
743 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
744 'Authorization': 'Bearer {}'.format(token
['id'])}
745 _url
= "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self
._base
_path
, id)
747 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
748 except Exception as e
:
750 result
['data'] = str(e
)
752 if r
.status_code
== requests
.codes
.ok
:
753 result
['error'] = False
754 result
['data'] = r
.text
757 result
['data'] = r
.json()
758 except Exception as e
:
763 def vnf_packages_artifacts(self
, token
, id):
764 result
= {'error': True, 'data': ''}
765 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
766 'Authorization': 'Bearer {}'.format(token
['id'])}
767 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self
._base
_path
, id)
769 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
770 except Exception as e
:
772 result
['data'] = str(e
)
774 if r
.status_code
== requests
.codes
.ok
:
775 result
['error'] = False
776 result
['data'] = r
.text
779 result
['data'] = r
.json()
780 except Exception as e
:
785 def ns_create(self
, token
, ns_data
):
786 result
= {'error': True, 'data': ''}
787 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
788 'Authorization': 'Bearer {}'.format(token
['id'])}
790 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
793 r
= requests
.post(_url
, json
=ns_data
, verify
=False, 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_op_list(self
, token
, id):
804 result
= {'error': True, 'data': ''}
805 headers
= {"Content-Type": "application/json", "accept": "application/json",
806 'Authorization': 'Bearer {}'.format(token
['id'])}
807 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self
._base
_path
, id)
810 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
811 except Exception as e
:
813 result
['data'] = str(e
)
815 if r
.status_code
== requests
.codes
.ok
:
816 result
['error'] = False
817 result
['data'] = Util
.json_loads_byteified(r
.text
)
821 def ns_op(self
, token
, id):
822 result
= {'error': True, 'data': ''}
823 headers
= {"Content-Type": "application/json", "accept": "application/json",
824 'Authorization': 'Bearer {}'.format(token
['id'])}
825 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self
._base
_path
, id)
828 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
829 except Exception as e
:
831 result
['data'] = str(e
)
833 if r
.status_code
== requests
.codes
.ok
:
834 result
['error'] = False
835 result
['data'] = Util
.json_loads_byteified(r
.text
)
839 def ns_action(self
, token
, id, action_payload
):
840 result
= {'error': True, 'data': ''}
841 headers
= {"Content-Type": "application/json", "accept": "application/json",
842 'Authorization': 'Bearer {}'.format(token
['id'])}
844 _url
= "{0}/nslcm/v1/ns_instances/{1}/action".format(self
._base
_path
, id)
847 r
= requests
.post(_url
, json
=action_payload
, verify
=False, headers
=headers
)
848 except Exception as e
:
850 result
['data'] = str(e
)
852 if r
.status_code
== requests
.codes
.created
:
853 result
['error'] = False
854 result
['data'] = Util
.json_loads_byteified(r
.text
)
857 def ns_delete(self
, token
, id, force
=None):
858 result
= {'error': True, 'data': ''}
859 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
860 'Authorization': 'Bearer {}'.format(token
['id'])}
863 query_path
= '?FORCE=true'
864 _url
= "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self
._base
_path
, id, query_path
)
866 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
867 except Exception as e
:
869 result
['data'] = str(e
)
872 result
['error'] = False
873 if r
.status_code
!= requests
.codes
.no_content
:
874 result
['data'] = Util
.json_loads_byteified(r
.text
)
877 def ns_get(self
, token
, id):
878 result
= {'error': True, 'data': ''}
879 headers
= {"Content-Type": "application/json", "accept": "application/json",
880 'Authorization': 'Bearer {}'.format(token
['id'])}
881 _url
= "{0}/nslcm/v1/ns_instances_content/{1}".format(self
._base
_path
, id)
884 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
885 except Exception as e
:
887 result
['data'] = str(e
)
889 if r
.status_code
== requests
.codes
.ok
:
890 result
['error'] = False
891 result
['data'] = Util
.json_loads_byteified(r
.text
)
894 def vnf_get(self
, token
, id):
895 result
= {'error': True, 'data': ''}
896 headers
= {"Content-Type": "application/json", "accept": "application/json",
897 'Authorization': 'Bearer {}'.format(token
['id'])}
898 _url
= "{0}/nslcm/v1/vnfrs/{1}".format(self
._base
_path
, id)
901 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
902 except Exception as e
:
904 result
['data'] = str(e
)
906 if r
.status_code
== requests
.codes
.ok
:
907 result
['error'] = False
908 result
['data'] = Util
.json_loads_byteified(r
.text
)
911 def ns_alarm_create(self
, token
, id, alarm_payload
):
912 result
= {'error': True, 'data': ''}
913 headers
= {"Content-Type": "application/json",
914 'Authorization': 'Bearer {}'.format(token
['id'])}
915 _url
= "{0}/test/message/alarm_request".format(self
._base
_path
)
917 r
= requests
.post(_url
, json
=alarm_payload
, verify
=False, headers
=headers
)
918 except Exception as e
:
920 result
['data'] = str(e
)
922 if r
.status_code
== requests
.codes
.ok
:
923 result
['error'] = False
924 # result['data'] = Util.json_loads_byteified(r.text)
925 result
['data'] = r
.text
928 def ns_metric_export(self
, token
, id, metric_payload
):
929 result
= {'error': True, 'data': ''}
930 headers
= {"Content-Type": "application/json",
931 'Authorization': 'Bearer {}'.format(token
['id'])}
932 _url
= "{0}/test/message/metric_request".format(self
._base
_path
)
934 r
= requests
.post(_url
, json
=metric_payload
, verify
=False, headers
=headers
)
935 except Exception as e
:
937 result
['data'] = str(e
)
939 if r
.status_code
== requests
.codes
.ok
:
940 result
['error'] = False
941 # result['data'] = Util.json_loads_byteified(r.text)
942 result
['data'] = r
.text
945 def vim_list(self
, token
):
946 result
= {'error': True, 'data': ''}
947 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
948 'Authorization': 'Bearer {}'.format(token
['id'])}
949 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
951 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
952 except Exception as e
:
954 result
['data'] = str(e
)
956 if r
.status_code
== requests
.codes
.ok
:
957 result
['error'] = False
958 result
['data'] = Util
.json_loads_byteified(r
.text
)
962 def vim_delete(self
, token
, id):
963 result
= {'error': True, 'data': ''}
964 headers
= {"accept": "application/json",
965 'Authorization': 'Bearer {}'.format(token
['id'])}
966 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
968 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
969 except Exception as e
:
971 result
['data'] = str(e
)
973 if r
.status_code
== requests
.codes
.accepted
:
974 result
['error'] = False
976 result
['data'] = r
.text
979 def vim_get(self
, token
, id):
981 result
= {'error': True, 'data': ''}
982 headers
= {"Content-Type": "application/json", "accept": "application/json",
983 'Authorization': 'Bearer {}'.format(token
['id'])}
984 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
987 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
988 except Exception as e
:
990 result
['data'] = str(e
)
992 if r
.status_code
== requests
.codes
.ok
:
993 result
['error'] = False
994 result
['data'] = Util
.json_loads_byteified(r
.text
)
997 def vim_create(self
, token
, vim_data
):
999 result
= {'error': True, 'data': ''}
1000 headers
= {"Content-Type": "application/json", "accept": "application/json",
1001 'Authorization': 'Bearer {}'.format(token
['id'])}
1003 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
1006 r
= requests
.post(_url
, json
=vim_data
, verify
=False, headers
=headers
)
1007 except Exception as e
:
1009 result
['data'] = str(e
)
1011 if r
.status_code
== requests
.codes
.created
:
1012 result
['error'] = False
1013 result
['data'] = Util
.json_loads_byteified(r
.text
)
1016 def sdn_list(self
, token
):
1017 result
= {'error': True, 'data': ''}
1018 headers
= {"accept": "application/json",
1019 'Authorization': 'Bearer {}'.format(token
['id'])}
1020 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
1022 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1023 except Exception as e
:
1025 result
['data'] = str(e
)
1027 if r
.status_code
== requests
.codes
.ok
:
1028 result
['error'] = False
1029 result
['data'] = Util
.json_loads_byteified(r
.text
)
1032 def sdn_delete(self
, token
, id):
1033 result
= {'error': True, 'data': ''}
1034 headers
= {"accept": "application/json",
1035 'Authorization': 'Bearer {}'.format(token
['id'])}
1036 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
1038 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
1039 except Exception as e
:
1041 result
['data'] = str(e
)
1043 if r
.status_code
== requests
.codes
.accepted
:
1044 result
['error'] = False
1046 result
['data'] = r
.text
1049 def sdn_get(self
, token
, id):
1050 result
= {'error': True, 'data': ''}
1051 headers
= {"accept": "application/json",
1052 'Authorization': 'Bearer {}'.format(token
['id'])}
1053 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
1056 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1057 except Exception as e
:
1059 result
['data'] = str(e
)
1061 if r
.status_code
== requests
.codes
.ok
:
1062 result
['error'] = False
1063 result
['data'] = Util
.json_loads_byteified(r
.text
)
1066 def sdn_create(self
, token
, sdn_data
):
1067 result
= {'error': True, 'data': ''}
1068 headers
= {"Content-Type": "application/json", "accept": "application/json",
1069 'Authorization': 'Bearer {}'.format(token
['id'])}
1071 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
1074 r
= requests
.post(_url
, json
=sdn_data
, verify
=False, headers
=headers
)
1075 except Exception as e
:
1077 result
['data'] = str(e
)
1079 if r
.status_code
== requests
.codes
.created
:
1080 result
['error'] = False
1081 result
['data'] = Util
.json_loads_byteified(r
.text
)
1086 hash_md5
= hashlib
.md5()
1087 for chunk
in iter(lambda: f
.read(1024), b
""):
1088 hash_md5
.update(chunk
)
1089 return hash_md5
.hexdigest()