aboutsummaryrefslogtreecommitdiffstats
path: root/participant/participant-impl/participant-impl-policy
diff options
context:
space:
mode:
authorFrancescoFioraEst <francesco.fiora@est.tech>2024-06-20 13:51:35 +0100
committerFrancesco Fiora <francesco.fiora@est.tech>2024-06-20 15:38:56 +0000
commit8b38d4bbc171247a676b88c57a16ab7a762e07f7 (patch)
tree31cbfd50092fe7540792211c5192880d1a9e2a3d /participant/participant-impl/participant-impl-policy
parent0460b264c3b02345cdbb46f05125289897d56304 (diff)
Remove local HashMap in policy participant
Issue-ID: POLICY-5055 Change-Id: Id2495008a28ea3e50a5c17695af529813b1c0c35 Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Diffstat (limited to 'participant/participant-impl/participant-impl-policy')
-rw-r--r--participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/acm/participant/policy/main/handler/AutomationCompositionElementHandler.java61
-rw-r--r--participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/handler/AutomationCompositionElementHandlerTest.java164
2 files changed, 78 insertions, 147 deletions
diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/acm/participant/policy/main/handler/AutomationCompositionElementHandler.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/acm/participant/policy/main/handler/AutomationCompositionElementHandler.java
index ae906e1ef..34c195501 100644
--- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/acm/participant/policy/main/handler/AutomationCompositionElementHandler.java
+++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/acm/participant/policy/main/handler/AutomationCompositionElementHandler.java
@@ -27,13 +27,13 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
-import java.util.concurrent.ConcurrentHashMap;
import org.apache.http.HttpStatus;
+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.AcElementListenerV2;
import org.onap.policy.clamp.acm.participant.policy.client.PolicyApiHttpClient;
import org.onap.policy.clamp.acm.participant.policy.client.PolicyPapHttpClient;
-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.models.base.PfModelException;
@@ -48,12 +48,10 @@ import org.springframework.stereotype.Component;
* This class handles implementation of automationCompositionElement updates.
*/
@Component
-public class AutomationCompositionElementHandler extends AcElementListenerV1 {
+public class AutomationCompositionElementHandler extends AcElementListenerV2 {
private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionElementHandler.class);
- private final Map<UUID, ToscaServiceTemplate> serviceTemplateMap = new ConcurrentHashMap<>();
-
private final PolicyApiHttpClient apiHttpClient;
private final PolicyPapHttpClient papHttpClient;
@@ -74,26 +72,28 @@ public class AutomationCompositionElementHandler extends AcElementListenerV1 {
/**
* Callback method to handle a automation composition element state change.
*
- * @param automationCompositionId the ID of the automation composition
- * @param automationCompositionElementId the ID of the 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 in case of a model exception
*/
@Override
- public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) throws PfModelException {
- var automationCompositionDefinition = serviceTemplateMap.get(automationCompositionElementId);
- if (automationCompositionDefinition == null) {
- LOGGER.debug("No policies to undeploy to {}", automationCompositionElementId);
- intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
- automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
+ public void undeploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
+ throws PfModelException {
+ var automationCompositionDefinition = instanceElement.toscaServiceTemplateFragment();
+ if (automationCompositionDefinition.getToscaTopologyTemplate() == null) {
+ LOGGER.debug("No policies to undeploy to {}", instanceElement.elementId());
+ intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
+ instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
"Undeployed");
return;
}
var policyList = getPolicyList(automationCompositionDefinition);
- undeployPolicies(policyList, automationCompositionElementId);
+ undeployPolicies(policyList, instanceElement.elementId());
var policyTypeList = getPolicyTypeList(automationCompositionDefinition);
deletePolicyData(policyTypeList, policyList);
- serviceTemplateMap.remove(automationCompositionElementId);
- intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, automationCompositionElementId,
- DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
+ intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
+ instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
+ "Undeployed");
}
private void deletePolicyData(List<ToscaConceptIdentifier> policyTypeList,
@@ -151,24 +151,23 @@ public class AutomationCompositionElementHandler extends AcElementListenerV1 {
/**
* Callback method to handle an update on automation composition element.
*
- * @param automationCompositionId the automationComposition Id
- * @param element the information on the automation composition element
- * @param properties properties Map
- * @throws PfModelException in case of an exception
+ * @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(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
+ public void deploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
throws PfModelException {
var createPolicyTypeResp = HttpStatus.SC_OK;
var createPolicyResp = HttpStatus.SC_OK;
- var automationCompositionDefinition = element.getToscaServiceTemplateFragment();
+ var automationCompositionDefinition = instanceElement.toscaServiceTemplateFragment();
if (automationCompositionDefinition.getToscaTopologyTemplate() == null) {
- intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
- DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "ToscaTopologyTemplate not defined");
+ intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
+ instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
+ "ToscaTopologyTemplate not defined");
return;
}
- serviceTemplateMap.put(element.getId(), automationCompositionDefinition);
if (automationCompositionDefinition.getPolicyTypes() != null) {
LOGGER.info("Found Policy Types in automation composition definition: {} , Creating Policy Types",
automationCompositionDefinition.getName());
@@ -186,12 +185,12 @@ public class AutomationCompositionElementHandler extends AcElementListenerV1 {
if (createPolicyTypeResp == HttpStatus.SC_OK && createPolicyResp == HttpStatus.SC_OK) {
LOGGER.info(
"PolicyTypes/Policies for the automation composition element : {} are created " + "successfully",
- element.getId());
+ instanceElement.elementId());
var policyList = getPolicyList(automationCompositionDefinition);
- deployPolicies(policyList, automationCompositionId, element.getId());
+ deployPolicies(policyList, instanceElement.instanceId(), instanceElement.elementId());
} else {
- intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
- DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
+ intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
+ instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
"Creation of PolicyTypes/Policies failed. Policies will not be deployed.");
}
}
diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/handler/AutomationCompositionElementHandlerTest.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/handler/AutomationCompositionElementHandlerTest.java
index 013cb3432..39f35e6df 100644
--- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/handler/AutomationCompositionElementHandlerTest.java
+++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/handler/AutomationCompositionElementHandlerTest.java
@@ -31,15 +31,13 @@ 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.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.policy.client.PolicyApiHttpClient;
import org.onap.policy.clamp.acm.participant.policy.client.PolicyPapHttpClient;
-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.clamp.models.acm.messages.rest.instantiation.DeployOrder;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
@@ -49,11 +47,8 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
class AutomationCompositionElementHandlerTest {
- private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
- private static final String ID_VERSION = "1.0.1";
- private static final UUID automationCompositionElementId = UUID.randomUUID();
- public static final UUID AC_ID = UUID.randomUUID();
- private static final ToscaConceptIdentifier DEFINITION = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
+ private static final ToscaConceptIdentifier DEFINITION =
+ new ToscaConceptIdentifier("1.0.1", "org.onap.PM_CDS_Blueprint");
@Test
void testHandlerUndeployNoPolicy() throws PfModelException {
@@ -61,22 +56,25 @@ class AutomationCompositionElementHandlerTest {
var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
mock(PolicyPapHttpClient.class), intermediaryApi);
- handler.undeploy(AC_ID, automationCompositionElementId);
- verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
- DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
+ var compositionElement = getCompositionElement();
+ var instanceElement = getInstanceElementWithNullTopology();
+
+ handler.undeploy(compositionElement, instanceElement);
+ verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
+ "Undeployed");
+ }
+
+ private CompositionElementDto getCompositionElement() {
+ return new CompositionElementDto(UUID.randomUUID(), DEFINITION, Map.of(), Map.of());
}
- private AcElementDeploy getTestingAcElement() {
- var element = new AcElementDeploy();
- element.setDefinition(DEFINITION);
- element.setId(automationCompositionElementId);
- element.setOrderedState(DeployOrder.DEPLOY);
+ private InstanceElementDto getInstanceElement() {
var template = new ToscaServiceTemplate();
template.setToscaTopologyTemplate(new ToscaTopologyTemplate());
template.getToscaTopologyTemplate().setPolicies(List.of(Map.of("DummyPolicy", new ToscaPolicy())));
template.setPolicyTypes(Map.of("dummy policy type", new ToscaPolicyType()));
- element.setToscaServiceTemplateFragment(template);
- return element;
+ return new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), template, Map.of(), Map.of());
}
@Test
@@ -92,13 +90,18 @@ class AutomationCompositionElementHandlerTest {
var intermediaryApi = mock(ParticipantIntermediaryApi.class);
var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
- handler.deploy(AC_ID, getTestingAcElement(), Map.of());
- verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
- DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
+ var compositionElement = getCompositionElement();
+ var instanceElement = getInstanceElement();
+
+ handler.deploy(compositionElement, instanceElement);
+ verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ instanceElement.elementId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR,
+ "Deployed");
- handler.undeploy(AC_ID, automationCompositionElementId);
- verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
- DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "Undeployed");
+ handler.undeploy(compositionElement, instanceElement);
+ verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
+ "Undeployed");
}
@Test
@@ -107,11 +110,18 @@ class AutomationCompositionElementHandlerTest {
var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
mock(PolicyPapHttpClient.class), intermediaryApi);
- var acElement = getTestingAcElement();
- acElement.getToscaServiceTemplateFragment().setToscaTopologyTemplate(null);
- handler.deploy(AC_ID, acElement, Map.of());
- verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
- DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "ToscaTopologyTemplate not defined");
+ var compositionElement = getCompositionElement();
+ var instanceElement = getInstanceElementWithNullTopology();
+ handler.deploy(compositionElement, instanceElement);
+ verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
+ "ToscaTopologyTemplate not defined");
+ }
+
+ private InstanceElementDto getInstanceElementWithNullTopology() {
+ var template = new ToscaServiceTemplate();
+ template.setToscaTopologyTemplate(null);
+ return new InstanceElementDto(UUID.randomUUID(), UUID.randomUUID(), template, Map.of(), Map.of());
}
@Test
@@ -126,12 +136,13 @@ class AutomationCompositionElementHandlerTest {
var intermediaryApi = mock(ParticipantIntermediaryApi.class);
var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
- var element = getTestingAcElement();
+ var compositionElement = getCompositionElement();
+ var instanceElement = getInstanceElement();
// Mock failure in policy type creation
- handler.deploy(AC_ID, element, Map.of());
- verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
- DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
+ handler.deploy(compositionElement, instanceElement);
+ verify(intermediaryApi).updateAutomationCompositionElementState(instanceElement.instanceId(),
+ instanceElement.elementId(), DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
"Creation of PolicyTypes/Policies failed. Policies will not be deployed.");
}
@@ -147,88 +158,9 @@ class AutomationCompositionElementHandlerTest {
var intermediaryApi = mock(ParticipantIntermediaryApi.class);
var handler = new AutomationCompositionElementHandler(api, pap, intermediaryApi);
- var element = getTestingAcElement();
- assertThatThrownBy(() -> handler.deploy(AC_ID, element, Map.of()))
+ var compositionElement = getCompositionElement();
+ var instanceElement = getInstanceElement();
+ assertThatThrownBy(() -> handler.deploy(compositionElement, instanceElement))
.hasMessageMatching("Deploy of Policy failed.");
}
-
- @Test
- void testUpdate() throws Exception {
- var intermediaryApi = mock(ParticipantIntermediaryApi.class);
- var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
- mock(PolicyPapHttpClient.class), intermediaryApi);
-
- var acElement = getTestingAcElement();
- acElement.getToscaServiceTemplateFragment().setToscaTopologyTemplate(null);
- handler.update(AC_ID, acElement, Map.of());
- verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
- DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Update not supported");
- }
-
- @Test
- void testLock() throws Exception {
- var intermediaryApi = mock(ParticipantIntermediaryApi.class);
- var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
- mock(PolicyPapHttpClient.class), intermediaryApi);
-
- handler.lock(AC_ID, automationCompositionElementId);
- verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId, null,
- LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
- }
-
- @Test
- void testUnlock() throws Exception {
- var intermediaryApi = mock(ParticipantIntermediaryApi.class);
- var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
- mock(PolicyPapHttpClient.class), intermediaryApi);
-
- handler.unlock(AC_ID, automationCompositionElementId);
- verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId, null,
- LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
- }
-
- @Test
- void testDelete() throws Exception {
- var intermediaryApi = mock(ParticipantIntermediaryApi.class);
- var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
- mock(PolicyPapHttpClient.class), intermediaryApi);
-
- handler.delete(AC_ID, automationCompositionElementId);
- verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
- DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
- }
-
- @Test
- void testPrime() throws Exception {
- var intermediaryApi = mock(ParticipantIntermediaryApi.class);
- var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
- mock(PolicyPapHttpClient.class), intermediaryApi);
-
- handler.prime(AC_ID, List.of());
- verify(intermediaryApi).updateCompositionState(AC_ID, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
- }
-
- @Test
- void testDeprime() throws Exception {
- var intermediaryApi = mock(ParticipantIntermediaryApi.class);
- var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
- mock(PolicyPapHttpClient.class), intermediaryApi);
-
- handler.deprime(AC_ID);
- verify(intermediaryApi).updateCompositionState(AC_ID, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
- "Deprimed");
- }
-
- @Test
- void testMigrate() throws Exception {
- var intermediaryApi = mock(ParticipantIntermediaryApi.class);
- var handler = new AutomationCompositionElementHandler(mock(PolicyApiHttpClient.class),
- mock(PolicyPapHttpClient.class), intermediaryApi);
-
- var acElement = getTestingAcElement();
- acElement.getToscaServiceTemplateFragment().setToscaTopologyTemplate(null);
- handler.migrate(AC_ID, acElement, UUID.randomUUID(), Map.of());
- verify(intermediaryApi).updateAutomationCompositionElementState(AC_ID, automationCompositionElementId,
- DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
- }
}