ONOS-4380 Refactor AlarmId and Alarm construction and update
Change-Id: I0117afda723ba27aadb1db306f7ce15b666f102d
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/DefaultAlarm.java b/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/DefaultAlarm.java
index 162b3df..15d6292 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/DefaultAlarm.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/DefaultAlarm.java
@@ -25,7 +25,6 @@
/**
* Default implementation of an alarm.
*/
-//TODO simpler creation and updating.
public final class DefaultAlarm implements Alarm {
private final AlarmId id;
@@ -34,14 +33,16 @@
private final String description;
private final AlarmEntityId source;
private final long timeRaised;
- private final long timeUpdated;
- private final Long timeCleared;
- private final SeverityLevel severity;
private final boolean isServiceAffecting;
private final boolean isAcknowledged;
private final boolean isManuallyClearable;
private final String assignedUser;
+ private final SeverityLevel severity;
+ private final long timeUpdated;
+ private final Long timeCleared;
+
+
//Only for Kryo
DefaultAlarm() {
id = null;
@@ -151,6 +152,11 @@
}
@Override
+ public boolean cleared() {
+ return severity.equals(SeverityLevel.CLEARED);
+ }
+
+ @Override
public boolean manuallyClearable() {
return isManuallyClearable;
}
@@ -230,6 +236,9 @@
.toString();
}
+ /**
+ * Builder for the DefaultAlarm object.
+ */
public static class Builder {
// Manadatory fields when constructing alarm ...
@@ -248,8 +257,13 @@
private boolean isManuallyClearable = false;
private String assignedUser = null;
+ /**
+ * Constructs a Builder to create a Default Alarm based on another alarm.
+ *
+ * @param alarm the other alarm
+ */
public Builder(final Alarm alarm) {
- this(alarm.deviceId(), alarm.description(), alarm.severity(), alarm.timeRaised());
+ this(alarm.id(), alarm.deviceId(), alarm.description(), alarm.severity(), alarm.timeRaised());
this.source = alarm.source();
this.timeUpdated = alarm.timeUpdated();
this.timeCleared = alarm.timeCleared();
@@ -260,10 +274,41 @@
}
+ /**
+ * Constructs a Builder to create a Default Alarm.
+ *
+ * @param deviceId the device ID
+ * @param description the Alarm description
+ * @param severity the severity
+ * @param timeRaised when the alarm was raised
+ * @deprecated 1.10.0 - Kingfisher
+ */
+ @Deprecated
public Builder(final DeviceId deviceId,
final String description, final SeverityLevel severity, final long timeRaised) {
super();
- this.id = AlarmId.NONE;
+ this.deviceId = deviceId;
+ this.description = description;
+ this.severity = severity;
+ this.timeRaised = timeRaised;
+ // Unless specified time-updated is same as raised.
+ this.timeUpdated = timeRaised;
+ this.id = AlarmId.alarmId(deviceId, Long.toString(timeRaised));
+ }
+
+ /**
+ * Constructs a Builder to create a Default Alarm.
+ *
+ * @param id the AlarmId
+ * @param deviceId the device ID
+ * @param description the Alarm description
+ * @param severity the severity
+ * @param timeRaised when the alarm was raised
+ */
+ public Builder(final AlarmId id, final DeviceId deviceId,
+ final String description, final SeverityLevel severity, final long timeRaised) {
+ super();
+ this.id = id;
this.deviceId = deviceId;
this.description = description;
this.severity = severity;
@@ -272,52 +317,112 @@
this.timeUpdated = timeRaised;
}
+ /**
+ * Sets the new alarm source.
+ *
+ * @param source the source
+ * @return self for chaining
+ */
public Builder forSource(final AlarmEntityId source) {
this.source = source;
return this;
}
+ /**
+ * Sets the new alarm time updated.
+ *
+ * @param timeUpdated the time
+ * @return self for chaining
+ */
public Builder withTimeUpdated(final long timeUpdated) {
this.timeUpdated = timeUpdated;
return this;
}
+ /**
+ * Sets the new alarm time cleared.
+ *
+ * @param timeCleared the time
+ * @return self for chaining
+ */
public Builder withTimeCleared(final Long timeCleared) {
this.timeCleared = timeCleared;
return this;
}
+ /**
+ * Sets the new alarm Id.
+ *
+ * @param id the id
+ * @return self for chaining
+ * @deprecated 1.10.0- Kingfisher
+ */
+ @Deprecated
public Builder withId(final AlarmId id) {
this.id = id;
return this;
}
+ /**
+ * Clears the alarm that is being created.
+ *
+ * @return self for chaining
+ */
public Builder clear() {
this.severity = SeverityLevel.CLEARED;
final long now = System.currentTimeMillis();
return withTimeCleared(now).withTimeUpdated(now);
}
+ /**
+ * Sets the new alarm service affecting flag.
+ *
+ * @param isServiceAffecting the service affecting flag
+ * @return self for chaining
+ */
public Builder withServiceAffecting(final boolean isServiceAffecting) {
this.isServiceAffecting = isServiceAffecting;
return this;
}
+ /**
+ * Sets the new alarm acknowledged flag.
+ *
+ * @param isAcknowledged the acknowledged flag
+ * @return self for chaining
+ */
public Builder withAcknowledged(final boolean isAcknowledged) {
this.isAcknowledged = isAcknowledged;
return this;
}
+ /**
+ * Sets the new alarm the manually clearable flag.
+ *
+ * @param isManuallyClearable the manually clearable flag
+ * @return self for chaining
+ */
public Builder withManuallyClearable(final boolean isManuallyClearable) {
this.isManuallyClearable = isManuallyClearable;
return this;
}
+ /**
+ * Sets the new alarm assigned user.
+ *
+ * @param assignedUser the user
+ * @return self for chaining
+ */
public Builder withAssignedUser(final String assignedUser) {
this.assignedUser = assignedUser;
return this;
}
+ /**
+ * Builds the alarm.
+ *
+ * @return self for chaining
+ */
public DefaultAlarm build() {
checkNotNull(id, "Must specify an alarm id");
checkNotNull(deviceId, "Must specify a device");