blob: fd4bad0008620231449d4f164833e392c1b241cd [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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 */
28public final class DefaultAlarm implements Alarm {
29
30 private final AlarmId id;
31
32 private final DeviceId deviceId;
33 private final String description;
34 private final AlarmEntityId source;
35 private final long timeRaised;
kmcpeake4fe18c82015-11-17 20:07:39 +000036 private final boolean isServiceAffecting;
37 private final boolean isAcknowledged;
38 private final boolean isManuallyClearable;
39 private final String assignedUser;
40
Andrea Campanella65f9eb92017-05-02 11:36:14 -070041 private final SeverityLevel severity;
42 private final long timeUpdated;
43 private final Long timeCleared;
44
45
Andrea Campanella8e94b0c2016-04-12 13:58:07 -070046 //Only for Kryo
47 DefaultAlarm() {
48 id = null;
49 deviceId = null;
50 description = null;
51 source = null;
52 timeRaised = -1;
53 timeUpdated = -1;
54 timeCleared = null;
55 severity = null;
56 isServiceAffecting = false;
57 isAcknowledged = false;
58 isManuallyClearable = false;
59 assignedUser = null;
60 }
61
kmcpeake4fe18c82015-11-17 20:07:39 +000062 /**
63 * Instantiates a new Default alarm.
64 *
Andrea Campanella8e94b0c2016-04-12 13:58:07 -070065 * @param id the id
66 * @param deviceId the device id
67 * @param description the description
68 * @param source the source, null indicates none.
69 * @param timeRaised the time raised.
70 * @param timeUpdated the time last updated.
71 * @param timeCleared the time cleared, null indicates uncleared.
72 * @param severity the severity
73 * @param isServiceAffecting the service affecting
74 * @param isAcknowledged the acknowledged
kmcpeake4fe18c82015-11-17 20:07:39 +000075 * @param isManuallyClearable the manually clearable
Andrea Campanella8e94b0c2016-04-12 13:58:07 -070076 * @param assignedUser the assigned user, `null` indicates none.
kmcpeake4fe18c82015-11-17 20:07:39 +000077 */
78 private DefaultAlarm(final AlarmId id,
Andrea Campanella8e94b0c2016-04-12 13:58:07 -070079 final DeviceId deviceId,
80 final String description,
81 final AlarmEntityId source,
82 final long timeRaised,
83 final long timeUpdated,
84 final Long timeCleared,
85 final SeverityLevel severity,
86 final boolean isServiceAffecting,
87 final boolean isAcknowledged,
88 final boolean isManuallyClearable,
89 final String assignedUser) {
kmcpeake4fe18c82015-11-17 20:07:39 +000090 this.id = id;
91 this.deviceId = deviceId;
92 this.description = description;
93 this.source = source;
94 this.timeRaised = timeRaised;
95 this.timeUpdated = timeUpdated;
96 this.timeCleared = timeCleared;
97 this.severity = severity;
98 this.isServiceAffecting = isServiceAffecting;
99 this.isAcknowledged = isAcknowledged;
100 this.isManuallyClearable = isManuallyClearable;
101 this.assignedUser = assignedUser;
102 }
103
104 @Override
105 public AlarmId id() {
106 return id;
107 }
108
109 @Override
110 public DeviceId deviceId() {
111 return deviceId;
112 }
113
114 @Override
115 public String description() {
116 return description;
117 }
118
119 @Override
120 public AlarmEntityId source() {
121 return source;
122 }
123
124 @Override
125 public long timeRaised() {
126 return timeRaised;
127 }
128
129 @Override
130 public long timeUpdated() {
131 return timeUpdated;
132 }
133
134 @Override
135 public Long timeCleared() {
136 return timeCleared;
137 }
138
139 @Override
140 public SeverityLevel severity() {
141 return severity;
142 }
143
144 @Override
145 public boolean serviceAffecting() {
146 return isServiceAffecting;
147 }
148
149 @Override
150 public boolean acknowledged() {
151 return isAcknowledged;
152 }
153
154 @Override
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700155 public boolean cleared() {
156 return severity.equals(SeverityLevel.CLEARED);
157 }
158
159 @Override
kmcpeake4fe18c82015-11-17 20:07:39 +0000160 public boolean manuallyClearable() {
161 return isManuallyClearable;
162 }
163
164 @Override
165 public String assignedUser() {
166 return assignedUser;
167 }
168
169 @Override
170 public int hashCode() {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000171 // id or timeRaised or timeUpdated may differ
172 return Objects.hash(deviceId, description,
Andrea Campanella8e94b0c2016-04-12 13:58:07 -0700173 source, timeCleared, severity,
174 isServiceAffecting, isAcknowledged,
175 isManuallyClearable, assignedUser);
kmcpeake4fe18c82015-11-17 20:07:39 +0000176 }
177
178 @Override
179 public boolean equals(final Object obj) {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000180 // Make sure equals() is tune with hashCode() so works ok in a hashSet !
kmcpeake4fe18c82015-11-17 20:07:39 +0000181 if (obj == null) {
182 return false;
183 }
184 if (getClass() != obj.getClass()) {
185 return false;
186 }
187 final DefaultAlarm other = (DefaultAlarm) obj;
kmcpeakeb172d5f2015-12-10 11:30:43 +0000188
189 // id or timeRaised or timeUpdated may differ
kmcpeake4fe18c82015-11-17 20:07:39 +0000190 if (!Objects.equals(this.deviceId, other.deviceId)) {
191 return false;
192 }
193 if (!Objects.equals(this.description, other.description)) {
194 return false;
195 }
196 if (!Objects.equals(this.source, other.source)) {
197 return false;
198 }
kmcpeakeb172d5f2015-12-10 11:30:43 +0000199
kmcpeake4fe18c82015-11-17 20:07:39 +0000200 if (!Objects.equals(this.timeCleared, other.timeCleared)) {
201 return false;
202 }
203 if (this.severity != other.severity) {
204 return false;
205 }
206 if (this.isServiceAffecting != other.isServiceAffecting) {
207 return false;
208 }
209 if (this.isAcknowledged != other.isAcknowledged) {
210 return false;
211 }
212 if (this.isManuallyClearable != other.isManuallyClearable) {
213 return false;
214 }
215 if (!Objects.equals(this.assignedUser, other.assignedUser)) {
216 return false;
217 }
218 return true;
219 }
220
221 @Override
222 public String toString() {
223 return toStringHelper(this)
224 .add("id", id)
225 .add("deviceId", deviceId)
226 .add("description", description)
227 .add("source", source)
228 .add("timeRaised", timeRaised)
229 .add("timeUpdated", timeUpdated)
230 .add("timeCleared", timeCleared)
231 .add("severity", severity)
232 .add("serviceAffecting", isServiceAffecting)
233 .add("acknowledged", isAcknowledged)
234 .add("manuallyClearable", isManuallyClearable)
235 .add("assignedUser", assignedUser)
236 .toString();
237 }
238
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700239 /**
240 * Builder for the DefaultAlarm object.
241 */
kmcpeake4fe18c82015-11-17 20:07:39 +0000242 public static class Builder {
243
kmcpeakeb172d5f2015-12-10 11:30:43 +0000244 // Manadatory fields when constructing alarm ...
245 private AlarmId id;
kmcpeake4fe18c82015-11-17 20:07:39 +0000246 private final DeviceId deviceId;
247 private final String description;
kmcpeakeb172d5f2015-12-10 11:30:43 +0000248 private SeverityLevel severity;
kmcpeake4fe18c82015-11-17 20:07:39 +0000249 private final long timeRaised;
250
251 // Optional fields ..
252 private AlarmEntityId source = AlarmEntityId.NONE;
253 private long timeUpdated;
254 private Long timeCleared = null;
255 private boolean isServiceAffecting = false;
256 private boolean isAcknowledged = false;
257 private boolean isManuallyClearable = false;
258 private String assignedUser = null;
259
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700260 /**
261 * Constructs a Builder to create a Default Alarm based on another alarm.
262 *
263 * @param alarm the other alarm
264 */
kmcpeake4fe18c82015-11-17 20:07:39 +0000265 public Builder(final Alarm alarm) {
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700266 this(alarm.id(), alarm.deviceId(), alarm.description(), alarm.severity(), alarm.timeRaised());
kmcpeakeb172d5f2015-12-10 11:30:43 +0000267 this.source = alarm.source();
kmcpeake4fe18c82015-11-17 20:07:39 +0000268 this.timeUpdated = alarm.timeUpdated();
269 this.timeCleared = alarm.timeCleared();
270 this.isServiceAffecting = alarm.serviceAffecting();
271 this.isAcknowledged = alarm.acknowledged();
272 this.isManuallyClearable = alarm.manuallyClearable();
273 this.assignedUser = alarm.assignedUser();
274
275 }
276
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700277 /**
278 * Constructs a Builder to create a Default Alarm.
279 *
280 * @param deviceId the device ID
281 * @param description the Alarm description
282 * @param severity the severity
283 * @param timeRaised when the alarm was raised
284 * @deprecated 1.10.0 - Kingfisher
285 */
286 @Deprecated
kmcpeakeb172d5f2015-12-10 11:30:43 +0000287 public Builder(final DeviceId deviceId,
Andrea Campanella8e94b0c2016-04-12 13:58:07 -0700288 final String description, final SeverityLevel severity, final long timeRaised) {
kmcpeake4fe18c82015-11-17 20:07:39 +0000289 super();
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700290 this.deviceId = deviceId;
291 this.description = description;
292 this.severity = severity;
293 this.timeRaised = timeRaised;
294 // Unless specified time-updated is same as raised.
295 this.timeUpdated = timeRaised;
296 this.id = AlarmId.alarmId(deviceId, Long.toString(timeRaised));
297 }
298
299 /**
300 * Constructs a Builder to create a Default Alarm.
301 *
302 * @param id the AlarmId
303 * @param deviceId the device ID
304 * @param description the Alarm description
305 * @param severity the severity
306 * @param timeRaised when the alarm was raised
307 */
308 public Builder(final AlarmId id, final DeviceId deviceId,
309 final String description, final SeverityLevel severity, final long timeRaised) {
310 super();
311 this.id = id;
kmcpeake4fe18c82015-11-17 20:07:39 +0000312 this.deviceId = deviceId;
313 this.description = description;
314 this.severity = severity;
315 this.timeRaised = timeRaised;
316 // Unless specified time-updated is same as raised.
317 this.timeUpdated = timeRaised;
318 }
319
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700320 /**
321 * Sets the new alarm source.
322 *
323 * @param source the source
324 * @return self for chaining
325 */
kmcpeake4fe18c82015-11-17 20:07:39 +0000326 public Builder forSource(final AlarmEntityId source) {
327 this.source = source;
328 return this;
329 }
330
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700331 /**
332 * Sets the new alarm time updated.
333 *
334 * @param timeUpdated the time
335 * @return self for chaining
336 */
kmcpeake4fe18c82015-11-17 20:07:39 +0000337 public Builder withTimeUpdated(final long timeUpdated) {
338 this.timeUpdated = timeUpdated;
339 return this;
340 }
341
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700342 /**
343 * Sets the new alarm time cleared.
344 *
345 * @param timeCleared the time
346 * @return self for chaining
347 */
kmcpeake4fe18c82015-11-17 20:07:39 +0000348 public Builder withTimeCleared(final Long timeCleared) {
349 this.timeCleared = timeCleared;
350 return this;
351 }
352
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700353 /**
354 * Sets the new alarm Id.
355 *
356 * @param id the id
357 * @return self for chaining
358 * @deprecated 1.10.0- Kingfisher
359 */
360 @Deprecated
kmcpeakeb172d5f2015-12-10 11:30:43 +0000361 public Builder withId(final AlarmId id) {
362 this.id = id;
363 return this;
364 }
365
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700366 /**
367 * Clears the alarm that is being created.
368 *
369 * @return self for chaining
370 */
kmcpeakeb172d5f2015-12-10 11:30:43 +0000371 public Builder clear() {
372 this.severity = SeverityLevel.CLEARED;
373 final long now = System.currentTimeMillis();
374 return withTimeCleared(now).withTimeUpdated(now);
375 }
376
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700377 /**
378 * Sets the new alarm service affecting flag.
379 *
380 * @param isServiceAffecting the service affecting flag
381 * @return self for chaining
382 */
kmcpeake4fe18c82015-11-17 20:07:39 +0000383 public Builder withServiceAffecting(final boolean isServiceAffecting) {
384 this.isServiceAffecting = isServiceAffecting;
385 return this;
386 }
387
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700388 /**
389 * Sets the new alarm acknowledged flag.
390 *
391 * @param isAcknowledged the acknowledged flag
392 * @return self for chaining
393 */
kmcpeake4fe18c82015-11-17 20:07:39 +0000394 public Builder withAcknowledged(final boolean isAcknowledged) {
395 this.isAcknowledged = isAcknowledged;
396 return this;
397 }
398
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700399 /**
400 * Sets the new alarm the manually clearable flag.
401 *
402 * @param isManuallyClearable the manually clearable flag
403 * @return self for chaining
404 */
kmcpeake4fe18c82015-11-17 20:07:39 +0000405 public Builder withManuallyClearable(final boolean isManuallyClearable) {
406 this.isManuallyClearable = isManuallyClearable;
407 return this;
408 }
409
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700410 /**
411 * Sets the new alarm assigned user.
412 *
413 * @param assignedUser the user
414 * @return self for chaining
415 */
kmcpeake4fe18c82015-11-17 20:07:39 +0000416 public Builder withAssignedUser(final String assignedUser) {
417 this.assignedUser = assignedUser;
418 return this;
419 }
420
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700421 /**
422 * Builds the alarm.
423 *
424 * @return self for chaining
425 */
kmcpeake4fe18c82015-11-17 20:07:39 +0000426 public DefaultAlarm build() {
427 checkNotNull(id, "Must specify an alarm id");
428 checkNotNull(deviceId, "Must specify a device");
429 checkNotNull(description, "Must specify a description");
kmcpeake4fe18c82015-11-17 20:07:39 +0000430 checkNotNull(severity, "Must specify a severity");
431
432 return new DefaultAlarm(id, deviceId, description, source, timeRaised, timeUpdated, timeCleared,
Andrea Campanella8e94b0c2016-04-12 13:58:07 -0700433 severity, isServiceAffecting, isAcknowledged, isManuallyClearable, assignedUser);
kmcpeake4fe18c82015-11-17 20:07:39 +0000434 }
435 }
436}