# Copyright (C) 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. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # pylint: disable=unused-argument from __future__ import annotations import json # from o2common.config import config # from o2common.service.messagebus import MessageBus from o2common.service.unit_of_work import AbstractUnitOfWork from o2ims.domain import events, commands, alarm_obj from o2ims.domain.alarm_obj import AlarmEventRecord, FaultGenericModel,\ AlarmNotificationEventEnum from o2common.helper import o2logging logger = o2logging.get_logger(__name__) def update_alarm( cmd: commands.UpdateAlarm, uow: AbstractUnitOfWork ): fmobj = cmd.data logger.info("add alarm event record:" + fmobj.name + " update_at: " + str(fmobj.updatetime) + " id: " + str(fmobj.id) + " hash: " + str(fmobj.hash)) with uow: resourcepool = uow.resource_pools.get(cmd.parentid) alarm_event_record = uow.alarm_event_records.get(fmobj.id) if not alarm_event_record: logger.info("add alarm event record:" + fmobj.name + " update_at: " + str(fmobj.updatetime) + " id: " + str(fmobj.id) + " hash: " + str(fmobj.hash)) localmodel = create_by(fmobj) content = json.loads(fmobj.content) entity_type_id = content['entity_type_id'] entity_instance_id = content['entity_instance_id'] logger.info('alarm entity instance id: ' + entity_instance_id) if 'host' == entity_type_id: # TODO: handle different resource type hostname = entity_instance_id.split('.')[0].split('=')[1] logger.debug('hostname: ' + hostname) restype = uow.resource_types.get_by_name('pserver') localmodel.resourceTypeId = restype.resourceTypeId hosts = uow.resources.list(resourcepool.resourcePoolId, **{ 'resourceTypeId': restype.resourceTypeId }) for host in hosts: if host.name == hostname: localmodel.resourceId = host.resourceId uow.alarm_event_records.add(localmodel) logger.info("Add the alarm event record: " + fmobj.id + ", name: " + fmobj.name) # localmodel.resourceTypeId = check_restype_id(uow, fmobj) # logger.debug("resource type ID: " + localmodel.resourceTypeId) # localmodel.resourceId = check_res_id(uow, fmobj) # logger.debug("resource ID: " + localmodel.resourceId) # uow.alarm_event_records.add(localmodel) else: restype = uow.resource_types.get_by_name('undefined_aggregate') localmodel.resourceTypeId = restype.resourceTypeId undefined_res = uow.resources.list( resourcepool.resourcePoolId, **{ 'resourceTypeId': restype.resourceTypeId }) localmodel.resourceId = undefined_res[0].resourceId uow.alarm_event_records.add(localmodel) logger.info("Add the alarm event record: " + fmobj.id + ", name: " + fmobj.name) else: localmodel = alarm_event_record if is_outdated(localmodel, fmobj): logger.info("update alarm event record:" + fmobj.name + " update_at: " + str(fmobj.updatetime) + " id: " + str(fmobj.id) + " hash: " + str(fmobj.hash)) update_by(localmodel, fmobj) uow.alarm_event_records.update(localmodel) logger.info("Update the alarm event record: " + fmobj.id + ", name: " + fmobj.name) uow.commit() def is_outdated(alarm_event_record: AlarmEventRecord, fmobj: FaultGenericModel): return True if alarm_event_record.hash != fmobj.hash else False def create_by(fmobj: FaultGenericModel) -> AlarmEventRecord: content = json.loads(fmobj.content) # globalcloudId = fmobj.id # to be updated alarm_definition_id = fmobj.alarm_def_id alarm_event_record = AlarmEventRecord( fmobj.id, "", "", alarm_definition_id, "", fmobj.timestamp) def severity_switch(val): if val == 'critical': return alarm_obj.PerceivedSeverityEnum.CRITICAL elif val == 'major': return alarm_obj.PerceivedSeverityEnum.MAJOR elif val == 'minor': return alarm_obj.PerceivedSeverityEnum.MINOR else: return alarm_obj.PerceivedSeverityEnum.WARNING alarm_event_record.perceivedSeverity = severity_switch(content['severity']) alarm_event_record.probableCauseId = fmobj.probable_cause_id 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, updatetime=fmobj.updatetime )) return alarm_event_record def update_by(target: AlarmEventRecord, fmobj: FaultGenericModel ) -> None: # content = json.loads(fmobj.content) target.hash = fmobj.hash if fmobj.status == 'clear': target.perceivedSeverity = alarm_obj.PerceivedSeverityEnum.CLEARED target.events.append(events.AlarmEventChanged( id=fmobj.id, notificationEventType=AlarmNotificationEventEnum.CLEAR, updatetime=fmobj.updatetime )) def check_restype_id(uow: AbstractUnitOfWork, fmobj: FaultGenericModel) -> str: content = json.loads(fmobj.content) entity_type_id = content['entity_type_id'] # Entity_Instance_ID: .lvmthinpool=/ # Entity_Instance_ID: ["image=, instance=", # Entity_Instance_ID: [host=.command=provision, # Entity_Instance_ID: [host=.event=discovered, # Entity_Instance_ID: [host=.state=disabled, # Entity_Instance_ID: [subcloud=.resource=] # Entity_Instance_ID: cinder_io_monitor # Entity_Instance_ID: cluster= # Entity_Instance_ID: cluster=.peergroup= # Entity_Instance_ID: fs_name= # Entity_Instance_ID: host= # Entity_Instance_ID: host=.network= # Entity_Instance_ID: host=.services=compute # Entity_Instance_ID: host= # Entity_Instance_ID: host=,agent=, # bgp-peer= # Entity_Instance_ID: host=.agent= # Entity_Instance_ID: host=.interface= # Entity_Instance_ID: host=.interface= # Entity_Instance_ID: host=.ml2driver= # Entity_Instance_ID: host=.network= # Entity_Instance_ID: host=.openflow-controller= # Entity_Instance_ID: host=.openflow-network= # Entity_Instance_ID: host=.port= # Entity_Instance_ID: host=.port= # Entity_Instance_ID: host=.process= # Entity_Instance_ID: host=.processor= # Entity_Instance_ID: host=.sdn-controller= # Entity_Instance_ID: host=.sensor= # Entity_Instance_ID: host=.service= # Entity_Instance_ID: host=.service=networking.providernet= # # Entity_Instance_ID: host=controller # Entity_Instance_ID: itenant=.instance= # Entity_Instance_ID: k8s_application= # Entity_Instance_ID: kubernetes=PV-migration-failed # Entity_Instance_ID: orchestration=fw-update # Entity_Instance_ID: orchestration=kube-rootca-update # Entity_Instance_ID: orchestration=kube-upgrade # Entity_Instance_ID: orchestration=sw-patch # Entity_Instance_ID: orchestration=sw-upgrade # Entity_Instance_ID: resource=,name= # Entity_Instance_ID: server-group # Entity_Instance_ID: service=networking.providernet= # Entity_Instance_ID: service_domain=.service_group= # Entity_Instance_ID: service_domain=.service_group=. # host= # Entity_Instance_ID: service_domain=.service_group= # # Entity_Instance_ID: service_domain=.service_group= # .host= # Entity_Instance_ID: storage_backend= # Entity_Instance_ID: subcloud= # Entity_Instance_ID: subsystem=vim # Entity_Instance_ID: tenant=.instance= if 'host' == entity_type_id: with uow: restype = uow.resource_types.get_by_name('pserver') return restype.resourceTypeId else: return "" def check_res_id(uow: AbstractUnitOfWork, fmobj: FaultGenericModel) -> str: content = json.loads(fmobj.content) 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) 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 }) for host in hosts: if host.name == hostname: return host.resourceId else: return ""