d900af68d70876d0a1975a6faefcfd90a2dd1f14
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 pdu_list(self
, token
):
329 result
= {'error': True, 'data': ''}
330 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
331 'Authorization': 'Bearer {}'.format(token
['id'])}
332 _url
= "{0}/pdu/v1/pdu_descriptors".format(self
._base
_path
)
334 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
335 except Exception as e
:
337 result
['data'] = str(e
)
339 if r
.status_code
== requests
.codes
.ok
:
340 result
['error'] = False
341 result
['data'] = Util
.json_loads_byteified(r
.text
)
345 def nsd_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}/nsd/v1/ns_descriptors_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
)
357 if r
.status_code
== requests
.codes
.ok
:
358 result
['error'] = False
359 result
['data'] = Util
.json_loads_byteified(r
.text
)
362 def vnfd_delete(self
, token
, id):
363 result
= {'error': True, 'data': ''}
364 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
365 'Authorization': 'Bearer {}'.format(token
['id'])}
367 _url
= "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self
._base
_path
, id)
369 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
370 except Exception as e
:
372 result
['data'] = str(e
)
375 result
['error'] = False
376 if r
.status_code
!= requests
.codes
.no_content
:
377 result
['data'] = Util
.json_loads_byteified(r
.text
)
380 def nsd_onboard(self
, token
, package
):
381 result
= {'error': True, 'data': ''}
382 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
383 'Authorization': 'Bearer {}'.format(token
['id'])}
384 with
open('/tmp/' + package
.name
, 'wb+') as destination
:
385 for chunk
in package
.chunks():
386 destination
.write(chunk
)
387 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + package
.name
, 'rb'))
388 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
390 r
= requests
.post(_url
, data
=open('/tmp/' + package
.name
, 'rb'), verify
=False, headers
=headers
)
391 except Exception as e
:
393 result
['data'] = str(e
)
395 if r
.status_code
== requests
.codes
.created
:
396 result
['error'] = False
397 result
['data'] = Util
.json_loads_byteified(r
.text
)
400 def vnfd_onboard(self
, token
, package
):
401 result
= {'error': True, 'data': ''}
402 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
403 'Authorization': 'Bearer {}'.format(token
['id'])}
404 with
open('/tmp/' + package
.name
, 'wb+') as destination
:
405 for chunk
in package
.chunks():
406 destination
.write(chunk
)
407 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + package
.name
, 'rb'))
408 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
410 r
= requests
.post(_url
, data
=open('/tmp/' + package
.name
, 'rb'), verify
=False, headers
=headers
)
411 except Exception as e
:
413 result
['data'] = str(e
)
415 if r
.status_code
== requests
.codes
.created
:
416 result
['error'] = False
417 result
['data'] = Util
.json_loads_byteified(r
.text
)
420 def nsd_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}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
428 self
._create
_base
_pkg
('nsd', pkg_name
)
429 headers
['Content-Filename'] = pkg_name
+ '.tar.gz'
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 vnfd_create_pkg_base(self
, token
, pkg_name
):
443 result
= {'error': True, 'data': ''}
444 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
445 'Authorization': 'Bearer {}'.format(token
['id'])}
447 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
450 self
._create
_base
_pkg
('vnfd', pkg_name
)
451 r
= requests
.post(_url
, data
=open('/tmp/' + pkg_name
+ '.tar.gz', 'rb'), verify
=False, headers
=headers
)
452 except Exception as e
:
454 result
['data'] = str(e
)
456 if r
.status_code
== requests
.codes
.created
:
457 result
['data'] = r
.json()
458 result
['error'] = False
459 if r
.status_code
== requests
.codes
.conflict
:
460 result
['data'] = "Invalid ID."
463 def nsd_clone(self
, token
, id):
464 result
= {'error': True, 'data': ''}
465 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
466 'Authorization': 'Bearer {}'.format(token
['id'])}
468 # get the package onboarded
469 tar_pkg
= self
.get_nsd_pkg(token
, id)
470 tarf
= tarfile
.open(fileobj
=tar_pkg
)
471 tarf
= self
._descriptor
_clone
(tarf
, 'nsd')
472 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
474 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
477 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
479 except Exception as e
:
481 result
['data'] = str(e
)
483 if r
.status_code
== requests
.codes
.created
:
484 result
['error'] = False
485 if r
.status_code
== requests
.codes
.conflict
:
486 result
['data'] = "Invalid ID."
490 def vnfd_clone(self
, token
, id):
491 result
= {'error': True, 'data': ''}
492 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
493 'Authorization': 'Bearer {}'.format(token
['id'])}
495 # get the package onboarded
496 tar_pkg
= self
.get_vnfd_pkg(token
, id)
497 tarf
= tarfile
.open(fileobj
=tar_pkg
)
499 tarf
= self
._descriptor
_clone
(tarf
, 'vnfd')
500 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
502 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
505 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
507 except Exception as e
:
509 result
['data'] = str(e
)
511 if r
.status_code
== requests
.codes
.created
:
512 result
['error'] = False
513 if r
.status_code
== requests
.codes
.conflict
:
514 result
['data'] = "Invalid ID."
518 def nsd_update(self
, token
, id, data
):
519 result
= {'error': True, 'data': ''}
520 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
521 'Authorization': 'Bearer {}'.format(token
['id'])}
523 # get the package onboarded
524 tar_pkg
= self
.get_nsd_pkg(token
, id)
525 tarf
= tarfile
.open(fileobj
=tar_pkg
)
527 tarf
= self
._descriptor
_update
(tarf
, data
)
528 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
530 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
533 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
535 except Exception as e
:
537 result
['data'] = str(e
)
539 if r
.status_code
== requests
.codes
.no_content
:
540 result
['error'] = False
543 result
['data'] = r
.json()
544 except Exception as e
:
549 def vnfd_update(self
, token
, id, data
):
550 result
= {'error': True, 'data': ''}
551 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
552 'Authorization': 'Bearer {}'.format(token
['id'])}
554 # get the package onboarded
555 tar_pkg
= self
.get_vnfd_pkg(token
, id)
556 tarf
= tarfile
.open(fileobj
=tar_pkg
)
558 tarf
= self
._descriptor
_update
(tarf
, data
)
559 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
561 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
564 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
566 except Exception as e
:
568 result
['data'] = str(e
)
570 if r
.status_code
== requests
.codes
.no_content
:
571 result
['error'] = False
574 result
['data'] = r
.json()
575 except Exception as e
:
580 def get_nsd_pkg(self
, token
, id):
581 result
= {'error': True, 'data': ''}
582 headers
= {"accept": "application/zip",
583 'Authorization': 'Bearer {}'.format(token
['id'])}
585 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_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 get_vnfd_pkg(self
, token
, id):
599 result
= {'error': True, 'data': ''}
600 headers
= {"accept": "application/zip",
601 'Authorization': 'Bearer {}'.format(token
['id'])}
602 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
604 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
605 except Exception as e
:
607 result
['data'] = str(e
)
609 if r
.status_code
== requests
.codes
.ok
:
610 result
['error'] = False
611 tarf
= StringIO
.StringIO(r
.content
)
615 def _descriptor_update(self
, tarf
, data
):
616 # extract the package on a tmp directory
617 tarf
.extractall('/tmp')
619 for name
in tarf
.getnames():
620 if name
.endswith(".yaml") or name
.endswith(".yml"):
621 with
open('/tmp/' + name
, 'w') as outfile
:
622 yaml
.safe_dump(data
, outfile
, default_flow_style
=False)
625 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", "w:gz")
628 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
632 def _create_base_pkg(self
, descriptor_type
, pkg_name
):
633 filename
= '/tmp/'+pkg_name
+'/' + pkg_name
+ '.yaml'
634 if descriptor_type
== 'nsd':
639 "short-name": str(pkg_name
),
640 "vendor": "OSM Composer",
641 "description": str(pkg_name
) + " descriptor",
643 "constituent-vnfd": [],
646 "name": str(pkg_name
)
652 elif descriptor_type
== 'vnfd':
654 "vnfd:vnfd-catalog": {
657 "short-name": str(pkg_name
),
666 "connection-point": [],
667 "name": str(pkg_name
)
673 if not os
.path
.exists(os
.path
.dirname(filename
)):
675 os
.makedirs(os
.path
.dirname(filename
))
676 except OSError as exc
: # Guard against race condition
677 if exc
.errno
!= errno
.EEXIST
:
680 with
open('/tmp/' + pkg_name
+ '/' + pkg_name
+ '.yaml', 'w') as yaml_file
:
681 yaml_file
.write(yaml
.dump(descriptor
, default_flow_style
=False))
683 tarf_temp
= tarfile
.open('/tmp/' + pkg_name
+ '.tar.gz', "w:gz")
684 tarf_temp
.add('/tmp/'+pkg_name
+'/' + pkg_name
+ '.yaml', pkg_name
+ '/' + pkg_name
+ '.yaml', recursive
=False)
687 def _descriptor_clone(self
, tarf
, descriptor_type
):
688 # extract the package on a tmp directory
689 tarf
.extractall('/tmp')
691 for name
in tarf
.getnames():
692 if name
.endswith(".yaml") or name
.endswith(".yml"):
693 with
open('/tmp/' + name
, 'r') as outfile
:
694 yaml_object
= yaml
.load(outfile
)
696 if descriptor_type
== 'nsd':
697 nsd_list
= yaml_object
['nsd:nsd-catalog']['nsd']
699 nsd
['id'] = 'clone_' + nsd
['id']
700 nsd
['name'] = 'clone_' + nsd
['name']
701 nsd
['short-name'] = 'clone_' + nsd
['short-name']
702 elif descriptor_type
== 'vnfd':
703 vnfd_list
= yaml_object
['vnfd:vnfd-catalog']['vnfd']
704 for vnfd
in vnfd_list
:
705 vnfd
['id'] = 'clone_' + vnfd
['id']
706 vnfd
['name'] = 'clone_' + vnfd
['name']
707 vnfd
['short-name'] = 'clone_' + vnfd
['short-name']
709 with
open('/tmp/' + name
, 'w') as yaml_file
:
710 yaml_file
.write(yaml
.dump(yaml_object
, default_flow_style
=False))
713 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", "w:gz")
716 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
720 def nsd_get(self
, token
, id):
721 result
= {'error': True, 'data': ''}
722 headers
= {'Content-Type': 'application/yaml',
723 'Authorization': 'Bearer {}'.format(token
['id'])}
724 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd".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 vnfd_get(self
, token
, id):
742 result
= {'error': True, 'data': ''}
743 headers
= {'Content-Type': 'application/yaml',
744 'Authorization': 'Bearer {}'.format(token
['id'])}
745 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".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 return yaml
.load(r
.text
)
757 result
['data'] = r
.json()
758 except Exception as e
:
762 def nsd_artifacts(self
, token
, id):
763 result
= {'error': True, 'data': ''}
764 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
765 'Authorization': 'Bearer {}'.format(token
['id'])}
766 _url
= "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self
._base
_path
, id)
768 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
769 except Exception as e
:
771 result
['data'] = str(e
)
773 if r
.status_code
== requests
.codes
.ok
:
774 result
['error'] = False
775 result
['data'] = r
.text
778 result
['data'] = r
.json()
779 except Exception as e
:
784 def vnf_packages_artifacts(self
, token
, id):
785 result
= {'error': True, 'data': ''}
786 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
787 'Authorization': 'Bearer {}'.format(token
['id'])}
788 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self
._base
_path
, id)
790 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
791 except Exception as e
:
793 result
['data'] = str(e
)
795 if r
.status_code
== requests
.codes
.ok
:
796 result
['error'] = False
797 result
['data'] = r
.text
800 result
['data'] = r
.json()
801 except Exception as e
:
806 def ns_create(self
, token
, ns_data
):
807 result
= {'error': True, 'data': ''}
808 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
809 'Authorization': 'Bearer {}'.format(token
['id'])}
811 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
814 r
= requests
.post(_url
, json
=ns_data
, verify
=False, 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
)
824 def pdu_create(self
, token
, pdu_data
):
825 result
= {'error': True, 'data': ''}
826 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
827 'Authorization': 'Bearer {}'.format(token
['id'])}
829 _url
= "{0}/pdu/v1/pdu_descriptors".format(self
._base
_path
)
832 r
= requests
.post(_url
, json
=pdu_data
, verify
=False, headers
=headers
)
833 except Exception as e
:
835 result
['data'] = str(e
)
837 if r
.status_code
== requests
.codes
.created
:
838 result
['error'] = False
839 result
['data'] = Util
.json_loads_byteified(r
.text
)
842 def ns_op_list(self
, token
, id):
843 result
= {'error': True, 'data': ''}
844 headers
= {"Content-Type": "application/json", "accept": "application/json",
845 'Authorization': 'Bearer {}'.format(token
['id'])}
846 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self
._base
_path
, id)
849 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
850 except Exception as e
:
852 result
['data'] = str(e
)
854 if r
.status_code
== requests
.codes
.ok
:
855 result
['error'] = False
856 result
['data'] = Util
.json_loads_byteified(r
.text
)
860 def ns_op(self
, token
, id):
861 result
= {'error': True, 'data': ''}
862 headers
= {"Content-Type": "application/json", "accept": "application/json",
863 'Authorization': 'Bearer {}'.format(token
['id'])}
864 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self
._base
_path
, id)
867 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
868 except Exception as e
:
870 result
['data'] = str(e
)
872 if r
.status_code
== requests
.codes
.ok
:
873 result
['error'] = False
874 result
['data'] = Util
.json_loads_byteified(r
.text
)
878 def ns_action(self
, token
, id, action_payload
):
879 result
= {'error': True, 'data': ''}
880 headers
= {"Content-Type": "application/json", "accept": "application/json",
881 'Authorization': 'Bearer {}'.format(token
['id'])}
883 _url
= "{0}/nslcm/v1/ns_instances/{1}/action".format(self
._base
_path
, id)
886 r
= requests
.post(_url
, json
=action_payload
, verify
=False, headers
=headers
)
887 except Exception as e
:
889 result
['data'] = str(e
)
891 if r
.status_code
== requests
.codes
.created
:
892 result
['error'] = False
893 result
['data'] = Util
.json_loads_byteified(r
.text
)
896 def ns_delete(self
, token
, id, force
=None):
897 result
= {'error': True, 'data': ''}
898 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
899 'Authorization': 'Bearer {}'.format(token
['id'])}
902 query_path
= '?FORCE=true'
903 _url
= "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self
._base
_path
, id, query_path
)
905 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
906 except Exception as e
:
908 result
['data'] = str(e
)
911 result
['error'] = False
912 if r
.status_code
!= requests
.codes
.no_content
:
913 result
['data'] = Util
.json_loads_byteified(r
.text
)
916 def pdu_delete(self
, token
, id):
917 result
= {'error': True, 'data': ''}
918 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
919 'Authorization': 'Bearer {}'.format(token
['id'])}
920 _url
= "{0}/pdu/v1/pdu_descriptors/{1}".format(self
._base
_path
, id)
922 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
923 except Exception as e
:
925 result
['data'] = str(e
)
928 result
['error'] = False
929 if r
.status_code
!= requests
.codes
.no_content
:
930 result
['data'] = Util
.json_loads_byteified(r
.text
)
933 def ns_get(self
, token
, id):
934 result
= {'error': True, 'data': ''}
935 headers
= {"Content-Type": "application/json", "accept": "application/json",
936 'Authorization': 'Bearer {}'.format(token
['id'])}
937 _url
= "{0}/nslcm/v1/ns_instances_content/{1}".format(self
._base
_path
, id)
940 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
941 except Exception as e
:
943 result
['data'] = str(e
)
945 if r
.status_code
== requests
.codes
.ok
:
946 result
['error'] = False
947 result
['data'] = Util
.json_loads_byteified(r
.text
)
950 def vnf_get(self
, token
, id):
951 result
= {'error': True, 'data': ''}
952 headers
= {"Content-Type": "application/json", "accept": "application/json",
953 'Authorization': 'Bearer {}'.format(token
['id'])}
954 _url
= "{0}/nslcm/v1/vnfrs/{1}".format(self
._base
_path
, id)
957 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
958 except Exception as e
:
960 result
['data'] = str(e
)
962 if r
.status_code
== requests
.codes
.ok
:
963 result
['error'] = False
964 result
['data'] = Util
.json_loads_byteified(r
.text
)
967 def pdu_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}/pdu/v1/pdu_descriptors/{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 ns_alarm_create(self
, token
, id, alarm_payload
):
985 result
= {'error': True, 'data': ''}
986 headers
= {"Content-Type": "application/json",
987 'Authorization': 'Bearer {}'.format(token
['id'])}
988 _url
= "{0}/test/message/alarm_request".format(self
._base
_path
)
990 r
= requests
.post(_url
, json
=alarm_payload
, verify
=False, headers
=headers
)
991 except Exception as e
:
993 result
['data'] = str(e
)
995 if r
.status_code
== requests
.codes
.ok
:
996 result
['error'] = False
997 # result['data'] = Util.json_loads_byteified(r.text)
998 result
['data'] = r
.text
1001 def ns_metric_export(self
, token
, id, metric_payload
):
1002 result
= {'error': True, 'data': ''}
1003 headers
= {"Content-Type": "application/json",
1004 'Authorization': 'Bearer {}'.format(token
['id'])}
1005 _url
= "{0}/test/message/metric_request".format(self
._base
_path
)
1007 r
= requests
.post(_url
, json
=metric_payload
, verify
=False, headers
=headers
)
1008 except Exception as e
:
1010 result
['data'] = str(e
)
1012 if r
.status_code
== requests
.codes
.ok
:
1013 result
['error'] = False
1014 # result['data'] = Util.json_loads_byteified(r.text)
1015 result
['data'] = r
.text
1018 def vim_list(self
, token
):
1019 result
= {'error': True, 'data': ''}
1020 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
1021 'Authorization': 'Bearer {}'.format(token
['id'])}
1022 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
1024 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1025 except Exception as e
:
1027 result
['data'] = str(e
)
1029 if r
.status_code
== requests
.codes
.ok
:
1030 result
['error'] = False
1031 result
['data'] = Util
.json_loads_byteified(r
.text
)
1035 def vim_delete(self
, token
, id):
1036 result
= {'error': True, 'data': ''}
1037 headers
= {"accept": "application/json",
1038 'Authorization': 'Bearer {}'.format(token
['id'])}
1039 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
1041 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
1042 except Exception as e
:
1044 result
['data'] = str(e
)
1046 if r
.status_code
== requests
.codes
.accepted
:
1047 result
['error'] = False
1049 result
['data'] = r
.text
1052 def vim_get(self
, token
, id):
1054 result
= {'error': True, 'data': ''}
1055 headers
= {"Content-Type": "application/json", "accept": "application/json",
1056 'Authorization': 'Bearer {}'.format(token
['id'])}
1057 _url
= "{0}/admin/v1/vims/{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 vim_create(self
, token
, vim_data
):
1072 result
= {'error': True, 'data': ''}
1073 headers
= {"Content-Type": "application/json", "accept": "application/json",
1074 'Authorization': 'Bearer {}'.format(token
['id'])}
1076 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
1079 r
= requests
.post(_url
, json
=vim_data
, verify
=False, headers
=headers
)
1080 except Exception as e
:
1082 result
['data'] = str(e
)
1084 if r
.status_code
== requests
.codes
.created
:
1085 result
['error'] = False
1086 result
['data'] = Util
.json_loads_byteified(r
.text
)
1089 def sdn_list(self
, token
):
1090 result
= {'error': True, 'data': ''}
1091 headers
= {"accept": "application/json",
1092 'Authorization': 'Bearer {}'.format(token
['id'])}
1093 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
1095 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1096 except Exception as e
:
1098 result
['data'] = str(e
)
1100 if r
.status_code
== requests
.codes
.ok
:
1101 result
['error'] = False
1102 result
['data'] = Util
.json_loads_byteified(r
.text
)
1105 def sdn_delete(self
, token
, id):
1106 result
= {'error': True, 'data': ''}
1107 headers
= {"accept": "application/json",
1108 'Authorization': 'Bearer {}'.format(token
['id'])}
1109 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
1111 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
1112 except Exception as e
:
1114 result
['data'] = str(e
)
1116 if r
.status_code
== requests
.codes
.accepted
:
1117 result
['error'] = False
1119 result
['data'] = r
.text
1122 def sdn_get(self
, token
, id):
1123 result
= {'error': True, 'data': ''}
1124 headers
= {"accept": "application/json",
1125 'Authorization': 'Bearer {}'.format(token
['id'])}
1126 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
1129 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1130 except Exception as e
:
1132 result
['data'] = str(e
)
1134 if r
.status_code
== requests
.codes
.ok
:
1135 result
['error'] = False
1136 result
['data'] = Util
.json_loads_byteified(r
.text
)
1139 def sdn_create(self
, token
, sdn_data
):
1140 result
= {'error': True, 'data': ''}
1141 headers
= {"Content-Type": "application/json", "accept": "application/json",
1142 'Authorization': 'Bearer {}'.format(token
['id'])}
1144 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
1147 r
= requests
.post(_url
, json
=sdn_data
, verify
=False, headers
=headers
)
1148 except Exception as e
:
1150 result
['data'] = str(e
)
1152 if r
.status_code
== requests
.codes
.created
:
1153 result
['error'] = False
1154 result
['data'] = Util
.json_loads_byteified(r
.text
)
1159 hash_md5
= hashlib
.md5()
1160 for chunk
in iter(lambda: f
.read(1024), b
""):
1161 hash_md5
.update(chunk
)
1162 return hash_md5
.hexdigest()