Fixed alarm related issues
[pti/o2.git] / o2ims / service / command / notify_alarm_handler.py
1 # Copyright (C) 2021-2022 Wind River Systems, Inc.
2 #
3 #  Licensed under the Apache License, Version 2.0 (the "License");
4 #  you may not use this file except in compliance with the License.
5 #  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
10 #  distributed under the License is distributed on an "AS IS" BASIS,
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #  See the License for the specific language governing permissions and
13 #  limitations under the License.
14
15 # import redis
16 # import requests
17 import json
18
19 from o2common.config import conf
20 from o2common.domain.filter import gen_orm_filter
21 from o2common.service.unit_of_work import AbstractUnitOfWork
22 from o2common.adapter.notifications import AbstractNotifications
23
24 from o2ims.domain import commands
25 from o2ims.domain.alarm_obj import AlarmSubscription, AlarmEvent2SMO, \
26     AlarmEventRecord
27
28 from o2common.helper import o2logging
29 logger = o2logging.get_logger(__name__)
30
31
32 def notify_alarm_to_smo(
33     cmd: commands.PubAlarm2SMO,
34     uow: AbstractUnitOfWork,
35     notifications: AbstractNotifications,
36 ):
37     logger.debug('In notify_alarm_to_smo')
38     data = cmd.data
39     with uow:
40         alarm = uow.alarm_event_records.get(data.id)
41         if alarm is None:
42             logger.warning('Alarm Event {} does not exists.'.format(data.id))
43             return
44
45         subs = uow.alarm_subscriptions.list()
46         for sub in subs:
47             sub_data = sub.serialize()
48             logger.debug('Alarm Subscription: {}'.format(
49                 sub_data['alarmSubscriptionId']))
50
51             if not sub_data.get('filter', None):
52                 callback_smo(notifications, sub, data, alarm)
53                 continue
54             try:
55                 args = gen_orm_filter(AlarmEventRecord, sub_data['filter'])
56             except KeyError:
57                 logger.warning(
58                     'Alarm Subscription {} filter {} has wrong attribute '
59                     'name or value. Ignore the filter'.format(
60                         sub_data['alarmSubscriptionId'],
61                         sub_data['filter']))
62                 callback_smo(notifications, sub, data, alarm)
63                 continue
64             args.append(AlarmEventRecord.alarmEventRecordId == data.id)
65             ret = uow.alarm_event_records.list_with_count(*args)
66             if ret[0] != 0:
67                 logger.debug(
68                     'Alarm Event {} skip for subscription {} because of '
69                     'the filter.'
70                     .format(data.id, sub_data['alarmSubscriptionId']))
71                 continue
72             callback_smo(notifications, sub, data, alarm)
73
74
75 def callback_smo(notifications: AbstractNotifications, sub: AlarmSubscription,
76                  msg: AlarmEvent2SMO, alarm: AlarmEventRecord):
77     sub_data = sub.serialize()
78     alarm_data = alarm.serialize()
79     callback = {
80         'globalCloudID': conf.DEFAULT.ocloud_global_id,
81         'consumerSubscriptionId': sub_data['consumerSubscriptionId'],
82         'notificationEventType': msg.notificationEventType,
83         'objectRef': msg.objectRef,
84         'alarmEventRecordId': alarm_data['alarmEventRecordId'],
85         'resourceTypeID': alarm_data['resourceTypeId'],
86         'resourceID': alarm_data['resourceId'],
87         'alarmDefinitionID': alarm_data['alarmDefinitionId'],
88         'probableCauseID': alarm_data['probableCauseId'],
89         'alarmRaisedTime': alarm_data['alarmRaisedTime'],
90         'alarmChangedTime': alarm_data['alarmChangedTime'],
91         'alarmAcknowledgeTime': alarm_data['alarmAcknowledgeTime'],
92         'alarmAcknowledged': alarm_data['alarmAcknowledged'],
93         'perceivedSeverity': alarm_data['perceivedSeverity'],
94         'extensions': json.loads(alarm_data['extensions'])
95     }
96     logger.info('callback URL: {}'.format(sub_data['callback']))
97     logger.debug('callback data: {}'.format(json.dumps(callback)))
98
99     return notifications.send(sub_data['callback'], callback)