# Path: monasca_notification/notification.py
# class Notification(object):
#     """An abstract base class used to define the notification interface
#        and common functions
#     """
#     __slots__ = (
#         'address',
#         'alarm_id',
#         'alarm_name',
#         'alarm_timestamp',
#         'id',
#         'message',
#         'name',
#         'notification_timestamp',
#         'state',
#         'severity',
#         'link',
#         'lifecycle_state',
#         'tenant_id',
#         'type',
#         'metrics',
#         'retry_count',
#         'raw_alarm',
#         'period',
#     )
# 
#     def __init__(self, id, type, name, address, period, retry_count, alarm):
#         """Setup the notification object
#              id - The notification id
#              type - The notification type
#              name - Name used in sending
#              address - where to send the notification
#              period - period of sending the notification
#              retry_count - number of times we've tried to send
#              alarm - info that caused the notification
#              notifications that come after this one to remain uncommitted.
#              Note that data may include six.text_type strings.
#         """
#         self.id = id
#         self.address = address
#         self.name = name
#         self.type = type
#         self.retry_count = retry_count
# 
#         self.raw_alarm = alarm
# 
#         self.alarm_id = alarm['alarmId']
#         self.alarm_name = alarm['alarmName']
#         # The event timestamp is in milliseconds
#         self.alarm_timestamp = int(alarm['timestamp'] / 1000.0)
#         self.message = alarm['stateChangeReason']
#         self.state = alarm['newState']
#         self.severity = alarm['severity']
#         self.link = alarm['link']
#         self.lifecycle_state = alarm['lifecycleState']
#         self.tenant_id = alarm['tenantId']
#         self.metrics = alarm['metrics']
# 
#         # to be updated on actual notification send time
#         self.notification_timestamp = None
# 
#         self.period = period
# 
#     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
# 
#     def __ne__(self, other):
#         return not self.__eq__(other)
# 
#     def to_json(self):
#         """Return json representation
#         """
#         notification_fields = [
#             'id',
#             'type',
#             'name',
#             'address',
#             'retry_count',
#             'raw_alarm',
#             'alarm_id',
#             'alarm_name',
#             'alarm_timestamp',
#             'message',
#             'notification_timestamp',
#             'state',
#             'severity',
#             'link',
#             'lifecycle_state',
#             'tenant_id',
#             'period',
#         ]
#         notification_data = {name: getattr(self, name)
#                              for name in notification_fields}
#         return json.dumps(notification_data)


import monascastatsd
from keystoneauth1 import exceptions as kaexception
from keystoneauth1 import loading as kaloading
from oslo_config import cfg
from oslo_log import log
from monasca_notification.common.repositories import exceptions
from monasca_notification.notification import Notification
            notification.name = stored_notification[0]
            notification.type = stored_notification[1]
            notification.address = stored_notification[2]
            notification.period = stored_notification[3]
            return notification
    except exceptions.DatabaseException:
        LOG.warn("Error querying mysql for notification method. "
                 "Using currently cached method.")
        return notification
    except Exception as e:
        LOG.warn("Error when attempting to construct notification {0}".format(e))
        return None


def grab_stored_notification_method(db_repo, notification_id):
    try:
        stored_notification = db_repo.get_notification(notification_id)
    except exceptions.DatabaseException:
        LOG.debug('Database Error.  Attempting reconnect')
        stored_notification = db_repo.get_notification(notification_id)

    return stored_notification


def get_statsd_client(dimensions=None):
    local_dims = dimensions.copy() if dimensions else {}
    local_dims.update(NOTIFICATION_DIMENSIONS)
    if CONF.statsd.enable:
        LOG.debug("Establishing connection with statsd on {0}:{1}"
                  .format(CONF.statsd.host, CONF.statsd.port))

# Path: monasca_notification/notification.py
# class Notification(object):
#     """An abstract base class used to define the notification interface
#        and common functions
#     """
#     __slots__ = (
#         'address',
#         'alarm_id',
#         'alarm_name',
#         'alarm_timestamp',
#         'id',
#         'message',
#         'name',
#         'notification_timestamp',
#         'state',
#         'severity',
#         'link',
#         'lifecycle_state',
#         'tenant_id',
#         'type',
#         'metrics',
#         'retry_count',
#         'raw_alarm',
#         'period',
#     )
# 
#     def __init__(self, id, type, name, address, period, retry_count, alarm):
#         """Setup the notification object
#              id - The notification id
#              type - The notification type
#              name - Name used in sending
#              address - where to send the notification
#              period - period of sending the notification
#              retry_count - number of times we've tried to send
#              alarm - info that caused the notification
#              notifications that come after this one to remain uncommitted.
#              Note that data may include six.text_type strings.
#         """
#         self.id = id
#         self.address = address
#         self.name = name
#         self.type = type
#         self.retry_count = retry_count
# 
#         self.raw_alarm = alarm
# 
#         self.alarm_id = alarm['alarmId']
#         self.alarm_name = alarm['alarmName']
#         # The event timestamp is in milliseconds
#         self.alarm_timestamp = int(alarm['timestamp'] / 1000.0)
#         self.message = alarm['stateChangeReason']
#         self.state = alarm['newState']
#         self.severity = alarm['severity']
#         self.link = alarm['link']
#         self.lifecycle_state = alarm['lifecycleState']
#         self.tenant_id = alarm['tenantId']
#         self.metrics = alarm['metrics']
# 
#         # to be updated on actual notification send time
#         self.notification_timestamp = None
# 
#         self.period = period
# 
#     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
# 
#     def __ne__(self, other):
#         return not self.__eq__(other)
# 
#     def to_json(self):
#         """Return json representation
#         """
#         notification_fields = [
#             'id',
#             'type',
#             'name',
#             'address',
#             'retry_count',
#             'raw_alarm',
#             'alarm_id',
#             'alarm_name',
#             'alarm_timestamp',
#             'message',
#             'notification_timestamp',
#             'state',
#             'severity',
#             'link',
#             'lifecycle_state',
#             'tenant_id',
#             'period',
#         ]
#         notification_data = {name: getattr(self, name)
#                              for name in notification_fields}
#         return json.dumps(notification_data)


# Path: monasca_notification/common/utils.py
import monascastatsd
from keystoneauth1 import exceptions as kaexception
from keystoneauth1 import loading as kaloading
from oslo_config import cfg
from oslo_log import log
from monasca_notification.common.repositories import exceptions
from monasca_notification.notification import Notification

            notification.name = stored_notification[0]
            notification.type = stored_notification[1]
            notification.address = stored_notification[2]
            notification.period = stored_notification[3]
            return notification
    except exceptions.DatabaseException:
        LOG.warn("Error querying mysql for notification method. "
                 "Using currently cached method.")
        return notification
    except Exception as e:
        LOG.warn("Error when attempting to construct notification {0}".format(e))
        return None


def grab_stored_notification_method(db_repo, notification_id):
    try:
        stored_notification = db_repo.get_notification(notification_id)
    except exceptions.DatabaseException:
        LOG.debug('Database Error.  Attempting reconnect')
        stored_notification = db_repo.get_notification(notification_id)

    return stored_notification


def get_statsd_client(dimensions=None):
    local_dims = dimensions.copy() if dimensions else {}
    local_dims.update(NOTIFICATION_DIMENSIONS)
    if CONF.statsd.enable:
        LOG.debug("Establishing connection with statsd on {0}:{1}"
                  .format(CONF.statsd.host, CONF.statsd.port))

        client = monascastatsd.Client(name='monasca',