-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgenieacs.py
506 lines (448 loc) · 20.2 KB
/
genieacs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# -*- coding: utf-8 -*-
#
# python-genieacs
# A Python API to interact with the GenieACS REST API
# https://github.com/TDT-GmbH/python-genieacs
import requests
import json
class Connection(object):
"""Connection object to interact with the GenieACS server."""
def __init__(self, ip, port=7557, ssl=False, verify=False, auth=False, user="", passwd="", url="", timeout=10):
self.server_ip = ip
self.server_port = port
self.use_ssl = ssl
self.ssl_verify = verify
self.use_auth = auth
self.username = user
self.password = passwd
self.server_url = url
self.timeout = timeout
self.base_url = ""
self.session = None
self.__set_base_url()
self.__create_session()
def __set_base_url(self):
if not self.use_ssl:
self.base_url = "http://"
else:
self.base_url = "https://"
self.base_url += self.server_ip + ":" + str(self.server_port) + self.server_url
def __create_session(self):
if self.session is None:
self.session = requests.Session()
if self.use_auth:
self.session.auth = (self.username, self.password)
if self.use_ssl:
self.session.verify = self.ssl_verify
try:
# do a request to test the connection
self.file_get_all()
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError):
raise ConnectionError
def __request_get(self, url):
request_url = self.base_url + url
try:
r = self.session.get(request_url, timeout=self.timeout)
r.raise_for_status()
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError):
raise ConnectionError
if r.text:
data = r.json()
return data
def __request_post(self, url, data, conn_request=True):
if conn_request:
request_url = self.base_url + url + "?connection_request"
else:
request_url = self.base_url + url
try:
r = self.session.post(request_url, json=data, timeout=self.timeout)
r.raise_for_status()
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError):
raise ConnectionError
if r.text:
data = r.json()
return data
def __request_put(self, url, data, headers=None):
request_url = self.base_url + url
try:
if headers is not None:
r = self.session.put(request_url, data, timeout=self.timeout, headers=headers)
else:
r = self.session.put(request_url, data, timeout=self.timeout)
r.raise_for_status()
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError):
raise ConnectionError
if r.text:
data = r.json()
return data
def __request_delete(self, url):
request_url = self.base_url + url
try:
r = self.session.delete(request_url, timeout=self.timeout)
r.raise_for_status()
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError):
raise ConnectionError
if r.text:
data = r.json()
return data
##### methods for devices #####
def device_get_all_IDs(self):
"""Get IDs of all devices"""
jsondata = self.__request_get("/devices/" + "?projection=_id")
data = []
for device in jsondata:
data.append(device["_id"])
return data
def device_get_by_id(self, device_id):
"""Get all data of a device identified by its ID"""
quoted_id = requests.utils.quote("{\"_id\":\"" + device_id + "\"}", safe = '')
return self.__request_get("/devices/" + "?query=" + quoted_id)
def device_get_by_MAC(self, device_MAC):
"""Get all data of a device identified by its MAC address"""
quoted_MAC = requests.utils.quote("{\"summary.mac\":\"" + device_MAC + "\"}", safe = '')
return self.__request_get("/devices/" + "?query=" + quoted_MAC)
def device_get_by_serial(self, device_serial):
"""Get all data of a device identified by its Serial"""
quoted_serial = requests.utils.quote("{\"InternetGatewayDevice.DeviceInfo.SerialNumber\":\"" + device_serial + "\"}", safe = '')
return self.__request_get("/devices/" + "?query=" + quoted_serial)
def device_get_parameter(self, device_id, parameter_name):
"""Directly get the value of a given parameter from a given device"""
quoted_id = requests.utils.quote("{\"_id\":\"" + device_id + "\"}", safe = '')
data = self.__request_get("/devices" + "?query=" + quoted_id + "&projection=" + parameter_name)
try:
if parameter_name in ["_tags", "_lastInform", "_registered", "_lastBootstrap", "_lastBoot"]:
return data[0][parameter_name]
else:
value = data[0]
for part in parameter_name.split('.'):
value = value[part]
return value["_value"]
except (IndexError, KeyError):
return None
def device_get_parameters(self, device_id, parameter_names):
"""Get a defined list of parameters from a given device"""
quoted_id = requests.utils.quote("{\"_id\":\"" + device_id + "\"}", safe = '')
data = self.__request_get("/devices" + "?query=" + quoted_id + "&projection=" + parameter_names)
try:
data = data[0]
values = {}
src = data
dest = values
except (IndexError):
return {}
for parameter in parameter_names.split(','):
parameter_parts = parameter.split('.')
for part in parameter_parts:
if part != parameter_parts[-1]:
try:
src = src[part]
except (KeyError):
src[part] = {}
src = src[part]
if part not in dest.keys():
dest[part] = {}
dest = dest[part]
else:
try:
dest[part] = src[part]["_value"]
except (KeyError):
dest[part] = None
dest = values
src = data
return values
def device_delete(self, device_id):
"""Delete a given device from the database"""
self.__request_delete("/devices/" + requests.utils.quote(device_id))
##### methods for tasks #####
def task_get_all(self, device_id=None):
if device_id:
"""Get all existing tasks of a given device"""
quoted_id = requests.utils.quote("{\"device\":\"" + device_id + "\"}", safe = '')
return self.__request_get("/tasks/" + "?query=" + quoted_id)
else:
"""Get all existing tasks"""
return self.__request_get("/tasks/")
def task_refresh_object(self, device_id, object_name, conn_request=True):
"""Create a refreshObject task for a given device"""
data = { "name": "refreshObject",
"objectName": object_name }
try:
return self.__request_post("/devices/" + requests.utils.quote(device_id) + "/tasks", data, conn_request)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
def task_set_parameter_values(self, device_id, parameter_values, conn_request=True):
"""Create a setParameterValues task for a given device"""
data = { "name": "setParameterValues",
"parameterValues": parameter_values }
try:
return self.__request_post("/devices/" + requests.utils.quote(device_id) + "/tasks", data, conn_request)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
def task_get_parameter_values(self, device_id, parameter_names, conn_request = True):
"""Create a getParameterValues task for a given device"""
data = { "name": "getParameterValues",
"parameterNames": parameter_names}
try:
return self.__request_post("/devices/" + requests.utils.quote(device_id) + "/tasks", data, conn_request)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
def task_add_object(self, device_id, object_name, object_path, conn_request=True):
"""Create an addObject task for a given device"""
data = { "name": "addObject", object_name : object_path}
try:
return self.__request_post("/devices/" + requests.utils.quote(device_id) + "/tasks", data, conn_request)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
def task_reboot(self, device_id, conn_request=True):
"""Create a reboot task for a given device"""
data = { "name": "reboot"}
try:
return self.__request_post("/devices/" + requests.utils.quote(device_id) + "/tasks", data, conn_request)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
def task_factory_reset(self, device_id, conn_request=True):
"""Create a factoryReset task for a given device"""
data = { "name": "factoryReset"}
try:
return self.__request_post("/devices/" + requests.utils.quote(device_id) + "/tasks", data, conn_request)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
def task_download(self, device_id, file_id, filename, conn_request=True):
"""Create a download task for a given device"""
data = { "name": "download", "file": file_id, "filename": filename}
try:
return self.__request_post("/devices/" + requests.utils.quote(device_id) + "/tasks", data, conn_request)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
def task_retry(self, task_id):
"Retry a faulty task at the next inform"
try:
return self.__request_post("/tasks/" + task_id + "/retry", None)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
def task_delete(self, task_id):
"""Delete a Task for a given device"""
try:
return self.__request_delete("/tasks/" + task_id)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
##### methods for tags ######
def tag_get_all(self, device_id):
"""Get all existing tags of a given device"""
quoted_id = requests.utils.quote("{\"_id\":\"" + device_id + "\"}", safe = '')
data = self.__request_get("/devices" + "?query=" + quoted_id + "&projection=_tags")
try:
return data[0]["_tags"]
except (IndexError, KeyError):
return []
def tag_assign(self, device_id, tag_name):
"""Assign a tag to a device"""
try:
self.__request_post("/devices/" + requests.utils.quote(device_id) + "/tags/" + tag_name, None, False)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
def tag_remove(self, device_id, tag_name):
"""Remove a tag from a device"""
try:
self.__request_delete("/devices/" + requests.utils.quote(device_id) + "/tags/" + tag_name)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
##### methods for presets #####
def preset_get_all(self, filename=None):
"""Get all existing presets as a json object, optionally write them to a file"""
data = self.__request_get("/presets")
try:
if filename is not None:
f = open(filename, 'w')
json.dump(data, f, indent=4, separators=(',', ': '))
f.close()
except IOError as err:
print("preset_get_all:\nIOError: " + str(err) + "\n")
finally:
return data
def preset_create(self, preset_name, data):
"""Create a new preset or update a preset with a given name"""
quoted_name = requests.utils.quote(preset_name)
try:
self.__request_put("/presets/" + quoted_name, data)
except requests.exceptions.HTTPError:
raise InvalidRequestDataError
def preset_create_all_from_file(self, filename):
"""Create all presets contained in a json file"""
try:
f = open(filename, 'r')
data = json.load(f)
f.close()
for preset in data:
preset_name = requests.utils.quote(preset["_id"])
del preset["_id"]
self.__request_put("/presets/" + preset_name, json.dumps(preset))
except IOError as err:
print("preset_create_all_from_file:\nIOError: " + str(err) + "\n")
except ValueError:
print("preset_create_all_from_file:\nValueError: File contains faulty values\n")
except KeyError:
print("preset_create_all_from_file:\nKeyError: File contains faulty keys\n")
def preset_delete(self, preset_name):
"""Delete a given preset"""
quoted_name = requests.utils.quote(preset_name)
try:
self.__request_delete("/presets/" + quoted_name)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
##### methods for objects #####
def object_get_all(self, filename=None):
"""Get all existing objects as a json object, optionally write them to a file"""
data = self.__request_get("/objects")
try:
if filename is not None:
f = open(filename, 'w')
json.dump(data, f, indent=4, separators=(',', ': '))
f.close()
except IOError as err:
print("object_get_all:\nIOError: " + str(err) + "\n")
finally:
return data
def object_create(self, object_name, data):
"""Create a new object or update an object with a given name"""
quoted_name = requests.utils.quote(object_name)
try:
self.__request_put("/objects/" + quoted_name, data)
except requests.exceptions.HTTPError:
raise InvalidRequestDataError
def object_create_all_from_file(self, filename):
"""Create all objects contained in a json file"""
try:
f = open(filename, 'r')
data = json.load(f)
f.close()
for gobject in data:
object_name = requests.utils.quote(gobject["_id"])
del gobject["_id"]
self.__request_put("/objects/" + object_name, json.dumps(gobject))
except IOError as err:
print("object_create_all_from_file:\nIOError: " + str(err) + "\n")
except ValueError:
print("object_create_all_from_file:\nValueError: File contains faulty values\n")
except KeyError:
print("object_create_all_from_file:\nKeyError: File contains faulty keys\n")
def object_delete(self, object_name):
"""Delete a given object"""
quoted_name = requests.utils.quote(object_name)
try:
self.__request_delete("/objects/" + quoted_name)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
##### methods for provisions #####
def provision_get_all(self, filename=None):
"""Get all existing provisions as a json object, optionally write them to a file"""
data = self.__request_get("/provisions")
try:
if filename is not None:
f = open(filename, 'w')
json.dump(data, f, indent=4, separators=(',', ': '))
f.close()
except IOError as err:
print("provision_get_all:\nIOError: " + str(err) + "\n")
finally:
return data
def provision_create(self, provision_name, data):
"""Create a new provision or update a provision with a given name"""
quoted_name = requests.utils.quote(provision_name)
try:
self.__request_put("/provisions/" + quoted_name, data)
except requests.exceptions.HTTPError:
raise InvalidRequestDataError
def provision_create_all_from_file(self, filename):
"""Create all provisions contained in a json file"""
try:
f = open(filename, 'r')
data = json.load(f)
f.close()
for provision in data:
provision_name = requests.utils.quote(provision["_id"])
provision_data = provision["script"]
self.__request_put("/provisions/" + provision_name, provision_data)
except IOError as err:
print("provision_create_all_from_file:\nIOError: " + str(err) + "\n")
except ValueError:
print("provision_create_all_from_file:\nValueError: File contains faulty values\n")
except KeyError:
print("provision_create_all_from_file:\nKeyError: File contains faulty keys\n")
def provision_delete(self, provision_name):
"""Delete a given provision"""
quoted_name = requests.utils.quote(provision_name)
try:
self.__request_delete("/provisions/" + quoted_name)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
##### methods for files #####
def file_upload(self, filename, fileType, oui, productClass, version):
"""Upload or update a file"""
try:
self.__request_put("/files/" + filename, data=open(filename, "rb"), headers={"fileType": fileType, "oui": oui, "productClass": productClass, "version" : version})
except IOError as err:
print("file_upload:\nIOError: " + str(err) + "\n")
def file_delete(self, filename):
"""Delete a given file"""
self.__request_delete("/files/" + filename)
def file_get_all(self):
"""Get all files as a json object"""
return self.__request_get("/files")
def file_get(self, filename=None, fileType=None, oui=None, productClass=None, version=None):
"""Get all data from one or several files"""
url = "{"
if filename is not None:
url += "\"filename\":\"" + filename + "\""
else:
if fileType is not None:
if url != "{":
url += ","
url += "\"metadata.fileType\":\"" + fileType + "\""
if oui is not None:
if url != "{":
url += ","
url += "\"metadata.oui\":\"" + oui + "\""
if productClass is not None:
if url != "{":
url += ","
url += "\"metadata.productClass\":\"" + productClass + "\""
if version is not None:
if url != "{":
url += ","
url += "\"metadata.version\":\"" + version + "\""
if url == "{":
raise InvalidRequestDataError
return self.__request_get("/files/?query=" + requests.utils.quote(url + "}", safe = ''))
##### methods for faults #####
def fault_get_all_IDs(self):
"""Get IDs of all faults"""
jsondata = self.__request_get("/faults/" + "?projection=_id")
data = []
for fault in jsondata:
data.append(fault["_id"])
return data
def fault_get_all(self, device_id=None):
if device_id:
"""Get all existing faults for a given device"""
quoted_id = requests.utils.quote("{\"device\":\"" + device_id + "\"}", safe = '')
return self.__request_get("/faults/" + "?query=" + quoted_id)
else:
"""Get all existing faults"""
return self.__request_get("/faults/")
def fault_delete(self, fault_id):
"""Delete a given fault"""
quoted_id = requests.utils.quote(fault_id)
try:
self.__request_delete("/faults/" + quoted_id)
except requests.exceptions.HTTPError:
raise ItemNotFoundError
class ConnectionError(Exception):
def __str__(self):
return "Could not (re-)connect to the ACS"
class ItemNotFoundError(Exception):
def __str__(self):
return "Could not find the requested item (device, task, preset, object, file, etc)"
class InvalidRequestDataError(Exception):
def __str__(self):
return "Request contained invalid data"