/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2021-2024 Nordix Foundation.
+ * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import jakarta.validation.Validation;
import jakarta.ws.rs.core.Response.Status;
import java.lang.invoke.MethodHandles;
+import java.util.HashMap;
import java.util.Map;
-import java.util.UUID;
import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest;
import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient;
+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.AcElementListenerV1;
+import org.onap.policy.clamp.acm.participant.intermediary.api.impl.AcElementListenerV3;
import org.onap.policy.clamp.common.acm.exception.AutomationCompositionException;
-import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
import org.onap.policy.clamp.models.acm.concepts.DeployState;
import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
import org.onap.policy.common.utils.coder.Coder;
* This class handles implementation of automationCompositionElement updates.
*/
@Component
-public class AutomationCompositionElementHandler extends AcElementListenerV1 {
+public class AutomationCompositionElementHandler extends AcElementListenerV3 {
private static final Coder CODER = new StandardCoder();
this.acHttpClient = acHttpClient;
}
- /**
- * Handle a automation composition element state change.
- *
- * @param automationCompositionElementId the ID of the automation composition element
- */
@Override
- public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) {
- intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, automationCompositionElementId,
- DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "");
- }
-
- /**
- * Callback method to handle an update on a automation composition element.
- *
- * @param automationCompositionId the automationComposition Id
- * @param element the information on the automation composition element
- * @param properties properties Map
- */
- @Override
- public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties) {
+ public void deploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement) {
try {
- var configRequest = getConfigRequest(properties);
+ var map = new HashMap<>(compositionElement.inProperties());
+ map.putAll(instanceElement.inProperties());
+ var configRequest = getConfigRequest(map);
var restResponseMap = acHttpClient.run(configRequest);
var failedResponseStatus = restResponseMap.values().stream()
.filter(response -> !HttpStatus.valueOf(response.getKey()).is2xxSuccessful())
.toList();
if (failedResponseStatus.isEmpty()) {
- intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
- DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
+ intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
+ instanceElement.elementId(), DeployState.DEPLOYED, null,
+ StateChangeResult.NO_ERROR, "Deployed");
} else {
- intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
- DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
- "Error on Invoking the http request: " + failedResponseStatus);
+ intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
+ instanceElement.elementId(), DeployState.UNDEPLOYED, null,
+ StateChangeResult.FAILED, "Error on Invoking the http request: " + failedResponseStatus);
}
} catch (AutomationCompositionException e) {
- intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
- DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, e.getMessage());
+ intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
+ instanceElement.elementId(), DeployState.UNDEPLOYED, null,
+ StateChangeResult.FAILED, e.getMessage());
}
}
throw new AutomationCompositionException(Status.BAD_REQUEST, "Error extracting ConfigRequest ", e);
}
}
+
+ @Override
+ public void undeploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement) {
+ intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
+ instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "");
+ }
}
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2021-2024 Nordix Foundation.
+ * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import static org.mockito.Mockito.when;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.lang3.tuple.Pair;
import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient;
import org.onap.policy.clamp.acm.participant.http.utils.CommonTestData;
import org.onap.policy.clamp.acm.participant.http.utils.ToscaUtils;
+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.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;
private static final String HTTP_AUTOMATION_COMPOSITION_ELEMENT =
"org.onap.domain.database.Http_PMSHMicroserviceAutomationCompositionElement";
+ private InstanceElementDto creteInstanceElementDto(AcElementDeploy element) {
+ return new InstanceElementDto(commonTestData.getAutomationCompositionId(), element.getId(),
+ element.getProperties(), new HashMap<>());
+ }
+
+ private CompositionElementDto createCompositionElementDto(AcElementDeploy element) {
+ return new CompositionElementDto(UUID.randomUUID(), element.getDefinition(), new HashMap<>(), new HashMap<>());
+ }
+
@Test
void testUndeploy() {
- var instanceId = commonTestData.getAutomationCompositionId();
var element = commonTestData.getAutomationCompositionElement();
- var acElementId = element.getId();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
-
+ var compositionElement = createCompositionElementDto(element);
+ var instanceElement = creteInstanceElementDto(element);
var automationCompositionElementHandler =
new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- automationCompositionElementHandler.undeploy(instanceId, acElementId);
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId,
- DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "");
+ automationCompositionElementHandler.undeploy(compositionElement, instanceElement);
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ element.getId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "");
}
@Test
void testDeployConstraintViolations() {
- var instanceId = commonTestData.getAutomationCompositionId();
- var element = commonTestData.getAutomationCompositionElement();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
-
var automationCompositionElementHandler =
new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- Map<String, Object> map = new HashMap<>();
- automationCompositionElementHandler.deploy(instanceId, element, map);
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
- DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Constraint violations in the config request");
+ var element = commonTestData.getAutomationCompositionElement();
+ var compositionElement = createCompositionElementDto(element);
+ var instanceElement = creteInstanceElementDto(element);
+ automationCompositionElementHandler.deploy(compositionElement, instanceElement);
+
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ element.getId(), DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
+ "Constraint violations in the config request");
}
@Test
void testDeployError() {
- var instanceId = commonTestData.getAutomationCompositionId();
var element = commonTestData.getAutomationCompositionElement();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
Map<String, Object> map = new HashMap<>();
map.put("httpHeaders", 1);
- automationCompositionElementHandler.deploy(instanceId, element, map);
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
- DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Error extracting ConfigRequest ");
+ var compositionElement = new CompositionElementDto(UUID.randomUUID(), element.getDefinition(),
+ map, new HashMap<>());
+ var instanceElement = creteInstanceElementDto(element);
+ automationCompositionElementHandler.deploy(compositionElement, instanceElement);
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ element.getId(), DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
+ "Error extracting ConfigRequest ");
}
- @Test
- void testDeployFailed() {
+ private CompositionElementDto createCompositionElementDtoFromSt(AcElementDeploy element) {
var serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
var map = new HashMap<>(nodeTemplatesMap.get(HTTP_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
+ return new CompositionElementDto(UUID.randomUUID(), element.getDefinition(), map, new HashMap<>());
+ }
+
+ @Test
+ void testDeployFailed() {
var element = commonTestData.getAutomationCompositionElement();
- map.putAll(element.getProperties());
- var instanceId = commonTestData.getAutomationCompositionId();
var acHttpClient = mock(AcHttpClient.class);
when(acHttpClient.run(any())).thenReturn(Map.of(new ToscaConceptIdentifier(),
Pair.of(HttpStatus.BAD_REQUEST.value(), "")));
var automationCompositionElementHandler =
new AutomationCompositionElementHandler(participantIntermediaryApi, acHttpClient);
- automationCompositionElementHandler.deploy(instanceId, element, map);
+ var compositionElement = createCompositionElementDtoFromSt(element);
+ var instanceElement = creteInstanceElementDto(element);
+ automationCompositionElementHandler.deploy(compositionElement, instanceElement);
verify(acHttpClient).run(any(ConfigRequest.class));
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
- DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ element.getId(), DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
"Error on Invoking the http request: [(400,)]");
}
@Test
void testDeploy() {
- var serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
- var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
- var map = new HashMap<>(nodeTemplatesMap.get(HTTP_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
var element = commonTestData.getAutomationCompositionElement();
- map.putAll(element.getProperties());
- var instanceId = commonTestData.getAutomationCompositionId();
var acHttpClient = mock(AcHttpClient.class);
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
-
var automationCompositionElementHandler =
new AutomationCompositionElementHandler(participantIntermediaryApi, acHttpClient);
- automationCompositionElementHandler.deploy(instanceId, element, map);
+ var compositionElement = createCompositionElementDtoFromSt(element);
+ var instanceElement = creteInstanceElementDto(element);
+ automationCompositionElementHandler.deploy(compositionElement, instanceElement);
verify(acHttpClient).run(any(ConfigRequest.class));
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
- DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ element.getId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
}
@Test
void testUpdate() throws PfModelException {
- var instanceId = commonTestData.getAutomationCompositionId();
var element = commonTestData.getAutomationCompositionElement();
- var acElementId = element.getId();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
var automationCompositionElementHandler =
new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- automationCompositionElementHandler.update(instanceId, element, Map.of());
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId,
- DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Update not supported");
+ var compositionElement = createCompositionElementDto(element);
+ var instanceElement = creteInstanceElementDto(element);
+
+ automationCompositionElementHandler.update(compositionElement, instanceElement, instanceElement);
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ element.getId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR,
+ "Update not supported");
}
@Test
void testLock() throws PfModelException {
- var instanceId = commonTestData.getAutomationCompositionId();
- var acElementId = UUID.randomUUID();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
var automationCompositionElementHandler =
new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- automationCompositionElementHandler.lock(instanceId, acElementId);
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, null,
- LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
+ var element = commonTestData.getAutomationCompositionElement();
+ var compositionElement = createCompositionElementDto(element);
+ var instanceElement = creteInstanceElementDto(element);
+ automationCompositionElementHandler.lock(compositionElement, instanceElement);
+
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ element.getId(), null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
}
@Test
void testUnlock() throws PfModelException {
- var instanceId = commonTestData.getAutomationCompositionId();
- var acElementId = UUID.randomUUID();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
var automationCompositionElementHandler =
new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- automationCompositionElementHandler.unlock(instanceId, acElementId);
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, null,
- LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
+ var element = commonTestData.getAutomationCompositionElement();
+ var compositionElement = createCompositionElementDto(element);
+ var instanceElement = creteInstanceElementDto(element);
+ automationCompositionElementHandler.unlock(compositionElement, instanceElement);
+
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ element.getId(), null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
}
@Test
void testDelete() throws PfModelException {
- var instanceId = commonTestData.getAutomationCompositionId();
- var acElementId = UUID.randomUUID();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
var automationCompositionElementHandler =
new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- automationCompositionElementHandler.delete(instanceId, acElementId);
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId,
- DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
+ var element = commonTestData.getAutomationCompositionElement();
+ var compositionElement = createCompositionElementDto(element);
+ var instanceElement = creteInstanceElementDto(element);
+ automationCompositionElementHandler.delete(compositionElement, instanceElement);
+
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ element.getId(), DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
}
@Test
void testPrime() throws PfModelException {
- var compositionId = UUID.randomUUID();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
-
var automationCompositionElementHandler =
new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- automationCompositionElementHandler.prime(compositionId, List.of());
+ var compositionId = UUID.randomUUID();
+ var composition = new CompositionDto(compositionId, Map.of(), Map.of());
+ automationCompositionElementHandler.prime(composition);
verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED,
StateChangeResult.NO_ERROR, "Primed");
}
@Test
void testDeprime() throws PfModelException {
- var compositionId = UUID.randomUUID();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
-
var automationCompositionElementHandler =
new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- automationCompositionElementHandler.deprime(compositionId);
+ var compositionId = UUID.randomUUID();
+ var composition = new CompositionDto(compositionId, Map.of(), Map.of());
+ automationCompositionElementHandler.deprime(composition);
verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
StateChangeResult.NO_ERROR, "Deprimed");
}
@Test
void testMigrate() throws PfModelException {
- var instanceId = commonTestData.getAutomationCompositionId();
- var element = commonTestData.getAutomationCompositionElement();
- var acElementId = element.getId();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
var automationCompositionElementHandler =
new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- automationCompositionElementHandler.migrate(instanceId, element, UUID.randomUUID(), Map.of());
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId,
- DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
+ var element = commonTestData.getAutomationCompositionElement();
+ var compositionElement = createCompositionElementDto(element);
+ var instanceElement = creteInstanceElementDto(element);
+ automationCompositionElementHandler.migrate(compositionElement, compositionElement,
+ instanceElement, instanceElement, 0);
+
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ element.getId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
}
}