1 # Copyright (C) 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.
15 # pylint: disable=unused-argument
16 from __future__ import annotations
19 # from o2common.config import config
20 # from o2common.service.messagebus import MessageBus
21 from o2common.service.unit_of_work import AbstractUnitOfWork
22 from o2ims.domain import events, commands, alarm_obj
23 from o2ims.domain.alarm_obj import AlarmEventRecord, FaultGenericModel,\
24 AlarmNotificationEventEnum
26 from o2common.helper import o2logging
27 logger = o2logging.get_logger(__name__)
31 cmd: commands.UpdateAlarm,
32 uow: AbstractUnitOfWork
35 logger.info("add alarm event record:" + fmobj.name
36 + " update_at: " + str(fmobj.updatetime)
37 + " id: " + str(fmobj.id)
38 + " hash: " + str(fmobj.hash))
40 logger.debug('+++test alarm dict:' +
41 str(len(uow.alarm_dictionaries.list())))
42 alarm_event_record = uow.alarm_event_records.get(fmobj.id)
43 if not alarm_event_record:
44 logger.info("add alarm event record:" + fmobj.name
45 + " update_at: " + str(fmobj.updatetime)
46 + " id: " + str(fmobj.id)
47 + " hash: " + str(fmobj.hash))
48 localmodel = create_by(fmobj)
49 content = json.loads(fmobj.content)
50 entity_type_id = content['entity_type_id']
51 entity_instance_id = content['entity_instance_id']
52 logger.info('alarm entity instance id: ' + entity_instance_id)
53 if 'host' == entity_type_id:
54 # TODO: handle different resource type
55 hostname = entity_instance_id.split('.')[0].split('=')[1]
56 logger.debug('hostname: ' + hostname)
57 respools = uow.resource_pools.list()
58 respoolids = [respool.resourcePoolId for respool in
59 respools if respool.oCloudId ==
60 respool.resourcePoolId]
61 restype = uow.resource_types.get_by_name('pserver')
62 localmodel.resourceTypeId = restype.resourceTypeId
63 hosts = uow.resources.list(respoolids[0], **{
64 'resourceTypeId': restype.resourceTypeId
67 if host.name == hostname:
68 localmodel.resourceId = host.resourceId
69 uow.alarm_event_records.add(localmodel)
70 logger.info("Add the alarm event record: " + fmobj.id
71 + ", name: " + fmobj.name)
72 # localmodel.resourceTypeId = check_restype_id(uow, fmobj)
73 # logger.debug("resource type ID: " + localmodel.resourceTypeId)
74 # localmodel.resourceId = check_res_id(uow, fmobj)
75 # logger.debug("resource ID: " + localmodel.resourceId)
76 # uow.alarm_event_records.add(localmodel)
79 localmodel = alarm_event_record
80 if is_outdated(localmodel, fmobj):
81 logger.info("update alarm event record:" + fmobj.name
82 + " update_at: " + str(fmobj.updatetime)
83 + " id: " + str(fmobj.id)
84 + " hash: " + str(fmobj.hash))
85 update_by(localmodel, fmobj)
86 uow.alarm_event_records.update(localmodel)
88 logger.info("Update the alarm event record: " + fmobj.id
89 + ", name: " + fmobj.name)
93 def is_outdated(alarm_event_record: AlarmEventRecord,
94 fmobj: FaultGenericModel):
95 return True if alarm_event_record.hash != fmobj.hash else False
98 def create_by(fmobj: FaultGenericModel) -> AlarmEventRecord:
99 content = json.loads(fmobj.content)
100 # globalcloudId = fmobj.id # to be updated
101 alarm_definition_id = fmobj.alarm_def_id
102 alarm_event_record = AlarmEventRecord(
104 alarm_definition_id, "",
107 def severity_switch(val):
108 if val == 'critical':
109 return alarm_obj.PerceivedSeverityEnum.CRITICAL
111 return alarm_obj.PerceivedSeverityEnum.MAJOR
113 return alarm_obj.PerceivedSeverityEnum.MINOR
115 return alarm_obj.PerceivedSeverityEnum.WARNING
116 alarm_event_record.perceivedSeverity = severity_switch(content['severity'])
117 alarm_event_record.probableCauseId = fmobj.probable_cause_id
118 alarm_event_record.hash = fmobj.hash
119 # logger.info('severity: ' + content['severity'])
120 # logger.info('perceived severity: '
121 # + alarm_event_record.perceivedSeverity)
122 alarm_event_record.events.append(events.AlarmEventChanged(
124 notificationEventType=AlarmNotificationEventEnum.NEW,
125 updatetime=fmobj.updatetime
128 return alarm_event_record
131 def update_by(target: AlarmEventRecord, fmobj: FaultGenericModel
133 # content = json.loads(fmobj.content)
134 target.hash = fmobj.hash
135 if fmobj.status == 'clear':
136 target.perceivedSeverity = alarm_obj.PerceivedSeverityEnum.CLEARED
137 target.events.append(events.AlarmEventChanged(
139 notificationEventType=AlarmNotificationEventEnum.CLEAR,
140 updatetime=fmobj.updatetime
144 def check_restype_id(uow: AbstractUnitOfWork, fmobj: FaultGenericModel) -> str:
145 content = json.loads(fmobj.content)
146 entity_type_id = content['entity_type_id']
147 # Entity_Instance_ID: <hostname>.lvmthinpool=<VG name>/<Pool name>
148 # Entity_Instance_ID: ["image=<image-uuid>, instance=<instance-uuid>",
149 # Entity_Instance_ID: [host=<hostname>.command=provision,
150 # Entity_Instance_ID: [host=<hostname>.event=discovered,
151 # Entity_Instance_ID: [host=<hostname>.state=disabled,
152 # Entity_Instance_ID: [subcloud=<subcloud>.resource=<compute | network
153 # | platform | volumev2>]
154 # Entity_Instance_ID: cinder_io_monitor
155 # Entity_Instance_ID: cluster=<dist-fs-uuid>
156 # Entity_Instance_ID: cluster=<dist-fs-uuid>.peergroup=<group-x>
157 # Entity_Instance_ID: fs_name=<image-conversion>
158 # Entity_Instance_ID: host=<host_name>
159 # Entity_Instance_ID: host=<host_name>.network=<network>
160 # Entity_Instance_ID: host=<host_name>.services=compute
161 # Entity_Instance_ID: host=<hostname>
162 # Entity_Instance_ID: host=<hostname>,agent=<agent-uuid>,
163 # bgp-peer=<bgp-peer>
164 # Entity_Instance_ID: host=<hostname>.agent=<agent-uuid>
165 # Entity_Instance_ID: host=<hostname>.interface=<if-name>
166 # Entity_Instance_ID: host=<hostname>.interface=<if-uuid>
167 # Entity_Instance_ID: host=<hostname>.ml2driver=<driver>
168 # Entity_Instance_ID: host=<hostname>.network=<mgmt | oam | cluster-host>
169 # Entity_Instance_ID: host=<hostname>.openflow-controller=<uri>
170 # Entity_Instance_ID: host=<hostname>.openflow-network=<name>
171 # Entity_Instance_ID: host=<hostname>.port=<port-name>
172 # Entity_Instance_ID: host=<hostname>.port=<port-uuid>
173 # Entity_Instance_ID: host=<hostname>.process=<processname>
174 # Entity_Instance_ID: host=<hostname>.processor=<processor>
175 # Entity_Instance_ID: host=<hostname>.sdn-controller=<uuid>
176 # Entity_Instance_ID: host=<hostname>.sensor=<sensorname>
177 # Entity_Instance_ID: host=<hostname>.service=<service>
178 # Entity_Instance_ID: host=<hostname>.service=networking.providernet=
180 # Entity_Instance_ID: host=controller
181 # Entity_Instance_ID: itenant=<tenant-uuid>.instance=<instance-uuid>
182 # Entity_Instance_ID: k8s_application=<appname>
183 # Entity_Instance_ID: kubernetes=PV-migration-failed
184 # Entity_Instance_ID: orchestration=fw-update
185 # Entity_Instance_ID: orchestration=kube-rootca-update
186 # Entity_Instance_ID: orchestration=kube-upgrade
187 # Entity_Instance_ID: orchestration=sw-patch
188 # Entity_Instance_ID: orchestration=sw-upgrade
189 # Entity_Instance_ID: resource=<crd-resource>,name=<resource-name>
190 # Entity_Instance_ID: server-group<server-group-uuid>
191 # Entity_Instance_ID: service=networking.providernet=<pnet-uuid>
192 # Entity_Instance_ID: service_domain=<domain>.service_group=<group>
193 # Entity_Instance_ID: service_domain=<domain>.service_group=<group>.
195 # Entity_Instance_ID: service_domain=<domain_name>.service_group=
197 # Entity_Instance_ID: service_domain=<domain_name>.service_group=
198 # <group_name>.host=<hostname>
199 # Entity_Instance_ID: storage_backend=<storage-backend-name>
200 # Entity_Instance_ID: subcloud=<subcloud>
201 # Entity_Instance_ID: subsystem=vim
202 # Entity_Instance_ID: tenant=<tenant-uuid>.instance=<instance-uuid>
203 if 'host' == entity_type_id:
205 restype = uow.resource_types.get_by_name('pserver')
206 return restype.resourceTypeId
211 def check_res_id(uow: AbstractUnitOfWork, fmobj: FaultGenericModel) -> str:
212 content = json.loads(fmobj.content)
213 entity_type_id = content['entity_type_id']
214 entity_instance_id = content['entity_instance_id']
215 if 'host' == entity_type_id:
216 logger.info('host: ' + entity_instance_id)
217 hostname = entity_instance_id.split('.')[0].split('=')[1]
219 respools = uow.resource_pools.list()
220 respoolids = [respool.resourcePoolId for respool in respools
221 if respool.oCloudId == respool.resourcePoolId]
222 restype = uow.resource_types.get_by_name('pserver')
223 hosts = uow.resources.list(respoolids[0], **{
224 'resourceTypeId': restype.resourceTypeId
227 if host.name == hostname:
228 return host.resourceId