pdu: list, create, show, delete
[osm/LW-UI.git] / lib / osm / osmclient / clientv2.py
1 #
2 # Copyright 2018 EveryUP Srl
3 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16 import errno
17 import requests
18 import logging
19 import tarfile
20 import yaml
21 import StringIO
22 from lib.util import Util
23 import hashlib
24 import os
25 from requests.packages.urllib3.exceptions import InsecureRequestWarning
26
27 requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
28
29 logging.basicConfig(level=logging.INFO)
30 log = logging.getLogger('helper.py')
31 logging.getLogger("urllib3").setLevel(logging.INFO)
32
33
34 class Client(object):
35 def __init__(self):
36 self._token_endpoint = 'admin/v1/tokens'
37 self._user_endpoint = 'admin/v1/users'
38 self._host = os.getenv('OSM_SERVER', "localhost")
39 self._so_port = 9999
40 self._base_path = 'https://{0}:{1}/osm'.format(self._host, self._so_port)
41
42 def auth(self, args):
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"}
46 try:
47 r = requests.post(token_url, json=args, verify=False, headers=headers)
48 except Exception as e:
49 log.exception(e)
50 result['data'] = str(e)
51 return result
52 if r.status_code == requests.codes.ok:
53 result['error'] = False
54
55 result['data'] = Util.json_loads_byteified(r.text)
56
57 return result
58
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"}
63 try:
64 r = requests.post(token_url, json=args, verify=False, headers=headers)
65 except Exception as e:
66 log.exception(e)
67 result['data'] = str(e)
68 return result
69 if r.status_code == requests.codes.ok:
70 result['error'] = False
71
72 result['data'] = Util.json_loads_byteified(r.text)
73
74 return result
75
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'])}
80
81 _url = "{0}/admin/v1/users".format(self._base_path)
82 try:
83 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
84 except Exception as e:
85 log.exception(e)
86 result['data'] = str(e)
87 return result
88 if r.status_code == requests.codes.ok:
89 result['error'] = False
90 result['data'] = Util.json_loads_byteified(r.text)
91
92 return result
93
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'])}
98
99 _url = "{0}/admin/v1/users".format(self._base_path)
100
101 try:
102 r = requests.post(_url, json=user_data, verify=False, headers=headers)
103 except Exception as e:
104 log.exception(e)
105 result['data'] = str(e)
106 return result
107 if r.status_code == requests.codes.created:
108 result['error'] = False
109 result['data'] = Util.json_loads_byteified(r.text)
110 return result
111
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'])}
116
117 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
118 try:
119 r = requests.patch(_url, json=user_data, verify=False, headers=headers)
120 except Exception as e:
121 log.exception(e)
122 result['data'] = str(e)
123 return result
124 if r.status_code == requests.codes.no_content:
125 result['error'] = False
126 else:
127 result['data'] = Util.json_loads_byteified(r.text)
128 return result
129
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'])}
134
135 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
136 try:
137 r = requests.delete(_url, params=None, verify=False, headers=headers)
138 except Exception as e:
139 log.exception(e)
140 result['data'] = str(e)
141 return result
142 if r.status_code == requests.codes.no_content:
143 result['error'] = False
144 else:
145 result['data'] = Util.json_loads_byteified(r.text)
146 return result
147
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)
153 try:
154 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
155 except Exception as e:
156 log.exception(e)
157 result['data'] = str(e)
158 return result
159 if r.status_code == requests.codes.ok:
160 result['error'] = False
161 result['data'] = Util.json_loads_byteified(r.text)
162 return result
163
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'])}
168
169 _url = "{0}/admin/v1/projects".format(self._base_path)
170 try:
171 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
172 except Exception as e:
173 log.exception(e)
174 result['data'] = str(e)
175 return result
176 if r.status_code == requests.codes.ok:
177 result['error'] = False
178 result['data'] = Util.json_loads_byteified(r.text)
179
180 return result
181
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)
187 try:
188 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
189 except Exception as e:
190 log.exception(e)
191 result['data'] = str(e)
192 return result
193 if r.status_code == requests.codes.ok:
194 result['error'] = False
195 result['data'] = Util.json_loads_byteified(r.text)
196 return result
197
198 def project_create(self, token, project_data):
199
200 result = {'error': True, 'data': ''}
201 headers = {"Content-Type": "application/json", "accept": "application/json",
202 'Authorization': 'Bearer {}'.format(token['id'])}
203
204 _url = "{0}/admin/v1/projects".format(self._base_path)
205
206 try:
207 r = requests.post(_url, json=project_data, verify=False, headers=headers)
208 except Exception as e:
209 log.exception(e)
210 result['data'] = str(e)
211 return result
212 if r.status_code == requests.codes.created:
213 result['error'] = False
214 result['data'] = Util.json_loads_byteified(r.text)
215 return result
216
217 def project_edit(self, token, id, project_data):
218
219 result = {'error': True, 'data': ''}
220 headers = {"Content-Type": "application/json", "accept": "application/json",
221 'Authorization': 'Bearer {}'.format(token['id'])}
222
223 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
224
225 try:
226 r = requests.put(_url, json=project_data, verify=False, headers=headers)
227 except Exception as e:
228 log.exception(e)
229 result['data'] = str(e)
230 return result
231 if r.status_code == requests.codes.no_content:
232 result['error'] = False
233 result['data'] = Util.json_loads_byteified(r.text)
234 return result
235
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'])}
240
241 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
242 try:
243 r = requests.delete(_url, params=None, verify=False, headers=headers)
244 except Exception as e:
245 log.exception(e)
246 result['data'] = str(e)
247 return result
248 if r.status_code == requests.codes.no_content:
249 result['error'] = False
250 else:
251 result['data'] = Util.json_loads_byteified(r.text)
252 return result
253
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'])}
258 query_path = ''
259 if filter:
260 query_path = '?_admin.type='+filter
261 _url = "{0}/nsd/v1/ns_descriptors_content{1}".format(self._base_path, query_path)
262 try:
263 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
264 except Exception as e:
265 log.exception(e)
266 result['data'] = str(e)
267 return result
268 if r.status_code == requests.codes.ok:
269 result['error'] = False
270 result['data'] = Util.json_loads_byteified(r.text)
271
272 return result
273
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'])}
278 query_path = ''
279 if filter:
280 query_path = '?_admin.type='+filter
281 _url = "{0}/vnfpkgm/v1/vnf_packages_content{1}".format(self._base_path, query_path)
282 try:
283 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
284 except Exception as e:
285 log.exception(e)
286 result['data'] = str(e)
287 return result
288 if r.status_code == requests.codes.ok:
289 result['error'] = False
290 result['data'] = Util.json_loads_byteified(r.text)
291
292 return result
293
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)
299 try:
300 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
301 except Exception as e:
302 log.exception(e)
303 result['data'] = str(e)
304 return result
305 if r.status_code == requests.codes.ok:
306 result['error'] = False
307 result['data'] = Util.json_loads_byteified(r.text)
308
309 return result
310
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)
316 try:
317 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
318 except Exception as e:
319 log.exception(e)
320 result['data'] = str(e)
321 return result
322 if r.status_code == requests.codes.ok:
323 result['error'] = False
324 result['data'] = Util.json_loads_byteified(r.text)
325
326 return result
327
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)
333 try:
334 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
335 except Exception as e:
336 log.exception(e)
337 result['data'] = str(e)
338 return result
339 if r.status_code == requests.codes.ok:
340 result['error'] = False
341 result['data'] = Util.json_loads_byteified(r.text)
342
343 return result
344
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'])}
349
350 _url = "{0}/nsd/v1/ns_descriptors_content/{1}".format(self._base_path, id)
351 try:
352 r = requests.delete(_url, params=None, verify=False, headers=headers)
353 except Exception as e:
354 log.exception(e)
355 result['data'] = str(e)
356 return result
357 if r.status_code == requests.codes.ok:
358 result['error'] = False
359 result['data'] = Util.json_loads_byteified(r.text)
360 return result
361
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'])}
366
367 _url = "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self._base_path, id)
368 try:
369 r = requests.delete(_url, params=None, verify=False, headers=headers)
370 except Exception as e:
371 log.exception(e)
372 result['data'] = str(e)
373 return result
374 if r:
375 result['error'] = False
376 if r.status_code != requests.codes.no_content:
377 result['data'] = Util.json_loads_byteified(r.text)
378 return result
379
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)
389 try:
390 r = requests.post(_url, data=open('/tmp/' + package.name, 'rb'), verify=False, headers=headers)
391 except Exception as e:
392 log.exception(e)
393 result['data'] = str(e)
394 return result
395 if r.status_code == requests.codes.created:
396 result['error'] = False
397 result['data'] = Util.json_loads_byteified(r.text)
398 return result
399
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)
409 try:
410 r = requests.post(_url, data=open('/tmp/' + package.name, 'rb'), verify=False, headers=headers)
411 except Exception as e:
412 log.exception(e)
413 result['data'] = str(e)
414 return result
415 if r.status_code == requests.codes.created:
416 result['error'] = False
417 result['data'] = Util.json_loads_byteified(r.text)
418 return result
419
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'])}
424
425 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
426
427 try:
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:
432 log.exception(e)
433 result['data'] = str(e)
434 return result
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."
440 return result
441
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'])}
446
447 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
448
449 try:
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:
453 log.exception(e)
454 result['data'] = str(e)
455 return result
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."
461 return result
462
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'])}
467
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'))
473
474 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
475
476 try:
477 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
478 headers=headers)
479 except Exception as e:
480 log.exception(e)
481 result['data'] = str(e)
482 return result
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."
487
488 return result
489
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'])}
494
495 # get the package onboarded
496 tar_pkg = self.get_vnfd_pkg(token, id)
497 tarf = tarfile.open(fileobj=tar_pkg)
498
499 tarf = self._descriptor_clone(tarf, 'vnfd')
500 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
501
502 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
503
504 try:
505 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
506 headers=headers)
507 except Exception as e:
508 log.exception(e)
509 result['data'] = str(e)
510 return result
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."
515
516 return result
517
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'])}
522
523 # get the package onboarded
524 tar_pkg = self.get_nsd_pkg(token, id)
525 tarf = tarfile.open(fileobj=tar_pkg)
526
527 tarf = self._descriptor_update(tarf, data)
528 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
529
530 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
531
532 try:
533 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
534 headers=headers)
535 except Exception as e:
536 log.exception(e)
537 result['data'] = str(e)
538 return result
539 if r.status_code == requests.codes.no_content:
540 result['error'] = False
541 else:
542 try:
543 result['data'] = r.json()
544 except Exception as e:
545 result['data'] = {}
546
547 return result
548
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'])}
553
554 # get the package onboarded
555 tar_pkg = self.get_vnfd_pkg(token, id)
556 tarf = tarfile.open(fileobj=tar_pkg)
557
558 tarf = self._descriptor_update(tarf, data)
559 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
560
561 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
562
563 try:
564 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
565 headers=headers)
566 except Exception as e:
567 log.exception(e)
568 result['data'] = str(e)
569 return result
570 if r.status_code == requests.codes.no_content:
571 result['error'] = False
572 else:
573 try:
574 result['data'] = r.json()
575 except Exception as e:
576 result['data'] = {}
577
578 return result
579
580 def get_nsd_pkg(self, token, id):
581 result = {'error': True, 'data': ''}
582 headers = {"accept": "application/zip",
583 'Authorization': 'Bearer {}'.format(token['id'])}
584
585 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
586 try:
587 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
588 except Exception as e:
589 log.exception(e)
590 result['data'] = str(e)
591 return result
592 if r.status_code == requests.codes.ok:
593 result['error'] = False
594 tarf = StringIO.StringIO(r.content)
595 return tarf
596 return result
597
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)
603 try:
604 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
605 except Exception as e:
606 log.exception(e)
607 result['data'] = str(e)
608 return result
609 if r.status_code == requests.codes.ok:
610 result['error'] = False
611 tarf = StringIO.StringIO(r.content)
612 return tarf
613 return result
614
615 def _descriptor_update(self, tarf, data):
616 # extract the package on a tmp directory
617 tarf.extractall('/tmp')
618
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)
623 break
624
625 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + ".tar.gz", "w:gz")
626
627 for tarinfo in tarf:
628 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
629 tarf_temp.close()
630 return tarf
631
632 def _create_base_pkg(self, descriptor_type, pkg_name):
633 filename = '/tmp/'+pkg_name+'/' + pkg_name + '.yaml'
634 if descriptor_type == 'nsd':
635 descriptor = {
636 "nsd:nsd-catalog": {
637 "nsd": [
638 {
639 "short-name": str(pkg_name),
640 "vendor": "OSM Composer",
641 "description": str(pkg_name) + " descriptor",
642 "vld": [],
643 "constituent-vnfd": [],
644 "version": "1.0",
645 "id": str(pkg_name),
646 "name": str(pkg_name)
647 }
648 ]
649 }
650 }
651
652 elif descriptor_type == 'vnfd':
653 descriptor = {
654 "vnfd:vnfd-catalog": {
655 "vnfd": [
656 {
657 "short-name": str(pkg_name),
658 "vdu": [],
659 "description": "",
660 "mgmt-interface": {
661 "cp": ""
662 },
663 "id": str(pkg_name),
664 "version": "1.0",
665 "internal-vld": [],
666 "connection-point": [],
667 "name": str(pkg_name)
668 }
669 ]
670 }
671 }
672
673 if not os.path.exists(os.path.dirname(filename)):
674 try:
675 os.makedirs(os.path.dirname(filename))
676 except OSError as exc: # Guard against race condition
677 if exc.errno != errno.EEXIST:
678 raise
679
680 with open('/tmp/' + pkg_name + '/' + pkg_name + '.yaml', 'w') as yaml_file:
681 yaml_file.write(yaml.dump(descriptor, default_flow_style=False))
682
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)
685 tarf_temp.close()
686
687 def _descriptor_clone(self, tarf, descriptor_type):
688 # extract the package on a tmp directory
689 tarf.extractall('/tmp')
690
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)
695
696 if descriptor_type == 'nsd':
697 nsd_list = yaml_object['nsd:nsd-catalog']['nsd']
698 for nsd in nsd_list:
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']
708
709 with open('/tmp/' + name, 'w') as yaml_file:
710 yaml_file.write(yaml.dump(yaml_object, default_flow_style=False))
711 break
712
713 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", "w:gz")
714
715 for tarinfo in tarf:
716 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
717 tarf_temp.close()
718 return tarf
719
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)
725 try:
726 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
727 except Exception as e:
728 log.exception(e)
729 result['data'] = str(e)
730 return result
731 if r.status_code == requests.codes.ok:
732 result['error'] = False
733 return yaml.load(r.text)
734 else:
735 try:
736 result['data'] = r.json()
737 except Exception as e:
738 result['data'] = {}
739 return result
740
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)
746 try:
747 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
748 except Exception as e:
749 log.exception(e)
750 result['data'] = str(e)
751 return result
752 if r.status_code == requests.codes.ok:
753 result['error'] = False
754 return yaml.load(r.text)
755 else:
756 try:
757 result['data'] = r.json()
758 except Exception as e:
759 result['data'] = {}
760 return result
761
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)
767 try:
768 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
769 except Exception as e:
770 log.exception(e)
771 result['data'] = str(e)
772 return result
773 if r.status_code == requests.codes.ok:
774 result['error'] = False
775 result['data'] = r.text
776 else:
777 try:
778 result['data'] = r.json()
779 except Exception as e:
780 result['data'] = {}
781
782 return result
783
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)
789 try:
790 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
791 except Exception as e:
792 log.exception(e)
793 result['data'] = str(e)
794 return result
795 if r.status_code == requests.codes.ok:
796 result['error'] = False
797 result['data'] = r.text
798 else:
799 try:
800 result['data'] = r.json()
801 except Exception as e:
802 result['data'] = {}
803
804 return result
805
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'])}
810
811 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
812
813 try:
814 r = requests.post(_url, json=ns_data, verify=False, headers=headers)
815 except Exception as e:
816 log.exception(e)
817 result['data'] = str(e)
818 return result
819 if r.status_code == requests.codes.ok:
820 result['error'] = False
821 result['data'] = Util.json_loads_byteified(r.text)
822 return result
823
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'])}
828
829 _url = "{0}/pdu/v1/pdu_descriptors".format(self._base_path)
830
831 try:
832 r = requests.post(_url, json=pdu_data, verify=False, headers=headers)
833 except Exception as e:
834 log.exception(e)
835 result['data'] = str(e)
836 return result
837 if r.status_code == requests.codes.created:
838 result['error'] = False
839 result['data'] = Util.json_loads_byteified(r.text)
840 return result
841
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)
847
848 try:
849 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
850 except Exception as e:
851 log.exception(e)
852 result['data'] = str(e)
853 return result
854 if r.status_code == requests.codes.ok:
855 result['error'] = False
856 result['data'] = Util.json_loads_byteified(r.text)
857
858 return result
859
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)
865
866 try:
867 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
868 except Exception as e:
869 log.exception(e)
870 result['data'] = str(e)
871 return result
872 if r.status_code == requests.codes.ok:
873 result['error'] = False
874 result['data'] = Util.json_loads_byteified(r.text)
875
876 return result
877
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'])}
882
883 _url = "{0}/nslcm/v1/ns_instances/{1}/action".format(self._base_path, id)
884
885 try:
886 r = requests.post(_url, json=action_payload, verify=False, headers=headers)
887 except Exception as e:
888 log.exception(e)
889 result['data'] = str(e)
890 return result
891 if r.status_code == requests.codes.created:
892 result['error'] = False
893 result['data'] = Util.json_loads_byteified(r.text)
894 return result
895
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'])}
900 query_path = ''
901 if force:
902 query_path = '?FORCE=true'
903 _url = "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self._base_path, id, query_path)
904 try:
905 r = requests.delete(_url, params=None, verify=False, headers=headers)
906 except Exception as e:
907 log.exception(e)
908 result['data'] = str(e)
909 return result
910 if r:
911 result['error'] = False
912 if r.status_code != requests.codes.no_content:
913 result['data'] = Util.json_loads_byteified(r.text)
914 return result
915
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)
921 try:
922 r = requests.delete(_url, params=None, verify=False, headers=headers)
923 except Exception as e:
924 log.exception(e)
925 result['data'] = str(e)
926 return result
927 if r:
928 result['error'] = False
929 if r.status_code != requests.codes.no_content:
930 result['data'] = Util.json_loads_byteified(r.text)
931 return result
932
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)
938
939 try:
940 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
941 except Exception as e:
942 log.exception(e)
943 result['data'] = str(e)
944 return result
945 if r.status_code == requests.codes.ok:
946 result['error'] = False
947 result['data'] = Util.json_loads_byteified(r.text)
948 return result
949
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)
955
956 try:
957 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
958 except Exception as e:
959 log.exception(e)
960 result['data'] = str(e)
961 return result
962 if r.status_code == requests.codes.ok:
963 result['error'] = False
964 result['data'] = Util.json_loads_byteified(r.text)
965 return result
966
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)
972
973 try:
974 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
975 except Exception as e:
976 log.exception(e)
977 result['data'] = str(e)
978 return result
979 if r.status_code == requests.codes.ok:
980 result['error'] = False
981 result['data'] = Util.json_loads_byteified(r.text)
982 return result
983
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)
989 try:
990 r = requests.post(_url, json=alarm_payload, verify=False, headers=headers)
991 except Exception as e:
992 log.exception(e)
993 result['data'] = str(e)
994 return result
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
999 return result
1000
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)
1006 try:
1007 r = requests.post(_url, json=metric_payload, verify=False, headers=headers)
1008 except Exception as e:
1009 log.exception(e)
1010 result['data'] = str(e)
1011 return result
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
1016 return result
1017
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)
1023 try:
1024 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1025 except Exception as e:
1026 log.exception(e)
1027 result['data'] = str(e)
1028 return result
1029 if r.status_code == requests.codes.ok:
1030 result['error'] = False
1031 result['data'] = Util.json_loads_byteified(r.text)
1032
1033 return result
1034
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)
1040 try:
1041 r = requests.delete(_url, params=None, verify=False, headers=headers)
1042 except Exception as e:
1043 log.exception(e)
1044 result['data'] = str(e)
1045 return result
1046 if r.status_code == requests.codes.accepted:
1047 result['error'] = False
1048 else:
1049 result['data'] = r.text
1050 return result
1051
1052 def vim_get(self, token, id):
1053
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)
1058
1059 try:
1060 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1061 except Exception as e:
1062 log.exception(e)
1063 result['data'] = str(e)
1064 return result
1065 if r.status_code == requests.codes.ok:
1066 result['error'] = False
1067 result['data'] = Util.json_loads_byteified(r.text)
1068 return result
1069
1070 def vim_create(self, token, vim_data):
1071
1072 result = {'error': True, 'data': ''}
1073 headers = {"Content-Type": "application/json", "accept": "application/json",
1074 'Authorization': 'Bearer {}'.format(token['id'])}
1075
1076 _url = "{0}/admin/v1/vims".format(self._base_path)
1077
1078 try:
1079 r = requests.post(_url, json=vim_data, verify=False, headers=headers)
1080 except Exception as e:
1081 log.exception(e)
1082 result['data'] = str(e)
1083 return result
1084 if r.status_code == requests.codes.created:
1085 result['error'] = False
1086 result['data'] = Util.json_loads_byteified(r.text)
1087 return result
1088
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)
1094 try:
1095 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1096 except Exception as e:
1097 log.exception(e)
1098 result['data'] = str(e)
1099 return result
1100 if r.status_code == requests.codes.ok:
1101 result['error'] = False
1102 result['data'] = Util.json_loads_byteified(r.text)
1103 return result
1104
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)
1110 try:
1111 r = requests.delete(_url, params=None, verify=False, headers=headers)
1112 except Exception as e:
1113 log.exception(e)
1114 result['data'] = str(e)
1115 return result
1116 if r.status_code == requests.codes.accepted:
1117 result['error'] = False
1118 else:
1119 result['data'] = r.text
1120 return result
1121
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)
1127
1128 try:
1129 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1130 except Exception as e:
1131 log.exception(e)
1132 result['data'] = str(e)
1133 return result
1134 if r.status_code == requests.codes.ok:
1135 result['error'] = False
1136 result['data'] = Util.json_loads_byteified(r.text)
1137 return result
1138
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'])}
1143
1144 _url = "{0}/admin/v1/sdns".format(self._base_path)
1145
1146 try:
1147 r = requests.post(_url, json=sdn_data, verify=False, headers=headers)
1148 except Exception as e:
1149 log.exception(e)
1150 result['data'] = str(e)
1151 return result
1152 if r.status_code == requests.codes.created:
1153 result['error'] = False
1154 result['data'] = Util.json_loads_byteified(r.text)
1155 return result
1156
1157 @staticmethod
1158 def md5(f):
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()