X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=o2ims%2Fservice%2Fcommand%2Fnotify_alarm_handler.py;h=3d44e64843f000d926d687e6a006c1ac0540bc87;hb=e1c1a75b2ca93f9d05a9431093554002efff994d;hp=b2ca61c209ff67b14e76f6ed9aca8c10c40f010a;hpb=d2f6cc674bf3623caf114a8d7709e70d55ec9340;p=pti%2Fo2.git diff --git a/o2ims/service/command/notify_alarm_handler.py b/o2ims/service/command/notify_alarm_handler.py index b2ca61c..3d44e64 100644 --- a/o2ims/service/command/notify_alarm_handler.py +++ b/o2ims/service/command/notify_alarm_handler.py @@ -1,4 +1,4 @@ -# Copyright (C) 2021 Wind River Systems, Inc. +# Copyright (C) 2021-2022 Wind River Systems, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,16 +12,23 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json # import redis # import requests -import http.client +import ssl +import json from urllib.parse import urlparse -# from o2common.config import config +from o2common.config import conf +from o2common.domain.filter import gen_orm_filter from o2common.service.unit_of_work import AbstractUnitOfWork +from o2common.service.command.handler import get_https_conn_default +from o2common.service.command.handler import get_http_conn +from o2common.service.command.handler import get_https_conn_selfsigned +from o2common.service.command.handler import post_data + from o2ims.domain import commands -from o2ims.domain.alarm_obj import AlarmSubscription, AlarmEvent2SMO +from o2ims.domain.alarm_obj import AlarmSubscription, AlarmEvent2SMO, \ + AlarmEventRecord from o2common.helper import o2logging logger = o2logging.get_logger(__name__) @@ -31,37 +38,96 @@ def notify_alarm_to_smo( cmd: commands.PubAlarm2SMO, uow: AbstractUnitOfWork, ): - logger.info('In notify_alarm_to_smo') + logger.debug('In notify_alarm_to_smo') data = cmd.data with uow: + alarm = uow.alarm_event_records.get(data.id) + if alarm is None: + logger.warning('Alarm Event {} does not exists.'.format(data.id)) + return + subs = uow.alarm_subscriptions.list() for sub in subs: sub_data = sub.serialize() logger.debug('Alarm Subscription: {}'.format( sub_data['alarmSubscriptionId'])) - callback_smo(sub, data) + if not sub_data.get('filter', None): + callback_smo(sub, data, alarm) + continue + try: + args = gen_orm_filter(AlarmEventRecord, sub_data['filter']) + except KeyError: + logger.warning( + 'Alarm Subscription {} filter {} has wrong attribute ' + 'name or value. Ignore the filter'.format( + sub_data['alarmSubscriptionId'], + sub_data['filter'])) + callback_smo(sub, data, alarm) + continue + args.append(AlarmEventRecord.alarmEventRecordId == data.id) + ret = uow.alarm_event_records.list_with_count(*args) + if ret[0] != 0: + logger.debug( + 'Alarm Event {} skip for subscription {} because of ' + 'the filter.' + .format(data.id, sub_data['alarmSubscriptionId'])) + continue + callback_smo(sub, data, alarm) -def callback_smo(sub: AlarmSubscription, msg: AlarmEvent2SMO): +def callback_smo(sub: AlarmSubscription, msg: AlarmEvent2SMO, + alarm: AlarmEventRecord): sub_data = sub.serialize() - callback_data = json.dumps({ + alarm_data = alarm.serialize() + callback = { + 'globalCloudID': conf.DEFAULT.ocloud_global_id, 'consumerSubscriptionId': sub_data['consumerSubscriptionId'], 'notificationEventType': msg.notificationEventType, 'objectRef': msg.objectRef, - 'updateTime': msg.updatetime - }) - logger.info('URL: {}, data: {}'.format( - sub_data['callback'], callback_data)) + 'alarmEventRecordId': alarm_data['alarmEventRecordId'], + 'resourceTypeID': alarm_data['resourceTypeId'], + 'resourceID': alarm_data['resourceId'], + 'alarmDefinitionID': alarm_data['alarmDefinitionId'], + 'probableCauseID': alarm_data['probableCauseId'], + 'alarmRaisedTime': alarm_data['alarmRaisedTime'], + 'alarmChangedTime': alarm_data['alarmChangedTime'], + 'alarmAcknowledgeTime': alarm_data['alarmAcknowledgeTime'], + 'alarmAcknowledged': alarm_data['alarmAcknowledged'], + 'perceivedSeverity': alarm_data['perceivedSeverity'], + 'extensions': json.loads(alarm_data['extensions']) + } + # logger.warning(callback) + callback_data = json.dumps(callback) + logger.info('callback URL: {}'.format(sub_data['callback'])) + logger.debug('callback data: {}'.format(callback_data)) + o = urlparse(sub_data['callback']) - conn = http.client.HTTPConnection(o.netloc) - headers = {'Content-type': 'application/json'} - conn.request('POST', o.path, callback_data, headers) - resp = conn.getresponse() - data = resp.read().decode('utf-8') - # json_data = json.loads(data) - if resp.status == 202 or resp.status == 200: - logger.info('Notify to SMO successed, response code {} {}, data {}'. - format(resp.status, resp.reason, data)) - return - logger.error('Response code is: {}'.format(resp.status)) + if o.scheme == 'https': + conn = get_https_conn_default(o.netloc) + else: + conn = get_http_conn(o.netloc) + try: + rst, status = post_data(conn, o.path, callback_data) + if rst is True: + logger.info( + 'Notify alarm to SMO successed with status: {}'.format(status)) + return + logger.error('Notify alarm Response code is: {}'.format(status)) + except ssl.SSLCertVerificationError as e: + logger.debug( + 'Notify alarm try to post data with trusted ca \ + failed: {}'.format(e)) + if 'self signed' in str(e): + conn = get_https_conn_selfsigned(o.netloc) + try: + return post_data(conn, o.path, callback_data) + except Exception as e: + logger.info( + 'Notify alarm with self-signed ca failed: {}'.format(e)) + # TODO: write the status to extension db table. + return False + return False + except Exception as e: + logger.critical('Notify alarm except: {}'.format(e)) + return False