diff options
author | Adheli Tavares <adheli.tavares@est.tech> | 2024-04-03 15:27:06 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2024-04-03 15:27:06 +0000 |
commit | a310036db03a1fea8631dc1b0b7451cd492ee137 (patch) | |
tree | 48bd8d85dc83a18ff2cffc3e9fc7ad47ac074621 | |
parent | 9e19b46da3955940e980d2504ffee99aa9c023d1 (diff) | |
parent | 7508b3c9be6312b951af7faf1e8bdd32a87d4375 (diff) |
Merge "Add support for testing in ACM Participant Simulator"
9 files changed, 1211 insertions, 574 deletions
diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandler.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandler.java deleted file mode 100644 index 2cba379c4..000000000 --- a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandler.java +++ /dev/null @@ -1,415 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2023-2024 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.sim.main.handler; - -import java.lang.invoke.MethodHandles; -import java.util.ArrayList; -import java.util.Map; -import java.util.UUID; -import lombok.Getter; -import lombok.Setter; -import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto; -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.api.ParticipantIntermediaryApi; -import org.onap.policy.clamp.acm.participant.intermediary.api.impl.AcElementListenerV2; -import org.onap.policy.clamp.acm.participant.sim.model.InternalData; -import org.onap.policy.clamp.acm.participant.sim.model.InternalDatas; -import org.onap.policy.clamp.acm.participant.sim.model.SimConfig; -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.AutomationCompositions; -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.StateChangeResult; -import org.onap.policy.clamp.models.acm.utils.AcmUtils; -import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -/** - * This class handles implementation of automationCompositionElement updates. - */ -@Component -public class AutomationCompositionElementHandler extends AcElementListenerV2 { - - private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - @Getter - @Setter - private SimConfig config = new SimConfig(); - - public AutomationCompositionElementHandler(ParticipantIntermediaryApi intermediaryApi) { - super(intermediaryApi); - } - - /** - * Handle a deploy on a automation composition element. - * - * @param compositionElement the information of the Automation Composition Definition Element - * @param instanceElement the information of the Automation Composition Instance Element - * @throws PfModelException from Policy framework - */ - @Override - public void deploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement) - throws PfModelException { - LOGGER.debug("deploy call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement); - - if (!execution(config.getDeployTimerMs(), "Current Thread deploy is Interrupted during execution {}", - instanceElement.elementId())) { - return; - } - - if (config.isDeploySuccess()) { - intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(), - instanceElement.elementId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, - "Deployed"); - } else { - intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(), - instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, - "Deploy failed!"); - } - } - - private boolean execution(int timeMs, String msg, UUID elementId) { - long endTime = System.currentTimeMillis() + timeMs; - while (System.currentTimeMillis() < endTime) { - try { - if (Thread.currentThread().isInterrupted()) { - LOGGER.debug(msg, elementId); - return false; - } - Thread.sleep(10L); - } catch (InterruptedException e) { - LOGGER.debug(msg, elementId); - Thread.currentThread().interrupt(); - return false; - } - } - return true; - } - - /** - * Handle a automation composition element state change. - * - * @param compositionElement the information of the Automation Composition Definition Element - * @param instanceElement the information of the Automation Composition Instance Element - * @throws PfModelException from Policy framework - */ - @Override - public void undeploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement) - throws PfModelException { - LOGGER.debug("undeploy call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement); - - if (!execution(config.getUndeployTimerMs(), "Current Thread undeploy is Interrupted during execution {}", - instanceElement.elementId())) { - return; - } - - if (config.isUndeploySuccess()) { - intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(), - instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, - "Undeployed"); - } else { - intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(), - instanceElement.elementId(), DeployState.DEPLOYED, null, StateChangeResult.FAILED, - "Undeploy failed!"); - } - } - - @Override - public void lock(CompositionElementDto compositionElement, InstanceElementDto instanceElement) - throws PfModelException { - LOGGER.debug("lock call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement); - - if (!execution(config.getLockTimerMs(), "Current Thread lock is Interrupted during execution {}", - instanceElement.elementId())) { - return; - } - - if (config.isLockSuccess()) { - intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(), - instanceElement.elementId(), null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked"); - } else { - intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(), - instanceElement.elementId(), null, LockState.UNLOCKED, StateChangeResult.FAILED, "Lock failed!"); - } - } - - @Override - public void unlock(CompositionElementDto compositionElement, InstanceElementDto instanceElement) - throws PfModelException { - LOGGER.debug("unlock call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement); - - if (!execution(config.getUnlockTimerMs(), "Current Thread unlock is Interrupted during execution {}", - instanceElement.elementId())) { - return; - } - - if (config.isUnlockSuccess()) { - intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(), - instanceElement.elementId(), null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked"); - } else { - intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(), - instanceElement.elementId(), null, LockState.LOCKED, StateChangeResult.FAILED, "Unlock failed!"); - } - } - - @Override - public void delete(CompositionElementDto compositionElement, InstanceElementDto instanceElement) - throws PfModelException { - LOGGER.debug("delete call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement); - - if (!execution(config.getDeleteTimerMs(), "Current Thread delete is Interrupted during execution {}", - instanceElement.elementId())) { - return; - } - - if (config.isDeleteSuccess()) { - intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(), - instanceElement.elementId(), DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted"); - } else { - intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(), - instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, - "Delete failed!"); - } - } - - @Override - public void update(CompositionElementDto compositionElement, InstanceElementDto instanceElement, - InstanceElementDto instanceElementUpdated) throws PfModelException { - LOGGER.debug("update call compositionElement: {}, instanceElement: {}, instanceElementUpdated: {}", - compositionElement, instanceElement, instanceElementUpdated); - - if (!execution(config.getUpdateTimerMs(), "Current Thread update is Interrupted during execution {}", - instanceElement.elementId())) { - return; - } - - if (config.isUpdateSuccess()) { - intermediaryApi.updateAutomationCompositionElementState( - instanceElement.instanceId(), instanceElement.elementId(), - DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated"); - } else { - intermediaryApi.updateAutomationCompositionElementState( - instanceElement.instanceId(), instanceElement.elementId(), - DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!"); - } - } - - /** - * Get AutomationComposition. - * - * @return the AutomationCompositions - */ - public AutomationCompositions getAutomationCompositions() { - var result = new AutomationCompositions(); - result.setAutomationCompositionList(new ArrayList<>(intermediaryApi.getAutomationCompositions().values())); - return result; - } - - public AutomationComposition getAutomationComposition(UUID instanceId) { - return intermediaryApi.getAutomationComposition(instanceId); - } - - /** - * Set OutProperties. - * - * @param automationCompositionId the automationComposition Id - * @param elementId the automationComposition Element Id - * @param useState the useState - * @param operationalState the operationalState - * @param outProperties the outProperties - */ - public void setOutProperties(UUID automationCompositionId, UUID elementId, String useState, String operationalState, - Map<String, Object> outProperties) { - intermediaryApi.sendAcElementInfo(automationCompositionId, elementId, useState, operationalState, - outProperties); - } - - @Override - public void prime(CompositionDto composition) throws PfModelException { - LOGGER.debug("prime call composition: {}", composition); - - if (!execution(config.getPrimeTimerMs(), "Current Thread prime is Interrupted during execution {}", - composition.compositionId())) { - return; - } - - if (config.isPrimeSuccess()) { - intermediaryApi.updateCompositionState(composition.compositionId(), - AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed"); - } else { - intermediaryApi.updateCompositionState(composition.compositionId(), - AcTypeState.COMMISSIONED, StateChangeResult.FAILED, "Prime failed!"); - } - } - - @Override - public void deprime(CompositionDto composition) throws PfModelException { - LOGGER.debug("deprime call composition: {}", composition); - - if (!execution(config.getDeprimeTimerMs(), "Current Thread deprime is Interrupted during execution {}", - composition.compositionId())) { - return; - } - - if (config.isDeprimeSuccess()) { - intermediaryApi.updateCompositionState(composition.compositionId(), AcTypeState.COMMISSIONED, - StateChangeResult.NO_ERROR, "Deprimed"); - } else { - intermediaryApi.updateCompositionState(composition.compositionId(), AcTypeState.PRIMED, - StateChangeResult.FAILED, "Deprime failed!"); - } - } - - /** - * Get Instance Data List. - * - * @return the InternalDatas - */ - public InternalDatas getDataList() { - var result = new InternalDatas(); - var map = intermediaryApi.getAutomationCompositions(); - for (var instance : map.values()) { - for (var element : instance.getElements().values()) { - var data = new InternalData(); - data.setCompositionId(instance.getCompositionId()); - data.setCompositionDefinitionElementId(element.getDefinition()); - data.setAutomationCompositionId(instance.getInstanceId()); - data.setAutomationCompositionElementId(element.getId()); - data.setIntProperties(element.getProperties()); - data.setOperationalState(element.getOperationalState()); - data.setUseState(element.getUseState()); - data.setOutProperties(element.getOutProperties()); - result.getList().add(data); - } - } - return result; - } - - @Override - public void handleRestartComposition(CompositionDto composition, AcTypeState state) throws PfModelException { - LOGGER.debug("restart composition definition call"); - switch (state) { - case PRIMING: - prime(composition); - break; - - case DEPRIMING: - deprime(composition); - break; - - default: - intermediaryApi.updateCompositionState(composition.compositionId(), state, - StateChangeResult.NO_ERROR, "Restarted"); - } - } - - @Override - public void handleRestartInstance(CompositionElementDto compositionElement, InstanceElementDto instanceElement, - DeployState deployState, LockState lockState) throws PfModelException { - LOGGER.debug("restart instance call"); - if (!AcmUtils.isInTransitionalState(deployState, lockState)) { - intermediaryApi.updateAutomationCompositionElementState( - instanceElement.instanceId(), instanceElement.elementId(), deployState, lockState, - StateChangeResult.NO_ERROR, "Restarted"); - return; - } - if (DeployState.DEPLOYING.equals(deployState)) { - deploy(compositionElement, instanceElement); - return; - } - if (DeployState.UNDEPLOYING.equals(deployState)) { - undeploy(compositionElement, instanceElement); - return; - } - if (DeployState.UPDATING.equals(deployState)) { - update(compositionElement, instanceElement, instanceElement); - return; - } - if (DeployState.DELETING.equals(deployState)) { - delete(compositionElement, instanceElement); - return; - } - if (LockState.LOCKING.equals(lockState)) { - lock(compositionElement, instanceElement); - return; - } - if (LockState.UNLOCKING.equals(lockState)) { - unlock(compositionElement, instanceElement); - } - } - - /** - * Get Composition Data List. - * - * @return the InternalDatas - */ - public InternalDatas getCompositionDataList() { - var acElementsDefinitions = intermediaryApi.getAcElementsDefinitions(); - var internalDatas = new InternalDatas(); - for (var entry : acElementsDefinitions.entrySet()) { - for (var acElementsDefinition : entry.getValue().values()) { - var internalData = new InternalData(); - internalData.setCompositionId(entry.getKey()); - internalData.setCompositionDefinitionElementId(acElementsDefinition.getAcElementDefinitionId()); - internalData.setIntProperties( - acElementsDefinition.getAutomationCompositionElementToscaNodeTemplate().getProperties()); - internalData.setOutProperties(acElementsDefinition.getOutProperties()); - internalDatas.getList().add(internalData); - } - } - return internalDatas; - } - - public void setCompositionOutProperties(UUID compositionId, ToscaConceptIdentifier compositionDefinitionElementId, - Map<String, Object> outProperties) { - intermediaryApi.sendAcDefinitionInfo(compositionId, compositionDefinitionElementId, outProperties); - - } - - @Override - public void migrate(CompositionElementDto compositionElement, CompositionElementDto compositionElementTarget, - InstanceElementDto instanceElement, InstanceElementDto instanceElementMigrate) - throws PfModelException { - LOGGER.debug("migrate call compositionElement: {}, compositionElementTarget: {}, instanceElement: {}," - + " instanceElementMigrate: {}", - compositionElement, compositionElementTarget, instanceElement, instanceElementMigrate); - - if (!execution(config.getMigrateTimerMs(), "Current Thread migrate is Interrupted during execution {}", - instanceElement.elementId())) { - return; - } - - if (config.isMigrateSuccess()) { - intermediaryApi.updateAutomationCompositionElementState( - instanceElement.instanceId(), instanceElement.elementId(), - DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated"); - } else { - intermediaryApi.updateAutomationCompositionElementState( - instanceElement.instanceId(), instanceElement.elementId(), - DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!"); - } - } -} diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV1.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV1.java new file mode 100644 index 000000000..f503987a2 --- /dev/null +++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV1.java @@ -0,0 +1,170 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.sim.main.handler; + +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.acm.participant.intermediary.api.impl.AcElementListenerV1; +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.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.StateChangeResult; +import org.onap.policy.clamp.models.acm.utils.AcmUtils; +import org.onap.policy.models.base.PfModelException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.stereotype.Component; + +@ConditionalOnExpression("'${element.handler}'=='AcElementHandlerV1'") +@Component +public class AutomationCompositionElementHandlerV1 extends AcElementListenerV1 { + + private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionElementHandlerV1.class); + + private final SimulatorService simulatorService; + + public AutomationCompositionElementHandlerV1(ParticipantIntermediaryApi intermediaryApi, + SimulatorService simulatorService) { + super(intermediaryApi); + this.simulatorService = simulatorService; + } + + @Override + public void deploy(UUID instanceId, AcElementDeploy element, Map<String, Object> properties) + throws PfModelException { + LOGGER.debug("deploy call instanceId: {}, element: {}, properties: {}", instanceId, element, properties); + simulatorService.deploy(instanceId, element.getId()); + } + + @Override + public void undeploy(UUID instanceId, UUID elementId) throws PfModelException { + LOGGER.debug("undeploy call instanceId: {}, elementId: {}", instanceId, elementId); + simulatorService.undeploy(instanceId, elementId); + } + + @Override + public void lock(UUID instanceId, UUID elementId) throws PfModelException { + LOGGER.debug("lock call instanceId: {}, elementId: {}", instanceId, elementId); + simulatorService.lock(instanceId, elementId); + } + + @Override + public void unlock(UUID instanceId, UUID elementId) throws PfModelException { + LOGGER.debug("unlock call instanceId: {}, elementId: {}", instanceId, elementId); + simulatorService.unlock(instanceId, elementId); + } + + @Override + public void delete(UUID instanceId, UUID elementId) throws PfModelException { + LOGGER.debug("delete call instanceId: {}, elementId: {}", instanceId, elementId); + simulatorService.delete(instanceId, elementId); + } + + @Override + public void update(UUID instanceId, AcElementDeploy element, Map<String, Object> properties) + throws PfModelException { + LOGGER.debug("update call instanceId: {}, element: {}, properties: {}", instanceId, element, properties); + simulatorService.update(instanceId, element.getId()); + } + + @Override + public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList) + throws PfModelException { + LOGGER.debug("prime call compositionId: {}, elementDefinitionList: {}", compositionId, elementDefinitionList); + simulatorService.prime(compositionId); + } + + @Override + public void deprime(UUID compositionId) throws PfModelException { + LOGGER.debug("deprime call compositionId: {}", compositionId); + simulatorService.deprime(compositionId); + } + + @Override + public void handleRestartComposition(UUID compositionId, + List<AutomationCompositionElementDefinition> elementDefinitionList, AcTypeState state) + throws PfModelException { + LOGGER.debug("restart composition definition call compositionId: {}, elementDefinitionList: {}, state: {}", + compositionId, elementDefinitionList, state); + + switch (state) { + case PRIMING: + prime(compositionId, elementDefinitionList); + break; + + case DEPRIMING: + deprime(compositionId); + break; + + default: + intermediaryApi.updateCompositionState(compositionId, state, StateChangeResult.NO_ERROR, "Restarted"); + } + } + + @Override + public void handleRestartInstance(UUID instanceId, AcElementDeploy element, + Map<String, Object> properties, DeployState deployState, LockState lockState) throws PfModelException { + LOGGER.debug("restart instance call instanceId: {}, element: {}, properties: {}," + + "deployState: {}, lockState: {}", instanceId, element, properties, deployState, lockState); + + if (!AcmUtils.isInTransitionalState(deployState, lockState)) { + intermediaryApi.updateAutomationCompositionElementState(instanceId, element.getId(), + deployState, lockState, StateChangeResult.NO_ERROR, "Restarted"); + return; + } + if (DeployState.DEPLOYING.equals(deployState)) { + deploy(instanceId, element, properties); + return; + } + if (DeployState.UNDEPLOYING.equals(deployState)) { + undeploy(instanceId, element.getId()); + return; + } + if (DeployState.UPDATING.equals(deployState)) { + update(instanceId, element, properties); + return; + } + if (DeployState.DELETING.equals(deployState)) { + delete(instanceId, element.getId()); + return; + } + if (LockState.LOCKING.equals(lockState)) { + lock(instanceId, element.getId()); + return; + } + if (LockState.UNLOCKING.equals(lockState)) { + unlock(instanceId, element.getId()); + } + } + + @Override + public void migrate(UUID instanceId, AcElementDeploy element, UUID compositionTargetId, + Map<String, Object> properties) throws PfModelException { + LOGGER.debug("migrate call instanceId: {}, element: {}, compositionTargetId: {}, properties: {}", + instanceId, element, compositionTargetId, properties); + simulatorService.migrate(instanceId, element.getId()); + } +} diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV2.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV2.java new file mode 100644 index 000000000..e836c9874 --- /dev/null +++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV2.java @@ -0,0 +1,187 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023-2024 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.sim.main.handler; + +import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto; +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.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.acm.participant.intermediary.api.impl.AcElementListenerV2; +import org.onap.policy.clamp.models.acm.concepts.AcTypeState; +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.StateChangeResult; +import org.onap.policy.clamp.models.acm.utils.AcmUtils; +import org.onap.policy.models.base.PfModelException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.stereotype.Component; + +/** + * This class handles implementation of automationCompositionElement updates. + */ +@ConditionalOnExpression("'${element.handler:AcElementHandlerV2}' == 'AcElementHandlerV2'") +@Component +public class AutomationCompositionElementHandlerV2 extends AcElementListenerV2 { + + private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionElementHandlerV2.class); + + private final SimulatorService simulatorService; + + public AutomationCompositionElementHandlerV2(ParticipantIntermediaryApi intermediaryApi, + SimulatorService simulatorService) { + super(intermediaryApi); + this.simulatorService = simulatorService; + } + + /** + * Handle a deploy on a automation composition element. + * + * @param compositionElement the information of the Automation Composition Definition Element + * @param instanceElement the information of the Automation Composition Instance Element + * @throws PfModelException from Policy framework + */ + @Override + public void deploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement) + throws PfModelException { + LOGGER.debug("deploy call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement); + simulatorService.deploy(instanceElement.instanceId(), instanceElement.elementId()); + } + + /** + * Handle a automation composition element state change. + * + * @param compositionElement the information of the Automation Composition Definition Element + * @param instanceElement the information of the Automation Composition Instance Element + * @throws PfModelException from Policy framework + */ + @Override + public void undeploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement) + throws PfModelException { + LOGGER.debug("undeploy call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement); + simulatorService.undeploy(instanceElement.instanceId(), instanceElement.elementId()); + } + + @Override + public void lock(CompositionElementDto compositionElement, InstanceElementDto instanceElement) + throws PfModelException { + LOGGER.debug("lock call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement); + simulatorService.lock(instanceElement.instanceId(), instanceElement.elementId()); + } + + @Override + public void unlock(CompositionElementDto compositionElement, InstanceElementDto instanceElement) + throws PfModelException { + LOGGER.debug("unlock call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement); + simulatorService.unlock(instanceElement.instanceId(), instanceElement.elementId()); + } + + @Override + public void delete(CompositionElementDto compositionElement, InstanceElementDto instanceElement) + throws PfModelException { + LOGGER.debug("delete call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement); + simulatorService.delete(instanceElement.instanceId(), instanceElement.elementId()); + } + + @Override + public void update(CompositionElementDto compositionElement, InstanceElementDto instanceElement, + InstanceElementDto instanceElementUpdated) throws PfModelException { + LOGGER.debug("update call compositionElement: {}, instanceElement: {}, instanceElementUpdated: {}", + compositionElement, instanceElement, instanceElementUpdated); + simulatorService.update(instanceElement.instanceId(), instanceElement.elementId()); + } + + @Override + public void prime(CompositionDto composition) throws PfModelException { + LOGGER.debug("prime call composition: {}", composition); + simulatorService.prime(composition.compositionId()); + } + + @Override + public void deprime(CompositionDto composition) throws PfModelException { + LOGGER.debug("deprime call composition: {}", composition); + simulatorService.deprime(composition.compositionId()); + } + + @Override + public void handleRestartComposition(CompositionDto composition, AcTypeState state) throws PfModelException { + LOGGER.debug("restart composition definition call"); + switch (state) { + case PRIMING: + prime(composition); + break; + + case DEPRIMING: + deprime(composition); + break; + + default: + intermediaryApi.updateCompositionState(composition.compositionId(), state, + StateChangeResult.NO_ERROR, "Restarted"); + } + } + + @Override + public void handleRestartInstance(CompositionElementDto compositionElement, InstanceElementDto instanceElement, + DeployState deployState, LockState lockState) throws PfModelException { + LOGGER.debug("restart instance call"); + if (!AcmUtils.isInTransitionalState(deployState, lockState)) { + intermediaryApi.updateAutomationCompositionElementState( + instanceElement.instanceId(), instanceElement.elementId(), deployState, lockState, + StateChangeResult.NO_ERROR, "Restarted"); + return; + } + if (DeployState.DEPLOYING.equals(deployState)) { + deploy(compositionElement, instanceElement); + return; + } + if (DeployState.UNDEPLOYING.equals(deployState)) { + undeploy(compositionElement, instanceElement); + return; + } + if (DeployState.UPDATING.equals(deployState)) { + update(compositionElement, instanceElement, instanceElement); + return; + } + if (DeployState.DELETING.equals(deployState)) { + delete(compositionElement, instanceElement); + return; + } + if (LockState.LOCKING.equals(lockState)) { + lock(compositionElement, instanceElement); + return; + } + if (LockState.UNLOCKING.equals(lockState)) { + unlock(compositionElement, instanceElement); + } + } + + @Override + public void migrate(CompositionElementDto compositionElement, CompositionElementDto compositionElementTarget, + InstanceElementDto instanceElement, InstanceElementDto instanceElementMigrate) + throws PfModelException { + LOGGER.debug("migrate call compositionElement: {}, compositionElementTarget: {}, instanceElement: {}," + + " instanceElementMigrate: {}", + compositionElement, compositionElementTarget, instanceElement, instanceElementMigrate); + simulatorService.migrate(instanceElement.instanceId(), instanceElement.elementId()); + } +} diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/SimulatorService.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/SimulatorService.java new file mode 100644 index 000000000..d37edf761 --- /dev/null +++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/main/handler/SimulatorService.java @@ -0,0 +1,345 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.sim.main.handler; + +import java.util.ArrayList; +import java.util.Map; +import java.util.UUID; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.acm.participant.sim.model.InternalData; +import org.onap.policy.clamp.acm.participant.sim.model.InternalDatas; +import org.onap.policy.clamp.acm.participant.sim.model.SimConfig; +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.AutomationCompositions; +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.StateChangeResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +/** + * This class handles implementation of Simulator Service. + */ +@Service +@RequiredArgsConstructor +public class SimulatorService { + + private final ParticipantIntermediaryApi intermediaryApi; + + private static final Logger LOGGER = LoggerFactory.getLogger(SimulatorService.class); + + @Getter + @Setter + private SimConfig config = new SimConfig(); + + /** + * Get AutomationComposition. + * + * @return the AutomationCompositions + */ + public AutomationCompositions getAutomationCompositions() { + var result = new AutomationCompositions(); + result.setAutomationCompositionList(new ArrayList<>(intermediaryApi.getAutomationCompositions().values())); + return result; + } + + public AutomationComposition getAutomationComposition(UUID instanceId) { + return intermediaryApi.getAutomationComposition(instanceId); + } + + /** + * Set OutProperties. + * + * @param instanceId the automationComposition Id + * @param elementId the automationComposition Element Id + * @param useState the useState + * @param operationalState the operationalState + * @param outProperties the outProperties + */ + public void setOutProperties(UUID instanceId, UUID elementId, String useState, String operationalState, + Map<String, Object> outProperties) { + intermediaryApi.sendAcElementInfo(instanceId, elementId, useState, operationalState, + outProperties); + } + + /** + * Get Instance Data List. + * + * @return the InternalDatas + */ + public InternalDatas getDataList() { + var result = new InternalDatas(); + var map = intermediaryApi.getAutomationCompositions(); + for (var instance : map.values()) { + for (var element : instance.getElements().values()) { + var data = new InternalData(); + data.setCompositionId(instance.getCompositionId()); + data.setAutomationCompositionId(instance.getInstanceId()); + data.setAutomationCompositionElementId(element.getId()); + data.setIntProperties(element.getProperties()); + data.setOperationalState(element.getOperationalState()); + data.setUseState(element.getUseState()); + data.setOutProperties(element.getOutProperties()); + result.getList().add(data); + } + } + return result; + } + + /** + * Get Composition Data List. + * + * @return the InternalDatas + */ + public InternalDatas getCompositionDataList() { + var acElementsDefinitions = intermediaryApi.getAcElementsDefinitions(); + var internalDatas = new InternalDatas(); + for (var entry : acElementsDefinitions.entrySet()) { + for (var acElementsDefinition : entry.getValue().values()) { + var internalData = new InternalData(); + internalData.setCompositionId(entry.getKey()); + internalData.setCompositionDefinitionElementId(acElementsDefinition.getAcElementDefinitionId()); + internalData.setIntProperties( + acElementsDefinition.getAutomationCompositionElementToscaNodeTemplate().getProperties()); + internalData.setOutProperties(acElementsDefinition.getOutProperties()); + internalDatas.getList().add(internalData); + } + } + return internalDatas; + } + + public void setCompositionOutProperties(UUID compositionId, ToscaConceptIdentifier compositionDefinitionElementId, + Map<String, Object> outProperties) { + intermediaryApi.sendAcDefinitionInfo(compositionId, compositionDefinitionElementId, outProperties); + + } + + private boolean execution(int timeMs, String msg, UUID elementId) { + long endTime = System.currentTimeMillis() + timeMs; + while (System.currentTimeMillis() < endTime) { + try { + if (Thread.currentThread().isInterrupted()) { + LOGGER.debug(msg, elementId); + return false; + } + Thread.sleep(10L); + } catch (InterruptedException e) { + LOGGER.debug(msg, elementId); + Thread.currentThread().interrupt(); + return false; + } + } + return true; + } + + /** + * Handle a deploy on a automation composition element. + * + * @param instanceId the instanceId + * @param elementId the elementId + */ + public void deploy(UUID instanceId, UUID elementId) { + if (!execution(getConfig().getDeployTimerMs(), + "Current Thread deploy is Interrupted during execution {}", elementId)) { + return; + } + + if (getConfig().isDeploySuccess()) { + intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed"); + } else { + intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, + DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!"); + } + } + + /** + * Handle an udeploy on a automation composition element. + * + * @param instanceId the instanceId + * @param elementId the elementId + */ + public void undeploy(UUID instanceId, UUID elementId) { + if (!execution(getConfig().getUndeployTimerMs(), + "Current Thread undeploy is Interrupted during execution {}", elementId)) { + return; + } + + if (getConfig().isUndeploySuccess()) { + intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, + DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed"); + } else { + intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, + DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Undeploy failed!"); + } + } + + /** + * Handle a lock on a automation composition element. + * + * @param instanceId the instanceId + * @param elementId the elementId + */ + public void lock(UUID instanceId, UUID elementId) { + if (!execution(getConfig().getLockTimerMs(), + "Current Thread lock is Interrupted during execution {}", elementId)) { + return; + } + + if (getConfig().isLockSuccess()) { + intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, + null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked"); + } else { + intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, + null, LockState.UNLOCKED, StateChangeResult.FAILED, "Lock failed!"); + } + } + + /** + * Handle an unlock on a automation composition element. + * + * @param instanceId the instanceId + * @param elementId the elementId + */ + public void unlock(UUID instanceId, UUID elementId) { + if (!execution(getConfig().getUnlockTimerMs(), + "Current Thread unlock is Interrupted during execution {}", elementId)) { + return; + } + + if (getConfig().isUnlockSuccess()) { + intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, + null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked"); + } else { + intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, + null, LockState.LOCKED, StateChangeResult.FAILED, "Unlock failed!"); + } + } + + /** + * Handle a delete on a automation composition element. + * + * @param instanceId the instanceId + * @param elementId the elementId + */ + public void delete(UUID instanceId, UUID elementId) { + if (!execution(getConfig().getDeleteTimerMs(), + "Current Thread delete is Interrupted during execution {}", elementId)) { + return; + } + + if (getConfig().isDeleteSuccess()) { + intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, + DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted"); + } else { + intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, + DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Delete failed!"); + } + } + + /** + * Handle an update on a automation composition element. + * + * @param instanceId the instanceId + * @param elementId the elementId + */ + public void update(UUID instanceId, UUID elementId) { + if (!execution(getConfig().getUpdateTimerMs(), + "Current Thread update is Interrupted during execution {}", elementId)) { + return; + } + + if (getConfig().isUpdateSuccess()) { + intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated"); + } else { + intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, + DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!"); + } + } + + /** + * Handle a prime on a automation composition definition. + * + * @param compositionId the compositionId + */ + public void prime(UUID compositionId) { + if (!execution(getConfig().getPrimeTimerMs(), + "Current Thread prime is Interrupted during execution {}", compositionId)) { + return; + } + + if (getConfig().isPrimeSuccess()) { + intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, + "Primed"); + } else { + intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.FAILED, + "Prime failed!"); + } + } + + /** + * Handle a deprime on a automation composition definition. + * + * @param compositionId the compositionId + */ + public void deprime(UUID compositionId) { + if (!execution(getConfig().getDeprimeTimerMs(), + "Current Thread deprime is Interrupted during execution {}", compositionId)) { + return; + } + + if (getConfig().isDeprimeSuccess()) { + intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR, + "Deprimed"); + } else { + intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED, + "Deprime failed!"); + } + } + + /** + * Handle a migrate on a automation composition element. + * + * @param instanceId the instanceId + * @param elementId the elementId + */ + public void migrate(UUID instanceId, UUID elementId) { + if (!execution(getConfig().getMigrateTimerMs(), + "Current Thread migrate is Interrupted during execution {}", elementId)) { + return; + } + + if (getConfig().isMigrateSuccess()) { + intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated"); + } else { + intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, + DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!"); + } + } +} diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/rest/SimulatorController.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/rest/SimulatorController.java index cf4ac8eb7..5863fedd2 100644 --- a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/rest/SimulatorController.java +++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/acm/participant/sim/rest/SimulatorController.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation. + * Copyright (C) 2023-2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +25,7 @@ import jakarta.ws.rs.core.MediaType; import java.util.UUID; import lombok.RequiredArgsConstructor; import org.onap.policy.clamp.acm.participant.sim.controller.genapi.SimulatorParticipantControllerApi; -import org.onap.policy.clamp.acm.participant.sim.main.handler.AutomationCompositionElementHandler; +import org.onap.policy.clamp.acm.participant.sim.main.handler.SimulatorService; import org.onap.policy.clamp.acm.participant.sim.model.InternalData; import org.onap.policy.clamp.acm.participant.sim.model.InternalDatas; import org.onap.policy.clamp.acm.participant.sim.model.SimConfig; @@ -42,33 +42,33 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping(value = "/v2", produces = {MediaType.APPLICATION_JSON}) public class SimulatorController implements SimulatorParticipantControllerApi { - private final AutomationCompositionElementHandler automationCompositionElementHandler; + private final SimulatorService simulatorService; @Override public ResponseEntity<SimConfig> getConfig(UUID xonapRequestId) { - return new ResponseEntity<>(automationCompositionElementHandler.getConfig(), HttpStatus.OK); + return new ResponseEntity<>(simulatorService.getConfig(), HttpStatus.OK); } @Override public ResponseEntity<Void> setConfig(UUID xonapRequestId, @Valid @RequestBody SimConfig body) { - automationCompositionElementHandler.setConfig(body); + simulatorService.setConfig(body); return new ResponseEntity<>(HttpStatus.OK); } @Override public ResponseEntity<AutomationCompositions> getAutomationCompositions(UUID xonapRequestId) { - return new ResponseEntity<>(automationCompositionElementHandler.getAutomationCompositions(), HttpStatus.OK); + return new ResponseEntity<>(simulatorService.getAutomationCompositions(), HttpStatus.OK); } @Override public ResponseEntity<AutomationComposition> getAutomationComposition(UUID instanceId, UUID xonapRequestId) { - return new ResponseEntity<>(automationCompositionElementHandler.getAutomationComposition(instanceId), + return new ResponseEntity<>(simulatorService.getAutomationComposition(instanceId), HttpStatus.OK); } @Override public ResponseEntity<InternalDatas> getDatas(UUID xonapRequestId) { - return new ResponseEntity<>(automationCompositionElementHandler.getDataList(), HttpStatus.OK); + return new ResponseEntity<>(simulatorService.getDataList(), HttpStatus.OK); } /** @@ -79,7 +79,7 @@ public class SimulatorController implements SimulatorParticipantControllerApi { */ @Override public ResponseEntity<Void> setData(UUID xonapRequestId, @Valid @RequestBody InternalData body) { - automationCompositionElementHandler.setOutProperties(body.getAutomationCompositionId(), + simulatorService.setOutProperties(body.getAutomationCompositionId(), body.getAutomationCompositionElementId(), body.getUseState(), body.getOperationalState(), body.getOutProperties()); return new ResponseEntity<>(HttpStatus.OK); @@ -87,12 +87,12 @@ public class SimulatorController implements SimulatorParticipantControllerApi { @Override public ResponseEntity<InternalDatas> getCompositionDatas(UUID xonapRequestId) { - return new ResponseEntity<>(automationCompositionElementHandler.getCompositionDataList(), HttpStatus.OK); + return new ResponseEntity<>(simulatorService.getCompositionDataList(), HttpStatus.OK); } @Override public ResponseEntity<Void> setCompositionData(UUID xonapRequestId, @Valid InternalData body) { - automationCompositionElementHandler.setCompositionOutProperties(body.getCompositionId(), + simulatorService.setCompositionOutProperties(body.getCompositionId(), body.getCompositionDefinitionElementId(), body.getOutProperties()); return new ResponseEntity<>(HttpStatus.OK); } diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV1Test.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV1Test.java new file mode 100644 index 000000000..1061b3ba1 --- /dev/null +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV1Test.java @@ -0,0 +1,285 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023-2024 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.sim.main.handler; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.acm.participant.sim.model.SimConfig; +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.DeployState; +import org.onap.policy.clamp.models.acm.concepts.LockState; +import org.onap.policy.clamp.models.acm.concepts.StateChangeResult; +import org.onap.policy.models.base.PfModelException; + +class AutomationCompositionElementHandlerV1Test { + + @Test + void testDeploy() throws PfModelException { + var config = new SimConfig(); + config.setDeployTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var element = new AcElementDeploy(); + element.setId(UUID.randomUUID()); + acElementHandler.deploy(instanceId, element, Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed"); + + config.setDeploySuccess(false); + acElementHandler.deploy(instanceId, element, Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!"); + } + + @Test + void testUndeploy() throws PfModelException { + var config = new SimConfig(); + config.setUndeployTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + acElementHandler.undeploy(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED, + null, StateChangeResult.NO_ERROR, "Undeployed"); + + config.setUndeploySuccess(false); + acElementHandler.undeploy(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED, + null, StateChangeResult.FAILED, "Undeploy failed!"); + } + + @Test + void testLock() throws PfModelException { + var config = new SimConfig(); + config.setLockTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + acElementHandler.lock(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED, + StateChangeResult.NO_ERROR, "Locked"); + + config.setLockSuccess(false); + acElementHandler.lock(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED, + StateChangeResult.FAILED, "Lock failed!"); + } + + @Test + void testUnlock() throws PfModelException { + var config = new SimConfig(); + config.setUnlockTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + acElementHandler.unlock(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED, + StateChangeResult.NO_ERROR, "Unlocked"); + + config.setUnlockSuccess(false); + acElementHandler.unlock(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED, + StateChangeResult.FAILED, "Unlock failed!"); + } + + @Test + void testUpdate() throws PfModelException { + var config = new SimConfig(); + config.setUpdateTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var element = new AcElementDeploy(); + element.setId(UUID.randomUUID()); + acElementHandler.update(instanceId, element, Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated"); + + config.setUpdateSuccess(false); + acElementHandler.update(instanceId, element, Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!"); + } + + @Test + void testDelete() throws PfModelException { + var config = new SimConfig(); + config.setDeleteTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + acElementHandler.delete(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, + null, StateChangeResult.NO_ERROR, "Deleted"); + + config.setDeleteSuccess(false); + acElementHandler.delete(instanceId, elementId); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED, + null, StateChangeResult.FAILED, "Delete failed!"); + } + + @Test + void testPrime() throws PfModelException { + var config = new SimConfig(); + config.setPrimeTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionId = UUID.randomUUID(); + acElementHandler.prime(compositionId, List.of()); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, + "Primed"); + + config.setPrimeSuccess(false); + acElementHandler.prime(compositionId, List.of()); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, + StateChangeResult.FAILED, "Prime failed!"); + } + + @Test + void testDeprime() throws PfModelException { + var config = new SimConfig(); + config.setDeprimeTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionId = UUID.randomUUID(); + acElementHandler.deprime(compositionId); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, + StateChangeResult.NO_ERROR, "Deprimed"); + + config.setDeprimeSuccess(false); + acElementHandler.deprime(compositionId); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED, + "Deprime failed!"); + } + + @Test + void testHandleRestartComposition() throws PfModelException { + var config = new SimConfig(); + config.setPrimeTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var compositionId = UUID.randomUUID(); + acElementHandler.handleRestartComposition(compositionId, List.of(), AcTypeState.PRIMING); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, + "Primed"); + + acElementHandler.handleRestartComposition(compositionId, List.of(), AcTypeState.PRIMED); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, + "Restarted"); + + acElementHandler.handleRestartComposition(compositionId, List.of(), AcTypeState.DEPRIMING); + verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED, + StateChangeResult.NO_ERROR, "Deprimed"); + } + + @Test + void testHandleRestartInstance() throws PfModelException { + var config = new SimConfig(); + config.setDeployTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var element = new AcElementDeploy(); + element.setId(UUID.randomUUID()); + acElementHandler.handleRestartInstance(instanceId, element, Map.of(), DeployState.DEPLOYING, LockState.NONE); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed"); + + acElementHandler.handleRestartInstance(instanceId, element, Map.of(), DeployState.DEPLOYED, LockState.LOCKED); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, LockState.LOCKED, StateChangeResult.NO_ERROR, "Restarted"); + + acElementHandler.handleRestartInstance(instanceId, element, Map.of(), DeployState.UPDATING, LockState.LOCKED); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated"); + + acElementHandler.handleRestartInstance(instanceId, element, Map.of(), DeployState.UNDEPLOYING, + LockState.LOCKED); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed"); + + acElementHandler.handleRestartInstance(instanceId, element, Map.of(), DeployState.DELETING, LockState.NONE); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted"); + + acElementHandler.handleRestartInstance(instanceId, element, Map.of(), DeployState.DEPLOYED, LockState.LOCKING); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), null, + LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked"); + + acElementHandler.handleRestartInstance(instanceId, element, Map.of(), DeployState.DEPLOYED, + LockState.UNLOCKING); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), null, + LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked"); + } + + @Test + void testMigrate() throws PfModelException { + var config = new SimConfig(); + config.setUpdateTimerMs(1); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService); + simulatorService.setConfig(config); + var instanceId = UUID.randomUUID(); + var element = new AcElementDeploy(); + element.setId(UUID.randomUUID()); + acElementHandler.migrate(instanceId, element, UUID.randomUUID(), Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated"); + + config.setMigrateSuccess(false); + acElementHandler.migrate(instanceId, element, UUID.randomUUID(), Map.of()); + verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), + DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!"); + } +} diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerTest.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV2Test.java index d63405369..c521d3755 100644 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerTest.java +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/AutomationCompositionElementHandlerV2Test.java @@ -20,11 +20,8 @@ package org.onap.policy.clamp.acm.participant.sim.main.handler; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.util.Map; import java.util.UUID; @@ -33,40 +30,38 @@ import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto; 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.api.ParticipantIntermediaryApi; -import org.onap.policy.clamp.acm.participant.sim.comm.CommonTestData; import org.onap.policy.clamp.acm.participant.sim.model.SimConfig; 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.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.StateChangeResult; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; -import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; -class AutomationCompositionElementHandlerTest { +class AutomationCompositionElementHandlerV2Test { @Test void testDeploy() throws PfModelException { var config = new SimConfig(); config.setDeployTimerMs(1); var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); + Map.of(), Map.of()); var instanceId = UUID.randomUUID(); var elementId = UUID.randomUUID(); var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); acElementHandler.deploy(compositionElement, instanceElement); verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED, - null, StateChangeResult.NO_ERROR, "Deployed"); + null, StateChangeResult.NO_ERROR, "Deployed"); config.setDeploySuccess(false); acElementHandler.deploy(compositionElement, instanceElement); verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.UNDEPLOYED, - null, StateChangeResult.FAILED, "Deploy failed!"); + null, StateChangeResult.FAILED, "Deploy failed!"); } @Test @@ -74,10 +69,11 @@ class AutomationCompositionElementHandlerTest { var config = new SimConfig(); config.setUndeployTimerMs(1); var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); + Map.of(), Map.of()); var instanceId = UUID.randomUUID(); var elementId = UUID.randomUUID(); var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); @@ -96,10 +92,11 @@ class AutomationCompositionElementHandlerTest { var config = new SimConfig(); config.setLockTimerMs(1); var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); + Map.of(), Map.of()); var instanceId = UUID.randomUUID(); var elementId = UUID.randomUUID(); var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); @@ -118,10 +115,11 @@ class AutomationCompositionElementHandlerTest { var config = new SimConfig(); config.setUnlockTimerMs(1); var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); + Map.of(), Map.of()); var instanceId = UUID.randomUUID(); var elementId = UUID.randomUUID(); var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); @@ -140,16 +138,17 @@ class AutomationCompositionElementHandlerTest { var config = new SimConfig(); config.setUpdateTimerMs(1); var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); + Map.of(), Map.of()); var instanceId = UUID.randomUUID(); var element = new AcElementDeploy(); element.setId(UUID.randomUUID()); var instanceElement = new InstanceElementDto(instanceId, element.getId(), null, Map.of(), Map.of()); var instanceElementUpdated = new InstanceElementDto(instanceId, element.getId(), null, - Map.of("key", "value"), Map.of()); + Map.of("key", "value"), Map.of()); acElementHandler.update(compositionElement, instanceElement, instanceElementUpdated); verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated"); @@ -165,10 +164,11 @@ class AutomationCompositionElementHandlerTest { var config = new SimConfig(); config.setDeleteTimerMs(1); var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); + Map.of(), Map.of()); var instanceId = UUID.randomUUID(); var elementId = UUID.randomUUID(); var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); @@ -183,64 +183,13 @@ class AutomationCompositionElementHandlerTest { } @Test - void testgetAutomationCompositions() throws PfModelException { - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - - var map = CommonTestData.getTestAutomationCompositionMap(); - when(intermediaryApi.getAutomationCompositions()).thenReturn(map); - var result = acElementHandler.getAutomationCompositions(); - assertEquals(map.values().iterator().next(), result.getAutomationCompositionList().get(0)); - } - - @Test - void testgetAutomationComposition() throws PfModelException { - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - - var instance = CommonTestData.getTestAutomationCompositionMap().values().iterator().next(); - when(intermediaryApi.getAutomationComposition(instance.getInstanceId())).thenReturn(instance); - var result = acElementHandler.getAutomationComposition(instance.getInstanceId()); - assertEquals(instance, result); - } - - @Test - void testsetOutProperties() { - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - - var instanceId = UUID.randomUUID(); - var elementId = UUID.randomUUID(); - var useState = "useState"; - var operationalState = "operationalState"; - Map<String, Object> map = Map.of("id", "1234"); - - acElementHandler.setOutProperties(instanceId, elementId, useState, operationalState, map); - verify(intermediaryApi).sendAcElementInfo(instanceId, elementId, useState, operationalState, map); - } - - @Test - void testgetDataList() { - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - - var map = CommonTestData.getTestAutomationCompositionMap(); - when(intermediaryApi.getAutomationCompositions()).thenReturn(map); - var result = acElementHandler.getDataList(); - var data = result.getList().get(0); - var automationcomposition = map.values().iterator().next(); - assertEquals(automationcomposition.getInstanceId(), data.getAutomationCompositionId()); - var element = automationcomposition.getElements().values().iterator().next(); - assertEquals(element.getId(), data.getAutomationCompositionElementId()); - } - - @Test void testPrime() throws PfModelException { var config = new SimConfig(); config.setPrimeTimerMs(1); var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); var compositionId = UUID.randomUUID(); var composition = new CompositionDto(compositionId, Map.of(), Map.of()); acElementHandler.prime(composition); @@ -258,8 +207,9 @@ class AutomationCompositionElementHandlerTest { var config = new SimConfig(); config.setDeprimeTimerMs(1); var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); var compositionId = UUID.randomUUID(); var composition = new CompositionDto(compositionId, Map.of(), Map.of()); acElementHandler.deprime(composition); @@ -277,8 +227,9 @@ class AutomationCompositionElementHandlerTest { var config = new SimConfig(); config.setPrimeTimerMs(1); var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); var compositionId = UUID.randomUUID(); var composition = new CompositionDto(compositionId, Map.of(), Map.of()); acElementHandler.handleRestartComposition(composition, AcTypeState.PRIMING); @@ -299,25 +250,26 @@ class AutomationCompositionElementHandlerTest { var config = new SimConfig(); config.setDeployTimerMs(1); var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); var instanceId = UUID.randomUUID(); var elementId = UUID.randomUUID(); var instanceElement = new InstanceElementDto(instanceId, elementId, null, Map.of(), Map.of()); var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); + Map.of(), Map.of()); acElementHandler.handleRestartInstance(compositionElement, instanceElement, - DeployState.DEPLOYING, LockState.NONE); + DeployState.DEPLOYING, LockState.NONE); verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed"); acElementHandler.handleRestartInstance(compositionElement, instanceElement, - DeployState.DEPLOYED, LockState.LOCKED); + DeployState.DEPLOYED, LockState.LOCKED); verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED, LockState.LOCKED, StateChangeResult.NO_ERROR, "Restarted"); acElementHandler.handleRestartInstance(compositionElement, instanceElement, - DeployState.UPDATING, LockState.LOCKED); + DeployState.UPDATING, LockState.LOCKED); verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated"); @@ -327,12 +279,12 @@ class AutomationCompositionElementHandlerTest { DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed"); acElementHandler.handleRestartInstance(compositionElement, instanceElement, - DeployState.DELETING, LockState.NONE); + DeployState.DELETING, LockState.NONE); verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted"); acElementHandler.handleRestartInstance(compositionElement, instanceElement, - DeployState.DEPLOYED, LockState.LOCKING); + DeployState.DEPLOYED, LockState.LOCKING); verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked"); @@ -342,66 +294,34 @@ class AutomationCompositionElementHandlerTest { LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked"); } - @Test - void testGetCompositionDataList() { - var acElementDefinition = new AutomationCompositionElementDefinition(); - var toscaConceptIdentifier = new ToscaConceptIdentifier("code", "1.0.0"); - acElementDefinition.setAcElementDefinitionId(toscaConceptIdentifier); - acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(new ToscaNodeTemplate()); - Map<String, Object> outProperties = Map.of("code", "value"); - Map<String, Object> inProperties = Map.of("key", "value"); - acElementDefinition.getAutomationCompositionElementToscaNodeTemplate().setProperties(inProperties); - acElementDefinition.setOutProperties(outProperties); - var elementsDefinitions = Map.of(toscaConceptIdentifier, acElementDefinition); - var compositionId = UUID.randomUUID(); - var map = Map.of(compositionId, elementsDefinitions); - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - when(intermediaryApi.getAcElementsDefinitions()).thenReturn(map); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - - var result = acElementHandler.getCompositionDataList(); - assertThat(result.getList()).hasSize(1); - assertEquals(result.getList().get(0).getCompositionId(), compositionId); - assertEquals(result.getList().get(0).getCompositionDefinitionElementId(), toscaConceptIdentifier); - assertEquals(result.getList().get(0).getOutProperties(), outProperties); - assertEquals(result.getList().get(0).getIntProperties(), inProperties); - } - @Test - void testSetCompositionData() { - var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - - var compositionId = UUID.randomUUID(); - acElementHandler.setCompositionOutProperties(compositionId, null, Map.of()); - verify(intermediaryApi).sendAcDefinitionInfo(compositionId, null, Map.of()); - } @Test void testMigrate() throws PfModelException { var config = new SimConfig(); config.setUpdateTimerMs(1); var intermediaryApi = mock(ParticipantIntermediaryApi.class); - var acElementHandler = new AutomationCompositionElementHandler(intermediaryApi); - acElementHandler.setConfig(config); + var simulatorService = new SimulatorService(intermediaryApi); + var acElementHandler = new AutomationCompositionElementHandlerV2(intermediaryApi, simulatorService); + simulatorService.setConfig(config); var compositionElement = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); + Map.of(), Map.of()); var compositionElementTraget = new CompositionElementDto(UUID.randomUUID(), new ToscaConceptIdentifier(), - Map.of(), Map.of()); + Map.of(), Map.of()); var instanceId = UUID.randomUUID(); var element = new AcElementDeploy(); element.setId(UUID.randomUUID()); var instanceElement = new InstanceElementDto(instanceId, element.getId(), null, Map.of(), Map.of()); var instanceElementMigrated = new InstanceElementDto(instanceId, element.getId(), - null, Map.of("key", "value"), Map.of()); + null, Map.of("key", "value"), Map.of()); acElementHandler - .migrate(compositionElement, compositionElementTraget, instanceElement, instanceElementMigrated); + .migrate(compositionElement, compositionElementTraget, instanceElement, instanceElementMigrated); verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated"); config.setMigrateSuccess(false); acElementHandler - .migrate(compositionElement, compositionElementTraget, instanceElement, instanceElementMigrated); + .migrate(compositionElement, compositionElementTraget, instanceElement, instanceElementMigrated); verify(intermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(), DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!"); } diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/SimulatorServiceTest.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/SimulatorServiceTest.java new file mode 100644 index 000000000..714f71781 --- /dev/null +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/main/handler/SimulatorServiceTest.java @@ -0,0 +1,126 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.sim.main.handler; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Map; +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.acm.participant.sim.comm.CommonTestData; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; + +class SimulatorServiceTest { + + @Test + void testGetAutomationCompositions() { + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + + var map = CommonTestData.getTestAutomationCompositionMap(); + when(intermediaryApi.getAutomationCompositions()).thenReturn(map); + var result = simulatorService.getAutomationCompositions(); + assertEquals(map.values().iterator().next(), result.getAutomationCompositionList().get(0)); + } + + @Test + void testGetAutomationComposition() { + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + + var instance = CommonTestData.getTestAutomationCompositionMap().values().iterator().next(); + when(intermediaryApi.getAutomationComposition(instance.getInstanceId())).thenReturn(instance); + var result = simulatorService.getAutomationComposition(instance.getInstanceId()); + assertEquals(instance, result); + } + + @Test + void testSetOutProperties() { + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + + var instanceId = UUID.randomUUID(); + var elementId = UUID.randomUUID(); + var useState = "useState"; + var operationalState = "operationalState"; + Map<String, Object> map = Map.of("id", "1234"); + + simulatorService.setOutProperties(instanceId, elementId, useState, operationalState, map); + verify(intermediaryApi).sendAcElementInfo(instanceId, elementId, useState, operationalState, map); + } + + @Test + void testGetDataList() { + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + + var map = CommonTestData.getTestAutomationCompositionMap(); + when(intermediaryApi.getAutomationCompositions()).thenReturn(map); + var result = simulatorService.getDataList(); + var data = result.getList().get(0); + var automationcomposition = map.values().iterator().next(); + assertEquals(automationcomposition.getInstanceId(), data.getAutomationCompositionId()); + var element = automationcomposition.getElements().values().iterator().next(); + assertEquals(element.getId(), data.getAutomationCompositionElementId()); + } + + @Test + void testGetCompositionDataList() { + var acElementDefinition = new AutomationCompositionElementDefinition(); + var toscaConceptIdentifier = new ToscaConceptIdentifier("code", "1.0.0"); + acElementDefinition.setAcElementDefinitionId(toscaConceptIdentifier); + acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(new ToscaNodeTemplate()); + Map<String, Object> outProperties = Map.of("code", "value"); + Map<String, Object> inProperties = Map.of("key", "value"); + acElementDefinition.getAutomationCompositionElementToscaNodeTemplate().setProperties(inProperties); + acElementDefinition.setOutProperties(outProperties); + var elementsDefinitions = Map.of(toscaConceptIdentifier, acElementDefinition); + var compositionId = UUID.randomUUID(); + var map = Map.of(compositionId, elementsDefinitions); + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + when(intermediaryApi.getAcElementsDefinitions()).thenReturn(map); + var simulatorService = new SimulatorService(intermediaryApi); + + var result = simulatorService.getCompositionDataList(); + assertThat(result.getList()).hasSize(1); + assertEquals(result.getList().get(0).getCompositionId(), compositionId); + assertEquals(result.getList().get(0).getCompositionDefinitionElementId(), toscaConceptIdentifier); + assertEquals(result.getList().get(0).getOutProperties(), outProperties); + assertEquals(result.getList().get(0).getIntProperties(), inProperties); + } + + @Test + void testSetCompositionData() { + var intermediaryApi = mock(ParticipantIntermediaryApi.class); + var simulatorService = new SimulatorService(intermediaryApi); + + var compositionId = UUID.randomUUID(); + simulatorService.setCompositionOutProperties(compositionId, null, Map.of()); + verify(intermediaryApi).sendAcDefinitionInfo(compositionId, null, Map.of()); + } +} diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/AcSimRestTest.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/AcSimRestTest.java index 3592a1998..f62d6cd2d 100644 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/AcSimRestTest.java +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/acm/participant/sim/rest/AcSimRestTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation. + * Copyright (C) 2023-2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,11 +32,12 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.onap.policy.clamp.acm.participant.sim.comm.CommonTestData; -import org.onap.policy.clamp.acm.participant.sim.main.handler.AutomationCompositionElementHandler; +import org.onap.policy.clamp.acm.participant.sim.main.handler.SimulatorService; import org.onap.policy.clamp.acm.participant.sim.model.InternalData; import org.onap.policy.clamp.acm.participant.sim.model.InternalDatas; import org.onap.policy.clamp.acm.participant.sim.model.SimConfig; import org.onap.policy.clamp.acm.participant.sim.parameters.ParticipantSimParameters; +import org.onap.policy.clamp.models.acm.concepts.AutomationComposition; import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.StandardCoder; @@ -70,7 +71,7 @@ class AcSimRestTest { private MockMvc mockMvc; @MockBean - private AutomationCompositionElementHandler automationCompositionElementHandler; + private SimulatorService simulatorService; @Autowired private WebApplicationContext context; @@ -81,10 +82,10 @@ class AcSimRestTest { } @Test - void testgetConfig() throws Exception { + void testGetConfig() throws Exception { var requestBuilder = MockMvcRequestBuilders.get(CONFIG_URL).accept(MediaType.APPLICATION_JSON_VALUE); - doReturn(new SimConfig()).when(automationCompositionElementHandler).getConfig(); + doReturn(new SimConfig()).when(simulatorService).getConfig(); mockMvc.perform(requestBuilder).andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) @@ -92,7 +93,7 @@ class AcSimRestTest { } @Test - void testsetConfig() throws Exception { + void testSetConfig() throws Exception { var requestBuilder = MockMvcRequestBuilders.put(CONFIG_URL).accept(MediaType.APPLICATION_JSON_VALUE) .content(CODER.encode(new SimConfig())).contentType(MediaType.APPLICATION_JSON_VALUE); @@ -100,13 +101,13 @@ class AcSimRestTest { } @Test - void testgetAutomationCompositions() throws Exception { + void testGetAutomationCompositions() throws Exception { var requestBuilder = MockMvcRequestBuilders.get(INSTANCE_URL).accept(MediaType.APPLICATION_JSON_VALUE); var automationCompositions = new AutomationCompositions(); automationCompositions.getAutomationCompositionList().add(CommonTestData.getTestAutomationComposition()); - doReturn(automationCompositions).when(automationCompositionElementHandler).getAutomationCompositions(); + doReturn(automationCompositions).when(simulatorService).getAutomationCompositions(); var result = mockMvc.perform(requestBuilder).andExpect(status().isOk()) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn(); @@ -117,13 +118,31 @@ class AcSimRestTest { } @Test - void testgetDatas() throws Exception { + void testGetAutomationComposition() throws Exception { + var automationComposition = CommonTestData.getTestAutomationComposition(); + + var requestBuilder = MockMvcRequestBuilders + .get(INSTANCE_URL + "/" + automationComposition.getInstanceId()) + .accept(MediaType.APPLICATION_JSON_VALUE); + + doReturn(automationComposition).when(simulatorService) + .getAutomationComposition(automationComposition.getInstanceId()); + + var result = mockMvc.perform(requestBuilder).andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andReturn(); + var body = result.getResponse().getContentAsString(); + var acsResult = CODER.decode(body, AutomationComposition.class); + assertEquals(automationComposition, acsResult); + } + + @Test + void testGetDatas() throws Exception { var internalDatas = new InternalDatas(); var internalData = new InternalData(); internalData.setAutomationCompositionId(UUID.randomUUID()); internalDatas.getList().add(internalData); - doReturn(internalDatas).when(automationCompositionElementHandler).getDataList(); + doReturn(internalDatas).when(simulatorService).getDataList(); var requestBuilder = MockMvcRequestBuilders.get(DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE); var result = mockMvc.perform(requestBuilder).andExpect(status().isOk()) @@ -135,7 +154,7 @@ class AcSimRestTest { } @Test - void testsetDatas() throws Exception { + void testSetDatas() throws Exception { var requestBuilder = MockMvcRequestBuilders.put(DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE) .content(CODER.encode(new InternalData())).contentType(MediaType.APPLICATION_JSON_VALUE); @@ -143,13 +162,13 @@ class AcSimRestTest { } @Test - void testgetCompositionDatas() throws Exception { + void testGetCompositionDatas() throws Exception { var internalDatas = new InternalDatas(); var internalData = new InternalData(); internalData.setCompositionId(UUID.randomUUID()); internalDatas.getList().add(internalData); - doReturn(internalDatas).when(automationCompositionElementHandler).getCompositionDataList(); + doReturn(internalDatas).when(simulatorService).getCompositionDataList(); var requestBuilder = MockMvcRequestBuilders.get(COMPOSITION_DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE); var result = mockMvc.perform(requestBuilder).andExpect(status().isOk()) @@ -160,7 +179,7 @@ class AcSimRestTest { } @Test - void testsetCompositionDatas() throws Exception { + void testSetCompositionDatas() throws Exception { var requestBuilder = MockMvcRequestBuilders.put(COMPOSITION_DATAS_URL).accept(MediaType.APPLICATION_JSON_VALUE) .content(CODER.encode(new InternalData())).contentType(MediaType.APPLICATION_JSON_VALUE); |