Network Slice Templates
[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 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)
259 try:
260 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
261 except Exception as e:
262 log.exception(e)
263 result['data'] = str(e)
264 return result
265 if r.status_code == requests.codes.ok:
266 result['error'] = False
267 result['data'] = Util.json_loads_byteified(r.text)
268
269 return result
270
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)
276 try:
277 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
278 except Exception as e:
279 log.exception(e)
280 result['data'] = str(e)
281 return result
282 if r.status_code == requests.codes.ok:
283 result['error'] = False
284 result['data'] = Util.json2yaml(yaml.load(str(r.text)))
285
286 return result
287
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'])}
292
293 _url = "{0}/nst/v1/netslice_templates".format(self._base_path)
294 try:
295 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
296 except Exception as e:
297 log.exception(e)
298 result['data'] = str(e)
299 return result
300 if r.status_code == requests.codes.ok:
301 result['error'] = False
302 result['data'] = Util.json_loads_byteified(r.text)
303
304 return result
305
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'])}
310 query_path = ''
311 if filter:
312 query_path = '?_admin.type='+filter
313 _url = "{0}/nsd/v1/ns_descriptors_content{1}".format(self._base_path, query_path)
314 try:
315 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
316 except Exception as e:
317 log.exception(e)
318 result['data'] = str(e)
319 return result
320 if r.status_code == requests.codes.ok:
321 result['error'] = False
322 result['data'] = Util.json_loads_byteified(r.text)
323
324 return result
325
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'])}
330 query_path = ''
331 if filter:
332 query_path = '?_admin.type='+filter
333 _url = "{0}/vnfpkgm/v1/vnf_packages_content{1}".format(self._base_path, query_path)
334 try:
335 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
336 except Exception as e:
337 log.exception(e)
338 result['data'] = str(e)
339 return result
340 if r.status_code == requests.codes.ok:
341 result['error'] = False
342 result['data'] = Util.json_loads_byteified(r.text)
343
344 return result
345
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)
351 try:
352 r = requests.get(_url, params=None, verify=False, stream=True, 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
361 return result
362
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)
368 try:
369 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
370 except Exception as e:
371 log.exception(e)
372 result['data'] = str(e)
373 return result
374 if r.status_code == requests.codes.ok:
375 result['error'] = False
376 result['data'] = Util.json_loads_byteified(r.text)
377
378 return result
379
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)
385 try:
386 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
387 except Exception as e:
388 log.exception(e)
389 result['data'] = str(e)
390 return result
391 if r.status_code == requests.codes.ok:
392 result['error'] = False
393 result['data'] = Util.json_loads_byteified(r.text)
394
395 return result
396
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'])}
401
402 _url = "{0}/nst/v1/netslice_templates/{1}?FORCE=True".format(self._base_path, id)
403 try:
404 r = requests.delete(_url, params=None, verify=False, headers=headers)
405 except Exception as e:
406 log.exception(e)
407 result['data'] = str(e)
408 return result
409 if r.status_code == requests.codes.no_content:
410 result['error'] = False
411
412 return result
413
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'])}
418
419 _url = "{0}/nsd/v1/ns_descriptors_content/{1}".format(self._base_path, id)
420 try:
421 r = requests.delete(_url, params=None, verify=False, headers=headers)
422 except Exception as e:
423 log.exception(e)
424 result['data'] = str(e)
425 return result
426 if r.status_code == requests.codes.ok:
427 result['error'] = False
428 result['data'] = Util.json_loads_byteified(r.text)
429 return result
430
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'])}
435
436 _url = "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self._base_path, id)
437 try:
438 r = requests.delete(_url, params=None, verify=False, headers=headers)
439 except Exception as e:
440 log.exception(e)
441 result['data'] = str(e)
442 return result
443 if r:
444 result['error'] = False
445 if r.status_code != requests.codes.no_content:
446 result['data'] = Util.json_loads_byteified(r.text)
447 return result
448
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)
454 try:
455 fileName, fileExtension = os.path.splitext(template.name)
456 if fileExtension == '.gz':
457 headers["Content-Type"] = "application/gzip"
458 else:
459 headers["Content-Type"] = "application/yaml"
460 r = requests.post(_url, data=template, verify=False, headers=headers)
461 except Exception as e:
462 log.exception(e)
463 result['data'] = str(e)
464 return result
465 if r.status_code == requests.codes.created:
466 result['error'] = False
467 result['data'] = Util.json_loads_byteified(r.text)
468 return result
469
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)
479 try:
480 r = requests.post(_url, data=open('/tmp/' + package.name, 'rb'), verify=False, headers=headers)
481 except Exception as e:
482 log.exception(e)
483 result['data'] = str(e)
484 return result
485 if r.status_code == requests.codes.created:
486 result['error'] = False
487 result['data'] = Util.json_loads_byteified(r.text)
488 return result
489
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)
499 try:
500 r = requests.post(_url, data=open('/tmp/' + package.name, 'rb'), verify=False, headers=headers)
501 except Exception as e:
502 log.exception(e)
503 result['data'] = str(e)
504 return result
505 if r.status_code == requests.codes.created:
506 result['error'] = False
507 result['data'] = Util.json_loads_byteified(r.text)
508 return result
509
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'])}
514
515 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
516
517 try:
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:
522 log.exception(e)
523 result['data'] = str(e)
524 return result
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."
530 return result
531
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'])}
536
537 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
538
539 try:
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:
543 log.exception(e)
544 result['data'] = str(e)
545 return result
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."
551 return result
552
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'])}
557
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'))
563
564 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
565
566 try:
567 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
568 headers=headers)
569 except Exception as e:
570 log.exception(e)
571 result['data'] = str(e)
572 return result
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."
577
578 return result
579
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'])}
584
585 # get the package onboarded
586 tar_pkg = self.get_vnfd_pkg(token, id)
587 tarf = tarfile.open(fileobj=tar_pkg)
588
589 tarf = self._descriptor_clone(tarf, 'vnfd')
590 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
591
592 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
593
594 try:
595 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
596 headers=headers)
597 except Exception as e:
598 log.exception(e)
599 result['data'] = str(e)
600 return result
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."
605
606 return result
607
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)
613 try:
614 r = requests.put(_url, data=template, verify=False, headers=headers)
615 except Exception as e:
616 log.exception(e)
617 result['data'] = str(e)
618 return result
619 if r.status_code == requests.codes.no_content:
620 result['error'] = False
621 return result
622
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'])}
627
628 # get the package onboarded
629 tar_pkg = self.get_nsd_pkg(token, id)
630 tarf = tarfile.open(fileobj=tar_pkg)
631
632 tarf = self._descriptor_update(tarf, data)
633 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
634
635 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
636
637 try:
638 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
639 headers=headers)
640 except Exception as e:
641 log.exception(e)
642 result['data'] = str(e)
643 return result
644 if r.status_code == requests.codes.no_content:
645 result['error'] = False
646 else:
647 try:
648 result['data'] = r.json()
649 except Exception as e:
650 result['data'] = {}
651
652 return result
653
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'])}
658
659 # get the package onboarded
660 tar_pkg = self.get_vnfd_pkg(token, id)
661 tarf = tarfile.open(fileobj=tar_pkg)
662
663 tarf = self._descriptor_update(tarf, data)
664 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
665
666 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
667
668 try:
669 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
670 headers=headers)
671 except Exception as e:
672 log.exception(e)
673 result['data'] = str(e)
674 return result
675 if r.status_code == requests.codes.no_content:
676 result['error'] = False
677 else:
678 try:
679 result['data'] = r.json()
680 except Exception as e:
681 result['data'] = {}
682
683 return result
684
685 def get_nsd_pkg(self, token, id):
686 result = {'error': True, 'data': ''}
687 headers = {"accept": "application/zip",
688 'Authorization': 'Bearer {}'.format(token['id'])}
689
690 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
691 try:
692 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
693 except Exception as e:
694 log.exception(e)
695 result['data'] = str(e)
696 return result
697 if r.status_code == requests.codes.ok:
698 result['error'] = False
699 tarf = StringIO.StringIO(r.content)
700 return tarf
701 return result
702
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)
708 try:
709 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
710 except Exception as e:
711 log.exception(e)
712 result['data'] = str(e)
713 return result
714 if r.status_code == requests.codes.ok:
715 result['error'] = False
716 tarf = StringIO.StringIO(r.content)
717 return tarf
718 return result
719
720 def _descriptor_update(self, tarf, data):
721 # extract the package on a tmp directory
722 tarf.extractall('/tmp')
723
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)
728 break
729
730 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + ".tar.gz", "w:gz")
731
732 for tarinfo in tarf:
733 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
734 tarf_temp.close()
735 return tarf
736
737 def _create_base_pkg(self, descriptor_type, pkg_name):
738 filename = '/tmp/'+pkg_name+'/' + pkg_name + '.yaml'
739 if descriptor_type == 'nsd':
740 descriptor = {
741 "nsd:nsd-catalog": {
742 "nsd": [
743 {
744 "short-name": str(pkg_name),
745 "vendor": "OSM Composer",
746 "description": str(pkg_name) + " descriptor",
747 "vld": [],
748 "constituent-vnfd": [],
749 "version": "1.0",
750 "id": str(pkg_name),
751 "name": str(pkg_name)
752 }
753 ]
754 }
755 }
756
757 elif descriptor_type == 'vnfd':
758 descriptor = {
759 "vnfd:vnfd-catalog": {
760 "vnfd": [
761 {
762 "short-name": str(pkg_name),
763 "vdu": [],
764 "description": "",
765 "mgmt-interface": {
766 "cp": ""
767 },
768 "id": str(pkg_name),
769 "version": "1.0",
770 "internal-vld": [],
771 "connection-point": [],
772 "name": str(pkg_name)
773 }
774 ]
775 }
776 }
777
778 if not os.path.exists(os.path.dirname(filename)):
779 try:
780 os.makedirs(os.path.dirname(filename))
781 except OSError as exc: # Guard against race condition
782 if exc.errno != errno.EEXIST:
783 raise
784
785 with open('/tmp/' + pkg_name + '/' + pkg_name + '.yaml', 'w') as yaml_file:
786 yaml_file.write(yaml.dump(descriptor, default_flow_style=False))
787
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)
790 tarf_temp.close()
791
792 def _descriptor_clone(self, tarf, descriptor_type):
793 # extract the package on a tmp directory
794 tarf.extractall('/tmp')
795
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)
800
801 if descriptor_type == 'nsd':
802 nsd_list = yaml_object['nsd:nsd-catalog']['nsd']
803 for nsd in nsd_list:
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']
813
814 with open('/tmp/' + name, 'w') as yaml_file:
815 yaml_file.write(yaml.dump(yaml_object, default_flow_style=False))
816 break
817
818 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", "w:gz")
819
820 for tarinfo in tarf:
821 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
822 tarf_temp.close()
823 return tarf
824
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)
830 try:
831 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
832 except Exception as e:
833 log.exception(e)
834 result['data'] = str(e)
835 return result
836 if r.status_code == requests.codes.ok:
837 result['error'] = False
838 return yaml.load(r.text)
839 else:
840 try:
841 result['data'] = r.json()
842 except Exception as e:
843 result['data'] = {}
844 return result
845
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)
851 try:
852 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
853 except Exception as e:
854 log.exception(e)
855 result['data'] = str(e)
856 return result
857 if r.status_code == requests.codes.ok:
858 result['error'] = False
859 return yaml.load(r.text)
860 else:
861 try:
862 result['data'] = r.json()
863 except Exception as e:
864 result['data'] = {}
865 return result
866
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)
872 try:
873 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
874 except Exception as e:
875 log.exception(e)
876 result['data'] = str(e)
877 return result
878 if r.status_code == requests.codes.ok:
879 result['error'] = False
880 result['data'] = r.text
881 else:
882 try:
883 result['data'] = r.json()
884 except Exception as e:
885 result['data'] = {}
886
887 return result
888
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)
894 try:
895 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
896 except Exception as e:
897 log.exception(e)
898 result['data'] = str(e)
899 return result
900 if r.status_code == requests.codes.ok:
901 result['error'] = False
902 result['data'] = r.text
903 else:
904 try:
905 result['data'] = r.json()
906 except Exception as e:
907 result['data'] = {}
908
909 return result
910
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'])}
915
916 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
917
918 try:
919 r = requests.post(_url, json=ns_data, verify=False, headers=headers)
920 except Exception as e:
921 log.exception(e)
922 result['data'] = str(e)
923 return result
924 if r.status_code == requests.codes.ok:
925 result['error'] = False
926 result['data'] = Util.json_loads_byteified(r.text)
927 return result
928
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'])}
933
934 _url = "{0}/pdu/v1/pdu_descriptors".format(self._base_path)
935
936 try:
937 r = requests.post(_url, json=pdu_data, verify=False, headers=headers)
938 except Exception as e:
939 log.exception(e)
940 result['data'] = str(e)
941 return result
942 if r.status_code == requests.codes.created:
943 result['error'] = False
944 result['data'] = Util.json_loads_byteified(r.text)
945 return result
946
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)
952
953 try:
954 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
955 except Exception as e:
956 log.exception(e)
957 result['data'] = str(e)
958 return result
959 if r.status_code == requests.codes.ok:
960 result['error'] = False
961 result['data'] = Util.json_loads_byteified(r.text)
962
963 return result
964
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)
970
971 try:
972 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
973 except Exception as e:
974 log.exception(e)
975 result['data'] = str(e)
976 return result
977 if r.status_code == requests.codes.ok:
978 result['error'] = False
979 result['data'] = Util.json_loads_byteified(r.text)
980
981 return result
982
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'])}
987
988 _url = "{0}/nslcm/v1/ns_instances/{1}/action".format(self._base_path, id)
989
990 try:
991 r = requests.post(_url, json=action_payload, verify=False, headers=headers)
992 except Exception as e:
993 log.exception(e)
994 result['data'] = str(e)
995 return result
996 if r.status_code == requests.codes.created:
997 result['error'] = False
998 result['data'] = Util.json_loads_byteified(r.text)
999 return result
1000
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'])}
1005 query_path = ''
1006 if force:
1007 query_path = '?FORCE=true'
1008 _url = "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self._base_path, id, query_path)
1009 try:
1010 r = requests.delete(_url, params=None, verify=False, headers=headers)
1011 except Exception as e:
1012 log.exception(e)
1013 result['data'] = str(e)
1014 return result
1015 if r:
1016 result['error'] = False
1017 if r.status_code != requests.codes.no_content:
1018 result['data'] = Util.json_loads_byteified(r.text)
1019 return result
1020
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)
1026 try:
1027 r = requests.delete(_url, params=None, verify=False, headers=headers)
1028 except Exception as e:
1029 log.exception(e)
1030 result['data'] = str(e)
1031 return result
1032 if r:
1033 result['error'] = False
1034 if r.status_code != requests.codes.no_content:
1035 result['data'] = Util.json_loads_byteified(r.text)
1036 return result
1037
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)
1043
1044 try:
1045 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1046 except Exception as e:
1047 log.exception(e)
1048 result['data'] = str(e)
1049 return result
1050 if r.status_code == requests.codes.ok:
1051 result['error'] = False
1052 result['data'] = Util.json_loads_byteified(r.text)
1053 return result
1054
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)
1060
1061 try:
1062 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1063 except Exception as e:
1064 log.exception(e)
1065 result['data'] = str(e)
1066 return result
1067 if r.status_code == requests.codes.ok:
1068 result['error'] = False
1069 result['data'] = Util.json_loads_byteified(r.text)
1070 return result
1071
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)
1077
1078 try:
1079 r = requests.get(_url, params=None, verify=False, stream=True, 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.ok:
1085 result['error'] = False
1086 result['data'] = Util.json_loads_byteified(r.text)
1087 return result
1088
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)
1094 try:
1095 r = requests.post(_url, json=alarm_payload, verify=False, 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 result['data'] = r.text
1104 return result
1105
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)
1111 try:
1112 r = requests.post(_url, json=metric_payload, verify=False, headers=headers)
1113 except Exception as e:
1114 log.exception(e)
1115 result['data'] = str(e)
1116 return result
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
1121 return result
1122
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)
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
1138 return result
1139
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)
1145 try:
1146 r = requests.delete(_url, params=None, verify=False, headers=headers)
1147 except Exception as e:
1148 log.exception(e)
1149 result['data'] = str(e)
1150 return result
1151 if r.status_code == requests.codes.accepted:
1152 result['error'] = False
1153 else:
1154 result['data'] = r.text
1155 return result
1156
1157 def vim_get(self, token, id):
1158
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)
1163
1164 try:
1165 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1166 except Exception as e:
1167 log.exception(e)
1168 result['data'] = str(e)
1169 return result
1170 if r.status_code == requests.codes.ok:
1171 result['error'] = False
1172 result['data'] = Util.json_loads_byteified(r.text)
1173 return result
1174
1175 def vim_create(self, token, vim_data):
1176
1177 result = {'error': True, 'data': ''}
1178 headers = {"Content-Type": "application/json", "accept": "application/json",
1179 'Authorization': 'Bearer {}'.format(token['id'])}
1180
1181 _url = "{0}/admin/v1/vims".format(self._base_path)
1182
1183 try:
1184 r = requests.post(_url, json=vim_data, verify=False, headers=headers)
1185 except Exception as e:
1186 log.exception(e)
1187 result['data'] = str(e)
1188 return result
1189 if r.status_code == requests.codes.created:
1190 result['error'] = False
1191 result['data'] = Util.json_loads_byteified(r.text)
1192 return result
1193
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)
1199 try:
1200 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1201 except Exception as e:
1202 log.exception(e)
1203 result['data'] = str(e)
1204 return result
1205 if r.status_code == requests.codes.ok:
1206 result['error'] = False
1207 result['data'] = Util.json_loads_byteified(r.text)
1208 return result
1209
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)
1215 try:
1216 r = requests.delete(_url, params=None, verify=False, headers=headers)
1217 except Exception as e:
1218 log.exception(e)
1219 result['data'] = str(e)
1220 return result
1221 if r.status_code == requests.codes.accepted:
1222 result['error'] = False
1223 else:
1224 result['data'] = r.text
1225 return result
1226
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)
1232
1233 try:
1234 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1235 except Exception as e:
1236 log.exception(e)
1237 result['data'] = str(e)
1238 return result
1239 if r.status_code == requests.codes.ok:
1240 result['error'] = False
1241 result['data'] = Util.json_loads_byteified(r.text)
1242 return result
1243
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'])}
1248
1249 _url = "{0}/admin/v1/sdns".format(self._base_path)
1250
1251 try:
1252 r = requests.post(_url, json=sdn_data, verify=False, headers=headers)
1253 except Exception as e:
1254 log.exception(e)
1255 result['data'] = str(e)
1256 return result
1257 if r.status_code == requests.codes.created:
1258 result['error'] = False
1259 result['data'] = Util.json_loads_byteified(r.text)
1260 return result
1261
1262 @staticmethod
1263 def md5(f):
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()