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 r
= requests
.post(_url
, data
=open('/tmp/' + pkg_name
+ '.tar.gz', 'rb'), verify
=False, headers
=headers
)
409 except Exception as e
:
411 result
['data'] = str(e
)
413 if r
.status_code
== requests
.codes
.created
:
414 result
['data'] = r
.json()
415 result
['error'] = False
416 if r
.status_code
== requests
.codes
.conflict
:
417 result
['data'] = "Invalid ID."
420 def vnfd_create_pkg_base(self
, token
, pkg_name
):
421 result
= {'error': True, 'data': ''}
422 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
423 'Authorization': 'Bearer {}'.format(token
['id'])}
425 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
428 self
._create
_base
_pkg
('vnfd', pkg_name
)
429 r
= requests
.post(_url
, data
=open('/tmp/' + pkg_name
+ '.tar.gz', 'rb'), verify
=False, headers
=headers
)
430 except Exception as e
:
432 result
['data'] = str(e
)
434 if r
.status_code
== requests
.codes
.created
:
435 result
['data'] = r
.json()
436 result
['error'] = False
437 if r
.status_code
== requests
.codes
.conflict
:
438 result
['data'] = "Invalid ID."
441 def nsd_clone(self
, token
, id):
442 result
= {'error': True, 'data': ''}
443 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
444 'Authorization': 'Bearer {}'.format(token
['id'])}
446 # get the package onboarded
447 tar_pkg
= self
.get_nsd_pkg(token
, id)
448 tarf
= tarfile
.open(fileobj
=tar_pkg
)
449 tarf
= self
._descriptor
_clone
(tarf
, 'nsd')
450 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
452 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
455 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
457 except Exception as e
:
459 result
['data'] = str(e
)
461 if r
.status_code
== requests
.codes
.created
:
462 result
['error'] = False
463 if r
.status_code
== requests
.codes
.conflict
:
464 result
['data'] = "Invalid ID."
468 def vnfd_clone(self
, token
, id):
469 result
= {'error': True, 'data': ''}
470 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
471 'Authorization': 'Bearer {}'.format(token
['id'])}
473 # get the package onboarded
474 tar_pkg
= self
.get_vnfd_pkg(token
, id)
475 tarf
= tarfile
.open(fileobj
=tar_pkg
)
477 tarf
= self
._descriptor
_clone
(tarf
, 'vnfd')
478 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
480 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
483 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
485 except Exception as e
:
487 result
['data'] = str(e
)
489 if r
.status_code
== requests
.codes
.created
:
490 result
['error'] = False
491 if r
.status_code
== requests
.codes
.conflict
:
492 result
['data'] = "Invalid ID."
496 def nsd_update(self
, token
, id, data
):
497 result
= {'error': True, 'data': ''}
498 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
499 'Authorization': 'Bearer {}'.format(token
['id'])}
501 # get the package onboarded
502 tar_pkg
= self
.get_nsd_pkg(token
, id)
503 tarf
= tarfile
.open(fileobj
=tar_pkg
)
505 tarf
= self
._descriptor
_update
(tarf
, data
)
506 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
508 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
511 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
513 except Exception as e
:
515 result
['data'] = str(e
)
517 if r
.status_code
== requests
.codes
.no_content
:
518 result
['error'] = False
522 def vnfd_update(self
, token
, id, data
):
523 result
= {'error': True, 'data': ''}
524 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
525 'Authorization': 'Bearer {}'.format(token
['id'])}
527 # get the package onboarded
528 tar_pkg
= self
.get_vnfd_pkg(token
, id)
529 tarf
= tarfile
.open(fileobj
=tar_pkg
)
531 tarf
= self
._descriptor
_update
(tarf
, data
)
532 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
534 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
537 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
539 except Exception as e
:
541 result
['data'] = str(e
)
543 if r
.status_code
== requests
.codes
.no_content
:
544 result
['error'] = False
548 def get_nsd_pkg(self
, token
, id):
549 result
= {'error': True, 'data': ''}
550 headers
= {"accept": "application/zip",
551 'Authorization': 'Bearer {}'.format(token
['id'])}
553 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
555 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
556 except Exception as e
:
558 result
['data'] = str(e
)
560 if r
.status_code
== requests
.codes
.ok
:
561 result
['error'] = False
562 tarf
= StringIO
.StringIO(r
.content
)
566 def get_vnfd_pkg(self
, token
, id):
567 result
= {'error': True, 'data': ''}
568 headers
= {"accept": "application/zip",
569 'Authorization': 'Bearer {}'.format(token
['id'])}
570 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
572 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
573 except Exception as e
:
575 result
['data'] = str(e
)
577 if r
.status_code
== requests
.codes
.ok
:
578 result
['error'] = False
579 tarf
= StringIO
.StringIO(r
.content
)
583 def _descriptor_update(self
, tarf
, data
):
584 # extract the package on a tmp directory
585 tarf
.extractall('/tmp')
587 for name
in tarf
.getnames():
588 if name
.endswith(".yaml") or name
.endswith(".yml"):
589 with
open('/tmp/' + name
, 'w') as outfile
:
590 yaml
.safe_dump(data
, outfile
, default_flow_style
=False)
593 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", "w:gz")
596 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
600 def _create_base_pkg(self
, descriptor_type
, pkg_name
):
601 filename
= '/tmp/'+pkg_name
+'/' + pkg_name
+ '.yaml'
602 if descriptor_type
== 'nsd':
607 "short-name": str(pkg_name
),
608 "vendor": "OSM Composer",
609 "description": str(pkg_name
) + " descriptor",
611 "constituent-vnfd": [],
614 "name": str(pkg_name
)
620 elif descriptor_type
== 'vnfd':
622 "vnfd:vnfd-catalog": {
625 "short-name": str(pkg_name
),
628 "mgmt-interface": {},
632 "connection-point": [],
633 "name": str(pkg_name
)
639 if not os
.path
.exists(os
.path
.dirname(filename
)):
641 os
.makedirs(os
.path
.dirname(filename
))
642 except OSError as exc
: # Guard against race condition
643 if exc
.errno
!= errno
.EEXIST
:
646 with
open('/tmp/' + pkg_name
+ '/' + pkg_name
+ '.yaml', 'w') as yaml_file
:
647 yaml_file
.write(yaml
.dump(descriptor
, default_flow_style
=False))
649 tarf_temp
= tarfile
.open('/tmp/' + pkg_name
+ '.tar.gz', "w:gz")
650 tarf_temp
.add('/tmp/'+pkg_name
+'/' + pkg_name
+ '.yaml', pkg_name
+ '/' + pkg_name
+ '.yaml', recursive
=False)
653 def _descriptor_clone(self
, tarf
, descriptor_type
):
654 # extract the package on a tmp directory
655 tarf
.extractall('/tmp')
657 for name
in tarf
.getnames():
658 if name
.endswith(".yaml") or name
.endswith(".yml"):
659 with
open('/tmp/' + name
, 'r') as outfile
:
660 yaml_object
= yaml
.load(outfile
)
662 if descriptor_type
== 'nsd':
663 nsd_list
= yaml_object
['nsd:nsd-catalog']['nsd']
665 nsd
['id'] = 'clone_' + nsd
['id']
666 nsd
['name'] = 'clone_' + nsd
['name']
667 nsd
['short-name'] = 'clone_' + nsd
['short-name']
668 elif descriptor_type
== 'vnfd':
669 vnfd_list
= yaml_object
['vnfd:vnfd-catalog']['vnfd']
670 for vnfd
in vnfd_list
:
671 vnfd
['id'] = 'clone_' + vnfd
['id']
672 vnfd
['name'] = 'clone_' + vnfd
['name']
673 vnfd
['short-name'] = 'clone_' + vnfd
['short-name']
675 with
open('/tmp/' + name
, 'w') as yaml_file
:
676 yaml_file
.write(yaml
.dump(yaml_object
, default_flow_style
=False))
679 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", "w:gz")
682 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
686 def nsd_get(self
, token
, id):
687 result
= {'error': True, 'data': ''}
688 headers
= {'Content-Type': 'application/yaml',
689 'Authorization': 'Bearer {}'.format(token
['id'])}
690 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self
._base
_path
, id)
692 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
693 except Exception as e
:
695 result
['data'] = str(e
)
697 if r
.status_code
== requests
.codes
.ok
:
698 result
['error'] = False
699 return yaml
.load(r
.text
)
702 result
['data'] = r
.json()
703 except Exception as e
:
707 def vnfd_get(self
, token
, id):
708 result
= {'error': True, 'data': ''}
709 headers
= {'Content-Type': 'application/yaml',
710 'Authorization': 'Bearer {}'.format(token
['id'])}
711 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self
._base
_path
, id)
713 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
714 except Exception as e
:
716 result
['data'] = str(e
)
718 if r
.status_code
== requests
.codes
.ok
:
719 result
['error'] = False
720 return yaml
.load(r
.text
)
723 result
['data'] = r
.json()
724 except Exception as e
:
728 def nsd_artifacts(self
, token
, id):
729 result
= {'error': True, 'data': ''}
730 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
731 'Authorization': 'Bearer {}'.format(token
['id'])}
732 _url
= "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self
._base
_path
, id)
734 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
735 except Exception as e
:
737 result
['data'] = str(e
)
739 if r
.status_code
== requests
.codes
.ok
:
740 result
['error'] = False
741 result
['data'] = r
.text
744 result
['data'] = r
.json()
745 except Exception as e
:
750 def vnf_packages_artifacts(self
, token
, id):
751 result
= {'error': True, 'data': ''}
752 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
753 'Authorization': 'Bearer {}'.format(token
['id'])}
754 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self
._base
_path
, id)
756 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
757 except Exception as e
:
759 result
['data'] = str(e
)
761 if r
.status_code
== requests
.codes
.ok
:
762 result
['error'] = False
763 result
['data'] = r
.text
766 result
['data'] = r
.json()
767 except Exception as e
:
772 def ns_create(self
, token
, ns_data
):
773 result
= {'error': True, 'data': ''}
774 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
775 'Authorization': 'Bearer {}'.format(token
['id'])}
777 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
780 r
= requests
.post(_url
, json
=ns_data
, verify
=False, headers
=headers
)
781 except Exception as e
:
783 result
['data'] = str(e
)
785 if r
.status_code
== requests
.codes
.ok
:
786 result
['error'] = False
787 result
['data'] = Util
.json_loads_byteified(r
.text
)
790 def ns_op_list(self
, token
, id):
791 result
= {'error': True, 'data': ''}
792 headers
= {"Content-Type": "application/json", "accept": "application/json",
793 'Authorization': 'Bearer {}'.format(token
['id'])}
794 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self
._base
_path
, id)
797 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, 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
)
808 def ns_op(self
, token
, id):
809 result
= {'error': True, 'data': ''}
810 headers
= {"Content-Type": "application/json", "accept": "application/json",
811 'Authorization': 'Bearer {}'.format(token
['id'])}
812 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self
._base
_path
, id)
815 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
816 except Exception as e
:
818 result
['data'] = str(e
)
820 if r
.status_code
== requests
.codes
.ok
:
821 result
['error'] = False
822 result
['data'] = Util
.json_loads_byteified(r
.text
)
826 def ns_action(self
, token
, id, action_payload
):
827 result
= {'error': True, 'data': ''}
828 headers
= {"Content-Type": "application/json", "accept": "application/json",
829 'Authorization': 'Bearer {}'.format(token
['id'])}
831 _url
= "{0}/nslcm/v1/ns_instances/{1}/action".format(self
._base
_path
, id)
834 r
= requests
.post(_url
, json
=action_payload
, verify
=False, headers
=headers
)
835 except Exception as e
:
837 result
['data'] = str(e
)
839 if r
.status_code
== requests
.codes
.created
:
840 result
['error'] = False
841 result
['data'] = Util
.json_loads_byteified(r
.text
)
844 def ns_delete(self
, token
, id, force
=None):
845 result
= {'error': True, 'data': ''}
846 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
847 'Authorization': 'Bearer {}'.format(token
['id'])}
850 query_path
= '?FORCE=true'
851 _url
= "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self
._base
_path
, id, query_path
)
853 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
854 except Exception as e
:
856 result
['data'] = str(e
)
859 result
['error'] = False
860 if r
.status_code
!= requests
.codes
.no_content
:
861 result
['data'] = Util
.json_loads_byteified(r
.text
)
864 def ns_get(self
, token
, id):
865 result
= {'error': True, 'data': ''}
866 headers
= {"Content-Type": "application/json", "accept": "application/json",
867 'Authorization': 'Bearer {}'.format(token
['id'])}
868 _url
= "{0}/nslcm/v1/ns_instances_content/{1}".format(self
._base
_path
, id)
871 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
872 except Exception as e
:
874 result
['data'] = str(e
)
876 if r
.status_code
== requests
.codes
.ok
:
877 result
['error'] = False
878 result
['data'] = Util
.json_loads_byteified(r
.text
)
881 def vnf_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/vnfrs/{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 ns_alarm_create(self
, token
, id, alarm_payload
):
899 result
= {'error': True, 'data': ''}
900 headers
= {"Content-Type": "application/json",
901 'Authorization': 'Bearer {}'.format(token
['id'])}
902 _url
= "{0}/test/message/alarm_request".format(self
._base
_path
)
904 r
= requests
.post(_url
, json
=alarm_payload
, verify
=False, headers
=headers
)
905 except Exception as e
:
907 result
['data'] = str(e
)
909 if r
.status_code
== requests
.codes
.ok
:
910 result
['error'] = False
911 # result['data'] = Util.json_loads_byteified(r.text)
912 result
['data'] = r
.text
915 def ns_metric_export(self
, token
, id, metric_payload
):
916 result
= {'error': True, 'data': ''}
917 headers
= {"Content-Type": "application/json",
918 'Authorization': 'Bearer {}'.format(token
['id'])}
919 _url
= "{0}/test/message/metric_request".format(self
._base
_path
)
921 r
= requests
.post(_url
, json
=metric_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 vim_list(self
, token
):
933 result
= {'error': True, 'data': ''}
934 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
935 'Authorization': 'Bearer {}'.format(token
['id'])}
936 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
938 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, 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
)
949 def vim_delete(self
, token
, id):
950 result
= {'error': True, 'data': ''}
951 headers
= {"accept": "application/json",
952 'Authorization': 'Bearer {}'.format(token
['id'])}
953 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
955 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
956 except Exception as e
:
958 result
['data'] = str(e
)
960 if r
.status_code
== requests
.codes
.accepted
:
961 result
['error'] = False
963 result
['data'] = r
.text
966 def vim_get(self
, token
, id):
968 result
= {'error': True, 'data': ''}
969 headers
= {"Content-Type": "application/json", "accept": "application/json",
970 'Authorization': 'Bearer {}'.format(token
['id'])}
971 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
974 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
975 except Exception as e
:
977 result
['data'] = str(e
)
979 if r
.status_code
== requests
.codes
.ok
:
980 result
['error'] = False
981 result
['data'] = Util
.json_loads_byteified(r
.text
)
984 def vim_create(self
, token
, vim_data
):
986 result
= {'error': True, 'data': ''}
987 headers
= {"Content-Type": "application/json", "accept": "application/json",
988 'Authorization': 'Bearer {}'.format(token
['id'])}
990 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
993 r
= requests
.post(_url
, json
=vim_data
, verify
=False, headers
=headers
)
994 except Exception as e
:
996 result
['data'] = str(e
)
998 if r
.status_code
== requests
.codes
.created
:
999 result
['error'] = False
1000 result
['data'] = Util
.json_loads_byteified(r
.text
)
1003 def sdn_list(self
, token
):
1004 result
= {'error': True, 'data': ''}
1005 headers
= {"accept": "application/json",
1006 'Authorization': 'Bearer {}'.format(token
['id'])}
1007 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
1009 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1010 except Exception as e
:
1012 result
['data'] = str(e
)
1014 if r
.status_code
== requests
.codes
.ok
:
1015 result
['error'] = False
1016 result
['data'] = Util
.json_loads_byteified(r
.text
)
1019 def sdn_delete(self
, token
, id):
1020 result
= {'error': True, 'data': ''}
1021 headers
= {"accept": "application/json",
1022 'Authorization': 'Bearer {}'.format(token
['id'])}
1023 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
1025 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
1026 except Exception as e
:
1028 result
['data'] = str(e
)
1030 if r
.status_code
== requests
.codes
.accepted
:
1031 result
['error'] = False
1033 result
['data'] = r
.text
1036 def sdn_get(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)
1043 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1044 except Exception as e
:
1046 result
['data'] = str(e
)
1048 if r
.status_code
== requests
.codes
.ok
:
1049 result
['error'] = False
1050 result
['data'] = Util
.json_loads_byteified(r
.text
)
1053 def sdn_create(self
, token
, sdn_data
):
1054 result
= {'error': True, 'data': ''}
1055 headers
= {"Content-Type": "application/json", "accept": "application/json",
1056 'Authorization': 'Bearer {}'.format(token
['id'])}
1058 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
1061 r
= requests
.post(_url
, json
=sdn_data
, verify
=False, headers
=headers
)
1062 except Exception as e
:
1064 result
['data'] = str(e
)
1066 if r
.status_code
== requests
.codes
.created
:
1067 result
['error'] = False
1068 result
['data'] = Util
.json_loads_byteified(r
.text
)
1073 hash_md5
= hashlib
.md5()
1074 for chunk
in iter(lambda: f
.read(1024), b
""):
1075 hash_md5
.update(chunk
)
1076 return hash_md5
.hexdigest()