blob: 6a7de8fda954819ab4ff1f7d02753a4b105806de [file] [log] [blame]
Chip Boling32aab302019-01-23 10:50:18 -06001#
2# Copyright 2017 the original author or authors.
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 "AS IS" 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#
16import copy
17from mib_db_api import *
18import json
19
20
21class MibDbVolatileDict(MibDbApi):
22 """
23 A very simple in-memory database for ME storage. Data is not persistent
24 across reboots.
25
26 In Phase 2, this DB will be instantiated on a per-ONU basis but act as if
27 it is shared for all ONUs. This class will be updated with and external
28 key-value store (or other appropriate database) in Voltha 1.3 Sprint 3
29
30 This class can be used for unit tests
31 """
32 CURRENT_VERSION = 1
33
34 def __init__(self, omci_agent):
35 """
36 Class initializer
37 :param omci_agent: (OpenOMCIAgent) OpenOMCI Agent
38 """
39 super(MibDbVolatileDict, self).__init__(omci_agent)
40 self._data = dict() # device_id -> ME ID -> Inst ID -> Attr Name -> Values
41
42 def start(self):
43 """
44 Start up/restore the database. For in-memory, will be a nop. For external
45 DB, may need to create the DB and fetch create/modified values
46 """
47 super(MibDbVolatileDict, self).start()
48 # TODO: Delete this method if nothing else is done except calling the base class
49
50 def stop(self):
51 """
52 Start up the database. For in-memory, will be a nop. For external
53 DB, may need to create the DB and fetch create/modified values
54 """
55 super(MibDbVolatileDict, self).stop()
56 # TODO: Delete this method if nothing else is done except calling the base class
57
58 def add(self, device_id, overwrite=False):
59 """
60 Add a new ONU to database
61
62 :param device_id: (str) Device ID of ONU to add
63 :param overwrite: (bool) Overwrite existing entry if found.
64
65 :raises KeyError: If device already exist and 'overwrite' is False
66 """
67 self.log.debug('add-device', device_id=device_id, overwrite=overwrite)
68
69 if not isinstance(device_id, basestring):
70 raise TypeError('Device ID should be an string')
71
72 if not self._started:
73 raise DatabaseStateError('The Database is not currently active')
74
75 if not overwrite and device_id in self._data:
76 raise KeyError('Device {} already exists in the database'
77 .format(device_id))
78
79 now = datetime.utcnow()
80 self._data[device_id] = {
81 DEVICE_ID_KEY: device_id,
82 CREATED_KEY: now,
83 LAST_SYNC_KEY: None,
84 MDS_KEY: 0,
85 VERSION_KEY: MibDbVolatileDict.CURRENT_VERSION,
86 ME_KEY: dict(),
87 MSG_TYPE_KEY: set()
88 }
89
90 def remove(self, device_id):
91 """
92 Remove an ONU from the database
93
94 :param device_id: (str) Device ID of ONU to remove from database
95 """
96 self.log.debug('remove-device', device_id=device_id)
97
98 if not isinstance(device_id, basestring):
99 raise TypeError('Device ID should be an string')
100
101 if not self._started:
102 raise DatabaseStateError('The Database is not currently active')
103
104 if device_id in self._data:
105 del self._data[device_id]
106 self._modified = datetime.utcnow()
107
108 def on_mib_reset(self, device_id):
109 """
110 Reset/clear the database for a specific Device
111
112 :param device_id: (str) ONU Device ID
113 :raises DatabaseStateError: If the database is not enabled
114 :raises KeyError: If the device does not exist in the database
115 """
116 if not self._started:
117 raise DatabaseStateError('The Database is not currently active')
118
119 if not isinstance(device_id, basestring):
120 raise TypeError('Device ID should be an string')
121
122 device_db = self._data[device_id]
123 self._modified = datetime.utcnow()
124
125 self._data[device_id] = {
126 DEVICE_ID_KEY: device_id,
127 CREATED_KEY: device_db[CREATED_KEY],
128 LAST_SYNC_KEY: device_db[LAST_SYNC_KEY],
129 MDS_KEY: 0,
130 VERSION_KEY: MibDbVolatileDict.CURRENT_VERSION,
131 ME_KEY: device_db[ME_KEY],
132 MSG_TYPE_KEY: device_db[MSG_TYPE_KEY]
133 }
134
135 def save_mib_data_sync(self, device_id, value):
136 """
137 Save the MIB Data Sync to the database in an easy location to access
138
139 :param device_id: (str) ONU Device ID
140 :param value: (int) Value to save
141 """
142 if not isinstance(device_id, basestring):
143 raise TypeError('Device ID should be an string')
144
145 if not isinstance(value, int):
146 raise TypeError('MIB Data Sync is an integer')
147
148 if not 0 <= value <= 255:
149 raise ValueError('Invalid MIB-data-sync value {}. Must be 0..255'.
150 format(value))
151
152 self._data[device_id][MDS_KEY] = value
153 self._modified = datetime.utcnow()
154
155 def get_mib_data_sync(self, device_id):
156 """
157 Get the MIB Data Sync value last saved to the database for a device
158
159 :param device_id: (str) ONU Device ID
160 :return: (int) The Value or None if not found
161 """
162 if not isinstance(device_id, basestring):
163 raise TypeError('Device ID should be an string')
164
165 if device_id not in self._data:
166 return None
167
168 return self._data[device_id].get(MDS_KEY)
169
170 def save_last_sync(self, device_id, value):
171 """
172 Save the Last Sync time to the database in an easy location to access
173
174 :param device_id: (str) ONU Device ID
175 :param value: (DateTime) Value to save
176 """
177 if not isinstance(device_id, basestring):
178 raise TypeError('Device ID should be an string')
179
180 if not isinstance(value, datetime):
181 raise TypeError('Expected a datetime object, got {}'.
182 format(type(datetime)))
183
184 self._data[device_id][LAST_SYNC_KEY] = value
185 self._modified = datetime.utcnow()
186
187 def get_last_sync(self, device_id):
188 """
189 Get the Last SYnc Time saved to the database for a device
190
191 :param device_id: (str) ONU Device ID
192 :return: (int) The Value or None if not found
193 """
194 if not isinstance(device_id, basestring):
195 raise TypeError('Device ID should be an string')
196
197 if device_id not in self._data:
198 return None
199
200 return self._data[device_id].get(LAST_SYNC_KEY)
201
202 def set(self, device_id, class_id, instance_id, attributes):
203 """
204 Set a database value. This should only be called by the MIB synchronizer
205 and its related tasks
206
207 :param device_id: (str) ONU Device ID
208 :param class_id: (int) ME Class ID
209 :param instance_id: (int) ME Entity ID
210 :param attributes: (dict) Attribute dictionary
211
212 :returns: (bool) True if the value was saved to the database. False if the
213 value was identical to the current instance
214
215 :raises KeyError: If device does not exist
216 :raises DatabaseStateError: If the database is not enabled
217 """
218 if not isinstance(device_id, basestring):
219 raise TypeError('Device ID should be a string')
220
221 if not 0 <= class_id <= 0xFFFF:
222 raise ValueError("Invalid Class ID: {}, should be 0..65535".format(class_id))
223
224 if not 0 <= instance_id <= 0xFFFF:
225 raise ValueError("Invalid Instance ID: {}, should be 0..65535".format(instance_id))
226
227 if not isinstance(attributes, dict):
228 raise TypeError("Attributes should be a dictionary")
229
230 if not self._started:
231 raise DatabaseStateError('The Database is not currently active')
232
233 now = datetime.utcnow()
234 try:
235 device_db = self._data[device_id]
236 class_db = device_db.get(class_id)
237 created = False
238
239 if class_db is None:
240 device_db[class_id] = {CLASS_ID_KEY: class_id}
241
242 class_db = device_db[class_id]
243 self._modified = now
244 created = True
245
246 instance_db = class_db.get(instance_id)
247 if instance_db is None:
248 class_db[instance_id] = {
249 INSTANCE_ID_KEY: instance_id,
250 CREATED_KEY: now,
251 MODIFIED_KEY: now,
252 ATTRIBUTES_KEY: dict()
253 }
254 instance_db = class_db[instance_id]
255 self._modified = now
256 created = True
257
258 changed = False
259
260 me_map = self._omci_agent.get_device(device_id).me_map
261 entity = me_map.get(class_id)
262
263 for attribute, value in attributes.items():
264 assert isinstance(attribute, basestring)
265 assert value is not None, "Attribute '{}' value cannot be 'None'".\
266 format(attribute)
267
268 db_value = instance_db[ATTRIBUTES_KEY].get(attribute) \
269 if ATTRIBUTES_KEY in instance_db else None
270
271 if entity is not None and isinstance(value, basestring):
272 from scapy.fields import StrFixedLenField
273 attr_index = entity.attribute_name_to_index_map[attribute]
274 eca = entity.attributes[attr_index]
275 field = eca.field
276
277 if isinstance(field, StrFixedLenField):
278 from scapy.base_classes import Packet_metaclass
279 if isinstance(field.default, Packet_metaclass) \
280 and hasattr(field.default, 'json_from_value'):
281 # Value/hex of Packet Class to string
282 value = field.default.json_from_value(value)
283
284 if entity is not None and attribute in entity.attribute_name_to_index_map:
285 attr_index = entity.attribute_name_to_index_map[attribute]
286 eca = entity.attributes[attr_index]
287 field = eca.field
288
289 if hasattr(field, 'to_json'):
290 value = field.to_json(value, db_value)
291
292 # Complex packet types may have an attribute encoded as an object, this
293 # can be check by seeing if there is a to_json() conversion callable
294 # defined
295 if hasattr(value, 'to_json'):
296 value = value.to_json()
297
298 # Other complex packet types may be a repeated list field (FieldListField)
299 elif isinstance(value, (list, dict)):
300 value = json.dumps(value, separators=(',', ':'))
301
302 assert db_value is None or isinstance(value, type(db_value)), \
303 "New value type for attribute '{}' type is changing from '{}' to '{}'".\
304 format(attribute, type(db_value), type(value))
305
306 if db_value is None or db_value != value:
307 instance_db[ATTRIBUTES_KEY][attribute] = value
308 changed = True
309
310 if changed:
311 instance_db[MODIFIED_KEY] = now
312 self._modified = now
313
314 return changed or created
315
316 except Exception as e:
317 self.log.error('set-failure', e=e, class_id=class_id,
318 instance_id=instance_id, attributes=attributes)
319 raise
320
321 def delete(self, device_id, class_id, instance_id):
322 """
323 Delete an entity from the database if it exists. If all instances
324 of a class are deleted, the class is deleted as well.
325
326 :param device_id: (str) ONU Device ID
327 :param class_id: (int) ME Class ID
328 :param instance_id: (int) ME Entity ID
329
330 :returns: (bool) True if the instance was found and deleted. False
331 if it did not exist.
332
333 :raises KeyError: If device does not exist
334 :raises DatabaseStateError: If the database is not enabled
335 """
336 if not self._started:
337 raise DatabaseStateError('The Database is not currently active')
338
339 if not isinstance(device_id, basestring):
340 raise TypeError('Device ID should be an string')
341
342 if not 0 <= class_id <= 0xFFFF:
343 raise ValueError('class-id is 0..0xFFFF')
344
345 if not 0 <= instance_id <= 0xFFFF:
346 raise ValueError('instance-id is 0..0xFFFF')
347
348 try:
349 device_db = self._data[device_id]
350 class_db = device_db.get(class_id)
351
352 if class_db is None:
353 return False
354
355 instance_db = class_db.get(instance_id)
356 if instance_db is None:
357 return False
358
359 now = datetime.utcnow()
360 del class_db[instance_id]
361
362 if len(class_db) == 1: # Is only 'CLASS_ID_KEY' remaining
363 del device_db[class_id]
364
365 self._modified = now
366 return True
367
368 except Exception as e:
369 self.log.error('delete-failure', e=e)
370 raise
371
372 def query(self, device_id, class_id=None, instance_id=None, attributes=None):
373 """
374 Get database information.
375
376 This method can be used to request information from the database to the detailed
377 level requested
378
379 :param device_id: (str) ONU Device ID
380 :param class_id: (int) Managed Entity class ID
381 :param instance_id: (int) Managed Entity instance
382 :param attributes: (list/set or str) Managed Entity instance's attributes
383
384 :return: (dict) The value(s) requested. If class/inst/attribute is
385 not found, an empty dictionary is returned
386 :raises KeyError: If the requested device does not exist
387 :raises DatabaseStateError: If the database is not enabled
388 """
389 self.log.debug('query', device_id=device_id, class_id=class_id,
390 instance_id=instance_id, attributes=attributes)
391
392 if not self._started:
393 raise DatabaseStateError('The Database is not currently active')
394
395 if not isinstance(device_id, basestring):
396 raise TypeError('Device ID is a string')
397
398 device_db = self._data[device_id]
399 if class_id is None:
400 return self._fix_dev_json_attributes(copy.copy(device_db), device_id)
401
402 if not isinstance(class_id, int):
403 raise TypeError('Class ID is an integer')
404
405 me_map = self._omci_agent.get_device(device_id).me_map
406 entity = me_map.get(class_id)
407
408 class_db = device_db.get(class_id, dict())
409 if instance_id is None or len(class_db) == 0:
410 return self._fix_cls_json_attributes(copy.copy(class_db), entity)
411
412 if not isinstance(instance_id, int):
413 raise TypeError('Instance ID is an integer')
414
415 instance_db = class_db.get(instance_id, dict())
416 if attributes is None or len(instance_db) == 0:
417 return self._fix_inst_json_attributes(copy.copy(instance_db), entity)
418
419 if not isinstance(attributes, (basestring, list, set)):
420 raise TypeError('Attributes should be a string or list/set of strings')
421
422 if not isinstance(attributes, (list, set)):
423 attributes = [attributes]
424
425 results = {attr: val for attr, val in instance_db[ATTRIBUTES_KEY].iteritems()
426 if attr in attributes}
427
428 for attr, attr_data in results.items():
429 attr_index = entity.attribute_name_to_index_map[attr]
430 eca = entity.attributes[attr_index]
431 results[attr] = self._fix_attr_json_attribute(copy.copy(attr_data), eca)
432
433 return results
434
435 #########################################################################
436 # Following routines are used to fix-up JSON encoded complex data. A
437 # nice side effect is that the values returned will be a deep-copy of
438 # the class/instance/attribute data of what is in the database. Note
439 # That other database values (created, modified, ...) will still reference
440 # back to the original DB.
441
442 def _fix_dev_json_attributes(self, dev_data, device_id):
443 for cls_id, cls_data in dev_data.items():
444 if isinstance(cls_id, int):
445 me_map = self._omci_agent.get_device(device_id).me_map
446 entity = me_map.get(cls_id)
447 dev_data[cls_id] = self._fix_cls_json_attributes(copy.copy(cls_data), entity)
448 return dev_data
449
450 def _fix_cls_json_attributes(self, cls_data, entity):
451 for inst_id, inst_data in cls_data.items():
452 if isinstance(inst_id, int):
453 cls_data[inst_id] = self._fix_inst_json_attributes(copy.copy(inst_data), entity)
454 return cls_data
455
456 def _fix_inst_json_attributes(self, inst_data, entity):
457 if ATTRIBUTES_KEY in inst_data:
458 for attr, attr_data in inst_data[ATTRIBUTES_KEY].items():
459 attr_index = entity.attribute_name_to_index_map[attr] \
460 if entity is not None and attr in entity.attribute_name_to_index_map else None
461 eca = entity.attributes[attr_index] if attr_index is not None else None
462 inst_data[ATTRIBUTES_KEY][attr] = self._fix_attr_json_attribute(copy.copy(attr_data), eca)
463 return inst_data
464
465 def _fix_attr_json_attribute(self, attr_data, eca):
466
467 try:
468 if eca is not None:
469 field = eca.field
470 if hasattr(field, 'load_json'):
471 value = field.load_json(attr_data)
472 return value
473
474 return json.loads(attr_data) if isinstance(attr_data, basestring) else attr_data
475
476 except ValueError:
477 return attr_data
478
479 except Exception as e:
480 pass
481
482 def update_supported_managed_entities(self, device_id, managed_entities):
483 """
484 Update the supported OMCI Managed Entities for this device
485
486 :param device_id: (str) ONU Device ID
487 :param managed_entities: (set) Managed Entity class IDs
488 """
489 now = datetime.utcnow()
490 try:
491 device_db = self._data[device_id]
492
493 entities = {class_id: self._managed_entity_to_name(device_id, class_id)
494 for class_id in managed_entities}
495
496 device_db[ME_KEY] = entities
497 self._modified = now
498
499 except Exception as e:
500 self.log.error('set-me-failure', e=e)
501 raise
502
503 def _managed_entity_to_name(self, device_id, class_id):
504 me_map = self._omci_agent.get_device(device_id).me_map
505 entity = me_map.get(class_id)
506
507 return entity.__name__ if entity is not None else 'UnknownManagedEntity'
508
509 def update_supported_message_types(self, device_id, msg_types):
510 """
511 Update the supported OMCI Managed Entities for this device
512
513 :param device_id: (str) ONU Device ID
514 :param msg_types: (set) Message Type values (ints)
515 """
516 now = datetime.utcnow()
517 try:
518 msg_type_set = {msg_type.value for msg_type in msg_types}
519 self._data[device_id][MSG_TYPE_KEY] = msg_type_set
520 self._modified = now
521
522 except Exception as e:
523 self.log.error('set-me-failure', e=e)
524 raise