diff options
author | FrancescoFioraEst <francesco.fiora@est.tech> | 2023-06-09 10:27:53 +0100 |
---|---|---|
committer | Francesco Fiora <francesco.fiora@est.tech> | 2023-06-09 09:42:08 +0000 |
commit | 5ccedbf315e6879005e1748baaccbfda9e81097e (patch) | |
tree | 7fccb4275be9b6405511a76e7b2127612c74f7a3 /participant/participant-intermediary/src/main | |
parent | d58c0ca04ae993702b2c399afd52b01e503ec0fe (diff) |
Add Minor Refactor for Intermediary to support failure handler
First part related to refactor Intermediary to support failure handler.
Rest of refactor will be implemented in POLICY-4707.
Issue-ID: POLICY-4732
Change-Id: I12fbec84c154937f3c197070862b18ad5675cc3e
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Diffstat (limited to 'participant/participant-intermediary/src/main')
3 files changed, 349 insertions, 1 deletions
diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/comm/AcPropertyUpdateListener.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/comm/AcPropertyUpdateListener.java index d6f1970d7..68eb17517 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/comm/AcPropertyUpdateListener.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/comm/AcPropertyUpdateListener.java @@ -21,7 +21,6 @@ package org.onap.policy.clamp.acm.participant.intermediary.comm; import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler; -import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy; import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType; import org.onap.policy.clamp.models.acm.messages.dmaap.participant.PropertiesUpdate; import org.springframework.stereotype.Component; 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 new file mode 100644 index 000000000..665baee42 --- /dev/null +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandler.java @@ -0,0 +1,199 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 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.acm.participant.intermediary.handler; + +import java.util.List; +import java.util.Map; +import java.util.UUID; +import lombok.RequiredArgsConstructor; +import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher; +import org.onap.policy.clamp.models.acm.concepts.AcElementDeployAck; +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.AutomationCompositionElementInfo; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo; +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.ParticipantState; +import org.onap.policy.clamp.models.acm.concepts.StateChangeResult; +import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeployAck; +import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType; +import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class AutomationCompositionOutHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionOutHandler.class); + + private final ParticipantMessagePublisher publisher; + private final CacheProvider cacheProvider; + + /** + * Handle a automation composition element state change message. + * + * @param automationCompositionId the automationComposition Id + * @param elementId the automationComposition Element Id + * @param deployState the DeployState state + * @param lockState the LockState state + * @param message the message + * @param stateChangeResult the indicator if error occurs + */ + public void updateAutomationCompositionElementState(UUID automationCompositionId, UUID elementId, + DeployState deployState, LockState lockState, StateChangeResult stateChangeResult, String message) { + + if (automationCompositionId == null || elementId == null) { + LOGGER.error("Cannot update Automation composition element state, id is null"); + return; + } + + if ((deployState != null && lockState != null) || (deployState == null && lockState == null)) { + LOGGER.error("state error {} and {} cannot be handled", deployState, lockState); + return; + } + + var automationComposition = cacheProvider.getAutomationComposition(automationCompositionId); + if (automationComposition == null) { + LOGGER.error("Cannot update Automation composition element state, Automation composition id {} not present", + automationCompositionId); + return; + } + + var element = automationComposition.getElements().get(elementId); + if (element == null) { + var msg = "Cannot update Automation composition element state, AC Element id {} not present"; + LOGGER.error(msg, elementId); + return; + } + + if (deployState != null) { + handleDeployState(automationComposition, element, deployState); + } + if (lockState != null) { + handleLockState(automationComposition, element, lockState); + } + + var automationCompositionStateChangeAck = + new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK); + automationCompositionStateChangeAck.setParticipantId(cacheProvider.getParticipantId()); + automationCompositionStateChangeAck.setMessage(message); + automationCompositionStateChangeAck.setStateChangeResult(stateChangeResult); + automationCompositionStateChangeAck.setAutomationCompositionId(automationCompositionId); + automationCompositionStateChangeAck.getAutomationCompositionResultMap().put(element.getId(), + new AcElementDeployAck(element.getDeployState(), element.getLockState(), element.getOperationalState(), + element.getUseState(), element.getOutProperties(), true, message)); + LOGGER.debug("Automation composition element {} state changed to {}", elementId, deployState); + automationCompositionStateChangeAck.setResult(true); + publisher.sendAutomationCompositionAck(automationCompositionStateChangeAck); + } + + private void handleDeployState(AutomationComposition automationComposition, AutomationCompositionElement element, + DeployState deployState) { + element.setDeployState(deployState); + element.setLockState(DeployState.DEPLOYED.equals(element.getDeployState()) ? LockState.LOCKED : LockState.NONE); + var checkOpt = automationComposition.getElements().values().stream() + .filter(acElement -> !deployState.equals(acElement.getDeployState())).findAny(); + if (checkOpt.isEmpty()) { + automationComposition.setDeployState(deployState); + automationComposition.setLockState(element.getLockState()); + + if (DeployState.DELETED.equals(deployState)) { + cacheProvider.removeAutomationComposition(automationComposition.getInstanceId()); + } + } + } + + private void handleLockState(AutomationComposition automationComposition, AutomationCompositionElement element, + LockState lockState) { + element.setLockState(lockState); + var checkOpt = automationComposition.getElements().values().stream() + .filter(acElement -> !lockState.equals(acElement.getLockState())).findAny(); + if (checkOpt.isEmpty()) { + automationComposition.setLockState(lockState); + } + } + + /** + * Send Ac Element Info. + * + * @param automationCompositionId the automationComposition Id + * @param elementId the automationComposition Element id + * @param useState the use State + * @param operationalState the operational State + * @param outProperties the output Properties Map + */ + public void sendAcElementInfo(UUID automationCompositionId, UUID elementId, String useState, + String operationalState, Map<String, Object> outProperties) { + + if (automationCompositionId == null || elementId == null) { + LOGGER.error("Cannot update Automation composition element state, id is null"); + return; + } + + var automationComposition = cacheProvider.getAutomationComposition(automationCompositionId); + if (automationComposition == null) { + LOGGER.error("Cannot update Automation composition element state, Automation composition id {} not present", + automationComposition); + return; + } + + var element = automationComposition.getElements().get(elementId); + if (element == null) { + var msg = "Cannot update Automation composition element state, AC Element id {} not present"; + LOGGER.error(msg, automationComposition); + return; + } + element.setOperationalState(operationalState); + element.setUseState(useState); + element.setOutProperties(outProperties); + + var statusMsg = new ParticipantStatus(); + statusMsg.setParticipantId(cacheProvider.getParticipantId()); + statusMsg.setState(ParticipantState.ON_LINE); + statusMsg.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes()); + var acInfo = new AutomationCompositionInfo(); + acInfo.setAutomationCompositionId(automationCompositionId); + acInfo.setDeployState(automationComposition.getDeployState()); + acInfo.setLockState(automationComposition.getLockState()); + acInfo.setElements(List.of(getAutomationCompositionElementInfo(element))); + statusMsg.setAutomationCompositionInfoList(List.of(acInfo)); + publisher.sendParticipantStatus(statusMsg); + } + + /** + * Get AutomationCompositionElementInfo from AutomationCompositionElement. + * + * @param element the AutomationCompositionElement + * @return the AutomationCompositionElementInfo + */ + public AutomationCompositionElementInfo getAutomationCompositionElementInfo(AutomationCompositionElement element) { + var elementInfo = new AutomationCompositionElementInfo(); + elementInfo.setAutomationCompositionElementId(element.getId()); + elementInfo.setDeployState(element.getDeployState()); + elementInfo.setLockState(element.getLockState()); + elementInfo.setOperationalState(element.getOperationalState()); + elementInfo.setUseState(element.getUseState()); + elementInfo.setOutProperties(element.getOutProperties()); + return elementInfo; + } +} 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 new file mode 100644 index 000000000..09e75e8d5 --- /dev/null +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/CacheProvider.java @@ -0,0 +1,150 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 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.acm.participant.intermediary.handler; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import lombok.Getter; +import lombok.NonNull; +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; +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.ParticipantSupportedElementType; +import org.onap.policy.models.base.PfUtils; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.springframework.stereotype.Component; + +@Component +public class CacheProvider { + + @Getter + private final UUID participantId; + + private final List<ParticipantSupportedElementType> supportedAcElementTypes; + + @Getter + private final Map<UUID, AutomationComposition> automationCompositions = new ConcurrentHashMap<>(); + + @Getter + private final Map<UUID, Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition>> acElementsDefinitions = + new ConcurrentHashMap<>(); + + /** + * Constructor. + * + * @param parameters the parameters of the participant + */ + public CacheProvider(ParticipantParameters parameters) { + this.participantId = parameters.getIntermediaryParameters().getParticipantId(); + this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes(); + } + + public List<ParticipantSupportedElementType> getSupportedAcElementTypes() { + return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new); + } + + /** + * Get AutomationComposition by id. + * + * @param automationCompositionId the AutomationComposition Id + * @return the AutomationComposition + */ + public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) { + return automationCompositions.get(automationCompositionId); + } + + /** + * Remove AutomationComposition. + * + * @param automationCompositionId the AutomationComposition Id + */ + public void removeAutomationComposition(@NonNull UUID automationCompositionId) { + automationCompositions.remove(automationCompositionId); + } + + /** + * Add ElementDefinition. + * + * @param compositionId the composition Id + * @param list the list of AutomationCompositionElementDefinition to add + */ + public void addElementDefinition(@NonNull UUID compositionId, List<AutomationCompositionElementDefinition> list) { + Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> map = new HashMap<>(); + for (var acElementDefinition : list) { + map.put(acElementDefinition.getAcElementDefinitionId(), acElementDefinition); + } + acElementsDefinitions.put(compositionId, map); + } + + public void removeElementDefinition(@NonNull UUID compositionId) { + acElementsDefinitions.remove(compositionId); + } + + /** + * Get CommonProperties. + * + * @param instanceId the Automation Composition Id + * @param acElementId the Automation Composition Element Id + * @return the common Properties as Map + */ + public Map<String, Object> getCommonProperties(@NonNull UUID instanceId, @NonNull UUID acElementId) { + var automationComposition = automationCompositions.get(instanceId); + var map = acElementsDefinitions.get(automationComposition.getCompositionId()); + var element = automationComposition.getElements().get(acElementId); + return map.get(element.getDefinition()).getAutomationCompositionElementToscaNodeTemplate().getProperties(); + } + + /** + * Initialize an AutomationComposition from a ParticipantDeploy. + * + * @param compositionId the composition Id + * @param instanceId the Automation Composition Id + * @param participantDeploy the ParticipantDeploy + */ + public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId, + ParticipantDeploy participantDeploy) { + Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>(); + for (var element : participantDeploy.getAcElementList()) { + var acElement = new AutomationCompositionElement(); + acElement.setId(element.getId()); + acElement.setParticipantId(getParticipantId()); + acElement.setDefinition(element.getDefinition()); + acElement.setDeployState(DeployState.DEPLOYING); + acElement.setLockState(LockState.NONE); + acElement.setProperties(element.getProperties()); + acElementMap.put(element.getId(), acElement); + } + + var automationComposition = new AutomationComposition(); + automationComposition.setCompositionId(compositionId); + automationComposition.setInstanceId(instanceId); + automationComposition.setElements(acElementMap); + automationCompositions.put(automationComposition.getInstanceId(), automationComposition); + } +} |