diff options
author | Ramesh Murugan Iyer <ramesh.murugan.iyer@est.tech> | 2024-08-09 16:10:33 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2024-08-09 16:10:33 +0000 |
commit | 036e873dea28748ded7a735b2aa3456865643af9 (patch) | |
tree | cd4e83c619845d59cb7284969a7241d5bb6e3de8 /participant/participant-intermediary | |
parent | 3f7d3771a0e5a5d8f186a966963d28528250fc6f (diff) | |
parent | a944487231ad341d8d6b0f302c47bad20169c107 (diff) |
Merge "Add validation for state set by the participant"
Diffstat (limited to 'participant/participant-intermediary')
2 files changed, 100 insertions, 42 deletions
diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandler.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandler.java index 77bcb19ef..7cf83db9d 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandler.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandler.java @@ -42,6 +42,7 @@ import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCom import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageType; import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrimeAck; import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus; +import org.onap.policy.clamp.models.acm.utils.AcmUtils; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -66,9 +67,7 @@ public class AutomationCompositionOutHandler { */ public void updateAutomationCompositionElementStage(UUID instance, UUID elementId, StateChangeResult stateChangeResult, int stage, String message) { - - if (instance == null || elementId == null) { - LOGGER.error("Cannot update Automation composition element stage, id is null"); + if (!validateData(instance, elementId, stateChangeResult)) { return; } @@ -86,12 +85,10 @@ public class AutomationCompositionOutHandler { return; } - element.setRestarting(null); - var automationCompositionStateChangeAck = new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK); automationCompositionStateChangeAck.setParticipantId(cacheProvider.getParticipantId()); - automationCompositionStateChangeAck.setMessage(message); + automationCompositionStateChangeAck.setMessage(AcmUtils.validatedMessage(message)); automationCompositionStateChangeAck.setResponseTo(cacheProvider.getMsgIdentification().get(element.getId())); automationCompositionStateChangeAck.setStateChangeResult(stateChangeResult); automationCompositionStateChangeAck.setStage(stage); @@ -105,6 +102,23 @@ public class AutomationCompositionOutHandler { cacheProvider.getMsgIdentification().remove(element.getId()); } + private boolean validateData(UUID instance, UUID elementId, StateChangeResult stateChangeResult) { + if (instance == null || elementId == null) { + LOGGER.error("Not valid Ac instance, id is null"); + return false; + } + if (stateChangeResult == null) { + LOGGER.error("Not valid Ac instance, stateChangeResult is null"); + return false; + } + if (!StateChangeResult.NO_ERROR.equals(stateChangeResult) + && !StateChangeResult.FAILED.equals(stateChangeResult)) { + LOGGER.error("Not valid Ac instance, stateChangeResult is not valid"); + return false; + } + return true; + } + /** * Handle a automation composition element state change message. * @@ -117,9 +131,13 @@ public class AutomationCompositionOutHandler { */ public void updateAutomationCompositionElementState(UUID instance, UUID elementId, DeployState deployState, LockState lockState, StateChangeResult stateChangeResult, String message) { + if (!validateData(instance, elementId, stateChangeResult)) { + return; + } - if (instance == null || elementId == null) { - LOGGER.error("Cannot update Automation composition element state, id is null"); + if ((deployState != null && lockState != null) || (deployState == null && lockState == null) + || AcmUtils.isInTransitionalState(deployState, lockState, SubState.NONE)) { + LOGGER.error("state error {} and {} cannot be handled", deployState, lockState); return; } @@ -137,13 +155,6 @@ public class AutomationCompositionOutHandler { return; } - if ((element.getRestarting() == null) - && ((deployState != null && lockState != null) || (deployState == null && lockState == null))) { - LOGGER.error("state error {} and {} cannot be handled", deployState, lockState); - return; - } - element.setRestarting(null); - if (deployState != null && !SubState.NONE.equals(element.getSubState())) { handleSubState(automationComposition, element); if (!StateChangeResult.NO_ERROR.equals(stateChangeResult)) { @@ -161,7 +172,7 @@ public class AutomationCompositionOutHandler { new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK); automationCompositionStateChangeAck.setParticipantId(cacheProvider.getParticipantId()); automationCompositionStateChangeAck.setReplicaId(cacheProvider.getReplicaId()); - automationCompositionStateChangeAck.setMessage(message); + automationCompositionStateChangeAck.setMessage(AcmUtils.validatedMessage(message)); automationCompositionStateChangeAck.setResponseTo(cacheProvider.getMsgIdentification().get(element.getId())); automationCompositionStateChangeAck.setStateChangeResult(stateChangeResult); automationCompositionStateChangeAck.setAutomationCompositionId(instance); @@ -289,9 +300,29 @@ public class AutomationCompositionOutHandler { */ public void updateCompositionState(UUID compositionId, AcTypeState state, StateChangeResult stateChangeResult, String message) { + if (compositionId == null) { + LOGGER.error("Cannot update Automation composition definition state, id is null"); + return; + } + + if (stateChangeResult == null) { + LOGGER.error("Cannot update Automation composition definition state, stateChangeResult is null"); + return; + } + if (!StateChangeResult.NO_ERROR.equals(stateChangeResult) + && !StateChangeResult.FAILED.equals(stateChangeResult)) { + LOGGER.error("Cannot update Automation composition definition state, stateChangeResult is not valid"); + return; + } + + if ((state == null) || AcTypeState.PRIMING.equals(state) || AcTypeState.DEPRIMING.equals(state)) { + LOGGER.error("state invalid {} cannot be handled", state); + return; + } + var participantPrimeAck = new ParticipantPrimeAck(); participantPrimeAck.setCompositionId(compositionId); - participantPrimeAck.setMessage(message); + participantPrimeAck.setMessage(AcmUtils.validatedMessage(message)); participantPrimeAck.setResult(true); participantPrimeAck.setResponseTo(cacheProvider.getMsgIdentification().get(compositionId)); participantPrimeAck.setCompositionState(state); diff --git a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandlerTest.java b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandlerTest.java index dd8747ff4..202f25c6f 100644 --- a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandlerTest.java +++ b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandlerTest.java @@ -20,7 +20,6 @@ package org.onap.policy.clamp.acm.participant.intermediary.handler; -import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; @@ -50,15 +49,25 @@ class AutomationCompositionOutHandlerTest { @Test void updateAutomationCompositionElementStateNullTest() { var cacheProvider = mock(CacheProvider.class); - var acOutHandler = new AutomationCompositionOutHandler(mock(ParticipantMessagePublisher.class), cacheProvider); + var publisher = mock(ParticipantMessagePublisher.class); + var acOutHandler = new AutomationCompositionOutHandler(publisher, cacheProvider); assertDoesNotThrow( () -> acOutHandler.updateAutomationCompositionElementState(null, null, null, null, null, null)); + assertDoesNotThrow(() -> acOutHandler.updateAutomationCompositionElementState(null, + UUID.randomUUID(), null, null, null, null)); + + assertDoesNotThrow(() -> acOutHandler.updateAutomationCompositionElementState(UUID.randomUUID(), + null, null, null, null, null)); + assertDoesNotThrow(() -> acOutHandler.updateAutomationCompositionElementState(UUID.randomUUID(), UUID.randomUUID(), null, null, null, null)); assertDoesNotThrow(() -> acOutHandler.updateAutomationCompositionElementState(UUID.randomUUID(), + UUID.randomUUID(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, null)); + + assertDoesNotThrow(() -> acOutHandler.updateAutomationCompositionElementState(UUID.randomUUID(), UUID.randomUUID(), DeployState.DEPLOYED, null, null, null)); var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next(); @@ -66,20 +75,33 @@ class AutomationCompositionOutHandlerTest { .thenReturn(automationComposition); assertDoesNotThrow(() -> acOutHandler.updateAutomationCompositionElementState( automationComposition.getInstanceId(), UUID.randomUUID(), DeployState.DEPLOYED, - null, null, null)); + null, StateChangeResult.NO_ERROR, null)); var elementId = automationComposition.getElements().values().iterator().next().getId(); assertDoesNotThrow(() -> acOutHandler.updateAutomationCompositionElementState( - automationComposition.getInstanceId(), elementId, null, null, null, null)); + automationComposition.getInstanceId(), elementId, null, null, + StateChangeResult.NO_ERROR, null)); assertDoesNotThrow(() -> acOutHandler.updateAutomationCompositionElementStage( - elementId, null, null, 0, null)); + elementId, null, StateChangeResult.NO_ERROR, 0, null)); assertDoesNotThrow(() -> acOutHandler.updateAutomationCompositionElementStage( - null, elementId, null, 0, null)); + null, elementId, StateChangeResult.NO_ERROR, 0, null)); assertDoesNotThrow(() -> acOutHandler.updateAutomationCompositionElementStage( - UUID.randomUUID(), elementId, null, 0, null)); + UUID.randomUUID(), elementId, StateChangeResult.NO_ERROR, 0, null)); assertDoesNotThrow(() -> acOutHandler.updateAutomationCompositionElementStage( - automationComposition.getInstanceId(), UUID.randomUUID(), null, 0, null)); + automationComposition.getInstanceId(), UUID.randomUUID(), + StateChangeResult.NO_ERROR, 0, null)); + assertDoesNotThrow(() -> acOutHandler.updateAutomationCompositionElementState( + automationComposition.getInstanceId(), elementId, DeployState.DEPLOYED, LockState.LOCKED, + StateChangeResult.NO_ERROR, null)); + assertDoesNotThrow(() -> acOutHandler.updateAutomationCompositionElementState( + automationComposition.getInstanceId(), elementId, DeployState.DEPLOYING, null, + StateChangeResult.NO_ERROR, "")); + assertDoesNotThrow(() -> acOutHandler.updateAutomationCompositionElementState( + automationComposition.getInstanceId(), elementId, DeployState.DEPLOYED, null, + StateChangeResult.TIMEOUT, "")); + + verify(publisher, times(0)).sendAutomationCompositionAck(any()); } @Test @@ -144,23 +166,6 @@ class AutomationCompositionOutHandlerTest { } @Test - void updateAutomationCompositionElementStateRestartedTest() { - var publisher = mock(ParticipantMessagePublisher.class); - var cacheProvider = mock(CacheProvider.class); - var acOutHandler = new AutomationCompositionOutHandler(publisher, cacheProvider); - - var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next(); - when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId())) - .thenReturn(automationComposition); - var element = automationComposition.getElements().values().iterator().next(); - element.setRestarting(true); - acOutHandler.updateAutomationCompositionElementState(automationComposition.getInstanceId(), element.getId(), - DeployState.DEPLOYED, LockState.LOCKED, StateChangeResult.NO_ERROR, "Restarted"); - verify(publisher).sendAutomationCompositionAck(any(AutomationCompositionDeployAck.class)); - assertThat(element.getRestarting()).isNull(); - } - - @Test void updateAutomationCompositionElementStateDeleteTest() { var publisher = mock(ParticipantMessagePublisher.class); var cacheProvider = mock(CacheProvider.class); @@ -211,6 +216,28 @@ class AutomationCompositionOutHandlerTest { } @Test + void updateCompositionStateNullTest() { + var publisher = mock(ParticipantMessagePublisher.class); + var cacheProvider = mock(CacheProvider.class); + var acOutHandler = new AutomationCompositionOutHandler(publisher, cacheProvider); + + assertDoesNotThrow( + () -> acOutHandler.updateCompositionState(null, null, null, null)); + assertDoesNotThrow(() -> acOutHandler.updateCompositionState(UUID.randomUUID(), null, + StateChangeResult.NO_ERROR, null)); + assertDoesNotThrow( + () -> acOutHandler.updateCompositionState(UUID.randomUUID(), AcTypeState.PRIMED, null, null)); + assertDoesNotThrow(() -> acOutHandler.updateCompositionState(UUID.randomUUID(), AcTypeState.PRIMING, + StateChangeResult.NO_ERROR, null)); + assertDoesNotThrow(() -> acOutHandler.updateCompositionState(UUID.randomUUID(), AcTypeState.DEPRIMING, + StateChangeResult.NO_ERROR, null)); + assertDoesNotThrow(() -> acOutHandler.updateCompositionState(UUID.randomUUID(), AcTypeState.PRIMED, + StateChangeResult.TIMEOUT, null)); + + verify(publisher, times(0)).sendParticipantPrimeAck(any()); + } + + @Test void updateCompositionStatePrimedTest() { var cacheProvider = mock(CacheProvider.class); when(cacheProvider.getParticipantId()).thenReturn(UUID.randomUUID()); |