Notification Engine for Monasca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

orm_repo.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright 2015-2017 FUJITSU LIMITED
  2. # (C) Copyright 2015,2016 Hewlett Packard Enterprise Development LP
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. # in compliance with the License. You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software distributed under the License
  10. # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. # or implied. See the License for the specific language governing permissions and limitations under
  12. # the License.
  13. from oslo_config import cfg
  14. from oslo_log import log as logging
  15. from sqlalchemy import engine_from_config, MetaData
  16. from sqlalchemy.sql import select, bindparam, and_, insert
  17. from sqlalchemy.exc import DatabaseError
  18. from monasca_notification.common.repositories import exceptions as exc
  19. from monasca_notification.common.repositories.orm import models
  20. LOG = logging.getLogger(__name__)
  21. CONF = cfg.CONF
  22. class OrmRepo(object):
  23. def __init__(self):
  24. self._orm_engine = engine_from_config({
  25. 'url': CONF.orm.url
  26. }, prefix='')
  27. metadata = MetaData()
  28. aa = models.create_alarm_action_model(metadata).alias('aa')
  29. nm = models.create_notification_method_model(metadata).alias('nm')
  30. nmt_insert = models.create_notification_method_type_model(metadata)
  31. nmt = nmt_insert.alias('nmt')
  32. a = models.create_alarm_model(metadata).alias('a')
  33. self._orm_query = select([nm.c.id, nm.c.type, nm.c.name, nm.c.address, nm.c.period])\
  34. .select_from(aa.join(nm, aa.c.action_id == nm.c.id))\
  35. .where(
  36. and_(aa.c.alarm_definition_id == bindparam('alarm_definition_id'),
  37. aa.c.alarm_state == bindparam('alarm_state')))
  38. self._orm_get_alarm_state = select([a.c.state]).where(a.c.id == bindparam('alarm_id'))
  39. self._orm_nmt_query = select([nmt.c.name])
  40. self._orm_get_notification = select([nm.c.name, nm.c.type, nm.c.address, nm.c.period])\
  41. .where(nm.c.id == bindparam('notification_id'))
  42. self._orm_add_notification_type = insert(nmt_insert).values(name=bindparam('b_name'))
  43. self._orm = None
  44. def fetch_notifications(self, alarm):
  45. try:
  46. with self._orm_engine.connect() as conn:
  47. LOG.debug('Orm query {%s}', str(self._orm_query))
  48. notifications = conn.execute(self._orm_query,
  49. alarm_definition_id=alarm['alarmDefinitionId'],
  50. alarm_state=alarm['newState'])
  51. return [(row[0], row[1].lower(), row[2], row[3], row[4]) for row in notifications]
  52. except DatabaseError as e:
  53. LOG.exception("Couldn't fetch alarms actions %s", e)
  54. raise exc.DatabaseException(e)
  55. def get_alarm_current_state(self, alarm_id):
  56. try:
  57. with self._orm_engine.connect() as conn:
  58. LOG.debug('Orm query {%s}', str(self._orm_get_alarm_state))
  59. result = conn.execute(self._orm_get_alarm_state,
  60. alarm_id=alarm_id)
  61. row = result.fetchone()
  62. state = row[0] if row is not None else None
  63. return state
  64. except DatabaseError as e:
  65. LOG.exception("Couldn't fetch the current alarm state %s", e)
  66. raise exc.DatabaseException(e)
  67. def fetch_notification_method_types(self):
  68. try:
  69. with self._orm_engine.connect() as conn:
  70. LOG.debug('Orm query {%s}', str(self._orm_nmt_query))
  71. notification_method_types = conn.execute(self._orm_nmt_query).fetchall()
  72. return [row[0] for row in notification_method_types]
  73. except DatabaseError as e:
  74. LOG.exception("Couldn't fetch notification method types %s", e)
  75. raise exc.DatabaseException(e)
  76. def insert_notification_method_types(self, notification_types):
  77. # This function can be called by multiple processes at same time.
  78. # In that case, only the first one will succeed and the others will fail.
  79. # We can ignore this error when the types have already been registered.
  80. try:
  81. with self._orm_engine.connect() as conn:
  82. for notification_type in notification_types:
  83. conn.execute(self._orm_add_notification_type, b_name=notification_type)
  84. except DatabaseError as e:
  85. LOG.debug("Failed to insert notification types %s", notification_types)
  86. raise exc.DatabaseException(e)
  87. def get_notification(self, notification_id):
  88. try:
  89. with self._orm_engine.connect() as conn:
  90. LOG.debug('Orm query {%s}', str(self._orm_get_notification))
  91. result = conn.execute(self._orm_get_notification,
  92. notification_id=notification_id)
  93. notification = result.fetchone()
  94. if notification is None:
  95. return None
  96. else:
  97. return [
  98. notification[0],
  99. notification[1].lower(),
  100. notification[2],
  101. notification[3]]
  102. except DatabaseError as e:
  103. LOG.exception("Couldn't fetch the notification method %s", e)
  104. raise exc.DatabaseException(e)