# Path: monasca_notification/common/utils.py
# def construct_notification_object(db_repo, notification_json):
#     try:
#         notification = Notification(notification_json['id'],
#                                     notification_json['type'],
#                                     notification_json['name'],
#                                     notification_json['address'],
#                                     notification_json['period'],
#                                     notification_json['retry_count'],
#                                     notification_json['raw_alarm'])
#         # Grab notification method from database to see if it was changed
#         stored_notification = grab_stored_notification_method(db_repo, notification.id)
#         # Notification method was deleted
#         if stored_notification is None:
#             LOG.debug("Notification method {0} was deleted from database. "
#                       "Will stop sending.".format(notification.id))
#             return None
#         # Update notification method with most up to date values
#         else:
#             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
# 
# Path: monasca_notification/common/utils.py
# def get_db_repo():
#     repo_driver = CONF.database.repo_driver
#     LOG.debug('Enabling the %s RDB repository', repo_driver)
#     return repo_driver(CONF)
# 
# Path: monasca_notification/common/utils.py
# 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',
#                                       host=CONF.statsd.host,
#                                       port=CONF.statsd.port,
#                                       dimensions=local_dims)
#     else:
#         LOG.warn("StatsD monitoring disabled. Overriding monascastatsd.Client to use it offline")
#         client = OfflineClient(name='monasca',
#                                host=CONF.statsd.host,
#                                port=CONF.statsd.port,
#                                dimensions=local_dims)
#     return client
# 
# Path: monasca_notification/processors/notification_processor.py
# class NotificationProcessor(object):
#     def __init__(self):
#     def _remaining_plugin_types(self):
#     def insert_configured_plugins(self):
#     def send(self, notifications):


import time
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils
from monasca_common.kafka import client_factory
from monasca_notification.common.repositories import exceptions
from monasca_notification.common.utils import construct_notification_object
from monasca_notification.common.utils import get_db_repo
from monasca_notification.common.utils import get_statsd_client
from monasca_notification.processors import notification_processor
log = logging.getLogger(__name__)
CONF = cfg.CONF


class PeriodicEngine(object):
    def __init__(self, period):
        self._topic_name = CONF.kafka.periodic[str(period)]

        self._statsd = get_statsd_client()

        self._consumer = client_factory.get_kafka_consumer(
            CONF.kafka.url,
            CONF.kafka.group,
            self._topic_name,
            CONF.zookeeper.url,
            CONF.zookeeper.periodic_path[str(period)],
            CONF.kafka.legacy_kafka_client_enabled)
        self._producer = client_factory.get_kafka_producer(
            CONF.kafka.url,
            CONF.kafka.legacy_kafka_client_enabled)

        self._notifier = notification_processor.NotificationProcessor()
        self._db_repo = get_db_repo()
        self._period = period

    def _keep_sending(self, alarm_id, original_state, period):
        try:
            current_state = self._db_repo.get_alarm_current_state(alarm_id)
        except exceptions.DatabaseException:
            log.debug('Database Error.  Attempting reconnect')

# Path: monasca_notification/common/utils.py
# def construct_notification_object(db_repo, notification_json):
#     try:
#         notification = Notification(notification_json['id'],
#                                     notification_json['type'],
#                                     notification_json['name'],
#                                     notification_json['address'],
#                                     notification_json['period'],
#                                     notification_json['retry_count'],
#                                     notification_json['raw_alarm'])
#         # Grab notification method from database to see if it was changed
#         stored_notification = grab_stored_notification_method(db_repo, notification.id)
#         # Notification method was deleted
#         if stored_notification is None:
#             LOG.debug("Notification method {0} was deleted from database. "
#                       "Will stop sending.".format(notification.id))
#             return None
#         # Update notification method with most up to date values
#         else:
#             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
# 
# Path: monasca_notification/common/utils.py
# def get_db_repo():
#     repo_driver = CONF.database.repo_driver
#     LOG.debug('Enabling the %s RDB repository', repo_driver)
#     return repo_driver(CONF)
# 
# Path: monasca_notification/common/utils.py
# 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',
#                                       host=CONF.statsd.host,
#                                       port=CONF.statsd.port,
#                                       dimensions=local_dims)
#     else:
#         LOG.warn("StatsD monitoring disabled. Overriding monascastatsd.Client to use it offline")
#         client = OfflineClient(name='monasca',
#                                host=CONF.statsd.host,
#                                port=CONF.statsd.port,
#                                dimensions=local_dims)
#     return client
# 
# Path: monasca_notification/processors/notification_processor.py
# class NotificationProcessor(object):
#     def __init__(self):
#     def _remaining_plugin_types(self):
#     def insert_configured_plugins(self):
#     def send(self, notifications):


# Path: monasca_notification/periodic_engine.py
import time
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils
from monasca_common.kafka import client_factory
from monasca_notification.common.repositories import exceptions
from monasca_notification.common.utils import construct_notification_object
from monasca_notification.common.utils import get_db_repo
from monasca_notification.common.utils import get_statsd_client
from monasca_notification.processors import notification_processor

log = logging.getLogger(__name__)
CONF = cfg.CONF


class PeriodicEngine(object):
    def __init__(self, period):
        self._topic_name = CONF.kafka.periodic[str(period)]

        self._statsd = get_statsd_client()

        self._consumer = client_factory.get_kafka_consumer(
            CONF.kafka.url,
            CONF.kafka.group,
            self._topic_name,
            CONF.zookeeper.url,
            CONF.zookeeper.periodic_path[str(period)],
            CONF.kafka.legacy_kafka_client_enabled)
        self._producer = client_factory.get_kafka_producer(
            CONF.kafka.url,
            CONF.kafka.legacy_kafka_client_enabled)

        self._notifier = notification_processor.NotificationProcessor()
        self._db_repo = get_db_repo()
        self._period = period

    def _keep_sending(self, alarm_id, original_state, period):
        try:
            current_state = self._db_repo.get_alarm_current_state(alarm_id)
        except exceptions.DatabaseException:
            log.debug('Database Error.  Attempting reconnect')

            current_state = self._db_repo.get_alarm_current_state(alarm_id)