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.
18 from urllib.parse import urlparse
20 # from o2common.config import config
21 from o2common.service.unit_of_work import AbstractUnitOfWork
22 from o2ims.domain import commands
23 from o2ims.domain.alarm_obj import AlarmSubscription, AlarmEvent2SMO
25 from o2common.service.command.handler import get_https_conn_default
26 from o2common.service.command.handler import get_http_conn
27 from o2common.service.command.handler import get_https_conn_selfsigned
28 from o2common.service.command.handler import post_data
29 from o2common.helper import o2logging
30 logger = o2logging.get_logger(__name__)
33 def notify_alarm_to_smo(
34 cmd: commands.PubAlarm2SMO,
35 uow: AbstractUnitOfWork,
37 logger.info('In notify_alarm_to_smo')
40 subs = uow.alarm_subscriptions.list()
42 sub_data = sub.serialize()
43 logger.debug('Alarm Subscription: {}'.format(
44 sub_data['alarmSubscriptionId']))
46 callback_smo(sub, data)
49 def callback_smo(sub: AlarmSubscription, msg: AlarmEvent2SMO):
50 sub_data = sub.serialize()
51 callback_data = json.dumps({
52 'consumerSubscriptionId': sub_data['consumerSubscriptionId'],
53 'notificationEventType': msg.notificationEventType,
54 'objectRef': msg.objectRef,
55 'updateTime': msg.updatetime
57 logger.info('URL: {}, data: {}'.format(
58 sub_data['callback'], callback_data))
59 o = urlparse(sub_data['callback'])
60 if o.scheme == 'https':
61 conn = get_https_conn_default(o.netloc)
63 conn = get_http_conn(o.netloc)
65 rst, status = post_data(conn, o.path, callback_data)
68 'Notify alarm to SMO successed with status: {}'.format(status))
70 logger.error('Notify alarm Response code is: {}'.format(status))
71 except ssl.SSLCertVerificationError as e:
73 'Notify alarm post data with trusted ca failed: {}'.format(e))
74 if 'self signed' in str(e):
75 conn = get_https_conn_selfsigned(o.netloc)
77 return post_data(conn, o.path, callback_data)
78 except Exception as e:
80 'Notify alarm with self-signed ca failed: {}'.format(e))
81 # TODO: write the status to extension db table.
84 except Exception as e:
85 logger.critical('Notify alarm except: {}'.format(e))