cee0647fd800a866a14d6ee22564073562cd8713
[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 import re
26 from requests.packages.urllib3.exceptions import InsecureRequestWarning
27
28 requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
29
30 logging.basicConfig(level=logging.INFO)
31 log = logging.getLogger('helper.py')
32 logging.getLogger("urllib3").setLevel(logging.INFO)
33
34
35 class Client(object):
36 def __init__(self):
37 self._token_endpoint = 'admin/v1/tokens'
38 self._user_endpoint = 'admin/v1/users'
39 self._host = os.getenv('OSM_SERVER', "localhost")
40 self._so_port = 9999
41 self._base_path = 'https://{0}:{1}/osm'.format(self._host, self._so_port)
42
43 def auth(self, args):
44 result = {'error': True, 'data': ''}
45 token_url = "{0}/{1}".format(self._base_path, self._token_endpoint)
46 headers = {"Content-Type": "application/yaml", "accept": "application/json"}
47 try:
48 r = requests.post(token_url, json=args, verify=False, headers=headers)
49 except Exception as e:
50 log.exception(e)
51 result['data'] = str(e)
52 return result
53 if r.status_code in (200, 201, 202, 204):
54 result['error'] = False
55
56 result['data'] = Util.json_loads_byteified(r.text)
57
58 return result
59
60 def switch_project(self, args):
61 result = {'error': True, 'data': ''}
62 token_url = "{0}/{1}".format(self._base_path, self._token_endpoint)
63 headers = {"Content-Type": "application/yaml", "accept": "application/json"}
64 try:
65 r = requests.post(token_url, json=args, verify=False, headers=headers)
66 except Exception as e:
67 log.exception(e)
68 result['data'] = str(e)
69 return result
70 if r.status_code in (200, 201, 202, 204):
71 result['error'] = False
72
73 result['data'] = Util.json_loads_byteified(r.text)
74
75 return result
76
77 def role_list(self, token):
78 result = {'error': True, 'data': ''}
79 headers = {"Content-Type": "application/json", "accept": "application/json",
80 'Authorization': 'Bearer {}'.format(token['id'])}
81
82 _url = "{0}/admin/v1/roles".format(self._base_path)
83 try:
84 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
85 except Exception as e:
86 log.exception(e)
87 result['data'] = str(e)
88 return result
89 if r.status_code in (200, 201, 202, 204):
90 result['error'] = False
91 result['data'] = Util.json_loads_byteified(r.text)
92
93 return result
94
95 def role_create(self, token, role_data):
96 result = {'error': True, 'data': ''}
97 headers = {"Content-Type": "application/json", "accept": "application/json",
98 'Authorization': 'Bearer {}'.format(token['id'])}
99 _url = "{0}/admin/v1/roles".format(self._base_path)
100
101 try:
102 r = requests.post(_url, json=role_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 in (200, 201, 202, 204):
108 result['error'] = False
109 result['data'] = Util.json_loads_byteified(r.text)
110 return result
111
112 def role_update(self, token, role_id, role_data):
113 result = {'error': True, 'data': ''}
114 headers = {"Content-Type": "application/json", "accept": "application/json",
115 'Authorization': 'Bearer {}'.format(token['id'])}
116 _url = "{0}/admin/v1/roles/{1}".format(self._base_path, role_id)
117
118 try:
119 r = requests.patch(_url, json=role_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 in (200, 201, 202, 204):
125 result['error'] = False
126 else:
127 result['data'] = Util.json_loads_byteified(r.text)
128 return result
129
130 def role_delete(self, token, id, force=None):
131 result = {'error': True, 'data': ''}
132 headers = {"Content-Type": "application/json", "accept": "application/json",
133 'Authorization': 'Bearer {}'.format(token['id'])}
134 query_path = ''
135 if force:
136 query_path = '?FORCE=true'
137 _url = "{0}/admin/v1/roles/{1}{2}".format(self._base_path, id, query_path)
138 try:
139 r = requests.delete(_url, params=None, verify=False, headers=headers)
140 except Exception as e:
141 log.exception(e)
142 result['data'] = str(e)
143 return result
144 if r.status_code in (200, 201, 202, 204):
145 result['error'] = False
146 else:
147 result['data'] = Util.json_loads_byteified(r.text)
148 return result
149
150 def role_get(self, token, id):
151 result = {'error': True, 'data': ''}
152 headers = {"Content-Type": "application/json", "accept": "application/json",
153 'Authorization': 'Bearer {}'.format(token['id'])}
154
155 _url = "{0}/admin/v1/roles/{1}".format(self._base_path, id)
156 try:
157 r = requests.get(_url, params=None, verify=False, headers=headers)
158 except Exception as e:
159 log.exception(e)
160 result['data'] = str(e)
161 return result
162 if r.status_code in (200, 201, 202, 204):
163 result['error'] = False
164 result['data'] = Util.json_loads_byteified(r.text)
165 return result
166
167 def user_list(self, token):
168 result = {'error': True, 'data': ''}
169 headers = {"Content-Type": "application/json", "accept": "application/json",
170 'Authorization': 'Bearer {}'.format(token['id'])}
171
172 _url = "{0}/admin/v1/users".format(self._base_path)
173 try:
174 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
175 except Exception as e:
176 log.exception(e)
177 result['data'] = str(e)
178 return result
179 if r.status_code in (200, 201, 202, 204):
180 result['error'] = False
181 result['data'] = Util.json_loads_byteified(r.text)
182
183 return result
184
185 def user_create(self, token, user_data):
186 result = {'error': True, 'data': ''}
187 headers = {"Content-Type": "application/json", "accept": "application/json",
188 'Authorization': 'Bearer {}'.format(token['id'])}
189
190 _url = "{0}/admin/v1/users".format(self._base_path)
191
192 try:
193 r = requests.post(_url, json=user_data, verify=False, headers=headers)
194 except Exception as e:
195 log.exception(e)
196 result['data'] = str(e)
197 return result
198 if r.status_code in (200, 201, 202, 204):
199 result['error'] = False
200 result['data'] = Util.json_loads_byteified(r.text)
201 return result
202
203 def user_update(self, token, id, user_data):
204 result = {'error': True, 'data': ''}
205 headers = {"Content-Type": "application/json", "accept": "application/json",
206 'Authorization': 'Bearer {}'.format(token['id'])}
207
208 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
209 try:
210 r = requests.patch(_url, json=user_data, verify=False, headers=headers)
211 except Exception as e:
212 log.exception(e)
213 result['data'] = str(e)
214 return result
215 if r.status_code in (200, 201, 202, 204):
216 result['error'] = False
217 else:
218 result['data'] = Util.json_loads_byteified(r.text)
219 return result
220
221 def user_delete(self, token, id):
222 result = {'error': True, 'data': ''}
223 headers = {"Content-Type": "application/yaml", "accept": "application/json",
224 'Authorization': 'Bearer {}'.format(token['id'])}
225
226 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
227 try:
228 r = requests.delete(_url, params=None, verify=False, headers=headers)
229 except Exception as e:
230 log.exception(e)
231 result['data'] = str(e)
232 return result
233 if r.status_code in (200, 201, 202, 204):
234 result['error'] = False
235 else:
236 result['data'] = Util.json_loads_byteified(r.text)
237 return result
238
239 def get_user_info(self, token, id):
240 result = {'error': True, 'data': ''}
241 headers = {"Content-Type": "application/yaml", "accept": "application/json",
242 'Authorization': 'Bearer {}'.format(token['id'])}
243 _url = "{0}/admin/v1/users/{1}".format(self._base_path, id)
244 try:
245 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
246 except Exception as e:
247 log.exception(e)
248 result['data'] = str(e)
249 return result
250 if r.status_code in (200, 201, 202, 204):
251 result['error'] = False
252 result['data'] = Util.json_loads_byteified(r.text)
253 return result
254
255 def get_projects(self, token, uuids):
256 result = {'error': False, 'data': ''}
257 headers = {"Content-Type": "application/yaml", "accept": "application/json",
258 'Authorization': 'Bearer {}'.format(token['id'])}
259
260 projects = []
261 try:
262 for uuid in uuids:
263 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, uuid)
264 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
265 if r.status_code not in (200, 201, 202, 204):
266 raise Exception()
267 projects.append(Util.json_loads_byteified(r.text))
268 except Exception as e:
269 log.exception(e)
270 result['error'] = True
271 result['data'] = str(e)
272 return result
273 result['data'] = projects
274 return result
275
276 def project_list(self, token):
277 result = {'error': True, 'data': ''}
278 headers = {"Content-Type": "application/yaml", "accept": "application/json",
279 'Authorization': 'Bearer {}'.format(token['id'])}
280
281 _url = "{0}/admin/v1/projects".format(self._base_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 in (200, 201, 202, 204):
289 result['error'] = False
290 result['data'] = Util.json_loads_byteified(r.text)
291
292 return result
293
294 def project_get(self, token, id):
295 result = {'error': True, 'data': ''}
296 headers = {"Content-Type": "application/yaml", "accept": "application/json",
297 'Authorization': 'Bearer {}'.format(token['id'])}
298 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
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 in (200, 201, 202, 204):
306 result['error'] = False
307 result['data'] = Util.json_loads_byteified(r.text)
308 return result
309
310 def project_create(self, token, project_data):
311
312 result = {'error': True, 'data': ''}
313 headers = {"Content-Type": "application/json", "accept": "application/json",
314 'Authorization': 'Bearer {}'.format(token['id'])}
315
316 _url = "{0}/admin/v1/projects".format(self._base_path)
317
318 try:
319 r = requests.post(_url, json=project_data, verify=False, headers=headers)
320 except Exception as e:
321 log.exception(e)
322 result['data'] = str(e)
323 return result
324 if r.status_code in (200, 201, 202, 204):
325 result['error'] = False
326 result['data'] = Util.json_loads_byteified(r.text)
327 return result
328
329 def project_edit(self, token, id, project_data):
330
331 result = {'error': True, 'data': ''}
332 headers = {"Content-Type": "application/json", "accept": "application/json",
333 'Authorization': 'Bearer {}'.format(token['id'])}
334
335 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
336
337 try:
338 r = requests.put(_url, json=project_data, verify=False, headers=headers)
339 except Exception as e:
340 log.exception(e)
341 result['data'] = str(e)
342 return result
343 if r.status_code in (200, 201, 202, 204):
344 result['error'] = False
345 return result
346
347 def project_delete(self, token, id):
348 result = {'error': True, 'data': ''}
349 headers = {"Content-Type": "application/yaml", "accept": "application/json",
350 'Authorization': 'Bearer {}'.format(token['id'])}
351
352 _url = "{0}/admin/v1/projects/{1}".format(self._base_path, id)
353 try:
354 r = requests.delete(_url, params=None, verify=False, headers=headers)
355 except Exception as e:
356 log.exception(e)
357 result['data'] = str(e)
358 return result
359 if r.status_code in (200, 201, 202, 204):
360 result['error'] = False
361 else:
362 result['data'] = Util.json_loads_byteified(r.text)
363 return result
364
365 def nst_details(self, token, id):
366 result = {'error': True, 'data': ''}
367 headers = {"Content-Type": "application/json", "accept": "application/json",
368 'Authorization': 'Bearer {}'.format(token['id'])}
369 _url = "{0}/nst/v1/netslice_templates/{1}".format(self._base_path,id)
370 try:
371 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
372 except Exception as e:
373 log.exception(e)
374 result['data'] = str(e)
375 return result
376 if r.status_code in (200, 201, 202, 204):
377 result['error'] = False
378 result['data'] = Util.json_loads_byteified(r.text)
379
380 return result
381
382 def nst_content(self, token, id):
383 result = {'error': True, 'data': ''}
384 headers = {"Content-Type": "application/json", "accept": "text/plain",
385 'Authorization': 'Bearer {}'.format(token['id'])}
386 _url = "{0}/nst/v1/netslice_templates/{1}/nst".format(self._base_path,id)
387 try:
388 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
389 except Exception as e:
390 log.exception(e)
391 result['data'] = str(e)
392 return result
393 if r.status_code in (200, 201, 202, 204):
394 result['error'] = False
395 result['data'] = Util.json2yaml(yaml.load(str(r.text)))
396
397 return result
398
399 def nst_list(self, token):
400 result = {'error': True, 'data': ''}
401 headers = {"Content-Type": "application/yaml", "accept": "application/json",
402 'Authorization': 'Bearer {}'.format(token['id'])}
403
404 _url = "{0}/nst/v1/netslice_templates".format(self._base_path)
405 try:
406 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
407 except Exception as e:
408 log.exception(e)
409 result['data'] = str(e)
410 return result
411 if r.status_code in (200, 201, 202, 204):
412 result['error'] = False
413 result['data'] = Util.json_loads_byteified(r.text)
414
415 return result
416
417 def nsd_list(self, token, filter=None):
418 result = {'error': True, 'data': ''}
419 headers = {"Content-Type": "application/yaml", "accept": "application/json",
420 'Authorization': 'Bearer {}'.format(token['id'])}
421 query_path = ''
422 if filter:
423 query_path = '?_admin.type='+filter
424 _url = "{0}/nsd/v1/ns_descriptors_content{1}".format(self._base_path, query_path)
425 try:
426 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
427 except Exception as e:
428 log.exception(e)
429 result['data'] = str(e)
430 return result
431 if r.status_code in (200, 201, 202, 204):
432 result['error'] = False
433 result['data'] = Util.json_loads_byteified(r.text)
434
435 return result
436
437 def vnfd_list(self, token, filter=None):
438 result = {'error': True, 'data': ''}
439 headers = {"Content-Type": "application/yaml", "accept": "application/json",
440 'Authorization': 'Bearer {}'.format(token['id'])}
441 query_path = ''
442 if filter:
443 query_path = '?_admin.type='+filter
444 _url = "{0}/vnfpkgm/v1/vnf_packages_content{1}".format(self._base_path, query_path)
445 try:
446 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
447 except Exception as e:
448 log.exception(e)
449 result['data'] = str(e)
450 return result
451 if r.status_code in (200, 201, 202, 204):
452 result['error'] = False
453 result['data'] = Util.json_loads_byteified(r.text)
454
455 return result
456
457 def nsi_list(self, token):
458 result = {'error': True, 'data': ''}
459 headers = {"Content-Type": "application/yaml", "accept": "application/json",
460 'Authorization': 'Bearer {}'.format(token['id'])}
461 _url = "{0}/nsilcm/v1/netslice_instances".format(self._base_path)
462 try:
463 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
464 except Exception as e:
465 log.exception(e)
466 result['data'] = str(e)
467 return result
468 if r.status_code in (200, 201, 202, 204):
469 result['error'] = False
470 result['data'] = Util.json_loads_byteified(r.text)
471
472 return result
473
474 def ns_list(self, token):
475 result = {'error': True, 'data': ''}
476 headers = {"Content-Type": "application/yaml", "accept": "application/json",
477 'Authorization': 'Bearer {}'.format(token['id'])}
478 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
479 try:
480 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
481 except Exception as e:
482 log.exception(e)
483 result['data'] = str(e)
484 return result
485 if r.status_code in (200, 201, 202, 204):
486 result['error'] = False
487 result['data'] = Util.json_loads_byteified(r.text)
488
489 return result
490
491 def vnf_list(self, token):
492 result = {'error': True, 'data': ''}
493 headers = {"Content-Type": "application/yaml", "accept": "application/json",
494 'Authorization': 'Bearer {}'.format(token['id'])}
495 _url = "{0}/nslcm/v1/vnfrs".format(self._base_path)
496 try:
497 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
498 except Exception as e:
499 log.exception(e)
500 result['data'] = str(e)
501 return result
502 if r.status_code in (200, 201, 202, 204):
503 result['error'] = False
504 result['data'] = Util.json_loads_byteified(r.text)
505
506 return result
507
508 def pdu_list(self, token):
509 result = {'error': True, 'data': ''}
510 headers = {"Content-Type": "application/yaml", "accept": "application/json",
511 'Authorization': 'Bearer {}'.format(token['id'])}
512 _url = "{0}/pdu/v1/pdu_descriptors".format(self._base_path)
513 try:
514 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
515 except Exception as e:
516 log.exception(e)
517 result['data'] = str(e)
518 return result
519 if r.status_code in (200, 201, 202, 204):
520 result['error'] = False
521 result['data'] = Util.json_loads_byteified(r.text)
522
523 return result
524
525 def nst_delete(self, token, id):
526 result = {'error': True, 'data': ''}
527 headers = {"Content-Type": "application/yaml", "accept": "application/json",
528 'Authorization': 'Bearer {}'.format(token['id'])}
529
530 _url = "{0}/nst/v1/netslice_templates/{1}?FORCE=True".format(self._base_path, id)
531 try:
532 r = requests.delete(_url, params=None, verify=False, headers=headers)
533 except Exception as e:
534 log.exception(e)
535 result['data'] = str(e)
536 return result
537 if r.status_code in (200, 201, 202, 204):
538 result['error'] = False
539
540 return result
541
542 def nsd_delete(self, token, id):
543 result = {'error': True, 'data': ''}
544 headers = {"Content-Type": "application/yaml", "accept": "application/json",
545 'Authorization': 'Bearer {}'.format(token['id'])}
546
547 _url = "{0}/nsd/v1/ns_descriptors_content/{1}".format(self._base_path, id)
548 try:
549 r = requests.delete(_url, params=None, verify=False, headers=headers)
550 except Exception as e:
551 log.exception(e)
552 result['data'] = str(e)
553 return result
554 if r:
555 result['error'] = False
556 if r.status_code != requests.codes.no_content:
557 result['data'] = Util.json_loads_byteified(r.text)
558 return result
559
560 def vnfd_delete(self, token, id):
561 result = {'error': True, 'data': ''}
562 headers = {"Content-Type": "application/yaml", "accept": "application/json",
563 'Authorization': 'Bearer {}'.format(token['id'])}
564
565 _url = "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self._base_path, id)
566 try:
567 r = requests.delete(_url, params=None, verify=False, headers=headers)
568 except Exception as e:
569 log.exception(e)
570 result['data'] = str(e)
571 return result
572 if r:
573 result['error'] = False
574 if r.status_code != requests.codes.no_content:
575 result['data'] = Util.json_loads_byteified(r.text)
576 return result
577
578 def nst_onboard(self, token, template):
579 result = {'error': True, 'data': ''}
580 headers = {"Content-Type": "application/gzip", "accept": "application/json",
581 'Authorization': 'Bearer {}'.format(token['id'])}
582 _url = "{0}/nst/v1/netslice_templates_content".format(self._base_path)
583 try:
584 fileName, fileExtension = os.path.splitext(template.name)
585 if fileExtension == '.gz':
586 headers["Content-Type"] = "application/gzip"
587 else:
588 headers["Content-Type"] = "application/yaml"
589 r = requests.post(_url, data=template, verify=False, headers=headers)
590 except Exception as e:
591 log.exception(e)
592 result['data'] = str(e)
593 return result
594 if r.status_code in (200, 201, 202, 204):
595 result['error'] = False
596 result['data'] = Util.json_loads_byteified(r.text)
597 return result
598
599 def nsd_onboard(self, token, package):
600 result = {'error': True, 'data': ''}
601 headers = {"Content-Type": "application/gzip", "accept": "application/json",
602 'Authorization': 'Bearer {}'.format(token['id'])}
603 with open('/tmp/' + package.name, 'wb+') as destination:
604 for chunk in package.chunks():
605 destination.write(chunk)
606 headers['Content-File-MD5'] = self.md5(open('/tmp/' + package.name, 'rb'))
607 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
608 try:
609 r = requests.post(_url, data=open('/tmp/' + package.name, 'rb'), verify=False, headers=headers)
610 except Exception as e:
611 log.exception(e)
612 result['data'] = str(e)
613 return result
614 if r.status_code in (200, 201, 202, 204):
615 result['error'] = False
616 result['data'] = Util.json_loads_byteified(r.text)
617 return result
618
619 def vnfd_onboard(self, token, package):
620 result = {'error': True, 'data': ''}
621 headers = {"Content-Type": "application/gzip", "accept": "application/json",
622 'Authorization': 'Bearer {}'.format(token['id'])}
623 with open('/tmp/' + package.name, 'wb+') as destination:
624 for chunk in package.chunks():
625 destination.write(chunk)
626 headers['Content-File-MD5'] = self.md5(open('/tmp/' + package.name, 'rb'))
627 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
628 try:
629 r = requests.post(_url, data=open('/tmp/' + package.name, 'rb'), verify=False, headers=headers)
630 except Exception as e:
631 log.exception(e)
632 result['data'] = str(e)
633 return result
634 if r.status_code in (200, 201, 202, 204):
635 result['error'] = False
636 result['data'] = Util.json_loads_byteified(r.text)
637 return result
638
639 def nsd_create_pkg_base(self, token, pkg_name):
640 result = {'error': True, 'data': ''}
641 headers = {"Content-Type": "application/gzip", "accept": "application/json",
642 'Authorization': 'Bearer {}'.format(token['id'])}
643
644 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
645
646 try:
647 self._create_base_pkg('nsd', pkg_name)
648 headers['Content-Filename'] = pkg_name + '.tar.gz'
649 r = requests.post(_url, data=open('/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
650 except Exception as e:
651 log.exception(e)
652 result['data'] = str(e)
653 return result
654 if r.status_code in (200, 201, 202, 204):
655 result['data'] = r.json()
656 result['error'] = False
657 if r.status_code == requests.codes.conflict:
658 result['data'] = "Invalid ID."
659 return result
660
661 def vnfd_create_pkg_base(self, token, pkg_name):
662 result = {'error': True, 'data': ''}
663 headers = {"Content-Type": "application/gzip", "accept": "application/json",
664 'Authorization': 'Bearer {}'.format(token['id'])}
665
666 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
667
668 try:
669 self._create_base_pkg('vnfd', pkg_name)
670 r = requests.post(_url, data=open('/tmp/' + pkg_name + '.tar.gz', 'rb'), verify=False, headers=headers)
671 except Exception as e:
672 log.exception(e)
673 result['data'] = str(e)
674 return result
675 if r.status_code in (200, 201, 202, 204):
676 result['data'] = r.json()
677 result['error'] = False
678 if r.status_code == requests.codes.conflict:
679 result['data'] = "Invalid ID."
680 return result
681
682 def nsd_clone(self, token, id):
683 result = {'error': True, 'data': ''}
684 headers = {"Content-Type": "application/gzip", "accept": "application/json",
685 'Authorization': 'Bearer {}'.format(token['id'])}
686
687 # get the package onboarded
688 tar_pkg = self.get_nsd_pkg(token, id)
689 tarf = tarfile.open(fileobj=tar_pkg)
690 tarf = self._descriptor_clone(tarf, 'nsd')
691 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
692
693 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
694
695 try:
696 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
697 headers=headers)
698 except Exception as e:
699 log.exception(e)
700 result['data'] = str(e)
701 return result
702 if r.status_code in (200, 201, 202, 204):
703 result['error'] = False
704 if r.status_code == requests.codes.conflict:
705 result['data'] = "Invalid ID."
706
707 return result
708
709 def vnfd_clone(self, token, id):
710 result = {'error': True, 'data': ''}
711 headers = {"Content-Type": "application/gzip", "accept": "application/json",
712 'Authorization': 'Bearer {}'.format(token['id'])}
713
714 # get the package onboarded
715 tar_pkg = self.get_vnfd_pkg(token, id)
716 tarf = tarfile.open(fileobj=tar_pkg)
717
718 tarf = self._descriptor_clone(tarf, 'vnfd')
719 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'))
720
721 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
722
723 try:
724 r = requests.post(_url, data=open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", 'rb'), verify=False,
725 headers=headers)
726 except Exception as e:
727 log.exception(e)
728 result['data'] = str(e)
729 return result
730 if r.status_code in (200, 201, 202, 204):
731 result['error'] = False
732 if r.status_code == requests.codes.conflict:
733 result['data'] = "Invalid ID."
734
735 return result
736
737 def nst_content_update(self, token, id, template):
738 result = {'error': True, 'data': ''}
739 headers = {"Content-Type": "application/yaml", "accept": "application/json",
740 'Authorization': 'Bearer {}'.format(token['id'])}
741 _url = "{0}/nst/v1/netslice_templates/{1}/nst_content".format(self._base_path,id)
742 try:
743 r = requests.put(_url, data=template, verify=False, headers=headers)
744 except Exception as e:
745 log.exception(e)
746 result['data'] = str(e)
747 return result
748 if r.status_code in (200, 201, 202, 204):
749 result['error'] = False
750 return result
751
752 def nsd_update(self, token, id, data):
753 result = {'error': True, 'data': ''}
754 headers = {"Content-Type": "application/gzip", "accept": "application/json",
755 'Authorization': 'Bearer {}'.format(token['id'])}
756
757 # get the package onboarded
758 tar_pkg = self.get_nsd_pkg(token, id)
759 tarf = tarfile.open(fileobj=tar_pkg)
760
761 tarf = self._descriptor_update(tarf, data)
762 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
763
764 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
765
766 try:
767 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
768 headers=headers)
769 except Exception as e:
770 log.exception(e)
771 result['data'] = str(e)
772 return result
773 if r.status_code in (200, 201, 202, 204):
774 result['error'] = False
775 else:
776 try:
777 result['data'] = r.json()
778 except Exception as e:
779 result['data'] = {}
780
781 return result
782
783 def vnfd_update(self, token, id, data):
784 result = {'error': True, 'data': ''}
785 headers = {"Content-Type": "application/gzip", "accept": "application/json",
786 'Authorization': 'Bearer {}'.format(token['id'])}
787
788 # get the package onboarded
789 tar_pkg = self.get_vnfd_pkg(token, id)
790 tarf = tarfile.open(fileobj=tar_pkg)
791
792 tarf = self._descriptor_update(tarf, data)
793 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
794
795 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
796
797 try:
798 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
799 headers=headers)
800 except Exception as e:
801 log.exception(e)
802 result['data'] = str(e)
803 return result
804 if r.status_code in (200, 201, 202, 204):
805 result['error'] = False
806 else:
807 try:
808 result['data'] = r.json()
809 except Exception as e:
810 result['data'] = {}
811
812 return result
813
814 def get_nsd_pkg(self, token, id):
815 result = {'error': True, 'data': ''}
816 headers = {"accept": "application/zip",
817 'Authorization': 'Bearer {}'.format(token['id'])}
818
819 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
820 try:
821 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
822 except Exception as e:
823 log.exception(e)
824 result['data'] = str(e)
825 return result
826 if r.status_code in (200, 201, 202, 204):
827 result['error'] = False
828 tarf = StringIO.StringIO(r.content)
829 return tarf
830 return result
831
832 def get_vnfd_pkg(self, token, id):
833 result = {'error': True, 'data': ''}
834 headers = {"accept": "application/zip",
835 'Authorization': 'Bearer {}'.format(token['id'])}
836 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
837 try:
838 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
839 except Exception as e:
840 log.exception(e)
841 result['data'] = str(e)
842 return result
843 if r.status_code in (200, 201, 202, 204):
844 result['error'] = False
845 tarf = StringIO.StringIO(r.content)
846 return tarf
847 return result
848
849 def _descriptor_update(self, tarf, data):
850 # extract the package on a tmp directory
851 tarf.extractall('/tmp')
852 regex = re.compile(r"^[^/]+(/[^/]+\.(yaml|yml))$", re.U)
853 for name in tarf.getnames():
854 if regex.match(name):
855 with open('/tmp/' + name, 'w') as outfile:
856 yaml.safe_dump(data, outfile, default_flow_style=False)
857 break
858
859 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + ".tar.gz", "w:gz")
860
861 for tarinfo in tarf:
862 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
863 tarf_temp.close()
864 return tarf
865
866 def _create_base_pkg(self, descriptor_type, pkg_name):
867 filename = '/tmp/'+pkg_name+'/' + pkg_name + '.yaml'
868 if descriptor_type == 'nsd':
869 descriptor = {
870 "nsd:nsd-catalog": {
871 "nsd": [
872 {
873 "short-name": str(pkg_name),
874 "vendor": "OSM Composer",
875 "description": str(pkg_name) + " descriptor",
876 "vld": [],
877 "constituent-vnfd": [],
878 "version": "1.0",
879 "id": str(pkg_name),
880 "name": str(pkg_name)
881 }
882 ]
883 }
884 }
885
886 elif descriptor_type == 'vnfd':
887 descriptor = {
888 "vnfd:vnfd-catalog": {
889 "vnfd": [
890 {
891 "short-name": str(pkg_name),
892 "vdu": [],
893 "description": "",
894 "mgmt-interface": {
895 "cp": ""
896 },
897 "id": str(pkg_name),
898 "version": "1.0",
899 "internal-vld": [],
900 "connection-point": [],
901 "name": str(pkg_name)
902 }
903 ]
904 }
905 }
906
907 if not os.path.exists(os.path.dirname(filename)):
908 try:
909 os.makedirs(os.path.dirname(filename))
910 except OSError as exc: # Guard against race condition
911 if exc.errno != errno.EEXIST:
912 raise
913
914 with open('/tmp/' + pkg_name + '/' + pkg_name + '.yaml', 'w') as yaml_file:
915 yaml_file.write(yaml.dump(descriptor, default_flow_style=False))
916
917 tarf_temp = tarfile.open('/tmp/' + pkg_name + '.tar.gz', "w:gz")
918 tarf_temp.add('/tmp/'+pkg_name+'/' + pkg_name + '.yaml', pkg_name + '/' + pkg_name + '.yaml', recursive=False)
919 tarf_temp.close()
920
921 def _descriptor_clone(self, tarf, descriptor_type):
922 # extract the package on a tmp directory
923 tarf.extractall('/tmp')
924
925 for name in tarf.getnames():
926 if name.endswith(".yaml") or name.endswith(".yml"):
927 with open('/tmp/' + name, 'r') as outfile:
928 yaml_object = yaml.load(outfile)
929
930 if descriptor_type == 'nsd':
931 nsd_list = yaml_object['nsd:nsd-catalog']['nsd']
932 for nsd in nsd_list:
933 nsd['id'] = 'clone_' + nsd['id']
934 nsd['name'] = 'clone_' + nsd['name']
935 nsd['short-name'] = 'clone_' + nsd['short-name']
936 elif descriptor_type == 'vnfd':
937 vnfd_list = yaml_object['vnfd:vnfd-catalog']['vnfd']
938 for vnfd in vnfd_list:
939 vnfd['id'] = 'clone_' + vnfd['id']
940 vnfd['name'] = 'clone_' + vnfd['name']
941 vnfd['short-name'] = 'clone_' + vnfd['short-name']
942
943 with open('/tmp/' + name, 'w') as yaml_file:
944 yaml_file.write(yaml.dump(yaml_object, default_flow_style=False))
945 break
946
947 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + "_clone.tar.gz", "w:gz")
948
949 for tarinfo in tarf:
950 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
951 tarf_temp.close()
952 return tarf
953
954 def nsd_get(self, token, id):
955 result = {'error': True, 'data': ''}
956 headers = {'Content-Type': 'application/yaml',
957 'Authorization': 'Bearer {}'.format(token['id'])}
958 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self._base_path, id)
959 try:
960 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
961 except Exception as e:
962 log.exception(e)
963 result['data'] = str(e)
964 return result
965 if r.status_code in (200, 201, 202, 204):
966 result['error'] = False
967 return yaml.load(r.text)
968 else:
969 try:
970 result['data'] = r.json()
971 except Exception as e:
972 result['data'] = {}
973 return result
974
975 def vnfd_get(self, token, id):
976 result = {'error': True, 'data': ''}
977 headers = {'Content-Type': 'application/yaml',
978 'Authorization': 'Bearer {}'.format(token['id'])}
979 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self._base_path, id)
980 try:
981 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
982 except Exception as e:
983 log.exception(e)
984 result['data'] = str(e)
985 return result
986 if r.status_code in (200, 201, 202, 204):
987 result['error'] = False
988 return yaml.load(r.text)
989 else:
990 try:
991 result['data'] = r.json()
992 except Exception as e:
993 result['data'] = {}
994 return result
995
996 def nsd_artifacts(self, token, id):
997 result = {'error': True, 'data': ''}
998 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
999 'Authorization': 'Bearer {}'.format(token['id'])}
1000 _url = "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self._base_path, id)
1001 try:
1002 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1003 except Exception as e:
1004 log.exception(e)
1005 result['data'] = str(e)
1006 return result
1007 if r.status_code in (200, 201, 202, 204):
1008 result['error'] = False
1009 result['data'] = r.text
1010 else:
1011 try:
1012 result['data'] = r.json()
1013 except Exception as e:
1014 result['data'] = {}
1015
1016 return result
1017
1018 def vnf_packages_artifacts(self, token, id):
1019 result = {'error': True, 'data': ''}
1020 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
1021 'Authorization': 'Bearer {}'.format(token['id'])}
1022 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self._base_path, id)
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 in (200, 201, 202, 204):
1030 result['error'] = False
1031 result['data'] = r.text
1032 else:
1033 try:
1034 result['data'] = r.json()
1035 except Exception as e:
1036 result['data'] = {}
1037
1038 return result
1039
1040 def nsi_create(self, token, nsi_data):
1041 result = {'error': True, 'data': ''}
1042 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1043 'Authorization': 'Bearer {}'.format(token['id'])}
1044
1045 _url = "{0}/nsilcm/v1/netslice_instances_content".format(self._base_path)
1046
1047 try:
1048 r = requests.post(_url, json=nsi_data, verify=False, headers=headers)
1049 except Exception as e:
1050 log.exception(e)
1051 result['data'] = str(e)
1052 return result
1053 if r.status_code in (200, 201, 202, 204):
1054 result['error'] = False
1055 result['data'] = Util.json_loads_byteified(r.text)
1056 return result
1057
1058 def ns_create(self, token, ns_data):
1059 result = {'error': True, 'data': ''}
1060 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1061 'Authorization': 'Bearer {}'.format(token['id'])}
1062
1063 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
1064
1065 try:
1066 r = requests.post(_url, json=ns_data, verify=False, headers=headers)
1067 except Exception as e:
1068 log.exception(e)
1069 result['data'] = str(e)
1070 return result
1071 if r.status_code in (200, 201, 202, 204):
1072 result['error'] = False
1073 result['data'] = Util.json_loads_byteified(r.text)
1074 return result
1075
1076 def pdu_create(self, token, pdu_data):
1077 result = {'error': True, 'data': ''}
1078 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1079 'Authorization': 'Bearer {}'.format(token['id'])}
1080
1081 _url = "{0}/pdu/v1/pdu_descriptors".format(self._base_path)
1082
1083 try:
1084 r = requests.post(_url, json=pdu_data, verify=False, headers=headers)
1085 except Exception as e:
1086 log.exception(e)
1087 result['data'] = str(e)
1088 return result
1089 if r.status_code in (200, 201, 202, 204):
1090 result['error'] = False
1091 result['data'] = Util.json_loads_byteified(r.text)
1092 return result
1093
1094 def ns_op_list(self, token, id):
1095 result = {'error': True, 'data': ''}
1096 headers = {"Content-Type": "application/json", "accept": "application/json",
1097 'Authorization': 'Bearer {}'.format(token['id'])}
1098 _url = "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self._base_path, id)
1099
1100 try:
1101 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1102 except Exception as e:
1103 log.exception(e)
1104 result['data'] = str(e)
1105 return result
1106 if r.status_code in (200, 201, 202, 204):
1107 result['error'] = False
1108 result['data'] = Util.json_loads_byteified(r.text)
1109
1110 return result
1111
1112 def nsi_op_list(self, token, id):
1113 result = {'error': True, 'data': ''}
1114 headers = {"Content-Type": "application/json", "accept": "application/json",
1115 'Authorization': 'Bearer {}'.format(token['id'])}
1116 _url = "{0}/nsilcm/v1/nsi_lcm_op_occs/?netsliceInstanceId={1}".format(self._base_path, id)
1117
1118 try:
1119 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1120 except Exception as e:
1121 log.exception(e)
1122 result['data'] = str(e)
1123 return result
1124 if r.status_code in (200, 201, 202, 204):
1125 result['error'] = False
1126 result['data'] = Util.json_loads_byteified(r.text)
1127
1128 return result
1129
1130 def ns_op(self, token, id):
1131 result = {'error': True, 'data': ''}
1132 headers = {"Content-Type": "application/json", "accept": "application/json",
1133 'Authorization': 'Bearer {}'.format(token['id'])}
1134 _url = "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self._base_path, id)
1135
1136 try:
1137 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1138 except Exception as e:
1139 log.exception(e)
1140 result['data'] = str(e)
1141 return result
1142 if r.status_code in (200, 201, 202, 204):
1143 result['error'] = False
1144 result['data'] = Util.json_loads_byteified(r.text)
1145
1146 return result
1147
1148 def ns_action(self, token, id, action_payload):
1149 result = {'error': True, 'data': ''}
1150 headers = {"Content-Type": "application/json", "accept": "application/json",
1151 'Authorization': 'Bearer {}'.format(token['id'])}
1152
1153 _url = "{0}/nslcm/v1/ns_instances/{1}/action".format(self._base_path, id)
1154
1155 try:
1156 r = requests.post(_url, json=action_payload, verify=False, headers=headers)
1157 except Exception as e:
1158 log.exception(e)
1159 result['data'] = str(e)
1160 return result
1161 if r.status_code in (200, 201, 202, 204):
1162 result['error'] = False
1163 result['data'] = Util.json_loads_byteified(r.text)
1164 return result
1165
1166 def nsi_delete(self, token, id, force=None):
1167 result = {'error': True, 'data': ''}
1168 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1169 'Authorization': 'Bearer {}'.format(token['id'])}
1170 query_path = ''
1171 if force:
1172 query_path = '?FORCE=true'
1173 _url = "{0}/nsilcm/v1/netslice_instances_content/{1}{2}".format(self._base_path, id, query_path)
1174 try:
1175 r = requests.delete(_url, params=None, verify=False, headers=headers)
1176 except Exception as e:
1177 log.exception(e)
1178 result['data'] = str(e)
1179 return result
1180 if r:
1181 result['error'] = False
1182 if r.status_code != requests.codes.no_content:
1183 result['data'] = Util.json_loads_byteified(r.text)
1184 return result
1185
1186 def ns_delete(self, token, id, force=None):
1187 result = {'error': True, 'data': ''}
1188 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1189 'Authorization': 'Bearer {}'.format(token['id'])}
1190 query_path = ''
1191 if force:
1192 query_path = '?FORCE=true'
1193 _url = "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self._base_path, id, query_path)
1194 try:
1195 r = requests.delete(_url, params=None, verify=False, headers=headers)
1196 except Exception as e:
1197 log.exception(e)
1198 result['data'] = str(e)
1199 return result
1200 if r:
1201 result['error'] = False
1202 if r.status_code != requests.codes.no_content:
1203 result['data'] = Util.json_loads_byteified(r.text)
1204 return result
1205
1206 def pdu_delete(self, token, id):
1207 result = {'error': True, 'data': ''}
1208 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1209 'Authorization': 'Bearer {}'.format(token['id'])}
1210 _url = "{0}/pdu/v1/pdu_descriptors/{1}".format(self._base_path, id)
1211 try:
1212 r = requests.delete(_url, params=None, verify=False, headers=headers)
1213 except Exception as e:
1214 log.exception(e)
1215 result['data'] = str(e)
1216 return result
1217 if r:
1218 result['error'] = False
1219 if r.status_code != requests.codes.no_content:
1220 result['data'] = Util.json_loads_byteified(r.text)
1221 return result
1222
1223 def nsi_get(self, token, id):
1224 result = {'error': True, 'data': ''}
1225 headers = {"Content-Type": "application/json", "accept": "application/json",
1226 'Authorization': 'Bearer {}'.format(token['id'])}
1227 _url = "{0}/nsilcm/v1/netslice_instances/{1}".format(self._base_path, id)
1228
1229 try:
1230 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1231 except Exception as e:
1232 log.exception(e)
1233 result['data'] = str(e)
1234 return result
1235 if r.status_code in (200, 201, 202, 204):
1236 result['error'] = False
1237 result['data'] = Util.json_loads_byteified(r.text)
1238 return result
1239
1240 def ns_get(self, token, id):
1241 result = {'error': True, 'data': ''}
1242 headers = {"Content-Type": "application/json", "accept": "application/json",
1243 'Authorization': 'Bearer {}'.format(token['id'])}
1244 _url = "{0}/nslcm/v1/ns_instances_content/{1}".format(self._base_path, id)
1245
1246 try:
1247 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1248 except Exception as e:
1249 log.exception(e)
1250 result['data'] = str(e)
1251 return result
1252 if r.status_code in (200, 201, 202, 204):
1253 result['error'] = False
1254 result['data'] = Util.json_loads_byteified(r.text)
1255 return result
1256
1257 def vnf_get(self, token, id):
1258 result = {'error': True, 'data': ''}
1259 headers = {"Content-Type": "application/json", "accept": "application/json",
1260 'Authorization': 'Bearer {}'.format(token['id'])}
1261 _url = "{0}/nslcm/v1/vnfrs/{1}".format(self._base_path, id)
1262
1263 try:
1264 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1265 except Exception as e:
1266 log.exception(e)
1267 result['data'] = str(e)
1268 return result
1269 if r.status_code in (200, 201, 202, 204):
1270 result['error'] = False
1271 result['data'] = Util.json_loads_byteified(r.text)
1272 return result
1273
1274 def pdu_get(self, token, id):
1275 result = {'error': True, 'data': ''}
1276 headers = {"Content-Type": "application/json", "accept": "application/json",
1277 'Authorization': 'Bearer {}'.format(token['id'])}
1278 _url = "{0}/pdu/v1/pdu_descriptors/{1}".format(self._base_path, id)
1279
1280 try:
1281 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1282 except Exception as e:
1283 log.exception(e)
1284 result['data'] = str(e)
1285 return result
1286 if r.status_code in (200, 201, 202, 204):
1287 result['error'] = False
1288 result['data'] = Util.json_loads_byteified(r.text)
1289 return result
1290
1291 def ns_alarm_create(self, token, id, alarm_payload):
1292 result = {'error': True, 'data': ''}
1293 headers = {"Content-Type": "application/json",
1294 'Authorization': 'Bearer {}'.format(token['id'])}
1295 _url = "{0}/test/message/alarm_request".format(self._base_path)
1296 try:
1297 r = requests.post(_url, json=alarm_payload, verify=False, headers=headers)
1298 except Exception as e:
1299 log.exception(e)
1300 result['data'] = str(e)
1301 return result
1302 if r.status_code in (200, 201, 202, 204):
1303 result['error'] = False
1304 # result['data'] = Util.json_loads_byteified(r.text)
1305 result['data'] = r.text
1306 return result
1307
1308 def ns_metric_export(self, token, id, metric_payload):
1309 result = {'error': True, 'data': ''}
1310 headers = {"Content-Type": "application/json",
1311 'Authorization': 'Bearer {}'.format(token['id'])}
1312 _url = "{0}/test/message/metric_request".format(self._base_path)
1313 try:
1314 r = requests.post(_url, json=metric_payload, verify=False, headers=headers)
1315 except Exception as e:
1316 log.exception(e)
1317 result['data'] = str(e)
1318 return result
1319 if r.status_code in (200, 201, 202, 204):
1320 result['error'] = False
1321 # result['data'] = Util.json_loads_byteified(r.text)
1322 result['data'] = r.text
1323 return result
1324
1325 def wim_list(self, token):
1326 result = {'error': True, 'data': ''}
1327 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1328 'Authorization': 'Bearer {}'.format(token['id'])}
1329 _url = "{0}/admin/v1/wim_accounts".format(self._base_path)
1330 try:
1331 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1332 except Exception as e:
1333 log.exception(e)
1334 result['data'] = str(e)
1335 return result
1336 if r.status_code in (200, 201, 202, 204):
1337 result['error'] = False
1338 result['data'] = Util.json_loads_byteified(r.text)
1339
1340 return result
1341
1342 def vim_list(self, token):
1343 result = {'error': True, 'data': ''}
1344 headers = {"Content-Type": "application/yaml", "accept": "application/json",
1345 'Authorization': 'Bearer {}'.format(token['id'])}
1346 _url = "{0}/admin/v1/vims".format(self._base_path)
1347 try:
1348 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1349 except Exception as e:
1350 log.exception(e)
1351 result['data'] = str(e)
1352 return result
1353 if r.status_code in (200, 201, 202, 204):
1354 result['error'] = False
1355 result['data'] = Util.json_loads_byteified(r.text)
1356
1357 return result
1358
1359 def wim_delete(self, token, id):
1360 result = {'error': True, 'data': ''}
1361 headers = {"accept": "application/json",
1362 'Authorization': 'Bearer {}'.format(token['id'])}
1363 _url = "{0}/admin/v1/wim_accounts/{1}".format(self._base_path, id)
1364 try:
1365 r = requests.delete(_url, params=None, verify=False, headers=headers)
1366 except Exception as e:
1367 log.exception(e)
1368 result['data'] = str(e)
1369 return result
1370 if r.status_code in (200, 201, 202, 204):
1371 result['error'] = False
1372 else:
1373 result['data'] = r.text
1374 return result
1375
1376 def vim_delete(self, token, id):
1377 result = {'error': True, 'data': ''}
1378 headers = {"accept": "application/json",
1379 'Authorization': 'Bearer {}'.format(token['id'])}
1380 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
1381 try:
1382 r = requests.delete(_url, params=None, verify=False, headers=headers)
1383 except Exception as e:
1384 log.exception(e)
1385 result['data'] = str(e)
1386 return result
1387 if r.status_code in (200, 201, 202, 204):
1388 result['error'] = False
1389 else:
1390 result['data'] = r.text
1391 return result
1392
1393 def wim_get(self, token, id):
1394
1395 result = {'error': True, 'data': ''}
1396 headers = {"Content-Type": "application/json", "accept": "application/json",
1397 'Authorization': 'Bearer {}'.format(token['id'])}
1398 _url = "{0}/admin/v1/wim_accounts/{1}".format(self._base_path, id)
1399
1400 try:
1401 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1402 except Exception as e:
1403 log.exception(e)
1404 result['data'] = str(e)
1405 return result
1406 if r.status_code in (200, 201, 202, 204):
1407 result['error'] = False
1408 result['data'] = Util.json_loads_byteified(r.text)
1409 return result
1410
1411 def vim_get(self, token, id):
1412
1413 result = {'error': True, 'data': ''}
1414 headers = {"Content-Type": "application/json", "accept": "application/json",
1415 'Authorization': 'Bearer {}'.format(token['id'])}
1416 _url = "{0}/admin/v1/vims/{1}".format(self._base_path, id)
1417
1418 try:
1419 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1420 except Exception as e:
1421 log.exception(e)
1422 result['data'] = str(e)
1423 return result
1424 if r.status_code in (200, 201, 202, 204):
1425 result['error'] = False
1426 result['data'] = Util.json_loads_byteified(r.text)
1427 return result
1428
1429 def wim_create(self, token, wim_data):
1430 result = {'error': True, 'data': ''}
1431 headers = {"Content-Type": "application/json", "accept": "application/json",
1432 'Authorization': 'Bearer {}'.format(token['id'])}
1433
1434 _url = "{0}/admin/v1/wim_accounts".format(self._base_path)
1435
1436 try:
1437 r = requests.post(_url, json=wim_data, verify=False, headers=headers)
1438 except Exception as e:
1439 log.exception(e)
1440 result['data'] = str(e)
1441 return result
1442 if r.status_code in (200, 201, 202, 204):
1443 result['error'] = False
1444 result['data'] = Util.json_loads_byteified(r.text)
1445 return result
1446
1447 def vim_create(self, token, vim_data):
1448
1449 result = {'error': True, 'data': ''}
1450 headers = {"Content-Type": "application/json", "accept": "application/json",
1451 'Authorization': 'Bearer {}'.format(token['id'])}
1452
1453 _url = "{0}/admin/v1/vims".format(self._base_path)
1454
1455 try:
1456 r = requests.post(_url, json=vim_data, verify=False, headers=headers)
1457 except Exception as e:
1458 log.exception(e)
1459 result['data'] = str(e)
1460 return result
1461 if r.status_code in (200, 201, 202, 204):
1462 result['error'] = False
1463 result['data'] = Util.json_loads_byteified(r.text)
1464 return result
1465
1466 def sdn_list(self, token):
1467 result = {'error': True, 'data': ''}
1468 headers = {"accept": "application/json",
1469 'Authorization': 'Bearer {}'.format(token['id'])}
1470 _url = "{0}/admin/v1/sdns".format(self._base_path)
1471 try:
1472 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1473 except Exception as e:
1474 log.exception(e)
1475 result['data'] = str(e)
1476 return result
1477 if r.status_code in (200, 201, 202, 204):
1478 result['error'] = False
1479 result['data'] = Util.json_loads_byteified(r.text)
1480 return result
1481
1482 def sdn_delete(self, token, id):
1483 result = {'error': True, 'data': ''}
1484 headers = {"accept": "application/json",
1485 'Authorization': 'Bearer {}'.format(token['id'])}
1486 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
1487 try:
1488 r = requests.delete(_url, params=None, verify=False, headers=headers)
1489 except Exception as e:
1490 log.exception(e)
1491 result['data'] = str(e)
1492 return result
1493 if r.status_code in (200, 201, 202, 204):
1494 result['error'] = False
1495 else:
1496 result['data'] = r.text
1497 return result
1498
1499 def sdn_get(self, token, id):
1500 result = {'error': True, 'data': ''}
1501 headers = {"accept": "application/json",
1502 'Authorization': 'Bearer {}'.format(token['id'])}
1503 _url = "{0}/admin/v1/sdns/{1}".format(self._base_path, id)
1504
1505 try:
1506 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
1507 except Exception as e:
1508 log.exception(e)
1509 result['data'] = str(e)
1510 return result
1511 if r.status_code in (200, 201, 202, 204):
1512 result['error'] = False
1513 result['data'] = Util.json_loads_byteified(r.text)
1514 return result
1515
1516 def sdn_create(self, token, sdn_data):
1517 result = {'error': True, 'data': ''}
1518 headers = {"Content-Type": "application/json", "accept": "application/json",
1519 'Authorization': 'Bearer {}'.format(token['id'])}
1520
1521 _url = "{0}/admin/v1/sdns".format(self._base_path)
1522
1523 try:
1524 r = requests.post(_url, json=sdn_data, verify=False, headers=headers)
1525 except Exception as e:
1526 log.exception(e)
1527 result['data'] = str(e)
1528 return result
1529 if r.status_code in (200, 201, 202, 204):
1530 result['error'] = False
1531 result['data'] = Util.json_loads_byteified(r.text)
1532 return result
1533
1534 @staticmethod
1535 def md5(f):
1536 hash_md5 = hashlib.md5()
1537 for chunk in iter(lambda: f.read(1024), b""):
1538 hash_md5.update(chunk)
1539 return hash_md5.hexdigest()