9b3ff4f0e70ef39600271fb3dbe6dd4bd0ef398b
[pti/o2.git] / o2ims / domain / alarm_obj.py
1 # Copyright (C) 2022 Wind River Systems, Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 from __future__ import annotations
16 from enum import Enum
17 import json
18 import datetime
19
20 from o2common.domain.base import AgRoot, Serializer
21
22 from o2common.helper import o2logging
23 logger = o2logging.get_logger(__name__)
24
25
26 class FaultGenericModel(AgRoot):
27     def __init__(self, type: str,
28                  api_response: dict = None, content_hash=None) -> None:
29         super().__init__()
30         if api_response:
31             self.id = str(api_response.uuid)
32             self.name = self.id
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
42
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
47
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:
52                 pass
53
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
58
59     def update_by(self, newmodel) -> None:
60         if self.id != newmodel.id:
61             pass
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
67
68
69 class EventTypeEnum(Enum):
70     ALARM = 'alarm'
71     EVENT = 'event'
72
73
74 class PerceivedSeverityEnum(str, Enum):
75     CRITICAL = 0
76     MAJOR = 1
77     MINOR = 2
78     WARNING = 3
79     INDETERMINATE = 4
80     CLEARED = 5
81
82
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,
86                  raised_time: str,
87                  perc_severity: PerceivedSeverityEnum =
88                  PerceivedSeverityEnum.WARNING
89                  ) -> None:
90         super().__init__()
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
101         self.extensions = []
102
103
104 class ProbableCause(AgRoot, Serializer):
105     def __init__(self, id: str, name: str, desc: str = '') -> None:
106         super().__init__()
107         self.probableCauseId = id
108         self.name = name
109         self.description = desc
110
111
112 class AlarmLastChangeEnum(str, Enum):
113     ADDED = 'ADDED'
114     DELETED = 'DELETED'
115     MODIFYED = 'MODIFYED'
116
117
118 class ClearingTypeEnum(str, Enum):
119     AUTOMATIC = 'AUTOMATIC'
120     MANUAL = 'MANUAL'
121
122
123 class AlarmDefinition(AgRoot, Serializer):
124     def __init__(self, id: str, name: str, last_change: AlarmLastChangeEnum,
125                  desc: str, prop_action: str, clearing_type: ClearingTypeEnum,
126                  pk_noti_field: str) -> None:
127         super().__init__()
128         self.alarmDefinitionId = id
129         self.alarmName = name
130         self.alarmLastChange = last_change
131         self.alarmDescription = desc
132         self.proposedRepairActions = prop_action
133         self.clearingType = clearing_type
134         self.managementInterfaceId = "O2IMS"
135         self.pkNotificationField = pk_noti_field
136         self.alarmAdditionalFields = ""
137
138
139 class AlarmDictionary(AgRoot, Serializer):
140     def __init__(self, id: str) -> None:
141         super().__init__()
142         self.id = id
143         self.alarmDictionaryVersion = ""
144         self.alarmDictionarySchemaVersion = ""
145         self.entityType = ""
146         self.vendor = ""
147         self.managementInterfaceId = "O2IMS"
148         self.pkNotificationField = ""
149         self.alarmDefinition = ""
150
151
152 class AlarmNotificationEventEnum(str, Enum):
153     NEW = 0
154     CHANGE = 1
155     CLEAR = 2
156     ACKNOWLEDGE = 3
157
158
159 class AlarmEvent2SMO(Serializer):
160     def __init__(self, eventtype: AlarmNotificationEventEnum,
161                  id: str, ref: str, updatetime: str) -> None:
162         self.notificationEventType = eventtype
163         self.objectRef = ref
164         self.id = id
165         self.updatetime = updatetime
166
167
168 class AlarmSubscription(AgRoot, Serializer):
169     def __init__(self, id: str, callback: str, consumersubid: str = '',
170                  filter: str = '') -> None:
171         super().__init__()
172         self.alarmSubscriptionId = id
173         self.version_number = 0
174         self.callback = callback
175         self.consumerSubscriptionId = consumersubid
176         self.filter = filter
177
178
179 class AlarmEventNotification(AgRoot, Serializer):
180     def __init__(self, alarm: AlarmEventRecord, to_smo: AlarmEvent2SMO,
181                  consumersubid: str) -> None:
182         super().__init__()
183         self.globalCloudId = ''
184         self.consumerSubscriptionId = consumersubid
185         self._convert_params(alarm, to_smo)
186
187     def _convert_params(self, alarm: AlarmEventRecord, to_smo: AlarmEvent2SMO):
188         self.notificationEventType = to_smo.notificationEventType
189         self.objectRef = to_smo.objectRef
190
191         self.alarmEventRecordId = alarm.alarmEventRecordId
192         self.resourceTypeId = alarm.resourceTypeId
193         self.resourceId = alarm.resourceId
194         self.alarmDefinitionId = alarm.alarmDefinitionId
195         self.probableCauseId = alarm.probableCauseId
196         self.perceivedSeverity = alarm.perceivedSeverity
197         self.alarmRaisedTime = alarm.alarmRaisedTime
198         self.alarmChangedTime = alarm.alarmChangedTime
199         self.alarmAcknowledgeTime = alarm.alarmAcknowledgeTime
200         self.alarmAcknowledged = alarm.alarmAcknowledged
201         self.extensions = []