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, ocloud
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 resourcepool = uow.resource_pools.get(cmd.parentid)
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)
58 restype = uow.resource_types.get_by_name('pserver')
59 localmodel.resourceTypeId = restype.resourceTypeId
60 args = [ocloud.Resource.resourceTypeId ==
61 restype.resourceTypeId]
62 hosts = uow.resources.list(resourcepool.resourcePoolId, *args)
64 logger.debug('host extensions: ' + host.extensions)
65 extensions = json.loads(host.extensions)
66 if extensions['hostname'] == hostname:
67 localmodel.resourceId = host.resourceId
68 uow.alarm_event_records.add(localmodel)
69 logger.info("Add the alarm event record: " + fmobj.id
70 + ", name: " + fmobj.name)
72 restype = uow.resource_types.get_by_name('undefined_aggregate')
73 localmodel.resourceTypeId = restype.resourceTypeId
75 args = [ocloud.Resource.resourceTypeId ==
76 restype.resourceTypeId]
77 undefined_res = uow.resources.list(
78 resourcepool.resourcePoolId, *args)
79 localmodel.resourceId = undefined_res[0].resourceId
80 uow.alarm_event_records.add(localmodel)
81 logger.info("Add the alarm event record: " + fmobj.id
82 + ", name: " + fmobj.name)
85 localmodel = alarm_event_record
86 if is_outdated(localmodel, fmobj):
87 logger.info("update alarm event record:" + fmobj.name
88 + " update_at: " + str(fmobj.updatetime)
89 + " id: " + str(fmobj.id)
90 + " hash: " + str(fmobj.hash))
91 update_by(localmodel, fmobj)
92 uow.alarm_event_records.update(localmodel)
94 logger.info("Update the alarm event record: " + fmobj.id
95 + ", name: " + fmobj.name)
99 def is_outdated(alarm_event_record: AlarmEventRecord,
100 fmobj: FaultGenericModel):
101 return True if alarm_event_record.hash != fmobj.hash else False
104 def create_by(fmobj: FaultGenericModel) -> AlarmEventRecord:
105 content = json.loads(fmobj.content)
106 # globalcloudId = fmobj.id # to be updated
107 alarm_definition_id = fmobj.alarm_def_id
108 alarm_event_record = AlarmEventRecord(
110 alarm_definition_id, "",
113 def severity_switch(val):
114 if val == 'critical':
115 return alarm_obj.PerceivedSeverityEnum.CRITICAL
117 return alarm_obj.PerceivedSeverityEnum.MAJOR
119 return alarm_obj.PerceivedSeverityEnum.MINOR
121 return alarm_obj.PerceivedSeverityEnum.WARNING
122 alarm_event_record.perceivedSeverity = severity_switch(content['severity'])
123 alarm_event_record.probableCauseId = fmobj.probable_cause_id
125 extensions = json.dumps(fmobj.filtered)
126 alarm_event_record.extensions = extensions
127 alarm_event_record.hash = fmobj.hash
128 alarm_event_record.events.append(events.AlarmEventChanged(
130 notificationEventType=AlarmNotificationEventEnum.NEW,
131 updatetime=fmobj.updatetime
134 return alarm_event_record
137 def update_by(target: AlarmEventRecord, fmobj: FaultGenericModel
139 # content = json.loads(fmobj.content)
140 if fmobj.status == 'clear':
141 target.perceivedSeverity = alarm_obj.PerceivedSeverityEnum.CLEARED
143 target.hash = fmobj.hash
144 target.events.append(events.AlarmEventChanged(
146 notificationEventType=AlarmNotificationEventEnum.CLEAR,
147 updatetime=fmobj.updatetime
151 def check_restype_id(uow: AbstractUnitOfWork, fmobj: FaultGenericModel) -> str:
152 content = json.loads(fmobj.content)
153 entity_type_id = content['entity_type_id']
154 # Entity_Instance_ID: <hostname>.lvmthinpool=<VG name>/<Pool name>
155 # Entity_Instance_ID: ["image=<image-uuid>, instance=<instance-uuid>",
156 # Entity_Instance_ID: [host=<hostname>.command=provision,
157 # Entity_Instance_ID: [host=<hostname>.event=discovered,
158 # Entity_Instance_ID: [host=<hostname>.state=disabled,
159 # Entity_Instance_ID: [subcloud=<subcloud>.resource=<compute | network
160 # | platform | volumev2>]
161 # Entity_Instance_ID: cinder_io_monitor
162 # Entity_Instance_ID: cluster=<dist-fs-uuid>
163 # Entity_Instance_ID: cluster=<dist-fs-uuid>.peergroup=<group-x>
164 # Entity_Instance_ID: fs_name=<image-conversion>
165 # Entity_Instance_ID: host=<host_name>
166 # Entity_Instance_ID: host=<host_name>.network=<network>
167 # Entity_Instance_ID: host=<host_name>.services=compute
168 # Entity_Instance_ID: host=<hostname>
169 # Entity_Instance_ID: host=<hostname>,agent=<agent-uuid>,
170 # bgp-peer=<bgp-peer>
171 # Entity_Instance_ID: host=<hostname>.agent=<agent-uuid>
172 # Entity_Instance_ID: host=<hostname>.interface=<if-name>
173 # Entity_Instance_ID: host=<hostname>.interface=<if-uuid>
174 # Entity_Instance_ID: host=<hostname>.ml2driver=<driver>
175 # Entity_Instance_ID: host=<hostname>.network=<mgmt | oam | cluster-host>
176 # Entity_Instance_ID: host=<hostname>.openflow-controller=<uri>
177 # Entity_Instance_ID: host=<hostname>.openflow-network=<name>
178 # Entity_Instance_ID: host=<hostname>.port=<port-name>
179 # Entity_Instance_ID: host=<hostname>.port=<port-uuid>
180 # Entity_Instance_ID: host=<hostname>.process=<processname>
181 # Entity_Instance_ID: host=<hostname>.processor=<processor>
182 # Entity_Instance_ID: host=<hostname>.sdn-controller=<uuid>
183 # Entity_Instance_ID: host=<hostname>.sensor=<sensorname>
184 # Entity_Instance_ID: host=<hostname>.service=<service>
185 # Entity_Instance_ID: host=<hostname>.service=networking.providernet=
187 # Entity_Instance_ID: host=controller
188 # Entity_Instance_ID: itenant=<tenant-uuid>.instance=<instance-uuid>
189 # Entity_Instance_ID: k8s_application=<appname>
190 # Entity_Instance_ID: kubernetes=PV-migration-failed
191 # Entity_Instance_ID: orchestration=fw-update
192 # Entity_Instance_ID: orchestration=kube-rootca-update
193 # Entity_Instance_ID: orchestration=kube-upgrade
194 # Entity_Instance_ID: orchestration=sw-patch
195 # Entity_Instance_ID: orchestration=sw-upgrade
196 # Entity_Instance_ID: resource=<crd-resource>,name=<resource-name>
197 # Entity_Instance_ID: server-group<server-group-uuid>
198 # Entity_Instance_ID: service=networking.providernet=<pnet-uuid>
199 # Entity_Instance_ID: service_domain=<domain>.service_group=<group>
200 # Entity_Instance_ID: service_domain=<domain>.service_group=<group>.
202 # Entity_Instance_ID: service_domain=<domain_name>.service_group=
204 # Entity_Instance_ID: service_domain=<domain_name>.service_group=
205 # <group_name>.host=<hostname>
206 # Entity_Instance_ID: storage_backend=<storage-backend-name>
207 # Entity_Instance_ID: subcloud=<subcloud>
208 # Entity_Instance_ID: subsystem=vim
209 # Entity_Instance_ID: tenant=<tenant-uuid>.instance=<instance-uuid>
210 if 'host' == entity_type_id:
212 restype = uow.resource_types.get_by_name('pserver')
213 return restype.resourceTypeId
218 def check_res_id(uow: AbstractUnitOfWork, fmobj: FaultGenericModel) -> str:
219 content = json.loads(fmobj.content)
220 entity_type_id = content['entity_type_id']
221 entity_instance_id = content['entity_instance_id']
222 if 'host' == entity_type_id:
223 logger.debug('host: ' + entity_instance_id)
224 hostname = entity_instance_id.split('.')[0].split('=')[1]
226 respools = uow.resource_pools.list()
227 respoolids = [respool.resourcePoolId for respool in respools
228 if respool.oCloudId == respool.resourcePoolId]
230 restype = uow.resource_types.get_by_name('pserver')
231 args = [ocloud.Resource.resourceTypeId ==
232 restype.resourceTypeId]
233 hosts = uow.resources.list(respoolids[0], *args)
235 logger.debug('host extensions: ' + host.extensions)
236 extensions = json.loads(host.extensions)
237 if extensions['hostname'] == hostname:
238 return host.resourceId