blob: afa366ad552fcb0c1ba09b2de82f2586297b0cc2 [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() {
148 return Objects.hash(id, deviceId, description,
149 source, timeRaised, timeUpdated, timeCleared, severity,
150 isServiceAffecting, isAcknowledged,
151 isManuallyClearable, assignedUser);
152 }
153
154 @Override
155 public boolean equals(final Object obj) {
156 if (obj == null) {
157 return false;
158 }
159 if (getClass() != obj.getClass()) {
160 return false;
161 }
162 final DefaultAlarm other = (DefaultAlarm) obj;
163 if (!Objects.equals(this.id, other.id)) {
164 return false;
165 }
166 if (!Objects.equals(this.deviceId, other.deviceId)) {
167 return false;
168 }
169 if (!Objects.equals(this.description, other.description)) {
170 return false;
171 }
172 if (!Objects.equals(this.source, other.source)) {
173 return false;
174 }
175 if (this.timeRaised != other.timeRaised) {
176 return false;
177 }
178 if (this.timeUpdated != other.timeUpdated) {
179 return false;
180 }
181 if (!Objects.equals(this.timeCleared, other.timeCleared)) {
182 return false;
183 }
184 if (this.severity != other.severity) {
185 return false;
186 }
187 if (this.isServiceAffecting != other.isServiceAffecting) {
188 return false;
189 }
190 if (this.isAcknowledged != other.isAcknowledged) {
191 return false;
192 }
193 if (this.isManuallyClearable != other.isManuallyClearable) {
194 return false;
195 }
196 if (!Objects.equals(this.assignedUser, other.assignedUser)) {
197 return false;
198 }
199 return true;
200 }
201
202 @Override
203 public String toString() {
204 return toStringHelper(this)
205 .add("id", id)
206 .add("deviceId", deviceId)
207 .add("description", description)
208 .add("source", source)
209 .add("timeRaised", timeRaised)
210 .add("timeUpdated", timeUpdated)
211 .add("timeCleared", timeCleared)
212 .add("severity", severity)
213 .add("serviceAffecting", isServiceAffecting)
214 .add("acknowledged", isAcknowledged)
215 .add("manuallyClearable", isManuallyClearable)
216 .add("assignedUser", assignedUser)
217 .toString();
218 }
219
220 public static class Builder {
221
222 // Manadatory fields ..
223 private final AlarmId id;
224 private final DeviceId deviceId;
225 private final String description;
226 private final SeverityLevel severity;
227 private final long timeRaised;
228
229 // Optional fields ..
230 private AlarmEntityId source = AlarmEntityId.NONE;
231 private long timeUpdated;
232 private Long timeCleared = null;
233 private boolean isServiceAffecting = false;
234 private boolean isAcknowledged = false;
235 private boolean isManuallyClearable = false;
236 private String assignedUser = null;
237
238 public Builder(final Alarm alarm) {
239 this(alarm.id(), alarm.deviceId(), alarm.description(), alarm.severity(), alarm.timeRaised());
240 this.source = AlarmEntityId.NONE;
241 this.timeUpdated = alarm.timeUpdated();
242 this.timeCleared = alarm.timeCleared();
243 this.isServiceAffecting = alarm.serviceAffecting();
244 this.isAcknowledged = alarm.acknowledged();
245 this.isManuallyClearable = alarm.manuallyClearable();
246 this.assignedUser = alarm.assignedUser();
247
248 }
249
250 public Builder(final AlarmId id, final DeviceId deviceId,
251 final String description, final SeverityLevel severity, final long timeRaised) {
252 super();
253 this.id = id;
254 this.deviceId = deviceId;
255 this.description = description;
256 this.severity = severity;
257 this.timeRaised = timeRaised;
258 // Unless specified time-updated is same as raised.
259 this.timeUpdated = timeRaised;
260 }
261
262 public Builder forSource(final AlarmEntityId source) {
263 this.source = source;
264 return this;
265 }
266
267 public Builder withTimeUpdated(final long timeUpdated) {
268 this.timeUpdated = timeUpdated;
269 return this;
270 }
271
272 public Builder withTimeCleared(final Long timeCleared) {
273 this.timeCleared = timeCleared;
274 return this;
275 }
276
277 public Builder withServiceAffecting(final boolean isServiceAffecting) {
278 this.isServiceAffecting = isServiceAffecting;
279 return this;
280 }
281
282 public Builder withAcknowledged(final boolean isAcknowledged) {
283 this.isAcknowledged = isAcknowledged;
284 return this;
285 }
286
287 public Builder withManuallyClearable(final boolean isManuallyClearable) {
288 this.isManuallyClearable = isManuallyClearable;
289 return this;
290 }
291
292 public Builder withAssignedUser(final String assignedUser) {
293 this.assignedUser = assignedUser;
294 return this;
295 }
296
297 public DefaultAlarm build() {
298 checkNotNull(id, "Must specify an alarm id");
299 checkNotNull(deviceId, "Must specify a device");
300 checkNotNull(description, "Must specify a description");
301 checkNotNull(timeRaised, "Must specify a time raised");
302 checkNotNull(timeUpdated, "Must specify a time updated");
303 checkNotNull(severity, "Must specify a severity");
304
305 return new DefaultAlarm(id, deviceId, description, source, timeRaised, timeUpdated, timeCleared,
306 severity, isServiceAffecting, isAcknowledged, isManuallyClearable, assignedUser);
307 }
308 }
309}