From c28ef7b282ab4378935387314f1a57d18e02fcf4 Mon Sep 17 00:00:00 2001 From: waynedunican Date: Wed, 14 Apr 2021 13:19:35 +0100 Subject: Support delta policies in apex-pdp Remove scanning of policy list from apex engine. Use the policiesToBeDeployed and policiesToBeUndeployed lists from PDP_UPDATE message instead. Issue-ID: POLICY-3170 Change-Id: Icdde66ef87d9feab83ccb1cf99a64d7870bcaaaa Signed-off-by: waynedunican --- .../services/onappf/handler/ApexEngineHandler.java | 16 ++++---- .../onappf/handler/PdpUpdateMessageHandler.java | 46 +++++++++++++++++----- 2 files changed, 45 insertions(+), 17 deletions(-) (limited to 'services/services-onappf/src/main/java/org') diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java index 65c4b0373..f3b883141 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java @@ -89,16 +89,18 @@ public class ApexEngineHandler { /** * Updates the Apex Engine with the policy model created from new list of policies. * - * @param policies the list of policies + + * @param polsToDeploy list of policies to deploy which will be modified to remove running policies + * @param polsToUndeploy list of policies to undeploy which will be modified to remove policies not running * @throws ApexStarterException if the apex engine instantiation failed using the policies passed */ - public void updateApexEngine(List policies) throws ApexStarterException { + public void updateApexEngine(List polsToDeploy, List polsToUndeploy) + throws ApexStarterException { List runningPolicies = getRunningPolicies(); - List policiesToDeploy = policies.stream() - .filter(policy -> !runningPolicies.contains(policy.getIdentifier())).collect(Collectors.toList()); - List policiesToUnDeploy = runningPolicies.stream() - .filter(polId -> policies.stream().noneMatch(policy -> policy.getIdentifier().equals(polId))) - .collect(Collectors.toList()); + List policiesToDeploy = polsToDeploy; + policiesToDeploy.removeIf(p -> runningPolicies.contains(p.getIdentifier())); + List policiesToUnDeploy = polsToUndeploy; + policiesToUnDeploy.removeIf(p -> !runningPolicies.contains(p)); Map undeployedPoliciesMainMap = new LinkedHashMap<>(); policiesToUnDeploy.forEach(policyId -> { ApexMain apexMain = apexMainMap.get(policyId); diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java index 8dc49eefb..0e7465d19 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java @@ -21,9 +21,11 @@ package org.onap.policy.apex.services.onappf.handler; +import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.stream.Collectors; import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager; import org.onap.policy.apex.services.onappf.ApexStarterConstants; import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher; @@ -36,6 +38,7 @@ import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.pdp.enums.PdpResponseStatus; import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -92,9 +95,14 @@ public class PdpUpdateMessageHandler { } pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup()); pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup()); - pdpStatusContext - .setPolicies(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())); - Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies()); + @SuppressWarnings("unchecked") + List policies = Registry.getOrDefault(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST, + List.class, new ArrayList<>()); + policies.addAll(pdpUpdateMsg.getPoliciesToBeDeployed()); + pdpStatusContext.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier) + .collect(Collectors.toList())); + Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST, + policies); if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) { pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler); @@ -135,7 +143,9 @@ public class PdpUpdateMessageHandler { } catch (final IllegalArgumentException e) { LOGGER.debug("ApenEngineHandler not in registry.", e); } - if (pdpUpdateMsg.getPolicies().isEmpty()) { + if (null != apexEngineHandler + && pdpUpdateMsg.getPoliciesToBeUndeployed().containsAll(apexEngineHandler.getRunningPolicies()) + && pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) { pdpResponseDetails = stopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler); } else { pdpResponseDetails = startApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler); @@ -166,15 +176,17 @@ public class PdpUpdateMessageHandler { try { if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) { - apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPolicies()); + apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPoliciesToBeDeployed(), + pdpUpdateMsg.getPoliciesToBeUndeployed()); } else { - apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPolicies()); + apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPoliciesToBeDeployed()); Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler); } if (apexEngineHandler.isApexEngineRunning()) { List runningPolicies = apexEngineHandler.getRunningPolicies(); - if (new HashSet<>(runningPolicies) - .equals(new HashSet<>(pdpMessageHandler.getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())))) { + if (new HashSet<>(runningPolicies).containsAll(new HashSet<>(pdpMessageHandler + .getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()))) + && !containsAny(runningPolicies, pdpUpdateMsg.getPoliciesToBeUndeployed())) { pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), PdpResponseStatus.SUCCESS, "Apex engine started and policies are running."); } else { @@ -217,8 +229,10 @@ public class PdpUpdateMessageHandler { && pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup()) && null != pdpStatusContext.getPdpSubgroup() && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup()) - && null != pdpStatusContext.getPolicies() && new PdpMessageHandler() - .getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()).equals(pdpStatusContext.getPolicies()); + && null != pdpStatusContext.getPolicies() + && pdpStatusContext.getPolicies().containsAll(new PdpMessageHandler().getToscaPolicyIdentifiers( + pdpUpdateMsg.getPoliciesToBeDeployed())) + && !containsAny(pdpStatusContext.getPolicies(), pdpUpdateMsg.getPoliciesToBeUndeployed()); } /** @@ -233,4 +247,16 @@ public class PdpUpdateMessageHandler { Registry.registerOrReplace(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, new PdpStatusPublisher(topicSinks, interval)); } + + /** + * Checks if one list contains any element of another. + * + * @param listToCheckWith list to check contents of + * @param listToCheckAgainst list to check against other list for similarities + * @return boolean flag which tells if lists share same elements or not + */ + private boolean containsAny(List listToCheckWith, + List listToCheckAgainst) { + return listToCheckAgainst.stream().anyMatch(listToCheckWith::contains); + } } -- cgit 1.2.3-korg