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 from __future__ import annotations
20 from o2common.domain.base import AgRoot, Serializer
22 from o2common.helper import o2logging
23 logger = o2logging.get_logger(__name__)
26 class FaultGenericModel(AgRoot):
27 def __init__(self, type: str,
28 api_response: dict = None, content_hash=None) -> None:
31 self.id = str(api_response.uuid)
33 self.alarm_type = api_response.alarm_type
34 self.alarm_def_name = api_response.alarm_id
35 self.alarm_def_id = api_response.alarm_def_id
36 self.probable_cause_id = api_response.probable_cause_id
37 self.status = api_response.state
38 # TODO: time less than second
39 self.timestamp = datetime.datetime.strptime(
40 api_response.timestamp.split('.')[0], "%Y-%m-%dT%H:%M:%S") \
41 if api_response.timestamp else None
43 # if hasattr(api_response, 'alarm_id'):
44 # self.alarm_id = api_response.alarm_id
45 # elif hasattr(api_response, 'event_log_id'):
46 # self.alarm_id = api_response.event_log_id
48 self.hash = content_hash if content_hash \
49 else str(hash((self.id, self.timestamp, self.status)))
50 self.content = json.dumps(api_response.to_dict())
51 if EventTypeEnum.ALARM == type:
54 def is_outdated(self, newmodel) -> bool:
55 # return self.updatetime < newmodel.updatetime
56 # logger.warning("hash1: " + self.hash + " vs hash2: " + newmodel.hash)
57 return self.hash != newmodel.hash
59 def update_by(self, newmodel) -> None:
60 if self.id != newmodel.id:
62 # raise MismatchedModel("Mismatched model")
63 self.name = newmodel.name
64 self.createtime = newmodel.createtime
65 self.updatetime = newmodel.updatetime
66 self.content = newmodel.content
69 class EventTypeEnum(Enum):
74 class PerceivedSeverityEnum(str, Enum):
83 class AlarmEventRecord(AgRoot, Serializer):
84 def __init__(self, id: str, res_type_id: str, res_id: str,
85 alarm_def_id: str, probable_cause_id: str,
87 perc_severity: PerceivedSeverityEnum =
88 PerceivedSeverityEnum.WARNING
91 self.alarmEventRecordId = id
92 self.resourceTypeId = res_type_id
93 self.resourceId = res_id
94 self.alarmDefinitionId = alarm_def_id
95 self.probableCauseId = probable_cause_id
96 self.perceivedSeverity = perc_severity
97 self.alarmRaisedTime = raised_time
98 self.alarmChangedTime = ''
99 self.alarmAcknowledgeTime = ''
100 self.alarmAcknowledged = False
104 class ProbableCause(AgRoot, Serializer):
105 def __init__(self, id: str, name: str, desc: str = '') -> None:
107 self.probableCauseId = id
109 self.description = desc
112 class AlarmChangeTypeEnum(str, Enum):
115 MODIFYED = 'MODIFYED'
118 class ClearingTypeEnum(str, Enum):
119 AUTOMATIC = 'AUTOMATIC'
123 class AlarmDefinition(AgRoot, Serializer):
124 def __init__(self, id: str, name: str, change_type: AlarmChangeTypeEnum,
125 desc: str, prop_action: str, clearing_type: ClearingTypeEnum,
126 pk_noti_field: str) -> None:
128 self.alarmDefinitionId = id
129 self.alarmName = name
130 self.alarmLastChange = '0.1'
131 self.alarmChangeType = change_type
132 self.alarmDescription = desc
133 self.proposedRepairActions = prop_action
134 self.clearingType = clearing_type
135 self.managementInterfaceId = "O2IMS"
136 self.pkNotificationField = pk_noti_field
137 self.alarmAdditionalFields = ""
140 class AlarmDictionary(AgRoot, Serializer):
141 def __init__(self, id: str) -> None:
144 self.alarmDictionaryVersion = ""
145 self.alarmDictionarySchemaVersion = ""
148 self.managementInterfaceId = "O2IMS"
149 self.pkNotificationField = ""
150 self.alarmDefinition = []
153 d = Serializer.serialize(self)
154 if 'alarmDefinition' in d and len(d['alarmDefinition']) > 0:
155 d['alarmDefinition'] = self.serialize_list(d['alarmDefinition'])
159 class AlarmNotificationEventEnum(str, Enum):
166 class AlarmEvent2SMO(Serializer):
167 def __init__(self, eventtype: AlarmNotificationEventEnum,
168 id: str, ref: str, updatetime: str) -> None:
169 self.notificationEventType = eventtype
172 self.updatetime = updatetime
175 class AlarmSubscription(AgRoot, Serializer):
176 def __init__(self, id: str, callback: str, consumersubid: str = '',
177 filter: str = '') -> None:
179 self.alarmSubscriptionId = id
180 self.version_number = 0
181 self.callback = callback
182 self.consumerSubscriptionId = consumersubid
186 class AlarmEventNotification(AgRoot, Serializer):
187 def __init__(self, alarm: AlarmEventRecord, to_smo: AlarmEvent2SMO,
188 consumersubid: str) -> None:
190 self.globalCloudId = ''
191 self.consumerSubscriptionId = consumersubid
192 self._convert_params(alarm, to_smo)
194 def _convert_params(self, alarm: AlarmEventRecord, to_smo: AlarmEvent2SMO):
195 self.notificationEventType = to_smo.notificationEventType
196 self.objectRef = to_smo.objectRef
198 self.alarmEventRecordId = alarm.alarmEventRecordId
199 self.resourceTypeId = alarm.resourceTypeId
200 self.resourceId = alarm.resourceId
201 self.alarmDefinitionId = alarm.alarmDefinitionId
202 self.probableCauseId = alarm.probableCauseId
203 self.perceivedSeverity = alarm.perceivedSeverity
204 self.alarmRaisedTime = alarm.alarmRaisedTime
205 self.alarmChangedTime = alarm.alarmChangedTime
206 self.alarmAcknowledgeTime = alarm.alarmAcknowledgeTime
207 self.alarmAcknowledged = alarm.alarmAcknowledged