diff options
author | FrancescoFioraEst <francesco.fiora@est.tech> | 2023-02-17 14:17:24 +0000 |
---|---|---|
committer | FrancescoFioraEst <francesco.fiora@est.tech> | 2023-02-17 15:27:25 +0000 |
commit | 6d530dbc21e2965bc49d03540ea7a3f4bf37d69e (patch) | |
tree | 8e5941f1df1fc354534c0c80d68fd3376f6bcb6b /participant/participant-impl/participant-impl-policy/src/main/java | |
parent | 4b45a63b9e8512f0502680eb99a7b6457b48b5a3 (diff) |
Fix UNDEPLOY transition in policyParticipant in ACM
Fix UNDEPLOY transition in policyParticipant when there are
more then one service template defined
Issue-ID: POLICY-4561
Change-Id: Ib05b995a215d9f5f62c69f7058db128474f48dab
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Diffstat (limited to 'participant/participant-impl/participant-impl-policy/src/main/java')
2 files changed, 84 insertions, 63 deletions
diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/acm/participant/policy/client/PolicyApiHttpClient.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/acm/participant/policy/client/PolicyApiHttpClient.java index a86576eed..bf1d24a74 100644 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/acm/participant/policy/client/PolicyApiHttpClient.java +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/acm/participant/policy/client/PolicyApiHttpClient.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2021,2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +24,6 @@ import javax.ws.rs.client.Entity; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.onap.policy.clamp.acm.participant.policy.main.parameters.ParticipantPolicyParameters; -import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.springframework.stereotype.Component; @@ -47,9 +46,8 @@ public class PolicyApiHttpClient extends AbstractHttpClient { * * @param toscaServiceTemplate the whole ToscaServiceTemplate * @return Response - * @throws PfModelException on errors creating the policy type */ - public Response createPolicyType(ToscaServiceTemplate toscaServiceTemplate) throws PfModelException { + public Response createPolicyType(ToscaServiceTemplate toscaServiceTemplate) { return executePost(POLICY_URI + "policytypes", Entity.entity(toscaServiceTemplate, MediaType.APPLICATION_JSON)); } 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 0b03e236f..3e740192b 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 @@ -22,9 +22,13 @@ package org.onap.policy.clamp.acm.participant.policy.main.handler; +import java.util.ArrayList; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.UUID; +import javax.ws.rs.core.Response.Status; +import lombok.RequiredArgsConstructor; import lombok.Setter; import org.apache.http.HttpStatus; import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener; @@ -35,9 +39,9 @@ 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.LockState; import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.pdp.concepts.DeploymentSubGroup; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -46,11 +50,12 @@ import org.springframework.stereotype.Component; * This class handles implementation of automationCompositionElement updates. */ @Component +@RequiredArgsConstructor public class AutomationCompositionElementHandler implements AutomationCompositionElementListener { private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionElementHandler.class); - private final Map<String, String> policyTypeMap = new LinkedHashMap<>(); - private final Map<String, String> policyMap = new LinkedHashMap<>(); + + private final Map<UUID, ToscaServiceTemplate> serviceTemplateMap = new LinkedHashMap<>(); private final PolicyApiHttpClient apiHttpClient; private final PolicyPapHttpClient papHttpClient; @@ -59,53 +64,48 @@ public class AutomationCompositionElementHandler implements AutomationCompositio private ParticipantIntermediaryApi intermediaryApi; /** - * constructor. - * - * @param apiHttpClient the Policy Api Http Client - * @param papHttpClient the Policy Pap Http Client - */ - public AutomationCompositionElementHandler(PolicyApiHttpClient apiHttpClient, PolicyPapHttpClient papHttpClient) { - this.papHttpClient = papHttpClient; - this.apiHttpClient = apiHttpClient; - } - - /** * 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 */ @Override - public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId) { - try { - undeployPolicies(automationCompositionElementId); - deletePolicyData(automationCompositionId, automationCompositionElementId); + 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, LockState.NONE); - } catch (PfModelRuntimeException e) { - LOGGER.error("Undeploying/Deleting policy failed {}", automationCompositionElementId, e); + return; } + var policyList = getPolicyList(automationCompositionDefinition); + undeployPolicies(policyList, automationCompositionElementId); + var policyTypeList = getPolicyTypeList(automationCompositionDefinition); + deletePolicyData(policyTypeList, policyList); + serviceTemplateMap.remove(automationCompositionElementId); + intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, + automationCompositionElementId, DeployState.UNDEPLOYED, LockState.NONE); } - private void deletePolicyData(UUID automationCompositionId, UUID automationCompositionElementId) { + private void deletePolicyData(List<ToscaConceptIdentifier> policyTypeList, + List<ToscaConceptIdentifier> policyList) { // Delete all policies of this automationComposition from policy framework - for (var policy : policyMap.entrySet()) { - apiHttpClient.deletePolicy(policy.getKey(), policy.getValue()); + for (var policy : policyList) { + apiHttpClient.deletePolicy(policy.getName(), policy.getVersion()); } - policyMap.clear(); // Delete all policy types of this automation composition from policy framework - for (var policyType : policyTypeMap.entrySet()) { - apiHttpClient.deletePolicyType(policyType.getKey(), policyType.getValue()); + for (var policyType : policyTypeList) { + apiHttpClient.deletePolicyType(policyType.getName(), policyType.getVersion()); } - policyTypeMap.clear(); } - private void deployPolicies(UUID automationCompositionId, UUID automationCompositionElementId) { + private void deployPolicies(List<ToscaConceptIdentifier> policyList, UUID automationCompositionId, + UUID automationCompositionElementId) throws PfModelException { var deployFailure = false; // Deploy all policies of this automationComposition from Policy Framework - if (!policyMap.entrySet().isEmpty()) { - for (var policy : policyMap.entrySet()) { - var deployPolicyResp = papHttpClient.handlePolicyDeployOrUndeploy(policy.getKey(), policy.getValue(), + if (!policyList.isEmpty()) { + for (var policy : policyList) { + var deployPolicyResp = papHttpClient.handlePolicyDeployOrUndeploy(policy.getName(), policy.getVersion(), DeploymentSubGroup.Action.POST).getStatus(); if (deployPolicyResp != HttpStatus.SC_ACCEPTED) { deployFailure = true; @@ -119,14 +119,16 @@ public class AutomationCompositionElementHandler implements AutomationCompositio // Update the AC element state intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, automationCompositionElementId, DeployState.DEPLOYED, LockState.LOCKED); + } else { + throw new PfModelException(Status.BAD_REQUEST, "Deploy of Policy failed."); } } - private void undeployPolicies(UUID automationCompositionElementId) { + private void undeployPolicies(List<ToscaConceptIdentifier> policyList, UUID automationCompositionElementId) { // Undeploy all policies of this automation composition from Policy Framework - if (!policyMap.entrySet().isEmpty()) { - for (var policy : policyMap.entrySet()) { - papHttpClient.handlePolicyDeployOrUndeploy(policy.getKey(), policy.getValue(), + if (!policyList.isEmpty()) { + for (var policy : policyList) { + papHttpClient.handlePolicyDeployOrUndeploy(policy.getName(), policy.getVersion(), DeploymentSubGroup.Action.DELETE); } LOGGER.debug("Undeployed policies from {} successfully", automationCompositionElementId); @@ -150,32 +152,53 @@ public class AutomationCompositionElementHandler implements AutomationCompositio var createPolicyResp = HttpStatus.SC_OK; var automationCompositionDefinition = element.getToscaServiceTemplateFragment(); - if (automationCompositionDefinition.getToscaTopologyTemplate() != null) { - if (automationCompositionDefinition.getPolicyTypes() != null) { - for (var policyType : automationCompositionDefinition.getPolicyTypes().values()) { - policyTypeMap.put(policyType.getName(), policyType.getVersion()); - } - LOGGER.info("Found Policy Types in automation composition definition: {} , Creating Policy Types", - automationCompositionDefinition.getName()); - createPolicyTypeResp = apiHttpClient.createPolicyType(automationCompositionDefinition).getStatus(); + if (automationCompositionDefinition.getToscaTopologyTemplate() == null) { + throw new PfModelException(Status.BAD_REQUEST, "ToscaTopologyTemplate not defined"); + } + serviceTemplateMap.put(element.getId(), automationCompositionDefinition); + if (automationCompositionDefinition.getPolicyTypes() != null) { + LOGGER.info("Found Policy Types in automation composition definition: {} , Creating Policy Types", + automationCompositionDefinition.getName()); + createPolicyTypeResp = apiHttpClient.createPolicyType(automationCompositionDefinition).getStatus(); + } + if (automationCompositionDefinition.getToscaTopologyTemplate().getPolicies() != null) { + LOGGER.info("Found Policies in automation composition definition: {} , Creating Policies", + automationCompositionDefinition.getName()); + createPolicyResp = apiHttpClient.createPolicy(automationCompositionDefinition).getStatus(); + } + if (createPolicyTypeResp == HttpStatus.SC_OK && createPolicyResp == HttpStatus.SC_OK) { + LOGGER.info( + "PolicyTypes/Policies for the automation composition element : {} are created " + "successfully", + element.getId()); + var policyList = getPolicyList(automationCompositionDefinition); + deployPolicies(policyList, automationCompositionId, element.getId()); + } else { + throw new PfModelException(Status.BAD_REQUEST, + "Creation of PolicyTypes/Policies failed. Policies will not be deployed."); + } + } + + private List<ToscaConceptIdentifier> getPolicyTypeList(ToscaServiceTemplate serviceTemplate) { + List<ToscaConceptIdentifier> policyTypeList = new ArrayList<>(); + if (serviceTemplate.getPolicyTypes() != null) { + for (var policyType : serviceTemplate.getPolicyTypes().values()) { + policyTypeList.add(policyType.getKey().asIdentifier()); } - if (automationCompositionDefinition.getToscaTopologyTemplate().getPolicies() != null) { - for (var gotPolicyMap : automationCompositionDefinition.getToscaTopologyTemplate().getPolicies()) { - for (ToscaPolicy policy : gotPolicyMap.values()) { - policyMap.put(policy.getName(), policy.getVersion()); - } + } + + return policyTypeList; + } + + private List<ToscaConceptIdentifier> getPolicyList(ToscaServiceTemplate serviceTemplate) { + List<ToscaConceptIdentifier> policyList = new ArrayList<>(); + if (serviceTemplate.getToscaTopologyTemplate().getPolicies() != null) { + for (var gotPolicyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) { + for (var policy : gotPolicyMap.values()) { + policyList.add(policy.getKey().asIdentifier()); } - LOGGER.info("Found Policies in automation composition definition: {} , Creating Policies", - automationCompositionDefinition.getName()); - createPolicyResp = apiHttpClient.createPolicy(automationCompositionDefinition).getStatus(); - } - if (createPolicyTypeResp == HttpStatus.SC_OK && createPolicyResp == HttpStatus.SC_OK) { - LOGGER.info("PolicyTypes/Policies for the automation composition element : {} are created " - + "successfully", element.getId()); - deployPolicies(automationCompositionId, element.getId()); - } else { - LOGGER.error("Creation of PolicyTypes/Policies failed. Policies will not be deployed."); } } + + return policyList; } } |