1 # Copyright (C) 2021-2022 Wind River Systems, Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
24 from o2ims.domain import commands
25 from o2ims.domain.alarm_obj import AlarmSubscription, AlarmEvent2SMO, \
28 from o2common.helper import o2logging
29 logger = o2logging.get_logger(__name__)
32 def notify_alarm_to_smo(
33 cmd: commands.PubAlarm2SMO,
34 uow: AbstractUnitOfWork,
35 notifications: AbstractNotifications,
37 logger.debug('In notify_alarm_to_smo')
40 alarm = uow.alarm_event_records.get(data.id)
42 logger.warning('Alarm Event {} does not exists.'.format(data.id))
45 subs = uow.alarm_subscriptions.list()
47 sub_data = sub.serialize()
48 logger.debug('Alarm Subscription: {}'.format(
49 sub_data['alarmSubscriptionId']))
51 if not sub_data.get('filter', None):
52 callback_smo(notifications, sub, data, alarm)
55 args = gen_orm_filter(AlarmEventRecord, sub_data['filter'])
58 'Alarm Subscription {} filter {} has wrong attribute '
59 'name or value. Ignore the filter'.format(
60 sub_data['alarmSubscriptionId'],
62 callback_smo(notifications, sub, data, alarm)
64 args.append(AlarmEventRecord.alarmEventRecordId == data.id)
65 ret = uow.alarm_event_records.list_with_count(*args)
68 'Alarm Event {} skip for subscription {} because of '
70 .format(data.id, sub_data['alarmSubscriptionId']))
72 callback_smo(notifications, sub, data, alarm)
75 def callback_smo(notifications: AbstractNotifications, sub: AlarmSubscription,
76 msg: AlarmEvent2SMO, alarm: AlarmEventRecord):
77 sub_data = sub.serialize()
78 alarm_data = alarm.serialize()
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'])
96 logger.info('callback URL: {}'.format(sub_data['callback']))
97 logger.debug('callback data: {}'.format(json.dumps(callback)))
99 return notifications.send(sub_data['callback'], callback)