diff options
author | jhh <jorge.hernandez-herrero@att.com> | 2020-01-14 22:48:45 -0600 |
---|---|---|
committer | jhh <jorge.hernandez-herrero@att.com> | 2020-01-16 08:44:53 -0600 |
commit | 08d766437dc19037f21b57578448748e013bc031 (patch) | |
tree | 1cccd9f08f5d342b160a32c8bcdff37968792c89 /models-interactions/model-impl | |
parent | b321d781b99a869e179b8003260f92969a81b43e (diff) |
Added Time agnostic Onset and Abated classes
These classes can be used for comparison of alarm skeletons
independently of the time at which they were produced.
Issue-ID: POLICY-2323
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: I85b9d6a429de56f056eb0a6caa9e4f90fbd68918
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Diffstat (limited to 'models-interactions/model-impl')
10 files changed, 493 insertions, 18 deletions
diff --git a/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/Abated.java b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/Abated.java new file mode 100644 index 000000000..3488e3b6a --- /dev/null +++ b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/Abated.java @@ -0,0 +1,56 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop; + +import lombok.ToString; + +/** + * An ABATED event. + */ +@ToString(callSuper = true) +public class Abated extends VirtualControlLoopEvent { + private static final long serialVersionUID = -90742191326653587L; + + /** + * No arguments constructor. + */ + public Abated() { + super(); + setClosedLoopEventStatus(ControlLoopEventStatus.ABATED); + } + + /** + * Constructor from a VirtualControlLoop event. + */ + public Abated(VirtualControlLoopEvent event) { + super(event); + setClosedLoopEventStatus(ControlLoopEventStatus.ABATED); + } + + @Override + public void setClosedLoopEventStatus(ControlLoopEventStatus status) { + if (status != ControlLoopEventStatus.ABATED) { + throw new IllegalArgumentException("Not an ABATED event status"); + } + + super.setClosedLoopEventStatus(status); + } +} diff --git a/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/CanonicalAbated.java b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/CanonicalAbated.java new file mode 100644 index 000000000..f1f2db3ff --- /dev/null +++ b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/CanonicalAbated.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop; + +import lombok.NoArgsConstructor; +import lombok.ToString; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; + +/** + * An ABATED event that determines equality with other ABATED + * with only non-time dependent values. + */ + +@NoArgsConstructor +@ToString(callSuper = true) +public class CanonicalAbated extends Abated { + private static final long serialVersionUID = 284865663873284818L; + + public CanonicalAbated(VirtualControlLoopEvent event) { + super(event); + } + + @Override + public boolean equals(Object other) { + // see hashcode method notes + return EqualsBuilder.reflectionEquals( + this, other, "requestId", "closedLoopAlarmStart", "closedLoopAlarmEnd"); + } + + @Override + public int hashCode() { + // The reflection based implementation has been chosen + // for maintenance reasons, even though may incur in some + // performance overhead. The other possibility is to use + // Objects.hash(..) but will require to spell out all fields + // to be considered, which are many more than the exceptions, + // in addition this class would need to be updated as new fields + // are added. Other option to consider in the future is to + // restructure the class hierarchy. Note that could not use + // lombok annotations to exclude fields from superclasses. + return + HashCodeBuilder.reflectionHashCode( + this, "requestId", "closedLoopAlarmStart", "closedLoopAlarmEnd"); + } + +} diff --git a/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/CanonicalOnset.java b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/CanonicalOnset.java new file mode 100644 index 000000000..98aa433f2 --- /dev/null +++ b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/CanonicalOnset.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop; + +import lombok.NoArgsConstructor; +import lombok.ToString; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; + +/** + * An ONSET event that determines equality with other ONSET events + * with only non-time dependent values. + */ + +@NoArgsConstructor +@ToString(callSuper = true) +public class CanonicalOnset extends Onset { + private static final long serialVersionUID = 284865663873284818L; + + public CanonicalOnset(VirtualControlLoopEvent event) { + super(event); + } + + @Override + public boolean equals(Object other) { + // see hashcode method notes + return EqualsBuilder.reflectionEquals( + this, other, "requestId", "closedLoopAlarmStart", "closedLoopAlarmEnd"); + } + + @Override + public int hashCode() { + // The reflection based implementation has been chosen + // for maintenance reasons, even though may incur in some + // performance overhead. The other possibility is to use + // Objects.hash(..) but will require to spell out all fields + // to be considered, which are many more than the exceptions, + // in addition this class would need to be updated as new fields + // are added. Other option to consider in the future is to + // restructure the class hierarchy. Note that could not use + // lombok annotations to exclude fields from superclasses. + return + HashCodeBuilder.reflectionHashCode( + this, "requestId", "closedLoopAlarmStart", "closedLoopAlarmEnd"); + } + +} diff --git a/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/ControlLoopEvent.java b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/ControlLoopEvent.java index bbe902ac2..3106e3909 100644 --- a/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/ControlLoopEvent.java +++ b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/ControlLoopEvent.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * controlloop * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -35,19 +35,19 @@ public abstract class ControlLoopEvent implements Serializable { private static final long serialVersionUID = 2391252138583119195L; @SerializedName("requestID") - private UUID requestId; + protected UUID requestId; @SerializedName("target_type") - private String targetType; - private String closedLoopControlName; - private String version = "1.0.2"; - private String closedLoopEventClient; - private String target; - private String from; - private String policyScope; - private String policyName; - private String policyVersion; - private ControlLoopEventStatus closedLoopEventStatus; - private Map<String, String> additionalEventParams; + protected String targetType; + protected String closedLoopControlName; + protected String version = "1.0.2"; + protected String closedLoopEventClient; + protected String target; + protected String from; + protected String policyScope; + protected String policyName; + protected String policyVersion; + protected ControlLoopEventStatus closedLoopEventStatus; + protected Map<String, String> additionalEventParams; /** * Construct an instance from an existing instance. diff --git a/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/Onset.java b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/Onset.java new file mode 100644 index 000000000..41a0ebab4 --- /dev/null +++ b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/Onset.java @@ -0,0 +1,58 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop; + +import lombok.NonNull; +import lombok.ToString; + +/** + * An ONSET event. + */ + +@ToString(callSuper = true) +public class Onset extends VirtualControlLoopEvent { + private static final long serialVersionUID = -90742191326653587L; + + /** + * No arguments constructor. + */ + public Onset() { + super(); + setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + } + + /** + * Constructor from a VirtualControlLoop event. + */ + public Onset(@NonNull VirtualControlLoopEvent event) { + super(event); + setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + } + + @Override + public void setClosedLoopEventStatus(ControlLoopEventStatus status) { + if (status != ControlLoopEventStatus.ONSET) { + throw new IllegalArgumentException("Not an ONSET event status"); + } + + super.setClosedLoopEventStatus(status); + } +} diff --git a/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/VirtualControlLoopEvent.java b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/VirtualControlLoopEvent.java index 24cc81dcd..3319105ea 100644 --- a/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/VirtualControlLoopEvent.java +++ b/models-interactions/model-impl/events/src/main/java/org/onap/policy/controlloop/VirtualControlLoopEvent.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * controlloop * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -29,19 +29,21 @@ import java.util.Map; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; +import lombok.ToString; @Data @EqualsAndHashCode(callSuper = true) @NoArgsConstructor +@ToString(callSuper = true) public class VirtualControlLoopEvent extends ControlLoopEvent { private static final long serialVersionUID = -5752405682246066226L; @SerializedName("AAI") - private Map<String, String> aai = new HashMap<>(); - private String payload; - private Instant closedLoopAlarmStart; - private Instant closedLoopAlarmEnd; + protected Map<String, String> aai = new HashMap<>(); + protected String payload; + protected Instant closedLoopAlarmStart; + protected Instant closedLoopAlarmEnd; /** * Construct an instance from an existing instance. diff --git a/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/AbatedTest.java b/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/AbatedTest.java new file mode 100644 index 000000000..e1ef77e22 --- /dev/null +++ b/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/AbatedTest.java @@ -0,0 +1,55 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.time.Instant; +import org.junit.Test; + +public class AbatedTest { + + @Test + public void testConstructors() { + VirtualControlLoopEvent event = new VirtualControlLoopEvent(); + event.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED); + event.setClosedLoopAlarmStart(Instant.now()); + event.setClosedLoopAlarmEnd(Instant.now()); + + Abated abated = new Abated(event); + assertEquals(abated, event); + assertEquals(event.getClosedLoopAlarmStart(), abated.getClosedLoopAlarmStart()); + assertEquals(ControlLoopEventStatus.ABATED, abated.getClosedLoopEventStatus()); + + abated.setClosedLoopAlarmEnd(Instant.now()); + assertNotEquals(abated, event); + + assertEquals(new Abated(abated), abated); + } + + @Test + public void testSetClosedLoopEventStatus() { + assertThatIllegalArgumentException() + .isThrownBy(() -> new Abated().setClosedLoopEventStatus(ControlLoopEventStatus.ONSET)); + } +}
\ No newline at end of file diff --git a/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/CanonicalAbatedTest.java b/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/CanonicalAbatedTest.java new file mode 100644 index 000000000..d7007bc97 --- /dev/null +++ b/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/CanonicalAbatedTest.java @@ -0,0 +1,59 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.time.Instant; +import java.util.UUID; +import org.junit.Test; + +public class CanonicalAbatedTest { + + @Test + public void testConstructors() { + CanonicalAbated abated1 = new CanonicalAbated(new VirtualControlLoopEvent()); + abated1.setRequestId(UUID.randomUUID()); + abated1.setClosedLoopAlarmStart(Instant.now()); + abated1.setClosedLoopAlarmEnd(Instant.now()); + + CanonicalAbated abated2 = new CanonicalAbated(new Abated()); + abated2.setRequestId(UUID.randomUUID()); + abated2.setClosedLoopAlarmStart(Instant.now()); + abated2.setClosedLoopAlarmEnd(Instant.now()); + + CanonicalAbated abated3 = new CanonicalAbated(abated2); + + assertEquals(abated1, abated2); + assertEquals(abated1, abated3); + assertEquals(ControlLoopEventStatus.ABATED, abated1.getClosedLoopEventStatus()); + assertEquals(ControlLoopEventStatus.ABATED, abated2.getClosedLoopEventStatus()); + assertEquals(ControlLoopEventStatus.ABATED, abated3.getClosedLoopEventStatus()); + + assertNotEquals(abated1.getRequestId(), abated2.getRequestId()); + assertNotEquals(abated1.getClosedLoopAlarmStart(), abated2.getClosedLoopAlarmStart()); + assertNotEquals(abated1.getClosedLoopAlarmEnd(), abated2.getClosedLoopAlarmEnd()); + + abated2.setFrom("here"); + assertNotEquals(abated1, abated2); + } +}
\ No newline at end of file diff --git a/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/CanonicalOnsetTest.java b/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/CanonicalOnsetTest.java new file mode 100644 index 000000000..7b0a43f9e --- /dev/null +++ b/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/CanonicalOnsetTest.java @@ -0,0 +1,59 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.time.Instant; +import java.util.UUID; +import org.junit.Test; + +public class CanonicalOnsetTest { + + @Test + public void testConstructors() { + CanonicalOnset onset1 = new CanonicalOnset(new VirtualControlLoopEvent()); + onset1.setRequestId(UUID.randomUUID()); + onset1.setClosedLoopAlarmStart(Instant.now()); + onset1.setClosedLoopAlarmEnd(Instant.now()); + + CanonicalOnset onset2 = new CanonicalOnset(new Onset()); + onset2.setRequestId(UUID.randomUUID()); + onset2.setClosedLoopAlarmStart(Instant.now()); + onset2.setClosedLoopAlarmEnd(Instant.now()); + + CanonicalOnset onset3 = new CanonicalOnset(onset2); + + assertEquals(onset1, onset2); + assertEquals(onset1, onset3); + assertEquals(ControlLoopEventStatus.ONSET, onset1.getClosedLoopEventStatus()); + assertEquals(ControlLoopEventStatus.ONSET, onset2.getClosedLoopEventStatus()); + assertEquals(ControlLoopEventStatus.ONSET, onset3.getClosedLoopEventStatus()); + + assertNotEquals(onset1.getRequestId(), onset2.getRequestId()); + assertNotEquals(onset1.getClosedLoopAlarmStart(), onset2.getClosedLoopAlarmStart()); + assertNotEquals(onset1.getClosedLoopAlarmEnd(), onset2.getClosedLoopAlarmEnd()); + + onset2.setFrom("here"); + assertNotEquals(onset1, onset2); + } +}
\ No newline at end of file diff --git a/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/OnsetTest.java b/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/OnsetTest.java new file mode 100644 index 000000000..b229b024b --- /dev/null +++ b/models-interactions/model-impl/events/src/test/java/org/onap/policy/controlloop/OnsetTest.java @@ -0,0 +1,56 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.time.Instant; +import org.junit.Test; + +public class OnsetTest { + + @Test + public void testConstructors() { + VirtualControlLoopEvent event = new VirtualControlLoopEvent(); + event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET); + event.setClosedLoopAlarmStart(Instant.now()); + event.setClosedLoopAlarmEnd(Instant.now()); + + Onset onset = new Onset(event); + assertEquals(onset, event); + assertEquals(event.getClosedLoopAlarmStart(), onset.getClosedLoopAlarmStart()); + assertEquals(ControlLoopEventStatus.ONSET, onset.getClosedLoopEventStatus()); + assertEquals(event.getClosedLoopAlarmEnd(), onset.getClosedLoopAlarmEnd()); + + onset.setClosedLoopAlarmEnd(Instant.now()); + assertNotEquals(onset, event); + + assertEquals(new Onset(onset), onset); + } + + @Test + public void testSetClosedLoopEventStatus() { + assertThatIllegalArgumentException() + .isThrownBy(() -> new Onset().setClosedLoopEventStatus(ControlLoopEventStatus.ABATED)); + } +}
\ No newline at end of file |