blob: 162b3df169f6d9bd9604155ac0b2107278574fac [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
kmcpeake4fe18c82015-11-17 20:07:39 +00003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.incubator.net.faultmanagement.alarm;
17
18import org.onosproject.net.DeviceId;
19
20import java.util.Objects;
21
22import static com.google.common.base.MoreObjects.toStringHelper;
23import static com.google.common.base.Preconditions.checkNotNull;
24
25/**
26 * Default implementation of an alarm.
27 */
Andrea Campanella8e94b0c2016-04-12 13:58:07 -070028//TODO simpler creation and updating.
kmcpeake4fe18c82015-11-17 20:07:39 +000029public final class DefaultAlarm implements Alarm {
30
31 private final AlarmId id;
32
33 private final DeviceId deviceId;
34 private final String description;
35 private final AlarmEntityId source;
36 private final long timeRaised;
37 private final long timeUpdated;
38 private final Long timeCleared;
39 private final SeverityLevel severity;
40 private final boolean isServiceAffecting;
41 private final boolean isAcknowledged;
42 private final boolean isManuallyClearable;
43 private final String assignedUser;
44
Andrea Campanella8e94b0c2016-04-12 13:58:07 -070045 //Only for Kryo
46 DefaultAlarm() {
47 id = null;
48 deviceId = null;
49 description = null;
50 source = null;
51 timeRaised = -1;
52 timeUpdated = -1;
53 timeCleared = null;
54 severity = null;
55 isServiceAffecting = false;
56 isAcknowledged = false;
57 isManuallyClearable = false;
58 assignedUser = null;
59 }
60
kmcpeake4fe18c82015-11-17 20:07:39 +000061 /**
62 * Instantiates a new Default alarm.
63 *
Andrea Campanella8e94b0c2016-04-12 13:58:07 -070064 * @param id the id
65 * @param deviceId the device id
66 * @param description the description
67 * @param source the source, null indicates none.
68 * @param timeRaised the time raised.
69 * @param timeUpdated the time last updated.
70 * @param timeCleared the time cleared, null indicates uncleared.
71 * @param severity the severity
72 * @param isServiceAffecting the service affecting
73 * @param isAcknowledged the acknowledged
kmcpeake4fe18c82015-11-17 20:07:39 +000074 * @param isManuallyClearable the manually clearable
Andrea Campanella8e94b0c2016-04-12 13:58:07 -070075 * @param assignedUser the assigned user, `null` indicates none.
kmcpeake4fe18c82015-11-17 20:07:39 +000076 */
77 private DefaultAlarm(final AlarmId id,
Andrea Campanella8e94b0c2016-04-12 13:58:07 -070078 final DeviceId deviceId,
79 final String description,
80 final AlarmEntityId source,
81 final long timeRaised,
82 final long timeUpdated,
83 final Long timeCleared,
84 final SeverityLevel severity,
85 final boolean isServiceAffecting,
86 final boolean isAcknowledged,
87 final boolean isManuallyClearable,
88 final String assignedUser) {
kmcpeake4fe18c82015-11-17 20:07:39 +000089 this.id = id;
90 this.deviceId = deviceId;
91 this.description = description;
92 this.source = source;
93 this.timeRaised = timeRaised;
94 this.timeUpdated = timeUpdated;
95 this.timeCleared = timeCleared;
96 this.severity = severity;
97 this.isServiceAffecting = isServiceAffecting;
98 this.isAcknowledged = isAcknowledged;
99 this.isManuallyClearable = isManuallyClearable;
100 this.assignedUser = assignedUser;
101 }
102
103 @Override
104 public AlarmId id() {
105 return id;
106 }
107
108 @Override
109 public DeviceId deviceId() {
110 return deviceId;
111 }
112
113 @Override
114 public String description() {
115 return description;
116 }
117
118 @Override
119 public AlarmEntityId source() {
120 return source;
121 }
122
123 @Override
124 public long timeRaised() {
125 return timeRaised;
126 }
127
128 @Override
129 public long timeUpdated() {
130 return timeUpdated;
131 }
132
133 @Override
134 public Long timeCleared() {
135 return timeCleared;
136 }
137
138 @Override
139 public SeverityLevel severity() {
140 return severity;
141 }
142
143 @Override
144 public boolean serviceAffecting() {
145 return isServiceAffecting;
146 }
147
148 @Override
149 public boolean acknowledged() {
150 return isAcknowledged;
151 }
152
153 @Override
154 public boolean manuallyClearable() {
155 return isManuallyClearable;
156 }
157
158 @Override
159 public String assignedUser() {
160 return assignedUser;
161 }
162
163 @Override
164 public int hashCode() {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000165 // id or timeRaised or timeUpdated may differ
166 return Objects.hash(deviceId, description,
Andrea Campanella8e94b0c2016-04-12 13:58:07 -0700167 source, timeCleared, severity,
168 isServiceAffecting, isAcknowledged,
169 isManuallyClearable, assignedUser);
kmcpeake4fe18c82015-11-17 20:07:39 +0000170 }
171
172 @Override
173 public boolean equals(final Object obj) {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000174 // Make sure equals() is tune with hashCode() so works ok in a hashSet !
kmcpeake4fe18c82015-11-17 20:07:39 +0000175 if (obj == null) {
176 return false;
177 }
178 if (getClass() != obj.getClass()) {
179 return false;
180 }
181 final DefaultAlarm other = (DefaultAlarm) obj;
kmcpeakeb172d5f2015-12-10 11:30:43 +0000182
183 // id or timeRaised or timeUpdated may differ
kmcpeake4fe18c82015-11-17 20:07:39 +0000184 if (!Objects.equals(this.deviceId, other.deviceId)) {
185 return false;
186 }
187 if (!Objects.equals(this.description, other.description)) {
188 return false;
189 }
190 if (!Objects.equals(this.source, other.source)) {
191 return false;
192 }
kmcpeakeb172d5f2015-12-10 11:30:43 +0000193
kmcpeake4fe18c82015-11-17 20:07:39 +0000194 if (!Objects.equals(this.timeCleared, other.timeCleared)) {
195 return false;
196 }
197 if (this.severity != other.severity) {
198 return false;
199 }
200 if (this.isServiceAffecting != other.isServiceAffecting) {
201 return false;
202 }
203 if (this.isAcknowledged != other.isAcknowledged) {
204 return false;
205 }
206 if (this.isManuallyClearable != other.isManuallyClearable) {
207 return false;
208 }
209 if (!Objects.equals(this.assignedUser, other.assignedUser)) {
210 return false;
211 }
212 return true;
213 }
214
215 @Override
216 public String toString() {
217 return toStringHelper(this)
218 .add("id", id)
219 .add("deviceId", deviceId)
220 .add("description", description)
221 .add("source", source)
222 .add("timeRaised", timeRaised)
223 .add("timeUpdated", timeUpdated)
224 .add("timeCleared", timeCleared)
225 .add("severity", severity)
226 .add("serviceAffecting", isServiceAffecting)
227 .add("acknowledged", isAcknowledged)
228 .add("manuallyClearable", isManuallyClearable)
229 .add("assignedUser", assignedUser)
230 .toString();
231 }
232
233 public static class Builder {
234
kmcpeakeb172d5f2015-12-10 11:30:43 +0000235 // Manadatory fields when constructing alarm ...
236 private AlarmId id;
kmcpeake4fe18c82015-11-17 20:07:39 +0000237 private final DeviceId deviceId;
238 private final String description;
kmcpeakeb172d5f2015-12-10 11:30:43 +0000239 private SeverityLevel severity;
kmcpeake4fe18c82015-11-17 20:07:39 +0000240 private final long timeRaised;
241
242 // Optional fields ..
243 private AlarmEntityId source = AlarmEntityId.NONE;
244 private long timeUpdated;
245 private Long timeCleared = null;
246 private boolean isServiceAffecting = false;
247 private boolean isAcknowledged = false;
248 private boolean isManuallyClearable = false;
249 private String assignedUser = null;
250
251 public Builder(final Alarm alarm) {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000252 this(alarm.deviceId(), alarm.description(), alarm.severity(), alarm.timeRaised());
253 this.source = alarm.source();
kmcpeake4fe18c82015-11-17 20:07:39 +0000254 this.timeUpdated = alarm.timeUpdated();
255 this.timeCleared = alarm.timeCleared();
256 this.isServiceAffecting = alarm.serviceAffecting();
257 this.isAcknowledged = alarm.acknowledged();
258 this.isManuallyClearable = alarm.manuallyClearable();
259 this.assignedUser = alarm.assignedUser();
260
261 }
262
kmcpeakeb172d5f2015-12-10 11:30:43 +0000263 public Builder(final DeviceId deviceId,
Andrea Campanella8e94b0c2016-04-12 13:58:07 -0700264 final String description, final SeverityLevel severity, final long timeRaised) {
kmcpeake4fe18c82015-11-17 20:07:39 +0000265 super();
kmcpeakeb172d5f2015-12-10 11:30:43 +0000266 this.id = AlarmId.NONE;
kmcpeake4fe18c82015-11-17 20:07:39 +0000267 this.deviceId = deviceId;
268 this.description = description;
269 this.severity = severity;
270 this.timeRaised = timeRaised;
271 // Unless specified time-updated is same as raised.
272 this.timeUpdated = timeRaised;
273 }
274
275 public Builder forSource(final AlarmEntityId source) {
276 this.source = source;
277 return this;
278 }
279
280 public Builder withTimeUpdated(final long timeUpdated) {
281 this.timeUpdated = timeUpdated;
282 return this;
283 }
284
285 public Builder withTimeCleared(final Long timeCleared) {
286 this.timeCleared = timeCleared;
287 return this;
288 }
289
kmcpeakeb172d5f2015-12-10 11:30:43 +0000290 public Builder withId(final AlarmId id) {
291 this.id = id;
292 return this;
293 }
294
295 public Builder clear() {
296 this.severity = SeverityLevel.CLEARED;
297 final long now = System.currentTimeMillis();
298 return withTimeCleared(now).withTimeUpdated(now);
299 }
300
kmcpeake4fe18c82015-11-17 20:07:39 +0000301 public Builder withServiceAffecting(final boolean isServiceAffecting) {
302 this.isServiceAffecting = isServiceAffecting;
303 return this;
304 }
305
306 public Builder withAcknowledged(final boolean isAcknowledged) {
307 this.isAcknowledged = isAcknowledged;
308 return this;
309 }
310
311 public Builder withManuallyClearable(final boolean isManuallyClearable) {
312 this.isManuallyClearable = isManuallyClearable;
313 return this;
314 }
315
316 public Builder withAssignedUser(final String assignedUser) {
317 this.assignedUser = assignedUser;
318 return this;
319 }
320
321 public DefaultAlarm build() {
322 checkNotNull(id, "Must specify an alarm id");
323 checkNotNull(deviceId, "Must specify a device");
324 checkNotNull(description, "Must specify a description");
325 checkNotNull(timeRaised, "Must specify a time raised");
326 checkNotNull(timeUpdated, "Must specify a time updated");
327 checkNotNull(severity, "Must specify a severity");
328
329 return new DefaultAlarm(id, deviceId, description, source, timeRaised, timeUpdated, timeCleared,
Andrea Campanella8e94b0c2016-04-12 13:58:07 -0700330 severity, isServiceAffecting, isAcknowledged, isManuallyClearable, assignedUser);
kmcpeake4fe18c82015-11-17 20:07:39 +0000331 }
332 }
333}