From adcc527c0a10426f99be50cc2267f7063a85c8d7 Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Tue, 9 Jul 2024 17:53:37 +0100 Subject: Fix sync message during migration Issue-ID: POLICY-5070 Change-Id: I6d6ca235fd5463cb58a8f43e027022244ffe5bbe Signed-off-by: FrancescoFioraEst --- .../concepts/JpaAutomationComposition.java | 23 +++++++++++----- .../provider/AutomationCompositionProvider.java | 32 ++++++++++++++++++---- .../AutomationCompositionProviderTest.java | 10 +++++-- .../runtime/supervision/SupervisionAcHandler.java | 8 ++---- .../runtime/supervision/SupervisionScanner.java | 5 ++-- .../comm/AcElementPropertiesPublisher.java | 2 +- .../comm/AutomationCompositionDeployPublisher.java | 2 +- .../comm/ParticipantPrimePublisher.java | 4 +-- .../comm/ParticipantRestartPublisher.java | 2 +- .../comm/ParticipantStatusReqPublisher.java | 2 +- .../supervision/comm/ParticipantSyncPublisher.java | 2 +- .../supervision/SupervisionAcHandlerTest.java | 12 +++++--- .../supervision/SupervisionScannerTest.java | 10 ++++--- 13 files changed, 78 insertions(+), 36 deletions(-) diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationComposition.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationComposition.java index 0bf6a9e1a..355fcaff0 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationComposition.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/concepts/JpaAutomationComposition.java @@ -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 diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java index 8be12960b..ce258094a 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProvider.java @@ -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); } diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java index 8e7e50de7..515dfaa83 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AutomationCompositionProviderTest.java @@ -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()); } diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAcHandler.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAcHandler.java index 3e2057ed5..75477866a 100644 --- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAcHandler.java +++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAcHandler.java @@ -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); } } diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionScanner.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionScanner.java index 75a2f0540..d1aa6c239 100644 --- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionScanner.java +++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionScanner.java @@ -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) { diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/comm/AcElementPropertiesPublisher.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/comm/AcElementPropertiesPublisher.java index 338f2960d..cc5d1461f 100644 --- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/comm/AcElementPropertiesPublisher.java +++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/comm/AcElementPropertiesPublisher.java @@ -56,7 +56,7 @@ public class AcElementPropertiesPublisher extends AbstractParticipantPublisher