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 nst_details(self
, token
, id):
255 result
= {'error': True, 'data': ''}
256 headers
= {"Content-Type": "application/json", "accept": "application/json",
257 'Authorization': 'Bearer {}'.format(token
['id'])}
258 _url
= "{0}/nst/v1/netslice_templates/{1}".format(self
._base
_path
,id)
260 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
261 except Exception as e
:
263 result
['data'] = str(e
)
265 if r
.status_code
== requests
.codes
.ok
:
266 result
['error'] = False
267 result
['data'] = Util
.json_loads_byteified(r
.text
)
271 def nst_content(self
, token
, id):
272 result
= {'error': True, 'data': ''}
273 headers
= {"Content-Type": "application/json", "accept": "text/plain",
274 'Authorization': 'Bearer {}'.format(token
['id'])}
275 _url
= "{0}/nst/v1/netslice_templates/{1}/nst".format(self
._base
_path
,id)
277 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
278 except Exception as e
:
280 result
['data'] = str(e
)
282 if r
.status_code
== requests
.codes
.ok
:
283 result
['error'] = False
284 result
['data'] = Util
.json2yaml(yaml
.load(str(r
.text
)))
288 def nst_list(self
, token
):
289 result
= {'error': True, 'data': ''}
290 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
291 'Authorization': 'Bearer {}'.format(token
['id'])}
293 _url
= "{0}/nst/v1/netslice_templates".format(self
._base
_path
)
295 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
296 except Exception as e
:
298 result
['data'] = str(e
)
300 if r
.status_code
== requests
.codes
.ok
:
301 result
['error'] = False
302 result
['data'] = Util
.json_loads_byteified(r
.text
)
306 def nsd_list(self
, token
, filter=None):
307 result
= {'error': True, 'data': ''}
308 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
309 'Authorization': 'Bearer {}'.format(token
['id'])}
312 query_path
= '?_admin.type='+filter
313 _url
= "{0}/nsd/v1/ns_descriptors_content{1}".format(self
._base
_path
, query_path
)
315 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
316 except Exception as e
:
318 result
['data'] = str(e
)
320 if r
.status_code
== requests
.codes
.ok
:
321 result
['error'] = False
322 result
['data'] = Util
.json_loads_byteified(r
.text
)
326 def vnfd_list(self
, token
, filter=None):
327 result
= {'error': True, 'data': ''}
328 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
329 'Authorization': 'Bearer {}'.format(token
['id'])}
332 query_path
= '?_admin.type='+filter
333 _url
= "{0}/vnfpkgm/v1/vnf_packages_content{1}".format(self
._base
_path
, query_path
)
335 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
336 except Exception as e
:
338 result
['data'] = str(e
)
340 if r
.status_code
== requests
.codes
.ok
:
341 result
['error'] = False
342 result
['data'] = Util
.json_loads_byteified(r
.text
)
346 def ns_list(self
, token
):
347 result
= {'error': True, 'data': ''}
348 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
349 'Authorization': 'Bearer {}'.format(token
['id'])}
350 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
352 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, 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
)
363 def vnf_list(self
, token
):
364 result
= {'error': True, 'data': ''}
365 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
366 'Authorization': 'Bearer {}'.format(token
['id'])}
367 _url
= "{0}/nslcm/v1/vnfrs".format(self
._base
_path
)
369 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
370 except Exception as e
:
372 result
['data'] = str(e
)
374 if r
.status_code
== requests
.codes
.ok
:
375 result
['error'] = False
376 result
['data'] = Util
.json_loads_byteified(r
.text
)
380 def pdu_list(self
, token
):
381 result
= {'error': True, 'data': ''}
382 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
383 'Authorization': 'Bearer {}'.format(token
['id'])}
384 _url
= "{0}/pdu/v1/pdu_descriptors".format(self
._base
_path
)
386 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
387 except Exception as e
:
389 result
['data'] = str(e
)
391 if r
.status_code
== requests
.codes
.ok
:
392 result
['error'] = False
393 result
['data'] = Util
.json_loads_byteified(r
.text
)
397 def nst_delete(self
, token
, id):
398 result
= {'error': True, 'data': ''}
399 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
400 'Authorization': 'Bearer {}'.format(token
['id'])}
402 _url
= "{0}/nst/v1/netslice_templates/{1}?FORCE=True".format(self
._base
_path
, id)
404 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
405 except Exception as e
:
407 result
['data'] = str(e
)
409 if r
.status_code
== requests
.codes
.no_content
:
410 result
['error'] = False
414 def nsd_delete(self
, token
, id):
415 result
= {'error': True, 'data': ''}
416 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
417 'Authorization': 'Bearer {}'.format(token
['id'])}
419 _url
= "{0}/nsd/v1/ns_descriptors_content/{1}".format(self
._base
_path
, id)
421 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
422 except Exception as e
:
424 result
['data'] = str(e
)
426 if r
.status_code
== requests
.codes
.ok
:
427 result
['error'] = False
428 result
['data'] = Util
.json_loads_byteified(r
.text
)
431 def vnfd_delete(self
, token
, id):
432 result
= {'error': True, 'data': ''}
433 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
434 'Authorization': 'Bearer {}'.format(token
['id'])}
436 _url
= "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self
._base
_path
, id)
438 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
439 except Exception as e
:
441 result
['data'] = str(e
)
444 result
['error'] = False
445 if r
.status_code
!= requests
.codes
.no_content
:
446 result
['data'] = Util
.json_loads_byteified(r
.text
)
449 def nst_onboard(self
, token
, template
):
450 result
= {'error': True, 'data': ''}
451 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
452 'Authorization': 'Bearer {}'.format(token
['id'])}
453 _url
= "{0}/nst/v1/netslice_templates_content".format(self
._base
_path
)
455 fileName
, fileExtension
= os
.path
.splitext(template
.name
)
456 if fileExtension
== '.gz':
457 headers
["Content-Type"] = "application/gzip"
459 headers
["Content-Type"] = "application/yaml"
460 r
= requests
.post(_url
, data
=template
, verify
=False, headers
=headers
)
461 except Exception as e
:
463 result
['data'] = str(e
)
465 if r
.status_code
== requests
.codes
.created
:
466 result
['error'] = False
467 result
['data'] = Util
.json_loads_byteified(r
.text
)
470 def nsd_onboard(self
, token
, package
):
471 result
= {'error': True, 'data': ''}
472 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
473 'Authorization': 'Bearer {}'.format(token
['id'])}
474 with
open('/tmp/' + package
.name
, 'wb+') as destination
:
475 for chunk
in package
.chunks():
476 destination
.write(chunk
)
477 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + package
.name
, 'rb'))
478 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
480 r
= requests
.post(_url
, data
=open('/tmp/' + package
.name
, 'rb'), verify
=False, headers
=headers
)
481 except Exception as e
:
483 result
['data'] = str(e
)
485 if r
.status_code
== requests
.codes
.created
:
486 result
['error'] = False
487 result
['data'] = Util
.json_loads_byteified(r
.text
)
490 def vnfd_onboard(self
, token
, package
):
491 result
= {'error': True, 'data': ''}
492 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
493 'Authorization': 'Bearer {}'.format(token
['id'])}
494 with
open('/tmp/' + package
.name
, 'wb+') as destination
:
495 for chunk
in package
.chunks():
496 destination
.write(chunk
)
497 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + package
.name
, 'rb'))
498 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
500 r
= requests
.post(_url
, data
=open('/tmp/' + package
.name
, 'rb'), verify
=False, headers
=headers
)
501 except Exception as e
:
503 result
['data'] = str(e
)
505 if r
.status_code
== requests
.codes
.created
:
506 result
['error'] = False
507 result
['data'] = Util
.json_loads_byteified(r
.text
)
510 def nsd_create_pkg_base(self
, token
, pkg_name
):
511 result
= {'error': True, 'data': ''}
512 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
513 'Authorization': 'Bearer {}'.format(token
['id'])}
515 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
518 self
._create
_base
_pkg
('nsd', pkg_name
)
519 headers
['Content-Filename'] = pkg_name
+ '.tar.gz'
520 r
= requests
.post(_url
, data
=open('/tmp/' + pkg_name
+ '.tar.gz', 'rb'), verify
=False, headers
=headers
)
521 except Exception as e
:
523 result
['data'] = str(e
)
525 if r
.status_code
== requests
.codes
.created
:
526 result
['data'] = r
.json()
527 result
['error'] = False
528 if r
.status_code
== requests
.codes
.conflict
:
529 result
['data'] = "Invalid ID."
532 def vnfd_create_pkg_base(self
, token
, pkg_name
):
533 result
= {'error': True, 'data': ''}
534 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
535 'Authorization': 'Bearer {}'.format(token
['id'])}
537 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
540 self
._create
_base
_pkg
('vnfd', pkg_name
)
541 r
= requests
.post(_url
, data
=open('/tmp/' + pkg_name
+ '.tar.gz', 'rb'), verify
=False, headers
=headers
)
542 except Exception as e
:
544 result
['data'] = str(e
)
546 if r
.status_code
== requests
.codes
.created
:
547 result
['data'] = r
.json()
548 result
['error'] = False
549 if r
.status_code
== requests
.codes
.conflict
:
550 result
['data'] = "Invalid ID."
553 def nsd_clone(self
, token
, id):
554 result
= {'error': True, 'data': ''}
555 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
556 'Authorization': 'Bearer {}'.format(token
['id'])}
558 # get the package onboarded
559 tar_pkg
= self
.get_nsd_pkg(token
, id)
560 tarf
= tarfile
.open(fileobj
=tar_pkg
)
561 tarf
= self
._descriptor
_clone
(tarf
, 'nsd')
562 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
564 _url
= "{0}/nsd/v1/ns_descriptors_content/".format(self
._base
_path
)
567 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
569 except Exception as e
:
571 result
['data'] = str(e
)
573 if r
.status_code
== requests
.codes
.created
:
574 result
['error'] = False
575 if r
.status_code
== requests
.codes
.conflict
:
576 result
['data'] = "Invalid ID."
580 def vnfd_clone(self
, token
, id):
581 result
= {'error': True, 'data': ''}
582 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
583 'Authorization': 'Bearer {}'.format(token
['id'])}
585 # get the package onboarded
586 tar_pkg
= self
.get_vnfd_pkg(token
, id)
587 tarf
= tarfile
.open(fileobj
=tar_pkg
)
589 tarf
= self
._descriptor
_clone
(tarf
, 'vnfd')
590 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'))
592 _url
= "{0}/vnfpkgm/v1/vnf_packages_content".format(self
._base
_path
)
595 r
= requests
.post(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", 'rb'), verify
=False,
597 except Exception as e
:
599 result
['data'] = str(e
)
601 if r
.status_code
== requests
.codes
.created
:
602 result
['error'] = False
603 if r
.status_code
== requests
.codes
.conflict
:
604 result
['data'] = "Invalid ID."
608 def nst_content_update(self
, token
, id, template
):
609 result
= {'error': True, 'data': ''}
610 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
611 'Authorization': 'Bearer {}'.format(token
['id'])}
612 _url
= "{0}/nst/v1/netslice_templates/{1}/nst_content".format(self
._base
_path
,id)
614 r
= requests
.put(_url
, data
=template
, verify
=False, headers
=headers
)
615 except Exception as e
:
617 result
['data'] = str(e
)
619 if r
.status_code
== requests
.codes
.no_content
:
620 result
['error'] = False
623 def nsd_update(self
, token
, id, data
):
624 result
= {'error': True, 'data': ''}
625 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
626 'Authorization': 'Bearer {}'.format(token
['id'])}
628 # get the package onboarded
629 tar_pkg
= self
.get_nsd_pkg(token
, id)
630 tarf
= tarfile
.open(fileobj
=tar_pkg
)
632 tarf
= self
._descriptor
_update
(tarf
, data
)
633 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
635 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self
._base
_path
, id)
638 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
640 except Exception as e
:
642 result
['data'] = str(e
)
644 if r
.status_code
== requests
.codes
.no_content
:
645 result
['error'] = False
648 result
['data'] = r
.json()
649 except Exception as e
:
654 def vnfd_update(self
, token
, id, data
):
655 result
= {'error': True, 'data': ''}
656 headers
= {"Content-Type": "application/gzip", "accept": "application/json",
657 'Authorization': 'Bearer {}'.format(token
['id'])}
659 # get the package onboarded
660 tar_pkg
= self
.get_vnfd_pkg(token
, id)
661 tarf
= tarfile
.open(fileobj
=tar_pkg
)
663 tarf
= self
._descriptor
_update
(tarf
, data
)
664 headers
['Content-File-MD5'] = self
.md5(open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'))
666 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
669 r
= requests
.put(_url
, data
=open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", 'rb'), verify
=False,
671 except Exception as e
:
673 result
['data'] = str(e
)
675 if r
.status_code
== requests
.codes
.no_content
:
676 result
['error'] = False
679 result
['data'] = r
.json()
680 except Exception as e
:
685 def get_nsd_pkg(self
, token
, id):
686 result
= {'error': True, 'data': ''}
687 headers
= {"accept": "application/zip",
688 'Authorization': 'Bearer {}'.format(token
['id'])}
690 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".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 tarf
= StringIO
.StringIO(r
.content
)
703 def get_vnfd_pkg(self
, token
, id):
704 result
= {'error': True, 'data': ''}
705 headers
= {"accept": "application/zip",
706 'Authorization': 'Bearer {}'.format(token
['id'])}
707 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self
._base
_path
, id)
709 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
710 except Exception as e
:
712 result
['data'] = str(e
)
714 if r
.status_code
== requests
.codes
.ok
:
715 result
['error'] = False
716 tarf
= StringIO
.StringIO(r
.content
)
720 def _descriptor_update(self
, tarf
, data
):
721 # extract the package on a tmp directory
722 tarf
.extractall('/tmp')
724 for name
in tarf
.getnames():
725 if name
.endswith(".yaml") or name
.endswith(".yml"):
726 with
open('/tmp/' + name
, 'w') as outfile
:
727 yaml
.safe_dump(data
, outfile
, default_flow_style
=False)
730 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + ".tar.gz", "w:gz")
733 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
737 def _create_base_pkg(self
, descriptor_type
, pkg_name
):
738 filename
= '/tmp/'+pkg_name
+'/' + pkg_name
+ '.yaml'
739 if descriptor_type
== 'nsd':
744 "short-name": str(pkg_name
),
745 "vendor": "OSM Composer",
746 "description": str(pkg_name
) + " descriptor",
748 "constituent-vnfd": [],
751 "name": str(pkg_name
)
757 elif descriptor_type
== 'vnfd':
759 "vnfd:vnfd-catalog": {
762 "short-name": str(pkg_name
),
771 "connection-point": [],
772 "name": str(pkg_name
)
778 if not os
.path
.exists(os
.path
.dirname(filename
)):
780 os
.makedirs(os
.path
.dirname(filename
))
781 except OSError as exc
: # Guard against race condition
782 if exc
.errno
!= errno
.EEXIST
:
785 with
open('/tmp/' + pkg_name
+ '/' + pkg_name
+ '.yaml', 'w') as yaml_file
:
786 yaml_file
.write(yaml
.dump(descriptor
, default_flow_style
=False))
788 tarf_temp
= tarfile
.open('/tmp/' + pkg_name
+ '.tar.gz', "w:gz")
789 tarf_temp
.add('/tmp/'+pkg_name
+'/' + pkg_name
+ '.yaml', pkg_name
+ '/' + pkg_name
+ '.yaml', recursive
=False)
792 def _descriptor_clone(self
, tarf
, descriptor_type
):
793 # extract the package on a tmp directory
794 tarf
.extractall('/tmp')
796 for name
in tarf
.getnames():
797 if name
.endswith(".yaml") or name
.endswith(".yml"):
798 with
open('/tmp/' + name
, 'r') as outfile
:
799 yaml_object
= yaml
.load(outfile
)
801 if descriptor_type
== 'nsd':
802 nsd_list
= yaml_object
['nsd:nsd-catalog']['nsd']
804 nsd
['id'] = 'clone_' + nsd
['id']
805 nsd
['name'] = 'clone_' + nsd
['name']
806 nsd
['short-name'] = 'clone_' + nsd
['short-name']
807 elif descriptor_type
== 'vnfd':
808 vnfd_list
= yaml_object
['vnfd:vnfd-catalog']['vnfd']
809 for vnfd
in vnfd_list
:
810 vnfd
['id'] = 'clone_' + vnfd
['id']
811 vnfd
['name'] = 'clone_' + vnfd
['name']
812 vnfd
['short-name'] = 'clone_' + vnfd
['short-name']
814 with
open('/tmp/' + name
, 'w') as yaml_file
:
815 yaml_file
.write(yaml
.dump(yaml_object
, default_flow_style
=False))
818 tarf_temp
= tarfile
.open('/tmp/' + tarf
.getnames()[0] + "_clone.tar.gz", "w:gz")
821 tarf_temp
.add('/tmp/' + tarinfo
.name
, tarinfo
.name
, recursive
=False)
825 def nsd_get(self
, token
, id):
826 result
= {'error': True, 'data': ''}
827 headers
= {'Content-Type': 'application/yaml',
828 'Authorization': 'Bearer {}'.format(token
['id'])}
829 _url
= "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self
._base
_path
, id)
831 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
832 except Exception as e
:
834 result
['data'] = str(e
)
836 if r
.status_code
== requests
.codes
.ok
:
837 result
['error'] = False
838 return yaml
.load(r
.text
)
841 result
['data'] = r
.json()
842 except Exception as e
:
846 def vnfd_get(self
, token
, id):
847 result
= {'error': True, 'data': ''}
848 headers
= {'Content-Type': 'application/yaml',
849 'Authorization': 'Bearer {}'.format(token
['id'])}
850 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self
._base
_path
, id)
852 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
853 except Exception as e
:
855 result
['data'] = str(e
)
857 if r
.status_code
== requests
.codes
.ok
:
858 result
['error'] = False
859 return yaml
.load(r
.text
)
862 result
['data'] = r
.json()
863 except Exception as e
:
867 def nsd_artifacts(self
, token
, id):
868 result
= {'error': True, 'data': ''}
869 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
870 'Authorization': 'Bearer {}'.format(token
['id'])}
871 _url
= "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self
._base
_path
, id)
873 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
874 except Exception as e
:
876 result
['data'] = str(e
)
878 if r
.status_code
== requests
.codes
.ok
:
879 result
['error'] = False
880 result
['data'] = r
.text
883 result
['data'] = r
.json()
884 except Exception as e
:
889 def vnf_packages_artifacts(self
, token
, id):
890 result
= {'error': True, 'data': ''}
891 headers
= {'Content-Type': 'application/yaml', 'accept': 'text/plain',
892 'Authorization': 'Bearer {}'.format(token
['id'])}
893 _url
= "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self
._base
_path
, id)
895 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
896 except Exception as e
:
898 result
['data'] = str(e
)
900 if r
.status_code
== requests
.codes
.ok
:
901 result
['error'] = False
902 result
['data'] = r
.text
905 result
['data'] = r
.json()
906 except Exception as e
:
911 def ns_create(self
, token
, ns_data
):
912 result
= {'error': True, 'data': ''}
913 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
914 'Authorization': 'Bearer {}'.format(token
['id'])}
916 _url
= "{0}/nslcm/v1/ns_instances_content".format(self
._base
_path
)
919 r
= requests
.post(_url
, json
=ns_data
, verify
=False, headers
=headers
)
920 except Exception as e
:
922 result
['data'] = str(e
)
924 if r
.status_code
== requests
.codes
.ok
:
925 result
['error'] = False
926 result
['data'] = Util
.json_loads_byteified(r
.text
)
929 def pdu_create(self
, token
, pdu_data
):
930 result
= {'error': True, 'data': ''}
931 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
932 'Authorization': 'Bearer {}'.format(token
['id'])}
934 _url
= "{0}/pdu/v1/pdu_descriptors".format(self
._base
_path
)
937 r
= requests
.post(_url
, json
=pdu_data
, verify
=False, headers
=headers
)
938 except Exception as e
:
940 result
['data'] = str(e
)
942 if r
.status_code
== requests
.codes
.created
:
943 result
['error'] = False
944 result
['data'] = Util
.json_loads_byteified(r
.text
)
947 def ns_op_list(self
, token
, id):
948 result
= {'error': True, 'data': ''}
949 headers
= {"Content-Type": "application/json", "accept": "application/json",
950 'Authorization': 'Bearer {}'.format(token
['id'])}
951 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self
._base
_path
, id)
954 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
955 except Exception as e
:
957 result
['data'] = str(e
)
959 if r
.status_code
== requests
.codes
.ok
:
960 result
['error'] = False
961 result
['data'] = Util
.json_loads_byteified(r
.text
)
965 def ns_op(self
, token
, id):
966 result
= {'error': True, 'data': ''}
967 headers
= {"Content-Type": "application/json", "accept": "application/json",
968 'Authorization': 'Bearer {}'.format(token
['id'])}
969 _url
= "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self
._base
_path
, id)
972 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
973 except Exception as e
:
975 result
['data'] = str(e
)
977 if r
.status_code
== requests
.codes
.ok
:
978 result
['error'] = False
979 result
['data'] = Util
.json_loads_byteified(r
.text
)
983 def ns_action(self
, token
, id, action_payload
):
984 result
= {'error': True, 'data': ''}
985 headers
= {"Content-Type": "application/json", "accept": "application/json",
986 'Authorization': 'Bearer {}'.format(token
['id'])}
988 _url
= "{0}/nslcm/v1/ns_instances/{1}/action".format(self
._base
_path
, id)
991 r
= requests
.post(_url
, json
=action_payload
, verify
=False, headers
=headers
)
992 except Exception as e
:
994 result
['data'] = str(e
)
996 if r
.status_code
== requests
.codes
.created
:
997 result
['error'] = False
998 result
['data'] = Util
.json_loads_byteified(r
.text
)
1001 def ns_delete(self
, token
, id, force
=None):
1002 result
= {'error': True, 'data': ''}
1003 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
1004 'Authorization': 'Bearer {}'.format(token
['id'])}
1007 query_path
= '?FORCE=true'
1008 _url
= "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self
._base
_path
, id, query_path
)
1010 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
1011 except Exception as e
:
1013 result
['data'] = str(e
)
1016 result
['error'] = False
1017 if r
.status_code
!= requests
.codes
.no_content
:
1018 result
['data'] = Util
.json_loads_byteified(r
.text
)
1021 def pdu_delete(self
, token
, id):
1022 result
= {'error': True, 'data': ''}
1023 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
1024 'Authorization': 'Bearer {}'.format(token
['id'])}
1025 _url
= "{0}/pdu/v1/pdu_descriptors/{1}".format(self
._base
_path
, id)
1027 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
1028 except Exception as e
:
1030 result
['data'] = str(e
)
1033 result
['error'] = False
1034 if r
.status_code
!= requests
.codes
.no_content
:
1035 result
['data'] = Util
.json_loads_byteified(r
.text
)
1038 def ns_get(self
, token
, id):
1039 result
= {'error': True, 'data': ''}
1040 headers
= {"Content-Type": "application/json", "accept": "application/json",
1041 'Authorization': 'Bearer {}'.format(token
['id'])}
1042 _url
= "{0}/nslcm/v1/ns_instances_content/{1}".format(self
._base
_path
, id)
1045 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1046 except Exception as e
:
1048 result
['data'] = str(e
)
1050 if r
.status_code
== requests
.codes
.ok
:
1051 result
['error'] = False
1052 result
['data'] = Util
.json_loads_byteified(r
.text
)
1055 def vnf_get(self
, token
, id):
1056 result
= {'error': True, 'data': ''}
1057 headers
= {"Content-Type": "application/json", "accept": "application/json",
1058 'Authorization': 'Bearer {}'.format(token
['id'])}
1059 _url
= "{0}/nslcm/v1/vnfrs/{1}".format(self
._base
_path
, id)
1062 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1063 except Exception as e
:
1065 result
['data'] = str(e
)
1067 if r
.status_code
== requests
.codes
.ok
:
1068 result
['error'] = False
1069 result
['data'] = Util
.json_loads_byteified(r
.text
)
1072 def pdu_get(self
, token
, id):
1073 result
= {'error': True, 'data': ''}
1074 headers
= {"Content-Type": "application/json", "accept": "application/json",
1075 'Authorization': 'Bearer {}'.format(token
['id'])}
1076 _url
= "{0}/pdu/v1/pdu_descriptors/{1}".format(self
._base
_path
, id)
1079 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1080 except Exception as e
:
1082 result
['data'] = str(e
)
1084 if r
.status_code
== requests
.codes
.ok
:
1085 result
['error'] = False
1086 result
['data'] = Util
.json_loads_byteified(r
.text
)
1089 def ns_alarm_create(self
, token
, id, alarm_payload
):
1090 result
= {'error': True, 'data': ''}
1091 headers
= {"Content-Type": "application/json",
1092 'Authorization': 'Bearer {}'.format(token
['id'])}
1093 _url
= "{0}/test/message/alarm_request".format(self
._base
_path
)
1095 r
= requests
.post(_url
, json
=alarm_payload
, verify
=False, 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)
1103 result
['data'] = r
.text
1106 def ns_metric_export(self
, token
, id, metric_payload
):
1107 result
= {'error': True, 'data': ''}
1108 headers
= {"Content-Type": "application/json",
1109 'Authorization': 'Bearer {}'.format(token
['id'])}
1110 _url
= "{0}/test/message/metric_request".format(self
._base
_path
)
1112 r
= requests
.post(_url
, json
=metric_payload
, verify
=False, headers
=headers
)
1113 except Exception as e
:
1115 result
['data'] = str(e
)
1117 if r
.status_code
== requests
.codes
.ok
:
1118 result
['error'] = False
1119 # result['data'] = Util.json_loads_byteified(r.text)
1120 result
['data'] = r
.text
1123 def vim_list(self
, token
):
1124 result
= {'error': True, 'data': ''}
1125 headers
= {"Content-Type": "application/yaml", "accept": "application/json",
1126 'Authorization': 'Bearer {}'.format(token
['id'])}
1127 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
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
)
1140 def vim_delete(self
, token
, id):
1141 result
= {'error': True, 'data': ''}
1142 headers
= {"accept": "application/json",
1143 'Authorization': 'Bearer {}'.format(token
['id'])}
1144 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
1146 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
1147 except Exception as e
:
1149 result
['data'] = str(e
)
1151 if r
.status_code
== requests
.codes
.accepted
:
1152 result
['error'] = False
1154 result
['data'] = r
.text
1157 def vim_get(self
, token
, id):
1159 result
= {'error': True, 'data': ''}
1160 headers
= {"Content-Type": "application/json", "accept": "application/json",
1161 'Authorization': 'Bearer {}'.format(token
['id'])}
1162 _url
= "{0}/admin/v1/vims/{1}".format(self
._base
_path
, id)
1165 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1166 except Exception as e
:
1168 result
['data'] = str(e
)
1170 if r
.status_code
== requests
.codes
.ok
:
1171 result
['error'] = False
1172 result
['data'] = Util
.json_loads_byteified(r
.text
)
1175 def vim_create(self
, token
, vim_data
):
1177 result
= {'error': True, 'data': ''}
1178 headers
= {"Content-Type": "application/json", "accept": "application/json",
1179 'Authorization': 'Bearer {}'.format(token
['id'])}
1181 _url
= "{0}/admin/v1/vims".format(self
._base
_path
)
1184 r
= requests
.post(_url
, json
=vim_data
, verify
=False, headers
=headers
)
1185 except Exception as e
:
1187 result
['data'] = str(e
)
1189 if r
.status_code
== requests
.codes
.created
:
1190 result
['error'] = False
1191 result
['data'] = Util
.json_loads_byteified(r
.text
)
1194 def sdn_list(self
, token
):
1195 result
= {'error': True, 'data': ''}
1196 headers
= {"accept": "application/json",
1197 'Authorization': 'Bearer {}'.format(token
['id'])}
1198 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
1200 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1201 except Exception as e
:
1203 result
['data'] = str(e
)
1205 if r
.status_code
== requests
.codes
.ok
:
1206 result
['error'] = False
1207 result
['data'] = Util
.json_loads_byteified(r
.text
)
1210 def sdn_delete(self
, token
, id):
1211 result
= {'error': True, 'data': ''}
1212 headers
= {"accept": "application/json",
1213 'Authorization': 'Bearer {}'.format(token
['id'])}
1214 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
1216 r
= requests
.delete(_url
, params
=None, verify
=False, headers
=headers
)
1217 except Exception as e
:
1219 result
['data'] = str(e
)
1221 if r
.status_code
== requests
.codes
.accepted
:
1222 result
['error'] = False
1224 result
['data'] = r
.text
1227 def sdn_get(self
, token
, id):
1228 result
= {'error': True, 'data': ''}
1229 headers
= {"accept": "application/json",
1230 'Authorization': 'Bearer {}'.format(token
['id'])}
1231 _url
= "{0}/admin/v1/sdns/{1}".format(self
._base
_path
, id)
1234 r
= requests
.get(_url
, params
=None, verify
=False, stream
=True, headers
=headers
)
1235 except Exception as e
:
1237 result
['data'] = str(e
)
1239 if r
.status_code
== requests
.codes
.ok
:
1240 result
['error'] = False
1241 result
['data'] = Util
.json_loads_byteified(r
.text
)
1244 def sdn_create(self
, token
, sdn_data
):
1245 result
= {'error': True, 'data': ''}
1246 headers
= {"Content-Type": "application/json", "accept": "application/json",
1247 'Authorization': 'Bearer {}'.format(token
['id'])}
1249 _url
= "{0}/admin/v1/sdns".format(self
._base
_path
)
1252 r
= requests
.post(_url
, json
=sdn_data
, verify
=False, headers
=headers
)
1253 except Exception as e
:
1255 result
['data'] = str(e
)
1257 if r
.status_code
== requests
.codes
.created
:
1258 result
['error'] = False
1259 result
['data'] = Util
.json_loads_byteified(r
.text
)
1264 hash_md5
= hashlib
.md5()
1265 for chunk
in iter(lambda: f
.read(1024), b
""):
1266 hash_md5
.update(chunk
)
1267 return hash_md5
.hexdigest()