# Path: monasca_notification/common/repositories/orm/models.py
# ALARM_STATES = ('UNDETERMINED', 'OK', 'ALARM')
# def create_alarm_action_model(metadata=None):
# def create_notification_method_model(metadata=None):
# def create_notification_method_type_model(metadata=None):
# def create_alarm_model(metadata=None):


from oslo_config import cfg
from oslo_log import log as logging
from sqlalchemy import engine_from_config, MetaData
from sqlalchemy.sql import select, bindparam, and_, insert
from sqlalchemy.exc import DatabaseError
from monasca_notification.common.repositories import exceptions as exc
from monasca_notification.common.repositories.orm import models
CONF = cfg.CONF


class OrmRepo(object):
    def __init__(self):
        self._orm_engine = engine_from_config({
            'url': CONF.orm.url
        }, prefix='')

        metadata = MetaData()

        aa = models.create_alarm_action_model(metadata).alias('aa')
        nm = models.create_notification_method_model(metadata).alias('nm')
        nmt_insert = models.create_notification_method_type_model(metadata)
        nmt = nmt_insert.alias('nmt')
        a = models.create_alarm_model(metadata).alias('a')

        self._orm_query = select([nm.c.id, nm.c.type, nm.c.name, nm.c.address, nm.c.period])\
            .select_from(aa.join(nm, aa.c.action_id == nm.c.id))\
            .where(
                and_(aa.c.alarm_definition_id == bindparam('alarm_definition_id'),
                     aa.c.alarm_state == bindparam('alarm_state')))

        self._orm_get_alarm_state = select([a.c.state]).where(a.c.id == bindparam('alarm_id'))

        self._orm_nmt_query = select([nmt.c.name])

        self._orm_get_notification = select([nm.c.name, nm.c.type, nm.c.address, nm.c.period])\
            .where(nm.c.id == bindparam('notification_id'))


# Path: monasca_notification/common/repositories/orm/models.py
# ALARM_STATES = ('UNDETERMINED', 'OK', 'ALARM')
# def create_alarm_action_model(metadata=None):
# def create_notification_method_model(metadata=None):
# def create_notification_method_type_model(metadata=None):
# def create_alarm_model(metadata=None):


# Path: monasca_notification/common/repositories/orm/orm_repo.py
from oslo_config import cfg
from oslo_log import log as logging
from sqlalchemy import engine_from_config, MetaData
from sqlalchemy.sql import select, bindparam, and_, insert
from sqlalchemy.exc import DatabaseError
from monasca_notification.common.repositories import exceptions as exc
from monasca_notification.common.repositories.orm import models

CONF = cfg.CONF


class OrmRepo(object):
    def __init__(self):
        self._orm_engine = engine_from_config({
            'url': CONF.orm.url
        }, prefix='')

        metadata = MetaData()

        aa = models.create_alarm_action_model(metadata).alias('aa')
        nm = models.create_notification_method_model(metadata).alias('nm')
        nmt_insert = models.create_notification_method_type_model(metadata)
        nmt = nmt_insert.alias('nmt')
        a = models.create_alarm_model(metadata).alias('a')

        self._orm_query = select([nm.c.id, nm.c.type, nm.c.name, nm.c.address, nm.c.period])\
            .select_from(aa.join(nm, aa.c.action_id == nm.c.id))\
            .where(
                and_(aa.c.alarm_definition_id == bindparam('alarm_definition_id'),
                     aa.c.alarm_state == bindparam('alarm_state')))

        self._orm_get_alarm_state = select([a.c.state]).where(a.c.id == bindparam('alarm_id'))

        self._orm_nmt_query = select([nmt.c.name])

        self._orm_get_notification = select([nm.c.name, nm.c.type, nm.c.address, nm.c.period])\
            .where(nm.c.id == bindparam('notification_id'))


        self._orm_add_notification_type = insert(nmt_insert).values(name=bindparam('b_name'))