working on client v2: projects and instances
[osm/LW-UI.git] / lib / osm / osmclient / clientv2.py
1 import requests
2 import logging
3 import json
4 import tarfile
5 import yaml
6 import pyaml
7 import StringIO
8 from lib.util import Util
9 import hashlib
10 import os
11
12 logging.basicConfig(level=logging.DEBUG)
13 log = logging.getLogger('helper.py')
14
15
16 class Client(object):
17 def __init__(self):
18 self._token_endpoint = 'admin/v1/tokens'
19 self._user_endpoint = 'admin/v1/users'
20 self._host = os.getenv('OSM_SERVER', "localhost")
21 self._so_port = 9999
22 self._base_path = "https://{0}:{1}/osm".format(self._host, self._so_port)
23
24 def auth(self, args):
25 result = {'error': True, 'data': ''}
26 token_url = "{0}/{1}".format(self._base_path, self._token_endpoint)
27 headers = {"Content-Type": "application/yaml", "accept": "application/json"}
28 try:
29 r = requests.post(token_url, json=args, verify=False, headers=headers)
30 except Exception as e:
31 print "saltata"
32 log.exception(e)
33 result['data'] = str(e)
34 return result
35 if r.status_code == requests.codes.ok:
36 result['error'] = False
37
38 result['data'] = Util.json_loads_byteified(r.text)
39
40 return result
41
42 def nsd_list(self, token):
43 result = {'error': True, 'data': ''}
44 headers = {"Content-Type": "application/yaml", "accept": "application/json",
45 'Authorization': 'Bearer {}'.format(token['id'])}
46
47 _url = "{0}/nsd/v1/ns_descriptors_content".format(self._base_path)
48 try:
49 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
50 except Exception as e:
51 log.exception(e)
52 result['data'] = str(e)
53 return result
54 if r.status_code == requests.codes.ok:
55 result['error'] = False
56 result['data'] = Util.json_loads_byteified(r.text)
57
58 return result
59
60 def vnfd_list(self, token):
61 result = {'error': True, 'data': ''}
62 headers = {"Content-Type": "application/yaml", "accept": "application/json",
63 'Authorization': 'Bearer {}'.format(token['id'])}
64
65 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
66 try:
67 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
68 except Exception as e:
69 log.exception(e)
70 result['data'] = str(e)
71 return result
72 if r.status_code == requests.codes.ok:
73 result['error'] = False
74 result['data'] = Util.json_loads_byteified(r.text)
75
76 return result
77
78 def ns_list(self, token):
79 result = {'error': True, 'data': ''}
80 headers = {"Content-Type": "application/yaml", "accept": "application/json",
81 'Authorization': 'Bearer {}'.format(token['id'])}
82 _url = "{0}/nslcm/v1/ns_instances_content".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 == requests.codes.ok:
90 result['error'] = False
91 result['data'] = Util.json_loads_byteified(r.text)
92
93 return result
94
95 def vnf_list(self, token):
96 result = {'error': True, 'data': ''}
97 headers = {"Content-Type": "application/yaml", "accept": "application/json",
98 'Authorization': 'Bearer {}'.format(token['id'])}
99 _url = "{0}/nslcm/v1/vnfrs".format(self._base_path)
100 try:
101 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
102 except Exception as e:
103 log.exception(e)
104 result['data'] = str(e)
105 return result
106 if r.status_code == requests.codes.ok:
107 result['error'] = False
108 result['data'] = Util.json_loads_byteified(r.text)
109
110 return result
111
112 def nsd_delete(self, token, id):
113 result = {'error': True, 'data': ''}
114 headers = {"Content-Type": "application/yaml", "accept": "application/json",
115 'Authorization': 'Bearer {}'.format(token['id'])}
116
117 _url = "{0}/nsd/v1/ns_descriptors_content/{1}".format(self._base_path, id)
118 try:
119 r = requests.delete(_url, params=None, 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.ok:
125 result['error'] = False
126 result['data'] = Util.json_loads_byteified(r.text)
127 return result
128
129 def vnfd_delete(self, token, id):
130 result = {'error': True, 'data': ''}
131 headers = {"Content-Type": "application/yaml", "accept": "application/json",
132 'Authorization': 'Bearer {}'.format(token['id'])}
133
134 _url = "{0}/vnfpkgm/v1/vnf_packages_content/{1}".format(self._base_path, id)
135 try:
136 r = requests.delete(_url, params=None, verify=False, headers=headers)
137 except Exception as e:
138 log.exception(e)
139 result['data'] = str(e)
140 return result
141 if r.status_code == requests.codes.ok:
142 result['error'] = False
143 result['data'] = Util.json_loads_byteified(r.text)
144 return result
145
146 def nsd_onboard(self, token, package):
147 result = {'error': True, 'data': ''}
148 headers = {"Content-Type": "application/gzip", "accept": "application/json",
149 'Authorization': 'Bearer {}'.format(token['id'])}
150 with open('/tmp/'+package.name, 'wb+') as destination:
151 for chunk in package.chunks():
152 destination.write(chunk)
153 headers['Content-File-MD5'] = self.md5(open('/tmp/'+package.name, 'rb'))
154 _url = "{0}/nsd/v1/ns_descriptors_content/".format(self._base_path)
155 try:
156 r = requests.post(_url, data=open('/tmp/'+package.name, 'rb'), verify=False, headers=headers)
157 except Exception as e:
158 log.exception(e)
159 result['data'] = str(e)
160 return result
161 if r.status_code == requests.codes.created:
162 result['error'] = False
163 result['data'] = Util.json_loads_byteified(r.text)
164 return result
165
166 def vnfd_onboard(self, token, package):
167 result = {'error': True, 'data': ''}
168 headers = {"Content-Type": "application/gzip", "accept": "application/json",
169 'Authorization': 'Bearer {}'.format(token['id'])}
170 with open('/tmp/'+package.name, 'wb+') as destination:
171 for chunk in package.chunks():
172 destination.write(chunk)
173 headers['Content-File-MD5'] = self.md5(open('/tmp/'+package.name, 'rb'))
174 _url = "{0}/vnfpkgm/v1/vnf_packages_content".format(self._base_path)
175 try:
176 r = requests.post(_url, data=open('/tmp/'+package.name, 'rb'), verify=False, headers=headers)
177 except Exception as e:
178 log.exception(e)
179 result['data'] = str(e)
180 return result
181 if r.status_code == requests.codes.created:
182 result['error'] = False
183 result['data'] = Util.json_loads_byteified(r.text)
184 return result
185
186 def nsd_update(self, token, id, data):
187 result = {'error': True, 'data': ''}
188 headers = {"Content-Type": "application/gzip", "accept": "application/json",
189 'Authorization': 'Bearer {}'.format(token['id'])}
190
191 # get the package onboarded
192 tar_pkg = self.get_nsd_pkg(token, id)
193 tarf = tarfile.open(fileobj=tar_pkg)
194
195 tarf = self._descriptor_update(tarf, data)
196 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
197
198 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".format(self._base_path, id)
199
200 try:
201 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
202 headers=headers)
203 except Exception as e:
204 log.exception(e)
205 result['data'] = str(e)
206 return result
207 if r.status_code == requests.codes.no_content:
208 result['error'] = False
209
210 return result
211
212 def vnfd_update(self, token, id, data):
213 result = {'error': True, 'data': ''}
214 headers = {"Content-Type": "application/gzip", "accept": "application/json",
215 'Authorization': 'Bearer {}'.format(token['id'])}
216
217 # get the package onboarded
218 tar_pkg = self.get_vnfd_pkg(token, id)
219 tarf = tarfile.open(fileobj=tar_pkg)
220
221 tarf = self._descriptor_update(tarf, data)
222 headers['Content-File-MD5'] = self.md5(open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'))
223
224 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
225
226 try:
227 r = requests.put(_url, data=open('/tmp/' + tarf.getnames()[0] + ".tar.gz", 'rb'), verify=False,
228 headers=headers)
229 except Exception as e:
230 log.exception(e)
231 result['data'] = str(e)
232 return result
233 if r.status_code == requests.codes.no_content:
234 result['error'] = False
235
236 return result
237
238 def get_nsd_pkg(self, token, id):
239 result = {'error': True, 'data': ''}
240 headers = { "accept": "application/zip",
241 'Authorization': 'Bearer {}'.format(token['id'])}
242
243 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd_content".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 == requests.codes.ok:
251 result['error'] = False
252 tarf = StringIO.StringIO(r.content)
253 return tarf
254 return result
255
256 def get_vnfd_pkg(self, token, id):
257 result = {'error': True, 'data': ''}
258 headers = {"accept": "application/zip",
259 'Authorization': 'Bearer {}'.format(token['id'])}
260 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/package_content".format(self._base_path, id)
261 try:
262 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
263 print r.status_code
264 except Exception as e:
265 log.exception(e)
266 result['data'] = str(e)
267 return result
268 if r.status_code == requests.codes.ok:
269 result['error'] = False
270 tarf = StringIO.StringIO(r.content)
271 return tarf
272 return result
273
274 def _descriptor_update(self, tarf, data):
275 print tarf.getnames()
276 # extract the package on a tmp directory
277 tarf.extractall('/tmp')
278
279 for name in tarf.getnames():
280 if name.endswith(".yaml") or name.endswith(".yml"):
281 with open('/tmp/' + name, 'w') as outfile:
282 yaml.safe_dump(data, outfile, default_flow_style=False)
283 break
284
285 tarf_temp = tarfile.open('/tmp/' + tarf.getnames()[0] + ".tar.gz", "w:gz")
286 # tarf_temp = tarfile.open("pippo.tar.gz", "w:gz")
287 print tarf_temp.getnames()
288 # tarf_temp.add('/tmp/'+tarf.getnames()[0])
289 for tarinfo in tarf:
290 # if tarinfo.name.startswith(tarf.getnames()[0]):
291 # new_name = tarinfo.name[len(tarf.getnames()[0]):]
292 tarf_temp.add('/tmp/' + tarinfo.name, tarinfo.name, recursive=False)
293 print tarf_temp.getnames()
294 tarf_temp.close()
295 return tarf
296
297 def nsd_get(self, token, id):
298 result = {'error': True, 'data': ''}
299 headers = {'Content-Type': 'application/yaml',
300 'Authorization': 'Bearer {}'.format(token['id'])}
301 _url = "{0}/nsd/v1/ns_descriptors/{1}/nsd".format(self._base_path, id)
302 try:
303 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
304 except Exception as e:
305 log.exception(e)
306 result['data'] = str(e)
307 return result
308 if r.status_code == requests.codes.ok:
309 result['error'] = False
310 return yaml.load(r.text)
311 else:
312 try:
313 result['data'] = r.json()
314 except Exception as e:
315 result['data'] = {}
316 return result
317
318 def vnfd_get(self, token, id):
319 result = {'error': True, 'data': ''}
320 headers = {'Content-Type': 'application/yaml',
321 'Authorization': 'Bearer {}'.format(token['id'])}
322 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/vnfd".format(self._base_path, id)
323 try:
324 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
325 except Exception as e:
326 log.exception(e)
327 result['data'] = str(e)
328 return result
329 if r.status_code == requests.codes.ok:
330 result['error'] = False
331 return yaml.load(r.text)
332 else:
333 try:
334 result['data'] = r.json()
335 except Exception as e:
336 result['data'] = {}
337 return result
338
339 def nsd_artifacts(self, token, id):
340 result = {'error': True, 'data': ''}
341 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
342 'Authorization': 'Bearer {}'.format(token['id'])}
343 _url = "{0}/nsd/v1/ns_descriptors/{1}/artifacts".format(self._base_path, id)
344 try:
345 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
346 except Exception as e:
347 log.exception(e)
348 result['data'] = str(e)
349 return result
350 if r.status_code == requests.codes.ok:
351 result['error'] = False
352 result['data'] = r.text
353 else:
354 try:
355 result['data'] = r.json()
356 except Exception as e:
357 result['data'] = {}
358
359 return result
360
361 def vnf_packages_artifacts(self, token, id):
362 result = {'error': True, 'data': ''}
363 headers = {'Content-Type': 'application/yaml', 'accept': 'text/plain',
364 'Authorization': 'Bearer {}'.format(token['id'])}
365 _url = "{0}/vnfpkgm/v1/vnf_packages/{1}/artifacts".format(self._base_path, id)
366 try:
367 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
368 except Exception as e:
369 log.exception(e)
370 result['data'] = str(e)
371 return result
372 if r.status_code == requests.codes.ok:
373 result['error'] = False
374 result['data'] = r.text
375 else:
376 try:
377 result['data'] = r.json()
378 except Exception as e:
379 result['data'] = {}
380
381 return result
382
383 def ns_create(self, token, ns_data):
384 token = self.get_token()
385 result = {'error': True, 'data': ''}
386 headers = {"Content-Type": "application/yaml", "accept": "application/json",
387 'Authorization': 'Bearer {}'.format(token['id'])}
388
389 _url = "{0}/nslcm/v1/ns_instances_content".format(self._base_path)
390
391 try:
392 r = requests.post(_url, json=ns_data, verify=False, headers=headers)
393 except Exception as e:
394 log.exception(e)
395 result['data'] = str(e)
396 return result
397 if r.status_code == requests.codes.ok:
398 result['error'] = False
399 result['data'] = Util.json_loads_byteified(r.text)
400 return result
401
402 def ns_op_list(self, token, id):
403 result = {'error': True, 'data': ''}
404 headers = {"Content-Type": "application/json", "accept": "application/json",
405 'Authorization': 'Bearer {}'.format(token['id'])}
406 _url = "{0}/nslcm/v1/ns_lcm_op_occs/?nsInstanceId={1}".format(self._base_path, id)
407
408 try:
409 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
410 except Exception as e:
411 log.exception(e)
412 result['data'] = str(e)
413 return result
414 if r.status_code == requests.codes.ok:
415 result['error'] = False
416 result['data'] = Util.json_loads_byteified(r.text)
417
418 return result
419
420 def ns_op(self, token, id):
421 result = {'error': True, 'data': ''}
422 headers = {"Content-Type": "application/json", "accept": "application/json",
423 'Authorization': 'Bearer {}'.format(token['id'])}
424 _url = "{0}/nslcm/v1/ns_lcm_op_occs/{1}".format(self._base_path, id)
425
426 try:
427 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
428 except Exception as e:
429 log.exception(e)
430 result['data'] = str(e)
431 return result
432 if r.status_code == requests.codes.ok:
433 result['error'] = False
434 result['data'] = Util.json_loads_byteified(r.text)
435
436 return result
437
438 def ns_action(self, token, id, action_payload):
439 result = {'error': True, 'data': ''}
440 headers = {"Content-Type": "application/json", "accept": "application/json",
441 'Authorization': 'Bearer {}'.format(token['id'])}
442
443 _url = "{0}/nslcm/v1/ns_instances/{1}/action".format(self._base_path, id)
444
445 try:
446 r = requests.post(_url, json=action_payload, verify=False, headers=headers)
447 except Exception as e:
448 log.exception(e)
449 result['data'] = str(e)
450 return result
451 print r.status_code
452 if r.status_code == requests.codes.created:
453 result['error'] = False
454 result['data'] = Util.json_loads_byteified(r.text)
455 return result
456
457 def ns_delete(self, token, id, force=None):
458 result = {'error': True, 'data': ''}
459 headers = {"Content-Type": "application/yaml", "accept": "application/json",
460 'Authorization': 'Bearer {}'.format(token['id'])}
461 query_path = ''
462 if force:
463 query_path = '?FORCE=true'
464 _url = "{0}/nslcm/v1/ns_instances_content/{1}{2}".format(self._base_path, id, query_path)
465 try:
466 r = requests.delete(_url, params=None, verify=False, headers=headers)
467 except Exception as e:
468 log.exception(e)
469 result['data'] = str(e)
470 return result
471 if r.status_code == requests.codes.ok:
472 result['error'] = False
473 result['data'] = Util.json_loads_byteified(r.text)
474 return result
475
476 def ns_get(self, token, id):
477 result = {'error': True, 'data': ''}
478 headers = {"Content-Type": "application/json", "accept": "application/json",
479 'Authorization': 'Bearer {}'.format(token['id'])}
480 _url = "{0}/nslcm/v1/ns_instances_content/{1}".format(self._base_path, id)
481
482 try:
483 r = requests.get(_url, params=None, verify=False, stream=True, headers=headers)
484 except Exception as e:
485 log.exception(e)
486 result['data'] = str(e)
487 return result
488 if r.status_code == requests.codes.ok:
489 result['error'] = False
490 result['data'] = Util.json_loads_byteified(r.text)
491 return result
492
493 def vnf_get(self, token, id):
494 result = {'error': True, 'data': ''}
495 headers = {"Content-Type": "application/json", "accept": "application/json",
496 'Authorization': 'Bearer {}'.format(token['id'])}
497 _url = "{0}/nslcm/v1/vnfrs/{1}".format(self._base_path, id)
498
499 try:
500 r = requests.get(_url, params=None, verify=False, stream=True, 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.ok:
506 result['error'] = False
507 result['data'] = Util.json_loads_byteified(r.text)
508 return result
509
510 def ns_alarm_create(self, token, id, alarm_payload):
511 result = {'error': True, 'data': ''}
512 headers = {"Content-Type": "application/json",
513 'Authorization': 'Bearer {}'.format(token['id'])}
514 _url = "{0}/test/message/alarm_request".format(self._base_path)
515 try:
516 r = requests.post(_url, json=alarm_payload, verify=False, headers=headers)
517 except Exception as e:
518 log.exception(e)
519 result['data'] = str(e)
520 return result
521 print r.status_code
522 if r.status_code == requests.codes.ok:
523 result['error'] = False
524 #result['data'] = Util.json_loads_byteified(r.text)
525 result['data'] = r.text
526 return result
527
528 def ns_metric_export(self, token, id, metric_payload):
529 result = {'error': True, 'data': ''}
530 headers = {"Content-Type": "application/json",
531 'Authorization': 'Bearer {}'.format(token['id'])}
532 _url = "{0}/test/message/metric_request".format(self._base_path)
533 try:
534 r = requests.post(_url, json=metric_payload, verify=False, headers=headers)
535 except Exception as e:
536 log.exception(e)
537 result['data'] = str(e)
538 return result
539 print r.status_code
540 if r.status_code == requests.codes.ok:
541 result['error'] = False
542 #result['data'] = Util.json_loads_byteified(r.text)
543 result['data'] = r.text
544 return result
545
546 @staticmethod
547 def md5(f):
548 hash_md5 = hashlib.md5()
549 for chunk in iter(lambda: f.read(1024), b""):
550 hash_md5.update(chunk)
551 return hash_md5.hexdigest()