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 config
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 subs = uow.alarm_subscriptions.list()
46 sub_data = sub.serialize()
47 logger.debug('Alarm Subscription: {}'.format(
48 sub_data['alarmSubscriptionId']))
50 alarm = uow.alarm_event_records.get(data.id)
52 logger.debug('Alarm Event {} does not exists.'.format(data.id))
54 if sub_data.get('filter', None):
56 args = gen_orm_filter(AlarmEventRecord, sub_data['filter'])
59 'Alarm Subscription {} filter {} has wrong attribute '
60 'name or value. Ignore the filter'.format(
61 sub_data['alarmSubscriptionId'],
63 callback_smo(sub, data)
65 args.append(AlarmEventRecord.alarmEventRecordId == data.id)
66 ret = uow.alarm_event_records.list_with_count(*args)
69 'Alarm Event {} skip for subscription {} because of '
71 .format(data.id, sub_data['alarmSubscriptionId']))
74 callback_smo(sub, data)
77 def callback_smo(sub: AlarmSubscription, msg: AlarmEvent2SMO):
78 sub_data = sub.serialize()
79 callback_data = json.dumps({
80 'consumerSubscriptionId': sub_data['consumerSubscriptionId'],
81 'notificationEventType': msg.notificationEventType,
82 'objectRef': msg.objectRef,
83 'updateTime': msg.updatetime
85 logger.info('URL: {}, data: {}'.format(
86 sub_data['callback'], callback_data))
87 o = urlparse(sub_data['callback'])
88 if o.scheme == 'https':
89 conn = get_https_conn_default(o.netloc)
91 conn = get_http_conn(o.netloc)
93 rst, status = post_data(conn, o.path, callback_data)
96 'Notify alarm to SMO successed with status: {}'.format(status))
98 logger.error('Notify alarm Response code is: {}'.format(status))
99 except ssl.SSLCertVerificationError as e:
101 'Notify alarm try to post data with trusted ca \
102 failed: {}'.format(e))
103 if 'self signed' in str(e):
104 conn = get_https_conn_selfsigned(o.netloc)
106 return post_data(conn, o.path, callback_data)
107 except Exception as e:
109 'Notify alarm with self-signed ca failed: {}'.format(e))
110 # TODO: write the status to extension db table.
113 except Exception as e:
114 logger.critical('Notify alarm except: {}'.format(e))