diff options
author | FrancescoFioraEst <francesco.fiora@est.tech> | 2024-04-08 08:39:50 +0100 |
---|---|---|
committer | Francesco Fiora <francesco.fiora@est.tech> | 2024-04-10 10:00:01 +0000 |
commit | 7c6f474a643730190961f4671004bf1794962e3b (patch) | |
tree | d84747c791dbb7a619e20db66383070f8f284bf7 /participant/participant-intermediary/src/main/java | |
parent | 261ada4c1c7f581e5480434541ff3ee558518e37 (diff) |
Refactor ACM participant-intermediary
Refactor AutomationCompositionHandler to improve the maintainability.
Issue-ID: POLICY-4959
Change-Id: Id112ade5b786d964d61021fe82689aa9512b4bc3
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Diffstat (limited to 'participant/participant-intermediary/src/main/java')
5 files changed, 285 insertions, 269 deletions
diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AcDefinitionHandler.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AcDefinitionHandler.java new file mode 100644 index 000000000..e1d4b0959 --- /dev/null +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AcDefinitionHandler.java @@ -0,0 +1,137 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.acm.participant.intermediary.handler; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; +import lombok.RequiredArgsConstructor; +import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto; +import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher; +import org.onap.policy.clamp.models.acm.concepts.AcTypeState; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition; +import org.onap.policy.clamp.models.acm.concepts.ParticipantState; +import org.onap.policy.clamp.models.acm.concepts.StateChangeResult; +import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrime; +import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrimeAck; +import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRestart; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class AcDefinitionHandler { + + private final CacheProvider cacheProvider; + private final ParticipantMessagePublisher publisher; + private final ThreadHandler listener; + + /** + * Handle a participant Prime message. + * + * @param participantPrimeMsg the ParticipantPrime message + */ + public void handlePrime(ParticipantPrime participantPrimeMsg) { + if (!participantPrimeMsg.getParticipantDefinitionUpdates().isEmpty()) { + // prime + List<AutomationCompositionElementDefinition> list = new ArrayList<>(); + for (var participantDefinition : participantPrimeMsg.getParticipantDefinitionUpdates()) { + if (participantDefinition.getParticipantId().equals(cacheProvider.getParticipantId())) { + list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList()); + } + } + if (!list.isEmpty()) { + cacheProvider.addElementDefinition(participantPrimeMsg.getCompositionId(), list); + prime(participantPrimeMsg.getMessageId(), participantPrimeMsg.getCompositionId(), list); + } + } else { + // deprime + deprime(participantPrimeMsg.getMessageId(), participantPrimeMsg.getCompositionId()); + } + } + + private void prime(UUID messageId, UUID compositionId, List<AutomationCompositionElementDefinition> list) { + var inPropertiesMap = list.stream().collect(Collectors.toMap( + AutomationCompositionElementDefinition::getAcElementDefinitionId, + el -> el.getAutomationCompositionElementToscaNodeTemplate().getProperties())); + var outPropertiesMap = list.stream().collect(Collectors.toMap( + AutomationCompositionElementDefinition::getAcElementDefinitionId, + AutomationCompositionElementDefinition::getOutProperties)); + listener.prime(messageId, new CompositionDto(compositionId, inPropertiesMap, outPropertiesMap)); + } + + private void deprime(UUID messageId, UUID compositionId) { + var acElementsDefinitions = cacheProvider.getAcElementsDefinitions().get(compositionId); + if (acElementsDefinitions == null) { + // this participant does not handle this composition + var participantPrimeAck = new ParticipantPrimeAck(); + participantPrimeAck.setCompositionId(compositionId); + participantPrimeAck.setMessage("Already deprimed or never primed"); + participantPrimeAck.setResult(true); + participantPrimeAck.setResponseTo(messageId); + participantPrimeAck.setCompositionState(AcTypeState.COMMISSIONED); + participantPrimeAck.setStateChangeResult(StateChangeResult.NO_ERROR); + participantPrimeAck.setParticipantId(cacheProvider.getParticipantId()); + participantPrimeAck.setState(ParticipantState.ON_LINE); + publisher.sendParticipantPrimeAck(participantPrimeAck); + return; + } + var list = new ArrayList<>(acElementsDefinitions.values()); + var inPropertiesMap = list.stream().collect(Collectors.toMap( + AutomationCompositionElementDefinition::getAcElementDefinitionId, + el -> el.getAutomationCompositionElementToscaNodeTemplate().getProperties())); + var outPropertiesMap = list.stream().collect(Collectors.toMap( + AutomationCompositionElementDefinition::getAcElementDefinitionId, + AutomationCompositionElementDefinition::getOutProperties)); + listener.deprime(messageId, new CompositionDto(compositionId, inPropertiesMap, outPropertiesMap)); + } + + /** + * Handle a ParticipantRestart message. + * + * @param participantRestartMsg the participantRestart message + */ + public void handleParticipantRestart(ParticipantRestart participantRestartMsg) { + List<AutomationCompositionElementDefinition> list = new ArrayList<>(); + for (var participantDefinition : participantRestartMsg.getParticipantDefinitionUpdates()) { + list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList()); + } + if (!AcTypeState.COMMISSIONED.equals(participantRestartMsg.getState())) { + cacheProvider.addElementDefinition(participantRestartMsg.getCompositionId(), list); + } + + for (var automationcomposition : participantRestartMsg.getAutomationcompositionList()) { + cacheProvider + .initializeAutomationComposition(participantRestartMsg.getCompositionId(), automationcomposition); + } + var inPropertiesMap = list.stream().collect(Collectors.toMap( + AutomationCompositionElementDefinition::getAcElementDefinitionId, + el -> el.getAutomationCompositionElementToscaNodeTemplate().getProperties())); + var outPropertiesMap = list.stream().collect(Collectors.toMap( + AutomationCompositionElementDefinition::getAcElementDefinitionId, + AutomationCompositionElementDefinition::getOutProperties)); + var composition = + new CompositionDto(participantRestartMsg.getCompositionId(), inPropertiesMap, outPropertiesMap); + listener.restarted(participantRestartMsg.getMessageId(), composition, participantRestartMsg.getState(), + participantRestartMsg.getAutomationcompositionList()); + } +} diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AcLockHandler.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AcLockHandler.java new file mode 100644 index 000000000..ddf465a2d --- /dev/null +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AcLockHandler.java @@ -0,0 +1,104 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.acm.participant.intermediary.handler; + +import java.util.UUID; +import lombok.RequiredArgsConstructor; +import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto; +import org.onap.policy.clamp.models.acm.concepts.AutomationComposition; +import org.onap.policy.clamp.models.acm.concepts.LockState; +import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils; +import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionStateChange; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class AcLockHandler { + + private static final Logger LOGGER = LoggerFactory.getLogger(AcLockHandler.class); + + private final CacheProvider cacheProvider; + private final ThreadHandler listener; + + /** + * Handle a automation composition state change message. + * + * @param stateChangeMsg the state change message + */ + public void handleAutomationCompositionStateChange(AutomationCompositionStateChange stateChangeMsg) { + if (stateChangeMsg.getAutomationCompositionId() == null) { + return; + } + + var automationComposition = cacheProvider.getAutomationComposition(stateChangeMsg.getAutomationCompositionId()); + + if (automationComposition == null) { + LOGGER.debug("Automation composition {} does not use this participant", + stateChangeMsg.getAutomationCompositionId()); + return; + } + + switch (stateChangeMsg.getLockOrderedState()) { + case LOCK -> handleLockState(stateChangeMsg.getMessageId(), automationComposition, + stateChangeMsg.getStartPhase()); + case UNLOCK -> handleUnlockState(stateChangeMsg.getMessageId(), automationComposition, + stateChangeMsg.getStartPhase()); + default -> LOGGER.error("StateChange message has no lock order {}", automationComposition.getKey()); + } + } + + private void handleLockState(UUID messageId, final AutomationComposition automationComposition, + Integer startPhaseMsg) { + for (var element : automationComposition.getElements().values()) { + var compositionInProperties = cacheProvider + .getCommonProperties(automationComposition.getCompositionId(), element.getDefinition()); + int startPhase = ParticipantUtils.findStartPhase(compositionInProperties); + if (startPhaseMsg.equals(startPhase)) { + element.setLockState(LockState.LOCKING); + var compositionElement = cacheProvider.createCompositionElementDto( + automationComposition.getCompositionId(), element, compositionInProperties); + var instanceElement = new InstanceElementDto(automationComposition.getInstanceId(), element.getId(), + null, element.getProperties(), element.getOutProperties()); + listener.lock(messageId, compositionElement, instanceElement); + } + } + } + + private void handleUnlockState(UUID messageId, final AutomationComposition automationComposition, + Integer startPhaseMsg) { + for (var element : automationComposition.getElements().values()) { + var compositionInProperties = cacheProvider + .getCommonProperties(automationComposition.getCompositionId(), element.getDefinition()); + int startPhase = ParticipantUtils.findStartPhase(compositionInProperties); + if (startPhaseMsg.equals(startPhase)) { + element.setLockState(LockState.UNLOCKING); + var compositionElement = cacheProvider.createCompositionElementDto( + automationComposition.getCompositionId(), element, compositionInProperties); + var instanceElement = new InstanceElementDto(automationComposition.getInstanceId(), element.getId(), + null, element.getProperties(), element.getOutProperties()); + listener.unlock(messageId, compositionElement, instanceElement); + } + } + } +} diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionHandler.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionHandler.java index 3f3d5756a..5c54861f7 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionHandler.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionHandler.java @@ -21,26 +21,18 @@ package org.onap.policy.clamp.acm.participant.intermediary.handler; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; -import java.util.stream.Collectors; -import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto; +import lombok.RequiredArgsConstructor; import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto; import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto; import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher; import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy; -import org.onap.policy.clamp.models.acm.concepts.AcTypeState; import org.onap.policy.clamp.models.acm.concepts.AutomationComposition; -import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement; -import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition; import org.onap.policy.clamp.models.acm.concepts.DeployState; -import org.onap.policy.clamp.models.acm.concepts.LockState; import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy; -import org.onap.policy.clamp.models.acm.concepts.ParticipantRestartAc; -import org.onap.policy.clamp.models.acm.concepts.ParticipantState; import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils; import org.onap.policy.clamp.models.acm.concepts.StateChangeResult; import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeploy; @@ -48,11 +40,8 @@ import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCom import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionMigration; import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionStateChange; 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.PropertiesUpdate; import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder; -import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder; -import org.onap.policy.clamp.models.acm.persistence.provider.AcInstanceStateResolver; import org.onap.policy.clamp.models.acm.utils.AcmUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -62,28 +51,14 @@ import org.springframework.stereotype.Component; * This class is responsible for managing the state of all automation compositions in the participant. */ @Component +@RequiredArgsConstructor public class AutomationCompositionHandler { private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionHandler.class); private final CacheProvider cacheProvider; private final ParticipantMessagePublisher publisher; private final ThreadHandler listener; - private final AcInstanceStateResolver acInstanceStateResolver; - /** - * Constructor, set the participant ID and messageSender. - * - * @param cacheProvider the Cache Provider - * @param publisher the ParticipantMessage Publisher - * @param listener the ThreadHandler Listener - */ - public AutomationCompositionHandler(CacheProvider cacheProvider, ParticipantMessagePublisher publisher, - ThreadHandler listener) { - this.cacheProvider = cacheProvider; - this.publisher = publisher; - this.listener = listener; - this.acInstanceStateResolver = new AcInstanceStateResolver(); - } /** * Handle a automation composition state change message. @@ -115,77 +90,13 @@ public class AutomationCompositionHandler { return; } - if (!checkConsistantOrderState(automationComposition, stateChangeMsg.getDeployOrderedState(), - stateChangeMsg.getLockOrderedState())) { - LOGGER.warn("Not Consistant OrderState Automation composition {}", - stateChangeMsg.getAutomationCompositionId()); - return; - } - - if (DeployOrder.NONE.equals(stateChangeMsg.getDeployOrderedState())) { - handleLockOrderState(stateChangeMsg.getMessageId(), automationComposition, - stateChangeMsg.getLockOrderedState(), stateChangeMsg.getStartPhase()); - } else { - handleDeployOrderState(stateChangeMsg.getMessageId(), automationComposition, - stateChangeMsg.getDeployOrderedState(), stateChangeMsg.getStartPhase()); - } - } - - private boolean checkConsistantOrderState(AutomationComposition automationComposition, DeployOrder deployOrder, - LockOrder lockOrder) { - if (DeployOrder.UPDATE.equals(deployOrder)) { - return true; - } - return acInstanceStateResolver.resolve(deployOrder, lockOrder, automationComposition.getDeployState(), - automationComposition.getLockState(), automationComposition.getStateChangeResult()) != null; - } - - /** - * Method to handle state changes. - * - * @param messageId the messageId - * @param automationComposition participant response - * @param orderedState automation composition ordered state - * @param startPhaseMsg startPhase from message - */ - private void handleDeployOrderState(UUID messageId, final AutomationComposition automationComposition, - DeployOrder orderedState, Integer startPhaseMsg) { - - switch (orderedState) { - case UNDEPLOY: - handleUndeployState(messageId, automationComposition, startPhaseMsg); - break; - case DELETE: - handleDeleteState(messageId, automationComposition, startPhaseMsg); - break; - - default: - LOGGER.error("StateChange message has no state, state is null {}", automationComposition.getKey()); - break; - } - } - - /** - * Method to handle state changes. - * - * @param messageId the messageId - * @param automationComposition participant response - * @param orderedState automation composition ordered state - * @param startPhaseMsg startPhase from message - */ - private void handleLockOrderState(UUID messageId, final AutomationComposition automationComposition, - LockOrder orderedState, Integer startPhaseMsg) { - - switch (orderedState) { - case LOCK: - handleLockState(messageId, automationComposition, startPhaseMsg); - break; - case UNLOCK: - handleUnlockState(messageId, automationComposition, startPhaseMsg); - break; - default: - LOGGER.error("StateChange message has no state, state is null {}", automationComposition.getKey()); - break; + switch (stateChangeMsg.getDeployOrderedState()) { + case UNDEPLOY -> handleUndeployState(stateChangeMsg.getMessageId(), automationComposition, + stateChangeMsg.getStartPhase()); + case DELETE -> handleDeleteState(stateChangeMsg.getMessageId(), automationComposition, + stateChangeMsg.getStartPhase()); + default -> + LOGGER.error("StateChange message has no state, state is null {}", automationComposition.getKey()); } } @@ -248,8 +159,8 @@ public class AutomationCompositionHandler { .getCommonProperties(automationComposition.getCompositionId(), element.getDefinition()); int startPhase = ParticipantUtils.findStartPhase(compositionInProperties); if (startPhaseMsg.equals(startPhase)) { - var compositionElement = createCompositionElementDto(automationComposition.getCompositionId(), - element, compositionInProperties); + var compositionElement = cacheProvider.createCompositionElementDto( + automationComposition.getCompositionId(), element, compositionInProperties); var instanceElement = new InstanceElementDto(instanceId, elementDeploy.getId(), elementDeploy.getToscaServiceTemplateFragment(), elementDeploy.getProperties(), element.getOutProperties()); @@ -258,20 +169,13 @@ public class AutomationCompositionHandler { } } - private CompositionElementDto createCompositionElementDto(UUID compositionId, AutomationCompositionElement element, - Map<String, Object> compositionInProperties) { - var compositionOutProperties = cacheProvider.getAcElementsDefinitions() - .get(compositionId).get(element.getDefinition()).getOutProperties(); - return new CompositionElementDto(compositionId, - element.getDefinition(), compositionInProperties, compositionOutProperties); - } - private Map<UUID, CompositionElementDto> getCompositionElementDtoMap(AutomationComposition automationComposition, UUID compositionId) { Map<UUID, CompositionElementDto> map = new HashMap<>(); for (var element : automationComposition.getElements().values()) { var compositionInProperties = cacheProvider.getCommonProperties(compositionId, element.getDefinition()); - var compositionElement = createCompositionElementDto(compositionId, element, compositionInProperties); + var compositionElement = cacheProvider + .createCompositionElementDto(compositionId, element, compositionInProperties); map.put(element.getId(), compositionElement); } return map; @@ -330,8 +234,8 @@ public class AutomationCompositionHandler { int startPhase = ParticipantUtils.findStartPhase(compositionInProperties); if (startPhaseMsg.equals(startPhase)) { element.setDeployState(DeployState.UNDEPLOYING); - var compositionElement = createCompositionElementDto(automationComposition.getCompositionId(), - element, compositionInProperties); + var compositionElement = cacheProvider.createCompositionElementDto( + automationComposition.getCompositionId(), element, compositionInProperties); var instanceElement = new InstanceElementDto(automationComposition.getInstanceId(), element.getId(), null, element.getProperties(), element.getOutProperties()); listener.undeploy(messageId, compositionElement, instanceElement); @@ -347,8 +251,8 @@ public class AutomationCompositionHandler { int startPhase = ParticipantUtils.findStartPhase(compositionInProperties); if (startPhaseMsg.equals(startPhase)) { element.setDeployState(DeployState.DELETING); - var compositionElement = createCompositionElementDto(automationComposition.getCompositionId(), - element, compositionInProperties); + var compositionElement = cacheProvider.createCompositionElementDto( + automationComposition.getCompositionId(), element, compositionInProperties); var instanceElement = new InstanceElementDto(automationComposition.getInstanceId(), element.getId(), null, element.getProperties(), element.getOutProperties()); listener.delete(messageId, compositionElement, instanceElement); @@ -357,128 +261,6 @@ public class AutomationCompositionHandler { } /** - * Method to handle when the new state from participant is PASSIVE state. - * - * @param messageId the messageId - * @param automationComposition participant response - * @param startPhaseMsg startPhase from message - */ - private void handleLockState(UUID messageId, final AutomationComposition automationComposition, - Integer startPhaseMsg) { - for (var element : automationComposition.getElements().values()) { - var compositionInProperties = cacheProvider - .getCommonProperties(automationComposition.getCompositionId(), element.getDefinition()); - int startPhase = ParticipantUtils.findStartPhase(compositionInProperties); - if (startPhaseMsg.equals(startPhase)) { - element.setLockState(LockState.LOCKING); - var compositionElement = createCompositionElementDto(automationComposition.getCompositionId(), - element, compositionInProperties); - var instanceElement = new InstanceElementDto(automationComposition.getInstanceId(), element.getId(), - null, element.getProperties(), element.getOutProperties()); - listener.lock(messageId, compositionElement, instanceElement); - } - } - } - - /** - * Method to handle when the new state from participant is RUNNING state. - * - * @param messageId the messageId - * @param automationComposition participant response - * @param startPhaseMsg startPhase from message - */ - private void handleUnlockState(UUID messageId, final AutomationComposition automationComposition, - Integer startPhaseMsg) { - for (var element : automationComposition.getElements().values()) { - var compositionInProperties = cacheProvider - .getCommonProperties(automationComposition.getCompositionId(), element.getDefinition()); - int startPhase = ParticipantUtils.findStartPhase(compositionInProperties); - if (startPhaseMsg.equals(startPhase)) { - element.setLockState(LockState.UNLOCKING); - var compositionElement = createCompositionElementDto(automationComposition.getCompositionId(), - element, compositionInProperties); - var instanceElement = new InstanceElementDto(automationComposition.getInstanceId(), element.getId(), - null, element.getProperties(), element.getOutProperties()); - listener.unlock(messageId, compositionElement, instanceElement); - } - } - } - - /** - * Handles prime a Composition Definition. - * - * @param messageId the messageId - * @param compositionId the compositionId - * @param list the list of AutomationCompositionElementDefinition - */ - public void prime(UUID messageId, UUID compositionId, List<AutomationCompositionElementDefinition> list) { - var inPropertiesMap = list.stream().collect(Collectors.toMap( - AutomationCompositionElementDefinition::getAcElementDefinitionId, - el -> el.getAutomationCompositionElementToscaNodeTemplate().getProperties())); - var outPropertiesMap = list.stream().collect(Collectors.toMap( - AutomationCompositionElementDefinition::getAcElementDefinitionId, - AutomationCompositionElementDefinition::getOutProperties)); - listener.prime(messageId, new CompositionDto(compositionId, inPropertiesMap, outPropertiesMap)); - } - - /** - * Handles deprime a Composition Definition. - * - * @param messageId the messageId - * @param compositionId the compositionId - */ - public void deprime(UUID messageId, UUID compositionId) { - var acElementsDefinitions = cacheProvider.getAcElementsDefinitions().get(compositionId); - if (acElementsDefinitions == null) { - // this participant does not handle this composition - var participantPrimeAck = new ParticipantPrimeAck(); - participantPrimeAck.setCompositionId(compositionId); - participantPrimeAck.setMessage("Already deprimed or never primed"); - participantPrimeAck.setResult(true); - participantPrimeAck.setResponseTo(messageId); - participantPrimeAck.setCompositionState(AcTypeState.COMMISSIONED); - participantPrimeAck.setStateChangeResult(StateChangeResult.NO_ERROR); - participantPrimeAck.setParticipantId(cacheProvider.getParticipantId()); - participantPrimeAck.setState(ParticipantState.ON_LINE); - publisher.sendParticipantPrimeAck(participantPrimeAck); - return; - } - var list = new ArrayList<>(acElementsDefinitions.values()); - var inPropertiesMap = list.stream().collect(Collectors.toMap( - AutomationCompositionElementDefinition::getAcElementDefinitionId, - el -> el.getAutomationCompositionElementToscaNodeTemplate().getProperties())); - var outPropertiesMap = list.stream().collect(Collectors.toMap( - AutomationCompositionElementDefinition::getAcElementDefinitionId, - AutomationCompositionElementDefinition::getOutProperties)); - listener.deprime(messageId, new CompositionDto(compositionId, inPropertiesMap, outPropertiesMap)); - } - - /** - * Handles restarted scenario. - * - * @param messageId the messageId - * @param compositionId the compositionId - * @param list the list of AutomationCompositionElementDefinition - * @param state the state of the composition - * @param automationCompositionList list of ParticipantRestartAc - */ - public void restarted(UUID messageId, UUID compositionId, List<AutomationCompositionElementDefinition> list, - AcTypeState state, List<ParticipantRestartAc> automationCompositionList) { - - for (var automationcomposition : automationCompositionList) { - cacheProvider.initializeAutomationComposition(compositionId, automationcomposition); - } - var inPropertiesMap = list.stream().collect(Collectors.toMap( - AutomationCompositionElementDefinition::getAcElementDefinitionId, - el -> el.getAutomationCompositionElementToscaNodeTemplate().getProperties())); - var outPropertiesMap = list.stream().collect(Collectors.toMap( - AutomationCompositionElementDefinition::getAcElementDefinitionId, - AutomationCompositionElementDefinition::getOutProperties)); - var composition = new CompositionDto(compositionId, inPropertiesMap, outPropertiesMap); - listener.restarted(messageId, composition, state, automationCompositionList); - } - - /** * Handles AutomationComposition Migration. * * @param migrationMsg the AutomationCompositionMigration diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/CacheProvider.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/CacheProvider.java index 630dfc6ab..f51e8baad 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/CacheProvider.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/CacheProvider.java @@ -28,6 +28,7 @@ import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import lombok.Getter; import lombok.NonNull; +import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto; import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantParameters; import org.onap.policy.clamp.models.acm.concepts.AutomationComposition; import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement; @@ -201,4 +202,20 @@ public class CacheProvider { automationComposition.setElements(acElementMap); automationCompositions.put(automationComposition.getInstanceId(), automationComposition); } + + /** + * Create CompositionElementDto. + * + * @param compositionId the composition Id + * @param element AutomationComposition Element + * @param compositionInProperties composition definition InProperties + * @return the CompositionElementDto + */ + public CompositionElementDto createCompositionElementDto(UUID compositionId, AutomationCompositionElement element, + Map<String, Object> compositionInProperties) { + var compositionOutProperties = getAcElementsDefinitions() + .get(compositionId).get(element.getDefinition()).getOutProperties(); + return new CompositionElementDto(compositionId, + element.getDefinition(), compositionInProperties, compositionOutProperties); + } } diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandler.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandler.java index ce0da680b..a4a92f883 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandler.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/ParticipantHandler.java @@ -23,12 +23,8 @@ package org.onap.policy.clamp.acm.participant.intermediary.handler; import io.micrometer.core.annotation.Timed; -import java.util.ArrayList; -import java.util.List; import lombok.RequiredArgsConstructor; import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher; -import org.onap.policy.clamp.models.acm.concepts.AcTypeState; -import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition; import org.onap.policy.clamp.models.acm.concepts.ParticipantState; import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeploy; import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionMigration; @@ -44,6 +40,7 @@ import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantRe import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatus; import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantStatusReq; import org.onap.policy.clamp.models.acm.messages.kafka.participant.PropertiesUpdate; +import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -57,6 +54,8 @@ public class ParticipantHandler { private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class); private final AutomationCompositionHandler automationCompositionHandler; + private final AcLockHandler acLockHandler; + private final AcDefinitionHandler acDefinitionHandler; private final ParticipantMessagePublisher publisher; private final CacheProvider cacheProvider; @@ -91,7 +90,11 @@ public class ParticipantHandler { value = "listener.automation_composition_state_change", description = "AUTOMATION_COMPOSITION_STATE_CHANGE messages received") public void handleAutomationCompositionStateChange(AutomationCompositionStateChange stateChangeMsg) { - automationCompositionHandler.handleAutomationCompositionStateChange(stateChangeMsg); + if (DeployOrder.NONE.equals(stateChangeMsg.getDeployOrderedState())) { + acLockHandler.handleAutomationCompositionStateChange(stateChangeMsg); + } else { + automationCompositionHandler.handleAutomationCompositionStateChange(stateChangeMsg); + } } /** @@ -187,25 +190,7 @@ public class ParticipantHandler { @Timed(value = "listener.participant_prime", description = "PARTICIPANT_PRIME messages received") public void handleParticipantPrime(ParticipantPrime participantPrimeMsg) { LOGGER.debug("ParticipantPrime message received for participantId {}", participantPrimeMsg.getParticipantId()); - - if (!participantPrimeMsg.getParticipantDefinitionUpdates().isEmpty()) { - // prime - List<AutomationCompositionElementDefinition> list = new ArrayList<>(); - for (var participantDefinition : participantPrimeMsg.getParticipantDefinitionUpdates()) { - if (participantDefinition.getParticipantId().equals(cacheProvider.getParticipantId())) { - list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList()); - } - } - if (!list.isEmpty()) { - cacheProvider.addElementDefinition(participantPrimeMsg.getCompositionId(), list); - automationCompositionHandler.prime(participantPrimeMsg.getMessageId(), - participantPrimeMsg.getCompositionId(), list); - } - } else { - // deprime - automationCompositionHandler.deprime(participantPrimeMsg.getMessageId(), - participantPrimeMsg.getCompositionId()); - } + acDefinitionHandler.handlePrime(participantPrimeMsg); } /** @@ -217,16 +202,7 @@ public class ParticipantHandler { public void handleParticipantRestart(ParticipantRestart participantRestartMsg) { LOGGER.debug("ParticipantRestart message received for participantId {}", participantRestartMsg.getParticipantId()); - List<AutomationCompositionElementDefinition> list = new ArrayList<>(); - for (var participantDefinition : participantRestartMsg.getParticipantDefinitionUpdates()) { - list.addAll(participantDefinition.getAutomationCompositionElementDefinitionList()); - } - if (!AcTypeState.COMMISSIONED.equals(participantRestartMsg.getState())) { - cacheProvider.addElementDefinition(participantRestartMsg.getCompositionId(), list); - } - automationCompositionHandler.restarted(participantRestartMsg.getMessageId(), - participantRestartMsg.getCompositionId(), list, participantRestartMsg.getState(), - participantRestartMsg.getAutomationcompositionList()); + acDefinitionHandler.handleParticipantRestart(participantRestartMsg); } /** |