From 8d4778eb89eed2da8ba3998d04063775360dd3e2 Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Tue, 4 Jun 2024 15:08:42 +0100 Subject: Create model in clamp runtime for the replica table Issue-ID: POLICY-5034 Change-Id: Id412b392e994e3ac18f03ccde0b69481b0fd48f6 Signed-off-by: FrancescoFioraEst --- .../clamp/models/acm/concepts/Participant.java | 4 + .../models/acm/concepts/ParticipantReplica.java | 53 +++++++++++++ .../acm/persistence/concepts/JpaParticipant.java | 32 +++++++- .../concepts/JpaParticipantReplica.java | 88 ++++++++++++++++++++++ .../acm/concepts/ParticipantReplicaTest.java | 68 +++++++++++++++++ .../concepts/JpaParticipantReplicaTest.java | 72 ++++++++++++++++++ .../persistence/concepts/JpaParticipantTest.java | 18 +++-- .../clamp/models/acm/utils/CommonTestData.java | 4 +- .../test/resources/providers/TestParticipant.json | 7 ++ 9 files changed, 334 insertions(+), 12 deletions(-) create mode 100644 models/src/main/java/org/onap/policy/clamp/models/acm/concepts/ParticipantReplica.java create mode 100644 models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantReplica.java create mode 100644 models/src/test/java/org/onap/policy/clamp/models/acm/concepts/ParticipantReplicaTest.java create mode 100644 models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantReplicaTest.java (limited to 'models') diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/Participant.java b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/Participant.java index 5bdf4d312..6ddec6179 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/Participant.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/Participant.java @@ -49,6 +49,9 @@ public class Participant { @NonNull private Map participantSupportedElementTypes = new HashMap<>(); + @NonNull + private Map replicas = new HashMap<>(); + /** * Copy constructor. * @@ -60,5 +63,6 @@ public class Participant { this.lastMsg = otherParticipant.lastMsg; this.participantSupportedElementTypes = PfUtils.mapMap(otherParticipant.getParticipantSupportedElementTypes(), ParticipantSupportedElementType::new); + this.replicas = PfUtils.mapMap(otherParticipant.replicas, ParticipantReplica::new); } } diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/ParticipantReplica.java b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/ParticipantReplica.java new file mode 100644 index 000000000..23f72191a --- /dev/null +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/ParticipantReplica.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.models.acm.concepts; + +import java.util.UUID; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import lombok.NonNull; + +@NoArgsConstructor +@Data +@EqualsAndHashCode +public class ParticipantReplica { + + @NonNull + private UUID replicaId; + + @NonNull + private ParticipantState participantState = ParticipantState.ON_LINE; + + @NonNull + private String lastMsg; + + /** + * Copy constructor. + * + * @param other the ParticipantReplica to copy from + */ + public ParticipantReplica(ParticipantReplica other) { + this.replicaId = other.replicaId; + this.participantState = other.participantState; + this.lastMsg = other.lastMsg; + } +} diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipant.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipant.java index cfde55767..f35fff9e7 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipant.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipant.java @@ -41,6 +41,8 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; import org.apache.commons.lang3.ObjectUtils; +import org.hibernate.annotations.LazyCollection; +import org.hibernate.annotations.LazyCollectionOption; import org.onap.policy.clamp.models.acm.concepts.Participant; import org.onap.policy.clamp.models.acm.concepts.ParticipantState; import org.onap.policy.clamp.models.acm.utils.TimestampHelper; @@ -84,11 +86,18 @@ public class JpaParticipant extends Validated foreignKey = @ForeignKey(name = "supported_element_fk")) private List<@NotNull @Valid JpaParticipantSupportedElementType> supportedElements; + @NotNull + @OneToMany + @LazyCollection(LazyCollectionOption.FALSE) + @JoinColumn(name = "participantId", referencedColumnName = "participantId", + foreignKey = @ForeignKey(name = "participant_replica_fk")) + private List<@NotNull @Valid JpaParticipantReplica> replicas; + /** * The Default Constructor creates a {@link JpaParticipant} object with a null key. */ public JpaParticipant() { - this(UUID.randomUUID().toString(), ParticipantState.ON_LINE, new ArrayList<>()); + this(UUID.randomUUID().toString(), ParticipantState.ON_LINE, new ArrayList<>(), new ArrayList<>()); } /** @@ -96,13 +105,17 @@ public class JpaParticipant extends Validated * * @param participantId the participant id * @param participantState the state of the participant + * @param supportedElements the list of supported Element Type + * @param replicas the list of replica */ public JpaParticipant(@NonNull String participantId, @NonNull final ParticipantState participantState, - @NonNull final List supportedElements) { + @NonNull final List supportedElements, + @NonNull final List replicas) { this.participantId = participantId; this.participantState = participantState; this.supportedElements = supportedElements; this.lastMsg = TimestampHelper.nowTimestamp(); + this.replicas = replicas; } /** @@ -115,6 +128,7 @@ public class JpaParticipant extends Validated this.description = copyConcept.description; this.participantId = copyConcept.participantId; this.supportedElements = copyConcept.supportedElements; + this.replicas = copyConcept.replicas; this.lastMsg = copyConcept.lastMsg; } @@ -139,7 +153,9 @@ public class JpaParticipant extends Validated participant.getParticipantSupportedElementTypes() .put(UUID.fromString(element.getId()), element.toAuthorative()); } - + for (var replica : this.replicas) { + participant.getReplicas().put(UUID.fromString(replica.getReplicaId()), replica.toAuthorative()); + } return participant; } @@ -148,14 +164,22 @@ public class JpaParticipant extends Validated this.setParticipantState(participant.getParticipantState()); this.participantId = participant.getParticipantId().toString(); this.lastMsg = TimestampHelper.toTimestamp(participant.getLastMsg()); - this.supportedElements = new ArrayList<>(participant.getParticipantSupportedElementTypes().size()); + this.supportedElements = new ArrayList<>(participant.getParticipantSupportedElementTypes().size()); for (var elementEntry : participant.getParticipantSupportedElementTypes().entrySet()) { var jpaParticipantSupportedElementType = new JpaParticipantSupportedElementType(); jpaParticipantSupportedElementType.setParticipantId(this.participantId); jpaParticipantSupportedElementType.fromAuthorative(elementEntry.getValue()); this.supportedElements.add(jpaParticipantSupportedElementType); } + + this.replicas = new ArrayList<>(participant.getReplicas().size()); + for (var replicaEntry : participant.getReplicas().entrySet()) { + var jpaReplica = new JpaParticipantReplica(); + jpaReplica.setParticipantId(this.participantId); + jpaReplica.fromAuthorative(replicaEntry.getValue()); + this.replicas.add(jpaReplica); + } } @Override diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantReplica.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantReplica.java new file mode 100644 index 000000000..beaab601f --- /dev/null +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantReplica.java @@ -0,0 +1,88 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.models.acm.persistence.concepts; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.Table; +import java.sql.Timestamp; +import java.util.UUID; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import org.onap.policy.clamp.models.acm.concepts.ParticipantReplica; +import org.onap.policy.clamp.models.acm.concepts.ParticipantState; +import org.onap.policy.clamp.models.acm.utils.TimestampHelper; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.models.base.PfAuthorative; +import org.onap.policy.models.base.Validated; + +@Entity +@Table(name = "ParticipantReplica") +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = false) +public class JpaParticipantReplica extends Validated implements PfAuthorative { + + @Id + @NotNull + private String replicaId; + + @NotNull + @Column + private String participantId; + + @Column + @NotNull + private ParticipantState participantState; + + @Column + @NotNull + private Timestamp lastMsg; + + public JpaParticipantReplica() { + this(UUID.randomUUID().toString(), UUID.randomUUID().toString()); + } + + public JpaParticipantReplica(@NonNull String replicaId, @NonNull String participantId) { + this.replicaId = replicaId; + this.participantId = participantId; + } + + @Override + public ParticipantReplica toAuthorative() { + var participantReplica = new ParticipantReplica(); + participantReplica.setReplicaId(UUID.fromString(replicaId)); + participantReplica.setParticipantState(participantState); + participantReplica.setLastMsg(lastMsg.toString()); + return participantReplica; + } + + @Override + public void fromAuthorative(@NonNull ParticipantReplica participantReplica) { + this.replicaId = participantReplica.getReplicaId().toString(); + this.participantState = participantReplica.getParticipantState(); + this.lastMsg = TimestampHelper.toTimestamp(participantReplica.getLastMsg()); + } +} diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/concepts/ParticipantReplicaTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/concepts/ParticipantReplicaTest.java new file mode 100644 index 000000000..8df6db65e --- /dev/null +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/concepts/ParticipantReplicaTest.java @@ -0,0 +1,68 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.models.acm.concepts; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.models.acm.utils.CommonTestData; + +class ParticipantReplicaTest { + + @Test + void testParticipantLombok() { + assertDoesNotThrow(() -> new ParticipantReplica()); + var p0 = new ParticipantReplica(); + + assertThat(p0.toString()).contains("ParticipantReplica("); + assertThat(p0.hashCode()).isNotZero(); + assertNotEquals(null, p0); + + var p1 = new ParticipantReplica(); + + p1.setReplicaId(CommonTestData.getReplicaId()); + p1.setParticipantState(ParticipantState.ON_LINE); + + assertThat(p1.toString()).contains("ParticipantReplica("); + assertNotEquals(0, p1.hashCode()); + assertNotEquals(p1, p0); + assertNotEquals(null, p1); + + var p2 = new ParticipantReplica(); + assertThatThrownBy(() -> p2.setParticipantState(null)).isInstanceOf(NullPointerException.class); + assertEquals(p2, p0); + } + + @Test + void testCopyConstructor() { + var p0 = new ParticipantReplica(); + p0.setReplicaId(UUID.randomUUID()); + p0.setParticipantState(ParticipantState.ON_LINE); + + var p2 = new ParticipantReplica(p0); + assertEquals(p2, p0); + } +} diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantReplicaTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantReplicaTest.java new file mode 100644 index 000000000..d77760860 --- /dev/null +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantReplicaTest.java @@ -0,0 +1,72 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.models.acm.persistence.concepts; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.models.acm.concepts.ParticipantReplica; +import org.onap.policy.clamp.models.acm.concepts.ParticipantState; +import org.onap.policy.clamp.models.acm.utils.TimestampHelper; + +class JpaParticipantReplicaTest { + + @Test + void testJpaParticipantReplicaConstructor() { + assertThatThrownBy(() -> new JpaParticipantReplica(UUID.randomUUID().toString(), null)) + .hasMessageMatching("participantId is marked .*ull but is null"); + + assertThatThrownBy(() -> new JpaParticipantReplica(null, UUID.randomUUID().toString())) + .hasMessageMatching("replicaId is marked .*ull but is null"); + + assertDoesNotThrow(() -> new JpaParticipantReplica()); + assertDoesNotThrow(() -> new JpaParticipantReplica(UUID.randomUUID().toString(), UUID.randomUUID().toString())); + } + + @Test + void testJpaParticipantReplica() { + var p0 = new JpaParticipantReplica(); + + assertThat(p0.toString()).contains("JpaParticipantReplica("); + assertThat(p0.hashCode()).isNotZero(); + assertNotEquals(null, p0); + + var p1 = new JpaParticipantReplica(); + p1.setParticipantState(ParticipantState.ON_LINE); + + assertThat(p1.toString()).contains("ParticipantReplica("); + assertNotEquals(0, p1.hashCode()); + assertNotEquals(p1, p0); + assertNotEquals(null, p1); + + var p2 = new JpaParticipantReplica(); + p2.setReplicaId(p0.getReplicaId()); + p2.setParticipantId(p0.getParticipantId()); + p2.setLastMsg(p0.getLastMsg()); + p2.setParticipantState(p0.getParticipantState()); + assertEquals(p2, p0); + } +} diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantTest.java index e64a6893f..e0f2f55c1 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantTest.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantTest.java @@ -52,20 +52,25 @@ class JpaParticipantTest { assertThatThrownBy(() -> new JpaParticipant((JpaParticipant) null)) .hasMessageMatching("copyConcept is marked .*ull but is null"); - assertThatThrownBy(() -> new JpaParticipant(null, null, null)).hasMessageMatching(NULL_KEY_ERROR); - - assertThatThrownBy(() -> new JpaParticipant(null, ParticipantState.ON_LINE, new ArrayList<>())) + assertThatThrownBy(() -> new JpaParticipant(null, ParticipantState.ON_LINE, + new ArrayList<>(), new ArrayList<>())) .hasMessageMatching(NULL_KEY_ERROR); - assertThatThrownBy(() -> new JpaParticipant(UUID.randomUUID().toString(), null, new ArrayList<>())) + assertThatThrownBy(() -> new JpaParticipant(UUID.randomUUID().toString(), null, + new ArrayList<>(), new ArrayList<>())) .hasMessageMatching("participantState is marked .*ull but is null"); - assertThatThrownBy(() -> new JpaParticipant(UUID.randomUUID().toString(), ParticipantState.ON_LINE, null)) + assertThatThrownBy(() -> new JpaParticipant(UUID.randomUUID().toString(), ParticipantState.ON_LINE, + null, new ArrayList<>())) .hasMessageMatching("supportedElements is marked .*ull but is null"); + assertThatThrownBy(() -> new JpaParticipant(UUID.randomUUID().toString(), ParticipantState.ON_LINE, + new ArrayList<>(), null)) + .hasMessageMatching("replicas is marked .*ull but is null"); + assertDoesNotThrow(() -> new JpaParticipant()); assertDoesNotThrow(() -> new JpaParticipant(UUID.randomUUID().toString(), - ParticipantState.ON_LINE, new ArrayList<>())); + ParticipantState.ON_LINE, new ArrayList<>(), new ArrayList<>())); } @Test @@ -158,6 +163,7 @@ class JpaParticipantTest { testParticipant.setParticipantId(UUID.randomUUID()); testParticipant.setLastMsg(TimestampHelper.now()); testParticipant.setParticipantSupportedElementTypes(new LinkedHashMap<>()); + testParticipant.setReplicas(new LinkedHashMap<>()); return testParticipant; } diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/utils/CommonTestData.java b/models/src/test/java/org/onap/policy/clamp/models/acm/utils/CommonTestData.java index b8075c3ef..3bd1549ad 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/utils/CommonTestData.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/utils/CommonTestData.java @@ -51,9 +51,9 @@ public class CommonTestData { } /** - * Returns participantId for test cases. + * Returns participant replica Id for test cases. * - * @return participant Id + * @return replica Id */ public static UUID getReplicaId() { return REPLICA_ID; diff --git a/models/src/test/resources/providers/TestParticipant.json b/models/src/test/resources/providers/TestParticipant.json index 3f19baab4..3335865e1 100644 --- a/models/src/test/resources/providers/TestParticipant.json +++ b/models/src/test/resources/providers/TestParticipant.json @@ -22,5 +22,12 @@ "typeName": "org.onap.policy.clamp.acm.AutomationCompositionElement", "typeVersion": "1.0.0" } + }, + "replicas": { + "82fd8ef9-1d1e-4343-9b28-7f9564ee3de6":{ + "replicaId": "82fd8ef9-1d1e-4343-9b28-7f9564ee3de6", + "lastMsg": "2024-05-22 10:04:37.6020187", + "participantState": "ON_LINE" + } } } -- cgit 1.2.3-korg