From b1427ab2a1c9c2a8d89d2082baa8a51453277096 Mon Sep 17 00:00:00 2001 From: "Zhang Rong(Jon)" Date: Fri, 25 Nov 2022 18:23:55 +0800 Subject: [PATCH] Fix INF-383 refactor to enrich alarmEventRecord with extension field Issue-ID: INF-383 Signed-off-by: Zhang Rong(Jon) Change-Id: Ic58afa0bad1db3aa66d0afb16d471a8c085ae9ab --- o2ims/adapter/clients/fault_client.py | 20 ++++++++++++++++++++ o2ims/adapter/orm.py | 1 + o2ims/domain/alarm_obj.py | 13 +++++++++---- o2ims/service/auditor/alarm_handler.py | 19 +++++++++++-------- o2ims/service/command/notify_alarm_handler.py | 2 +- o2ims/views/alarm_dto.py | 3 ++- 6 files changed, 44 insertions(+), 14 deletions(-) diff --git a/o2ims/adapter/clients/fault_client.py b/o2ims/adapter/clients/fault_client.py index 1c228e0..53602b4 100644 --- a/o2ims/adapter/clients/fault_client.py +++ b/o2ims/adapter/clients/fault_client.py @@ -221,6 +221,16 @@ class StxFaultClientImp(object): @ staticmethod def _alarmconverter(alarm): + selected_keys = [ + 'alarm_id', 'alarm_state', 'entity_type_id', 'entity_instance_id', + 'reason_text', 'alarm_type', 'probable_cause', + 'proposed_repair_action', 'service_affecting', 'suppression', + 'suppression_status', 'mgmt_affecting', 'degrade_affecting' + ] + content = alarm.to_dict() + filtered = dict( + filter(lambda item: item[0] in selected_keys, content.items())) + setattr(alarm, 'filtered', filtered) # setattr(alarm, 'alarm_def_id', uuid.uuid3( # uuid.NAMESPACE_URL, alarm.alarm_id)) setattr(alarm, 'state', alarm.alarm_state) @@ -233,6 +243,16 @@ class StxFaultClientImp(object): @ staticmethod def _eventconverter(event, clear=False): + selected_keys = [ + 'event_log_id', 'state', 'entity_type_id', + 'entity_instance_id', 'reason_text', 'event_log_type', + 'probable_cause', 'proposed_repair_action', + 'service_affecting', 'suppression', 'suppression_status' + ] + content = event.to_dict() + filtered = dict( + filter(lambda item: item[0] in selected_keys, content.items())) + setattr(event, 'filtered', filtered) setattr(event, 'alarm_id', event.event_log_id) setattr(event, 'alarm_type', event.event_log_type) if clear: diff --git a/o2ims/adapter/orm.py b/o2ims/adapter/orm.py index 3c0440b..e47707c 100644 --- a/o2ims/adapter/orm.py +++ b/o2ims/adapter/orm.py @@ -216,6 +216,7 @@ alarm_event_record = Table( Column("alarmChangedTime", String(255)), Column("alarmAcknowledgeTime", String(255)), Column("alarmAcknowledged", String(255)), + Column("extensions", String()) ) alarm_probable_cause = Table( diff --git a/o2ims/domain/alarm_obj.py b/o2ims/domain/alarm_obj.py index cd653f8..050e7b0 100644 --- a/o2ims/domain/alarm_obj.py +++ b/o2ims/domain/alarm_obj.py @@ -45,8 +45,13 @@ class FaultGenericModel(AgRoot): # elif hasattr(api_response, 'event_log_id'): # self.alarm_id = api_response.event_log_id - self.hash = content_hash if content_hash \ - else str(hash((self.id, self.timestamp, self.status))) + self.hash = content_hash + if not self.hash: + if hasattr(api_response, 'filtered'): + self.filtered = api_response.filtered + self.hash = str(hash((self.id, str(self.filtered)))) + else: + self.hash = str(hash((self.id, self.updatetime))) self.content = json.dumps(api_response.to_dict()) if EventTypeEnum.ALARM == type: pass @@ -98,7 +103,7 @@ class AlarmEventRecord(AgRoot, Serializer): self.alarmChangedTime = '' self.alarmAcknowledgeTime = '' self.alarmAcknowledged = False - self.extensions = [] + self.extensions = '' class ProbableCause(AgRoot, Serializer): @@ -205,4 +210,4 @@ class AlarmEventNotification(AgRoot, Serializer): self.alarmChangedTime = alarm.alarmChangedTime self.alarmAcknowledgeTime = alarm.alarmAcknowledgeTime self.alarmAcknowledged = alarm.alarmAcknowledged - self.extensions = [] + self.extensions = alarm.extensions diff --git a/o2ims/service/auditor/alarm_handler.py b/o2ims/service/auditor/alarm_handler.py index 305e919..4c7c054 100644 --- a/o2ims/service/auditor/alarm_handler.py +++ b/o2ims/service/auditor/alarm_handler.py @@ -121,10 +121,10 @@ def create_by(fmobj: FaultGenericModel) -> AlarmEventRecord: return alarm_obj.PerceivedSeverityEnum.WARNING alarm_event_record.perceivedSeverity = severity_switch(content['severity']) alarm_event_record.probableCauseId = fmobj.probable_cause_id + + extensions = json.dumps(fmobj.filtered) + alarm_event_record.extensions = extensions alarm_event_record.hash = fmobj.hash - # logger.info('severity: ' + content['severity']) - # logger.info('perceived severity: ' - # + alarm_event_record.perceivedSeverity) alarm_event_record.events.append(events.AlarmEventChanged( id=fmobj.id, notificationEventType=AlarmNotificationEventEnum.NEW, @@ -219,18 +219,21 @@ def check_res_id(uow: AbstractUnitOfWork, fmobj: FaultGenericModel) -> str: entity_type_id = content['entity_type_id'] entity_instance_id = content['entity_instance_id'] if 'host' == entity_type_id: - logger.info('host: ' + entity_instance_id) + logger.debug('host: ' + entity_instance_id) hostname = entity_instance_id.split('.')[0].split('=')[1] with uow: respools = uow.resource_pools.list() respoolids = [respool.resourcePoolId for respool in respools if respool.oCloudId == respool.resourcePoolId] + restype = uow.resource_types.get_by_name('pserver') - hosts = uow.resources.list(respoolids[0], **{ - 'resourceTypeId': restype.resourceTypeId - }) + args = [ocloud.Resource.resourceTypeId == + restype.resourceTypeId] + hosts = uow.resources.list(respoolids[0], *args) for host in hosts: - if host.name == hostname: + logger.debug('host extensions: ' + host.extensions) + extensions = json.loads(host.extensions) + if extensions['hostname'] == hostname: return host.resourceId else: return "" diff --git a/o2ims/service/command/notify_alarm_handler.py b/o2ims/service/command/notify_alarm_handler.py index ac5bb39..d5e049e 100644 --- a/o2ims/service/command/notify_alarm_handler.py +++ b/o2ims/service/command/notify_alarm_handler.py @@ -95,7 +95,7 @@ def callback_smo(sub: AlarmSubscription, msg: AlarmEvent2SMO, 'alarmAcknowledgeTime': alarm_data['alarmAcknowledgeTime'], 'alarmAcknowledged': alarm_data['alarmAcknowledged'], 'perceivedSeverity': alarm_data['perceivedSeverity'], - 'extensions': '' + 'extensions': json.loads(alarm_data['extensions']) } # logger.warning(callback) callback_data = json.dumps(callback) diff --git a/o2ims/views/alarm_dto.py b/o2ims/views/alarm_dto.py index dd90e75..5d0bca5 100644 --- a/o2ims/views/alarm_dto.py +++ b/o2ims/views/alarm_dto.py @@ -14,6 +14,7 @@ from flask_restx import fields +from o2common.views.flask_restx_fields import Json2Dict from o2ims.views.api_ns import api_ims_monitoring as api_monitoring_v1 @@ -38,7 +39,7 @@ class AlarmDTO: 'alarmAcknowledgeTime': fields.String, 'alarmAcknowledged': fields.Boolean, 'perceivedSeverity': fields.String, - 'extensions': fields.Raw(attribute='extensions'), + 'extensions': Json2Dict(attribute='extensions') } # mask='{alarmEventRecordId,resourceTypeID,resourceID,' + # 'alarmDefinitionID,probableCauseID,' + -- 2.16.6