Create model in clamp runtime for the replica table 50/138150/1
authorFrancescoFioraEst <francesco.fiora@est.tech>
Tue, 4 Jun 2024 14:08:42 +0000 (15:08 +0100)
committerFrancesco Fiora <francesco.fiora@est.tech>
Thu, 6 Jun 2024 16:14:28 +0000 (16:14 +0000)
Issue-ID: POLICY-5034
Change-Id: Id412b392e994e3ac18f03ccde0b69481b0fd48f6
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
models/src/main/java/org/onap/policy/clamp/models/acm/concepts/Participant.java
models/src/main/java/org/onap/policy/clamp/models/acm/concepts/ParticipantReplica.java [new file with mode: 0644]
models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipant.java
models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantReplica.java [new file with mode: 0644]
models/src/test/java/org/onap/policy/clamp/models/acm/concepts/ParticipantReplicaTest.java [new file with mode: 0644]
models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantReplicaTest.java [new file with mode: 0644]
models/src/test/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaParticipantTest.java
models/src/test/java/org/onap/policy/clamp/models/acm/utils/CommonTestData.java
models/src/test/resources/providers/TestParticipant.json

index 5bdf4d3..6ddec61 100644 (file)
@@ -49,6 +49,9 @@ public class Participant {
     @NonNull
     private Map<UUID, ParticipantSupportedElementType> participantSupportedElementTypes = new HashMap<>();
 
+    @NonNull
+    private Map<UUID, ParticipantReplica> 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 (file)
index 0000000..23f7219
--- /dev/null
@@ -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;
+    }
+}
index cfde557..f35fff9 100644 (file)
@@ -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<JpaParticipantSupportedElementType> supportedElements) {
+            @NonNull final List<JpaParticipantSupportedElementType> supportedElements,
+            @NonNull final List<JpaParticipantReplica> 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 (file)
index 0000000..beaab60
--- /dev/null
@@ -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<ParticipantReplica> {
+
+    @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 (file)
index 0000000..8df6db6
--- /dev/null
@@ -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 (file)
index 0000000..d777608
--- /dev/null
@@ -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);
+    }
+}
index e64a689..e0f2f55 100644 (file)
@@ -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;
     }
index b8075c3..3bd1549 100644 (file)
@@ -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;
index 3f19baa..3335865 100644 (file)
       "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"
+    }
   }
 }