From 40a1f22ff8d28e78b6512c0a10d454b37f015fdb Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Tue, 5 Nov 2019 14:37:09 +0000 Subject: Retaining context in APEX Engine based on policies received in PdpUpdate Change-Id: I73fad5bf76ed6b4979f5ab76013f204ea82da30b Issue-ID: POLICY-2215 Signed-off-by: a.sreekumar --- .../services/onappf/handler/ApexEngineHandler.java | 37 ++++++++++++++++++---- .../onappf/handler/PdpUpdateMessageHandler.java | 7 ++-- 2 files changed, 34 insertions(+), 10 deletions(-) (limited to 'services/services-onappf/src') 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 1953939b7..6a5bb17ff 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 @@ -56,6 +56,35 @@ public class ApexEngineHandler { * @throws ApexStarterException if the apex engine instantiation failed using the policies passed */ public ApexEngineHandler(List policies) throws ApexStarterException { + Map policyArgsMap = createPolicyArgsMap(policies); + LOGGER.debug("Starting apex engine."); + try { + apexMain = new ApexMain(policyArgsMap); + } catch (ApexException e) { + throw new ApexStarterException(e); + } + } + + /** + * Updates the Apex Engine with the policy model created from new list of policies. + * + * @param policies the list of policies + * @throws ApexStarterException if the apex engine instantiation failed using the policies passed + */ + public void updateApexEngine(List policies) throws ApexStarterException { + if (null == apexMain || !apexMain.isAlive()) { + throw new ApexStarterException("Apex Engine not initialized."); + } + Map policyArgsMap = createPolicyArgsMap(policies); + try { + apexMain.updateModel(policyArgsMap); + } catch (ApexException e) { + throw new ApexStarterException(e); + } + } + + private Map createPolicyArgsMap(List policies) + throws ApexStarterException { Map policyArgsMap = new LinkedHashMap<>(); for (ToscaPolicy policy : policies) { Object properties = policy.getProperties().get("content"); @@ -78,13 +107,7 @@ public class ApexEngineHandler { final String[] apexArgs = { "-c", apexConfigFilePath, "-m", modelFilePath }; policyArgsMap.put(policy.getIdentifier(), apexArgs); } - - LOGGER.debug("Starting apex engine."); - try { - apexMain = new ApexMain(policyArgsMap); - } catch (ApexException e) { - throw new ApexStarterException(e); - } + return policyArgsMap; } /** 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 ecc0bec21..33ac81f5d 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 @@ -162,10 +162,11 @@ public class PdpUpdateMessageHandler { PdpResponseDetails pdpResponseDetails = null; try { if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) { - apexEngineHandler.shutdown(); + apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPolicies()); + } else { + apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPolicies()); + Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler); } - apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPolicies()); - Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler); if (apexEngineHandler.isApexEngineRunning()) { List runningPolicies = apexEngineHandler.getRunningPolicies(); if (new HashSet<>(runningPolicies) -- cgit 1.2.3-korg