2014-03-07 17:08:39 -07:00
|
|
|
import json
|
2014-02-27 16:55:07 -07:00
|
|
|
|
2014-03-10 16:51:08 -06:00
|
|
|
|
2014-02-27 16:55:07 -07:00
|
|
|
class Notification(object):
|
2014-03-17 16:29:09 -06:00
|
|
|
"""An abstract base class used to define the notification interface and common functions
|
2014-02-27 16:55:07 -07:00
|
|
|
"""
|
2014-03-12 11:31:57 -06:00
|
|
|
__slots__ = (
|
|
|
|
'address',
|
|
|
|
'alarm_id',
|
|
|
|
'alarm_name',
|
|
|
|
'alarm_timestamp',
|
|
|
|
'message',
|
|
|
|
'name',
|
|
|
|
'notification_timestamp',
|
|
|
|
'src_partition',
|
|
|
|
'src_offset',
|
|
|
|
'state',
|
|
|
|
'tenant_id',
|
|
|
|
'type'
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, ntype, src_partition, src_offset, name, address, alarm):
|
2014-03-17 16:29:09 -06:00
|
|
|
"""Setup the notification object
|
|
|
|
The src_partition and src_offset allow the notification to be linked to the alarm that it came from
|
|
|
|
ntype - The notification type
|
|
|
|
name - Name used in sending
|
|
|
|
address - to send the notification to
|
|
|
|
alarm_data - info that caused the notification
|
|
|
|
notifications that come after this one to remain uncommitted.
|
2014-03-07 17:08:39 -07:00
|
|
|
"""
|
2014-03-11 09:50:24 -06:00
|
|
|
self.address = address
|
|
|
|
self.name = name
|
2014-03-07 17:08:39 -07:00
|
|
|
self.src_partition = src_partition
|
|
|
|
self.src_offset = src_offset
|
2014-03-12 11:31:57 -06:00
|
|
|
self.type = ntype
|
2014-03-07 17:08:39 -07:00
|
|
|
|
2014-03-11 16:46:02 -06:00
|
|
|
self.alarm_id = alarm['alarmId']
|
|
|
|
self.alarm_name = alarm['alarmName']
|
|
|
|
self.alarm_timestamp = alarm['timestamp']
|
|
|
|
self.message = alarm['stateChangeReason']
|
|
|
|
self.state = alarm['newState']
|
|
|
|
self.tenant_id = alarm['tenantId']
|
|
|
|
|
|
|
|
self.notification_timestamp = None # to be updated on actual notification send time
|
|
|
|
|
2014-03-21 12:23:12 -06:00
|
|
|
def __eq__(self, other):
|
|
|
|
if not isinstance(other, Notification):
|
|
|
|
return False
|
|
|
|
|
|
|
|
for attrib in self.__slots__:
|
|
|
|
if not getattr(self, attrib) == getattr(other, attrib):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2014-03-07 17:08:39 -07:00
|
|
|
def to_json(self):
|
2014-03-17 16:29:09 -06:00
|
|
|
"""Return json representation
|
2014-03-07 17:08:39 -07:00
|
|
|
"""
|
2014-03-11 16:46:02 -06:00
|
|
|
notification_fields = [
|
|
|
|
'address',
|
|
|
|
'name',
|
|
|
|
'alarm_id',
|
|
|
|
'alarm_name',
|
|
|
|
'alarm_timestamp',
|
|
|
|
'message',
|
|
|
|
'notification_timestamp',
|
|
|
|
'state',
|
|
|
|
'tenant_id'
|
|
|
|
]
|
2014-03-17 16:47:23 -06:00
|
|
|
notification_data = {name: getattr(self, name) for name in notification_fields}
|
2014-03-11 16:46:02 -06:00
|
|
|
return json.dumps(notification_data)
|