kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 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 | */ |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 16 | package org.onosproject.alarm; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 17 | |
| 18 | import org.onosproject.net.DeviceId; |
| 19 | |
| 20 | import java.util.Objects; |
| 21 | |
| 22 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | |
| 25 | /** |
| 26 | * Default implementation of an alarm. |
| 27 | */ |
| 28 | public 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; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 36 | private final boolean isServiceAffecting; |
| 37 | private final boolean isAcknowledged; |
| 38 | private final boolean isManuallyClearable; |
| 39 | private final String assignedUser; |
| 40 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 41 | private final SeverityLevel severity; |
| 42 | private final long timeUpdated; |
| 43 | private final Long timeCleared; |
| 44 | |
| 45 | |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 46 | //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 | |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 62 | /** |
| 63 | * Instantiates a new Default alarm. |
| 64 | * |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 65 | * @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 |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 75 | * @param isManuallyClearable the manually clearable |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 76 | * @param assignedUser the assigned user, `null` indicates none. |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 77 | */ |
| 78 | private DefaultAlarm(final AlarmId id, |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 79 | 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) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 90 | 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 Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 155 | public boolean cleared() { |
| 156 | return severity.equals(SeverityLevel.CLEARED); |
| 157 | } |
| 158 | |
| 159 | @Override |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 160 | 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() { |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 171 | // id or timeRaised or timeUpdated may differ |
| 172 | return Objects.hash(deviceId, description, |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 173 | source, timeCleared, severity, |
| 174 | isServiceAffecting, isAcknowledged, |
| 175 | isManuallyClearable, assignedUser); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public boolean equals(final Object obj) { |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 180 | // Make sure equals() is tune with hashCode() so works ok in a hashSet ! |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 181 | if (obj == null) { |
| 182 | return false; |
| 183 | } |
| 184 | if (getClass() != obj.getClass()) { |
| 185 | return false; |
| 186 | } |
| 187 | final DefaultAlarm other = (DefaultAlarm) obj; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 188 | |
| 189 | // id or timeRaised or timeUpdated may differ |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 190 | 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 | } |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 199 | |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 200 | 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 Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 239 | /** |
| 240 | * Builder for the DefaultAlarm object. |
| 241 | */ |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 242 | public static class Builder { |
| 243 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 244 | // Manadatory fields when constructing alarm ... |
| 245 | private AlarmId id; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 246 | private final DeviceId deviceId; |
| 247 | private final String description; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 248 | private SeverityLevel severity; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 249 | 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 Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 260 | /** |
| 261 | * Constructs a Builder to create a Default Alarm based on another alarm. |
| 262 | * |
| 263 | * @param alarm the other alarm |
| 264 | */ |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 265 | public Builder(final Alarm alarm) { |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 266 | this(alarm.id(), alarm.deviceId(), alarm.description(), alarm.severity(), alarm.timeRaised()); |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 267 | this.source = alarm.source(); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 268 | 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 Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 277 | /** |
| 278 | * Constructs a Builder to create a Default Alarm. |
| 279 | * |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 280 | * @param id the AlarmId |
| 281 | * @param deviceId the device ID |
| 282 | * @param description the Alarm description |
| 283 | * @param severity the severity |
| 284 | * @param timeRaised when the alarm was raised |
| 285 | */ |
| 286 | public Builder(final AlarmId id, final DeviceId deviceId, |
| 287 | final String description, final SeverityLevel severity, final long timeRaised) { |
| 288 | super(); |
| 289 | this.id = id; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 290 | 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 | } |
| 297 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 298 | /** |
| 299 | * Sets the new alarm source. |
| 300 | * |
| 301 | * @param source the source |
| 302 | * @return self for chaining |
| 303 | */ |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 304 | public Builder forSource(final AlarmEntityId source) { |
| 305 | this.source = source; |
| 306 | return this; |
| 307 | } |
| 308 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 309 | /** |
| 310 | * Sets the new alarm time updated. |
| 311 | * |
| 312 | * @param timeUpdated the time |
| 313 | * @return self for chaining |
| 314 | */ |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 315 | public Builder withTimeUpdated(final long timeUpdated) { |
| 316 | this.timeUpdated = timeUpdated; |
| 317 | return this; |
| 318 | } |
| 319 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 320 | /** |
| 321 | * Sets the new alarm time cleared. |
| 322 | * |
| 323 | * @param timeCleared the time |
| 324 | * @return self for chaining |
| 325 | */ |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 326 | public Builder withTimeCleared(final Long timeCleared) { |
| 327 | this.timeCleared = timeCleared; |
| 328 | return this; |
| 329 | } |
| 330 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 331 | /** |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 332 | * Clears the alarm that is being created. |
| 333 | * |
| 334 | * @return self for chaining |
| 335 | */ |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 336 | public Builder clear() { |
| 337 | this.severity = SeverityLevel.CLEARED; |
| 338 | final long now = System.currentTimeMillis(); |
| 339 | return withTimeCleared(now).withTimeUpdated(now); |
| 340 | } |
| 341 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 342 | /** |
| 343 | * Sets the new alarm service affecting flag. |
| 344 | * |
| 345 | * @param isServiceAffecting the service affecting flag |
| 346 | * @return self for chaining |
| 347 | */ |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 348 | public Builder withServiceAffecting(final boolean isServiceAffecting) { |
| 349 | this.isServiceAffecting = isServiceAffecting; |
| 350 | return this; |
| 351 | } |
| 352 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 353 | /** |
| 354 | * Sets the new alarm acknowledged flag. |
| 355 | * |
| 356 | * @param isAcknowledged the acknowledged flag |
| 357 | * @return self for chaining |
| 358 | */ |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 359 | public Builder withAcknowledged(final boolean isAcknowledged) { |
| 360 | this.isAcknowledged = isAcknowledged; |
| 361 | return this; |
| 362 | } |
| 363 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 364 | /** |
| 365 | * Sets the new alarm the manually clearable flag. |
| 366 | * |
| 367 | * @param isManuallyClearable the manually clearable flag |
| 368 | * @return self for chaining |
| 369 | */ |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 370 | public Builder withManuallyClearable(final boolean isManuallyClearable) { |
| 371 | this.isManuallyClearable = isManuallyClearable; |
| 372 | return this; |
| 373 | } |
| 374 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 375 | /** |
| 376 | * Sets the new alarm assigned user. |
| 377 | * |
| 378 | * @param assignedUser the user |
| 379 | * @return self for chaining |
| 380 | */ |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 381 | public Builder withAssignedUser(final String assignedUser) { |
| 382 | this.assignedUser = assignedUser; |
| 383 | return this; |
| 384 | } |
| 385 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 386 | /** |
| 387 | * Builds the alarm. |
| 388 | * |
| 389 | * @return self for chaining |
| 390 | */ |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 391 | public DefaultAlarm build() { |
| 392 | checkNotNull(id, "Must specify an alarm id"); |
| 393 | checkNotNull(deviceId, "Must specify a device"); |
| 394 | checkNotNull(description, "Must specify a description"); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 395 | checkNotNull(severity, "Must specify a severity"); |
| 396 | |
| 397 | return new DefaultAlarm(id, deviceId, description, source, timeRaised, timeUpdated, timeCleared, |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 398 | severity, isServiceAffecting, isAcknowledged, isManuallyClearable, assignedUser); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | } |