Fix sync message during migration 12/138412/2
authorFrancescoFioraEst <francesco.fiora@est.tech>
Tue, 9 Jul 2024 16:53:37 +0000 (17:53 +0100)
committerFrancescoFioraEst <francesco.fiora@est.tech>
Wed, 10 Jul 2024 12:47:59 +0000 (13:47 +0100)
Issue-ID: POLICY-5070
Change-Id: I6d6ca235fd5463cb58a8f43e027022244ffe5bbe
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
13 files changed:
models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationComposition.java
models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java
models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java
runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAcHandler.java
runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionScanner.java
runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/comm/AcElementPropertiesPublisher.java
runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/comm/AutomationCompositionDeployPublisher.java
runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/comm/ParticipantPrimePublisher.java
runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/comm/ParticipantRestartPublisher.java
runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/comm/ParticipantStatusReqPublisher.java
runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/comm/ParticipantSyncPublisher.java
runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAcHandlerTest.java
runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionScannerTest.java

index 0bf6a9e..355fcaf 100644 (file)
@@ -202,6 +202,22 @@ public class JpaAutomationComposition extends Validated
 
     @Override
     public void fromAuthorative(@NonNull final AutomationComposition automationComposition) {
+        this.fromAuthorativeBase(automationComposition);
+        this.elements = new ArrayList<>(automationComposition.getElements().size());
+        for (var elementEntry : automationComposition.getElements().entrySet()) {
+            var jpaAutomationCompositionElement =
+                    new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
+            jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
+            this.elements.add(jpaAutomationCompositionElement);
+        }
+    }
+
+    /**
+     * Set an instance of the persist concept to the equivalent values as the other concept without copy the elements.
+     *
+     * @param automationComposition the authorative concept
+     */
+    public void fromAuthorativeBase(@NonNull final AutomationComposition automationComposition) {
         this.instanceId = automationComposition.getInstanceId().toString();
         this.name = automationComposition.getName();
         this.version = automationComposition.getVersion();
@@ -216,13 +232,6 @@ public class JpaAutomationComposition extends Validated
         this.phase = automationComposition.getPhase();
         this.description = automationComposition.getDescription();
         this.stateChangeResult = automationComposition.getStateChangeResult();
-        this.elements = new ArrayList<>(automationComposition.getElements().size());
-        for (var elementEntry : automationComposition.getElements().entrySet()) {
-            var jpaAutomationCompositionElement =
-                    new JpaAutomationCompositionElement(elementEntry.getKey().toString(), this.instanceId);
-            jpaAutomationCompositionElement.fromAuthorative(elementEntry.getValue());
-            this.elements.add(jpaAutomationCompositionElement);
-        }
     }
 
     @Override
index 8be1296..ce25809 100644 (file)
@@ -117,6 +117,23 @@ public class AutomationCompositionProvider {
         return result.toAuthorative();
     }
 
+
+    /**
+     * Update automation composition state.
+     *
+     * @param acSource the automation composition to update
+     * @return the updated automation composition
+     */
+    public AutomationComposition updateAcState(final AutomationComposition acSource) {
+        var automationComposition = automationCompositionRepository
+                .getReferenceById(acSource.getInstanceId().toString());
+        automationComposition.fromAuthorativeBase(acSource);
+        var result = automationCompositionRepository.save(automationComposition);
+        automationCompositionRepository.flush();
+        // Return the saved automation composition
+        return result.toAuthorative();
+    }
+
     /**
      * Update automation composition.
      *
@@ -232,12 +249,17 @@ public class AutomationCompositionProvider {
      * Update AutomationCompositionElement.
      *
      * @param element the AutomationCompositionElement
-     * @param instanceId the instance Id
      */
-    public void updateAutomationCompositionElement(@NonNull final AutomationCompositionElement element,
-                @NonNull final UUID instanceId) {
-        var jpaAcElement = new JpaAutomationCompositionElement(element.getId().toString(), instanceId.toString());
-        jpaAcElement.fromAuthorative(element);
+    public void updateAutomationCompositionElement(@NonNull final AutomationCompositionElement element) {
+        var jpaAcElement = acElementRepository.getReferenceById(element.getId().toString());
+        jpaAcElement.setMessage(element.getMessage());
+        jpaAcElement.setOutProperties(element.getOutProperties());
+        jpaAcElement.setOperationalState(element.getOperationalState());
+        jpaAcElement.setUseState(element.getUseState());
+        jpaAcElement.setDeployState(element.getDeployState());
+        jpaAcElement.setLockState(element.getLockState());
+        jpaAcElement.setRestarting(element.getRestarting());
+
         ProviderUtils.validate(element, jpaAcElement, "AutomationCompositionElement");
         acElementRepository.save(jpaAcElement);
     }
index 8e7e50d..515dfaa 100644 (file)
@@ -225,12 +225,18 @@ class AutomationCompositionProviderTest {
         var automationCompositionProvider = new AutomationCompositionProvider(
             mock(AutomationCompositionRepository.class), acElementRepository);
 
-        assertThatThrownBy(() -> automationCompositionProvider.updateAutomationCompositionElement(null, null))
+        assertThatThrownBy(() -> automationCompositionProvider.updateAutomationCompositionElement(null))
             .hasMessageMatching(ACELEMENT_IS_NULL);
 
         var acElement = inputAutomationCompositions.getAutomationCompositionList().get(0).getElements().values()
             .iterator().next();
-        automationCompositionProvider.updateAutomationCompositionElement(acElement, UUID.randomUUID());
+        var jpa = new JpaAutomationCompositionElement();
+        jpa.setElementId(acElement.getId().toString());
+        jpa.setInstanceId(UUID.randomUUID().toString());
+        jpa.fromAuthorative(acElement);
+        when(acElementRepository.getReferenceById(acElement.getId().toString())).thenReturn(jpa);
+
+        automationCompositionProvider.updateAutomationCompositionElement(acElement);
         verify(acElementRepository).save(any());
     }
 
index 3e2057e..7547786 100644 (file)
@@ -247,8 +247,7 @@ public class SupervisionAcHandler {
                 for (var element : automationComposition.getElements().values()) {
                     if (element.getParticipantId().equals(automationCompositionAckMessage.getParticipantId())) {
                         element.setDeployState(DeployState.DELETED);
-                        automationCompositionProvider.updateAutomationCompositionElement(element,
-                            automationComposition.getInstanceId());
+                        automationCompositionProvider.updateAutomationCompositionElement(element);
                     }
                 }
             } else {
@@ -263,7 +262,7 @@ public class SupervisionAcHandler {
                 automationCompositionAckMessage.getAutomationCompositionResultMap().entrySet(),
                 automationCompositionAckMessage.getStateChangeResult());
         if (updated) {
-            automationCompositionProvider.updateAutomationComposition(automationComposition);
+            automationComposition = automationCompositionProvider.updateAcState(automationComposition);
             var acDefinition = acDefinitionProvider.getAcDefinition(automationComposition.getCompositionId());
             participantSyncPublisher.sendSync(acDefinition.getServiceTemplate(), automationComposition);
         }
@@ -289,8 +288,7 @@ public class SupervisionAcHandler {
                 element.setDeployState(acElementAck.getValue().getDeployState());
                 element.setLockState(acElementAck.getValue().getLockState());
                 element.setRestarting(null);
-                automationCompositionProvider.updateAutomationCompositionElement(element,
-                    automationComposition.getInstanceId());
+                automationCompositionProvider.updateAutomationCompositionElement(element);
             }
         }
 
index 75a2f05..d1aa6c2 100644 (file)
@@ -201,12 +201,13 @@ public class SupervisionScanner {
         if (StateChangeResult.TIMEOUT.equals(automationComposition.getStateChangeResult())) {
             automationComposition.setStateChangeResult(StateChangeResult.NO_ERROR);
         }
+        var acToUpdate = automationComposition;
         if (DeployState.DELETED.equals(automationComposition.getDeployState())) {
             automationCompositionProvider.deleteAutomationComposition(automationComposition.getInstanceId());
         } else {
-            automationCompositionProvider.updateAutomationComposition(automationComposition);
+            acToUpdate = automationCompositionProvider.updateAcState(acToUpdate);
         }
-        participantSyncPublisher.sendSync(serviceTemplate, automationComposition);
+        participantSyncPublisher.sendSync(serviceTemplate, acToUpdate);
     }
 
     private void handleTimeout(AutomationCompositionDefinition acDefinition) {
index 338f296..cc5d146 100644 (file)
@@ -56,7 +56,7 @@ public class AcElementPropertiesPublisher extends AbstractParticipantPublisher<P
         propertiesUpdate.setParticipantUpdatesList(
                 AcmUtils.createParticipantDeployList(automationComposition, DeployOrder.UPDATE));
 
-        LOGGER.debug("AC Element properties update sent {}", propertiesUpdate);
+        LOGGER.debug("AC Element properties update sent {}", propertiesUpdate.getMessageId());
         super.send(propertiesUpdate);
     }
 }
index 2b6435e..7fe63a7 100644 (file)
@@ -87,7 +87,7 @@ public class AutomationCompositionDeployPublisher extends AbstractParticipantPub
         acDeployMsg.setTimestamp(Instant.now());
         acDeployMsg.setParticipantUpdatesList(participantDeploys);
 
-        LOGGER.debug("AutomationCompositionDeploy message sent {}", acDeployMsg);
+        LOGGER.debug("AutomationCompositionDeploy message sent {}", acDeployMsg.getMessageId());
         super.send(acDeployMsg);
     }
 }
index b0848bd..360e526 100644 (file)
@@ -72,7 +72,7 @@ public class ParticipantPrimePublisher extends AbstractParticipantPublisher<Part
         message.setParticipantId(participantId);
         message.setTimestamp(Instant.now());
         message.setParticipantDefinitionUpdates(participantDefinitions);
-        LOGGER.debug("Participant Update sent {}", message);
+        LOGGER.debug("Participant Update sent {}", message.getMessageId());
         super.send(message);
     }
 
@@ -127,7 +127,7 @@ public class ParticipantPrimePublisher extends AbstractParticipantPublisher<Part
         // DeCommission the automation composition but deleting participantdefinitions on participants
         message.setParticipantDefinitionUpdates(null);
 
-        LOGGER.debug("Participant Update sent {}", message);
+        LOGGER.debug("Participant Update sent {}", message.getMessageId());
         super.send(message);
     }
 }
index 96d6338..540cf62 100644 (file)
@@ -69,7 +69,7 @@ public class ParticipantRestartPublisher extends AbstractParticipantPublisher<Pa
             message.getAutomationcompositionList().add(restartAc);
         }
 
-        LOGGER.debug("Participant Restart sent {}", message);
+        LOGGER.debug("Participant Restart sent {}", message.getMessageId());
         super.send(message);
     }
 }
index 1f6b7c8..96abac4 100644 (file)
@@ -44,7 +44,7 @@ public class ParticipantStatusReqPublisher extends AbstractParticipantPublisher<
         message.setParticipantId(participantId);
         message.setTimestamp(Instant.now());
 
-        LOGGER.debug("Participant StatusReq sent {}", message);
+        LOGGER.debug("Participant StatusReq sent {}", message.getMessageId());
         super.send(message);
     }
 
index b63bc0a..eb1db6f 100644 (file)
@@ -144,7 +144,7 @@ public class ParticipantSyncPublisher extends AbstractParticipantPublisher<Parti
         }
         message.getAutomationcompositionList().add(syncAc);
 
-        LOGGER.debug("Participant AutomationComposition Sync sent {}", message);
+        LOGGER.debug("Participant AutomationComposition Sync sent {}", message.getMessageId());
         super.send(message);
     }
 }
index 0bec9d0..e895401 100644 (file)
@@ -66,6 +66,8 @@ class SupervisionAcHandlerTest {
         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
         when(automationCompositionProvider.findAutomationComposition(IDENTIFIER))
                 .thenReturn(Optional.of(automationComposition));
+        when(automationCompositionProvider.updateAcState(any(AutomationComposition.class)))
+                .thenReturn(automationComposition);
 
         var acDefinitionProvider = mock(AcDefinitionProvider.class);
         when(acDefinitionProvider.getAcDefinition(automationComposition.getCompositionId()))
@@ -82,7 +84,7 @@ class SupervisionAcHandlerTest {
         handler.handleAutomationCompositionStateChangeAckMessage(automationCompositionAckMessage);
 
         verify(automationCompositionProvider, times(3))
-            .updateAutomationCompositionElement(any(AutomationCompositionElement.class), any());
+            .updateAutomationCompositionElement(any(AutomationCompositionElement.class));
     }
 
     private AutomationCompositionDeployAck getAutomationCompositionDeployAck(ParticipantMessageType messageType,
@@ -107,6 +109,8 @@ class SupervisionAcHandlerTest {
         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
         when(automationCompositionProvider.findAutomationComposition(IDENTIFIER))
                 .thenReturn(Optional.of(automationComposition));
+        when(automationCompositionProvider.updateAcState(any(AutomationComposition.class)))
+                .thenReturn(automationComposition);
 
         var acDefinitionProvider = mock(AcDefinitionProvider.class);
         when(acDefinitionProvider.getAcDefinition(automationComposition.getCompositionId()))
@@ -124,7 +128,7 @@ class SupervisionAcHandlerTest {
 
         handler.handleAutomationCompositionUpdateAckMessage(automationCompositionAckMessage);
 
-        verify(automationCompositionProvider).updateAutomationComposition(any(AutomationComposition.class));
+        verify(automationCompositionProvider).updateAcState(any(AutomationComposition.class));
     }
 
     @Test
@@ -162,7 +166,7 @@ class SupervisionAcHandlerTest {
         handler.handleAutomationCompositionUpdateAckMessage(automationCompositionAckMessage);
 
         verify(automationCompositionProvider)
-            .updateAutomationCompositionElement(any(AutomationCompositionElement.class), any());
+            .updateAutomationCompositionElement(any(AutomationCompositionElement.class));
     }
 
     @Test
@@ -322,7 +326,7 @@ class SupervisionAcHandlerTest {
         handler.handleAutomationCompositionUpdateAckMessage(automationCompositionAckMessage);
 
         verify(automationCompositionProvider)
-            .updateAutomationCompositionElement(any(AutomationCompositionElement.class), any());
+            .updateAutomationCompositionElement(any(AutomationCompositionElement.class));
     }
 
     @Test
index fa5929f..6bdab70 100644 (file)
@@ -186,6 +186,7 @@ class SupervisionScannerTest {
         automationComposition.setCompositionId(compositionId);
         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
         when(automationCompositionProvider.getAcInstancesInTransition()).thenReturn(List.of(automationComposition));
+        when(automationCompositionProvider.updateAcState(any())).thenReturn(automationComposition);
 
         var automationCompositionDeployPublisher = mock(AutomationCompositionDeployPublisher.class);
         var automationCompositionStateChangePublisher = mock(AutomationCompositionStateChangePublisher.class);
@@ -196,7 +197,7 @@ class SupervisionScannerTest {
                 mock(ParticipantSyncPublisher.class), acRuntimeParameterGroup);
         supervisionScanner.run();
 
-        verify(automationCompositionProvider).updateAutomationComposition(any(AutomationComposition.class));
+        verify(automationCompositionProvider).updateAcState(any(AutomationComposition.class));
     }
 
     @Test
@@ -255,7 +256,7 @@ class SupervisionScannerTest {
 
         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
         when(automationCompositionProvider.getAcInstancesInTransition()).thenReturn(List.of(automationComposition));
-
+        when(automationCompositionProvider.updateAcState(any())).thenReturn(automationComposition);
         var automationCompositionDeployPublisher = mock(AutomationCompositionDeployPublisher.class);
         var automationCompositionStateChangePublisher = mock(AutomationCompositionStateChangePublisher.class);
         var acRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner");
@@ -282,7 +283,7 @@ class SupervisionScannerTest {
             entry.getValue().setDeployState(DeployState.DEPLOYED);
         }
         scannerObj2.run();
-        verify(automationCompositionProvider, times(1)).updateAutomationComposition(any(AutomationComposition.class));
+        verify(automationCompositionProvider, times(1)).updateAcState(any(AutomationComposition.class));
         assertEquals(StateChangeResult.NO_ERROR, automationComposition.getStateChangeResult());
     }
 
@@ -341,6 +342,7 @@ class SupervisionScannerTest {
 
         var automationCompositionProvider = mock(AutomationCompositionProvider.class);
         when(automationCompositionProvider.getAcInstancesInTransition()).thenReturn(List.of(automationComposition));
+        when(automationCompositionProvider.updateAcState(any())).thenReturn(automationComposition);
 
         var automationCompositionDeployPublisher = mock(AutomationCompositionDeployPublisher.class);
         var automationCompositionStateChangePublisher = mock(AutomationCompositionStateChangePublisher.class);
@@ -358,7 +360,7 @@ class SupervisionScannerTest {
         automationComposition.getElements().entrySet().iterator().next().getValue()
                 .setDeployState(DeployState.DEPLOYED);
         supervisionScanner.run();
-        verify(automationCompositionProvider, times(1)).updateAutomationComposition(any(AutomationComposition.class));
+        verify(automationCompositionProvider, times(1)).updateAcState(any(AutomationComposition.class));
 
         assertEquals(DeployState.DEPLOYED, automationComposition.getDeployState());
         assertEquals(compositionTargetId, automationComposition.getCompositionId());