b4e22a142563eb8b801211be4bd13e053dac114b
[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 json
16 # import redis
17 # import requests
18 from urllib.parse import urlparse
19
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
24 import ssl
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__)
31
32
33 def notify_alarm_to_smo(
34     cmd: commands.PubAlarm2SMO,
35     uow: AbstractUnitOfWork,
36 ):
37     logger.info('In notify_alarm_to_smo')
38     data = cmd.data
39     with uow:
40         subs = uow.alarm_subscriptions.list()
41         for sub in subs:
42             sub_data = sub.serialize()
43             logger.debug('Alarm Subscription: {}'.format(
44                 sub_data['alarmSubscriptionId']))
45
46             callback_smo(sub, data)
47
48
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
56     })
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)
62     else:
63         conn = get_http_conn(o.netloc)
64     try:
65         rst, status = post_data(conn, o.path, callback_data)
66         if rst is True:
67             logger.info(
68                 'Notify alarm to SMO successed with status: {}'.format(status))
69             return
70         logger.error('Notify alarm Response code is: {}'.format(status))
71     except ssl.SSLCertVerificationError as e:
72         logger.debug(
73             'Notify alarm try to post data with trusted ca \
74                 failed: {}'.format(e))
75         if 'self signed' in str(e):
76             conn = get_https_conn_selfsigned(o.netloc)
77             try:
78                 return post_data(conn, o.path, callback_data)
79             except Exception as e:
80                 logger.info(
81                     'Notify alarm with self-signed ca failed: {}'.format(e))
82                 # TODO: write the status to extension db table.
83                 return False
84         return False
85     except Exception as e:
86         logger.critical('Notify alarm except: {}'.format(e))
87         return False