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 urllib.parse import urlparse
21 from o2common.config import conf
22 from o2common.domain.filter import gen_orm_filter
23 from o2common.service.unit_of_work import AbstractUnitOfWork
24 from o2common.service.command.handler import get_https_conn_default
25 from o2common.service.command.handler import get_http_conn
26 from o2common.service.command.handler import get_https_conn_selfsigned
27 from o2common.service.command.handler import post_data
29 from o2ims.domain import commands
30 from o2ims.domain.alarm_obj import AlarmSubscription, AlarmEvent2SMO, \
33 from o2common.helper import o2logging
34 logger = o2logging.get_logger(__name__)
37 def notify_alarm_to_smo(
38 cmd: commands.PubAlarm2SMO,
39 uow: AbstractUnitOfWork,
41 logger.debug('In notify_alarm_to_smo')
44 alarm = uow.alarm_event_records.get(data.id)
46 logger.warning('Alarm Event {} does not exists.'.format(data.id))
49 subs = uow.alarm_subscriptions.list()
51 sub_data = sub.serialize()
52 logger.debug('Alarm Subscription: {}'.format(
53 sub_data['alarmSubscriptionId']))
55 if not sub_data.get('filter', None):
56 callback_smo(sub, data, alarm)
59 args = gen_orm_filter(AlarmEventRecord, sub_data['filter'])
62 'Alarm Subscription {} filter {} has wrong attribute '
63 'name or value. Ignore the filter'.format(
64 sub_data['alarmSubscriptionId'],
66 callback_smo(sub, data, alarm)
68 args.append(AlarmEventRecord.alarmEventRecordId == data.id)
69 ret = uow.alarm_event_records.list_with_count(*args)
72 'Alarm Event {} skip for subscription {} because of '
74 .format(data.id, sub_data['alarmSubscriptionId']))
76 callback_smo(sub, data, alarm)
79 def callback_smo(sub: AlarmSubscription, msg: AlarmEvent2SMO,
80 alarm: AlarmEventRecord):
81 sub_data = sub.serialize()
82 alarm_data = alarm.serialize()
84 'globalCloudID': conf.DEFAULT.ocloud_global_id,
85 'consumerSubscriptionId': sub_data['consumerSubscriptionId'],
86 'notificationEventType': msg.notificationEventType,
87 'objectRef': msg.objectRef,
88 'alarmEventRecordId': alarm_data['alarmEventRecordId'],
89 'resourceTypeID': alarm_data['resourceTypeId'],
90 'resourceID': alarm_data['resourceId'],
91 'alarmDefinitionID': alarm_data['alarmDefinitionId'],
92 'probableCauseID': alarm_data['probableCauseId'],
93 'alarmRaisedTime': alarm_data['alarmRaisedTime'],
94 'alarmChangedTime': alarm_data['alarmChangedTime'],
95 'alarmAcknowledgeTime': alarm_data['alarmAcknowledgeTime'],
96 'alarmAcknowledged': alarm_data['alarmAcknowledged'],
97 'perceivedSeverity': alarm_data['perceivedSeverity'],
98 'extensions': json.loads(alarm_data['extensions'])
100 # logger.warning(callback)
101 callback_data = json.dumps(callback)
102 logger.info('callback URL: {}'.format(sub_data['callback']))
103 logger.debug('callback data: {}'.format(callback_data))
105 o = urlparse(sub_data['callback'])
106 if o.scheme == 'https':
107 conn = get_https_conn_default(o.netloc)
109 conn = get_http_conn(o.netloc)
111 rst, status = post_data(conn, o.path, callback_data)
114 'Notify alarm to SMO successed with status: {}'.format(status))
116 logger.error('Notify alarm Response code is: {}'.format(status))
117 except ssl.SSLCertVerificationError as e:
119 'Notify alarm try to post data with trusted ca \
120 failed: {}'.format(e))
121 if 'self signed' in str(e):
122 conn = get_https_conn_selfsigned(o.netloc)
124 return post_data(conn, o.path, callback_data)
125 except Exception as e:
127 'Notify alarm with self-signed ca failed: {}'.format(e))
128 # TODO: write the status to extension db table.
131 except Exception as e:
132 logger.critical('Notify alarm except: {}'.format(e))