blob: 013eec1b212518c63d458f743a5bd0706b838e89 [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
2 * Copyright 2014-2015 Open Networking Laboratory
3 *
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;
36 private final long timeUpdated;
37 private final Long timeCleared;
38 private final SeverityLevel severity;
39 private final boolean isServiceAffecting;
40 private final boolean isAcknowledged;
41 private final boolean isManuallyClearable;
42 private final String assignedUser;
43
44 /**
45 * Instantiates a new Default alarm.
46 *
47 * @param id the id
48 * @param deviceId the device id
49 * @param description the description
50 * @param source the source, null indicates none.
51 * @param timeRaised the time raised.
52 * @param timeUpdated the time last updated.
53 * @param timeCleared the time cleared, null indicates uncleared.
54 * @param severity the severity
55 * @param isServiceAffecting the service affecting
56 * @param isAcknowledged the acknowledged
57 * @param isManuallyClearable the manually clearable
58 * @param assignedUser the assigned user, `null` indicates none.
59 */
60 private DefaultAlarm(final AlarmId id,
61 final DeviceId deviceId,
62 final String description,
63 final AlarmEntityId source,
64 final long timeRaised,
65 final long timeUpdated,
66 final Long timeCleared,
67 final SeverityLevel severity,
68 final boolean isServiceAffecting,
69 final boolean isAcknowledged,
70 final boolean isManuallyClearable,
71 final String assignedUser) {
72 this.id = id;
73 this.deviceId = deviceId;
74 this.description = description;
75 this.source = source;
76 this.timeRaised = timeRaised;
77 this.timeUpdated = timeUpdated;
78 this.timeCleared = timeCleared;
79 this.severity = severity;
80 this.isServiceAffecting = isServiceAffecting;
81 this.isAcknowledged = isAcknowledged;
82 this.isManuallyClearable = isManuallyClearable;
83 this.assignedUser = assignedUser;
84 }
85
86 @Override
87 public AlarmId id() {
88 return id;
89 }
90
91 @Override
92 public DeviceId deviceId() {
93 return deviceId;
94 }
95
96 @Override
97 public String description() {
98 return description;
99 }
100
101 @Override
102 public AlarmEntityId source() {
103 return source;
104 }
105
106 @Override
107 public long timeRaised() {
108 return timeRaised;
109 }
110
111 @Override
112 public long timeUpdated() {
113 return timeUpdated;
114 }
115
116 @Override
117 public Long timeCleared() {
118 return timeCleared;
119 }
120
121 @Override
122 public SeverityLevel severity() {
123 return severity;
124 }
125
126 @Override
127 public boolean serviceAffecting() {
128 return isServiceAffecting;
129 }
130
131 @Override
132 public boolean acknowledged() {
133 return isAcknowledged;
134 }
135
136 @Override
137 public boolean manuallyClearable() {
138 return isManuallyClearable;
139 }
140
141 @Override
142 public String assignedUser() {
143 return assignedUser;
144 }
145
146 @Override
147 public int hashCode() {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000148 // id or timeRaised or timeUpdated may differ
149 return Objects.hash(deviceId, description,
150 source, timeCleared, severity,
kmcpeake4fe18c82015-11-17 20:07:39 +0000151 isServiceAffecting, isAcknowledged,
152 isManuallyClearable, assignedUser);
153 }
154
155 @Override
156 public boolean equals(final Object obj) {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000157 // Make sure equals() is tune with hashCode() so works ok in a hashSet !
kmcpeake4fe18c82015-11-17 20:07:39 +0000158 if (obj == null) {
159 return false;
160 }
161 if (getClass() != obj.getClass()) {
162 return false;
163 }
164 final DefaultAlarm other = (DefaultAlarm) obj;
kmcpeakeb172d5f2015-12-10 11:30:43 +0000165
166 // id or timeRaised or timeUpdated may differ
kmcpeake4fe18c82015-11-17 20:07:39 +0000167 if (!Objects.equals(this.deviceId, other.deviceId)) {
168 return false;
169 }
170 if (!Objects.equals(this.description, other.description)) {
171 return false;
172 }
173 if (!Objects.equals(this.source, other.source)) {
174 return false;
175 }
kmcpeakeb172d5f2015-12-10 11:30:43 +0000176
kmcpeake4fe18c82015-11-17 20:07:39 +0000177 if (!Objects.equals(this.timeCleared, other.timeCleared)) {
178 return false;
179 }
180 if (this.severity != other.severity) {
181 return false;
182 }
183 if (this.isServiceAffecting != other.isServiceAffecting) {
184 return false;
185 }
186 if (this.isAcknowledged != other.isAcknowledged) {
187 return false;
188 }
189 if (this.isManuallyClearable != other.isManuallyClearable) {
190 return false;
191 }
192 if (!Objects.equals(this.assignedUser, other.assignedUser)) {
193 return false;
194 }
195 return true;
196 }
197
198 @Override
199 public String toString() {
200 return toStringHelper(this)
201 .add("id", id)
202 .add("deviceId", deviceId)
203 .add("description", description)
204 .add("source", source)
205 .add("timeRaised", timeRaised)
206 .add("timeUpdated", timeUpdated)
207 .add("timeCleared", timeCleared)
208 .add("severity", severity)
209 .add("serviceAffecting", isServiceAffecting)
210 .add("acknowledged", isAcknowledged)
211 .add("manuallyClearable", isManuallyClearable)
212 .add("assignedUser", assignedUser)
213 .toString();
214 }
215
216 public static class Builder {
217
kmcpeakeb172d5f2015-12-10 11:30:43 +0000218 // Manadatory fields when constructing alarm ...
219 private AlarmId id;
kmcpeake4fe18c82015-11-17 20:07:39 +0000220 private final DeviceId deviceId;
221 private final String description;
kmcpeakeb172d5f2015-12-10 11:30:43 +0000222 private SeverityLevel severity;
kmcpeake4fe18c82015-11-17 20:07:39 +0000223 private final long timeRaised;
224
225 // Optional fields ..
226 private AlarmEntityId source = AlarmEntityId.NONE;
227 private long timeUpdated;
228 private Long timeCleared = null;
229 private boolean isServiceAffecting = false;
230 private boolean isAcknowledged = false;
231 private boolean isManuallyClearable = false;
232 private String assignedUser = null;
233
234 public Builder(final Alarm alarm) {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000235 this(alarm.deviceId(), alarm.description(), alarm.severity(), alarm.timeRaised());
236 this.source = alarm.source();
kmcpeake4fe18c82015-11-17 20:07:39 +0000237 this.timeUpdated = alarm.timeUpdated();
238 this.timeCleared = alarm.timeCleared();
239 this.isServiceAffecting = alarm.serviceAffecting();
240 this.isAcknowledged = alarm.acknowledged();
241 this.isManuallyClearable = alarm.manuallyClearable();
242 this.assignedUser = alarm.assignedUser();
243
244 }
245
kmcpeakeb172d5f2015-12-10 11:30:43 +0000246 public Builder(final DeviceId deviceId,
kmcpeake4fe18c82015-11-17 20:07:39 +0000247 final String description, final SeverityLevel severity, final long timeRaised) {
248 super();
kmcpeakeb172d5f2015-12-10 11:30:43 +0000249 this.id = AlarmId.NONE;
kmcpeake4fe18c82015-11-17 20:07:39 +0000250 this.deviceId = deviceId;
251 this.description = description;
252 this.severity = severity;
253 this.timeRaised = timeRaised;
254 // Unless specified time-updated is same as raised.
255 this.timeUpdated = timeRaised;
256 }
257
258 public Builder forSource(final AlarmEntityId source) {
259 this.source = source;
260 return this;
261 }
262
263 public Builder withTimeUpdated(final long timeUpdated) {
264 this.timeUpdated = timeUpdated;
265 return this;
266 }
267
268 public Builder withTimeCleared(final Long timeCleared) {
269 this.timeCleared = timeCleared;
270 return this;
271 }
272
kmcpeakeb172d5f2015-12-10 11:30:43 +0000273 public Builder withId(final AlarmId id) {
274 this.id = id;
275 return this;
276 }
277
278 public Builder clear() {
279 this.severity = SeverityLevel.CLEARED;
280 final long now = System.currentTimeMillis();
281 return withTimeCleared(now).withTimeUpdated(now);
282 }
283
kmcpeake4fe18c82015-11-17 20:07:39 +0000284 public Builder withServiceAffecting(final boolean isServiceAffecting) {
285 this.isServiceAffecting = isServiceAffecting;
286 return this;
287 }
288
289 public Builder withAcknowledged(final boolean isAcknowledged) {
290 this.isAcknowledged = isAcknowledged;
291 return this;
292 }
293
294 public Builder withManuallyClearable(final boolean isManuallyClearable) {
295 this.isManuallyClearable = isManuallyClearable;
296 return this;
297 }
298
299 public Builder withAssignedUser(final String assignedUser) {
300 this.assignedUser = assignedUser;
301 return this;
302 }
303
304 public DefaultAlarm build() {
305 checkNotNull(id, "Must specify an alarm id");
306 checkNotNull(deviceId, "Must specify a device");
307 checkNotNull(description, "Must specify a description");
308 checkNotNull(timeRaised, "Must specify a time raised");
309 checkNotNull(timeUpdated, "Must specify a time updated");
310 checkNotNull(severity, "Must specify a severity");
311
312 return new DefaultAlarm(id, deviceId, description, source, timeRaised, timeUpdated, timeCleared,
313 severity, isServiceAffecting, isAcknowledged, isManuallyClearable, assignedUser);
314 }
315 }
316}