From 68536f36971c101150ec35d44bd445355ffd0384 Mon Sep 17 00:00:00 2001 From: Ralph Straubs Date: Fri, 2 Jun 2017 11:10:04 -0500 Subject: Change 'policy-core' to support app persistence This includes the following: 1) A new hook method 'newPolicySession(PolicySession)' that is called after the session has been created and initialized 2) Support for "adjunct" objects in 'PolicySession' 3) Added 'try/catch' blocks around feature method invocations 4) Added 'default' methods to interface 'PolicySessionFeatureAPI' 5) Removed 'PolicySessionFeatureAPI.isPersistenceEnabled()' Conflicts: policy-core/src/main/java/org/openecomp/policy/drools/core/PolicySessionFeatureAPI.java Change-Id: Ibc6d9eeacb6118d617e6c5ac53f6cef4c6ee1417 Signed-off-by: Ralph Straubs --- .../policy/drools/core/PolicyContainer.java | 86 +++++++++++++++++++--- .../policy/drools/core/PolicySession.java | 41 +++++++++++ .../drools/core/PolicySessionFeatureAPI.java | 29 ++++---- 3 files changed, 133 insertions(+), 23 deletions(-) (limited to 'policy-core/src/main') diff --git a/policy-core/src/main/java/org/openecomp/policy/drools/core/PolicyContainer.java b/policy-core/src/main/java/org/openecomp/policy/drools/core/PolicyContainer.java index d47ed617..fca30e00 100644 --- a/policy-core/src/main/java/org/openecomp/policy/drools/core/PolicyContainer.java +++ b/policy-core/src/main/java/org/openecomp/policy/drools/core/PolicyContainer.java @@ -280,11 +280,20 @@ public class PolicyContainer implements Startable // loop through all of the features, and give each one // a chance to create the 'KieSession' - for (PolicySessionFeatureAPI feature : PolicySessionFeatureAPI.impl.getList()) + for (PolicySessionFeatureAPI feature : + PolicySessionFeatureAPI.impl.getList()) { - if ((kieSession = feature.activatePolicySession - (this, name, kieBaseName)) != null) - break; + try + { + if ((kieSession = feature.activatePolicySession + (this, name, kieBaseName)) != null) + break; + } + catch (Exception e) + { + logger.error("ERROR: Feature API: " + + feature.getClass().getName(), e); + } } // if none of the features created the session, create one now @@ -299,6 +308,21 @@ public class PolicyContainer implements Startable // a PolicySession session = new PolicySession(name, this, kieSession); sessions.put(name, session); + + // notify features + for (PolicySessionFeatureAPI feature : + PolicySessionFeatureAPI.impl.getList()) + { + try + { + feature.newPolicySession(session); + } + catch (Exception e) + { + logger.error("ERROR: Feature API: " + + feature.getClass().getName(), e); + } + } logger.info("activatePolicySession:new session was added in sessions with name " + name); } } @@ -374,6 +398,21 @@ public class PolicyContainer implements Startable PolicySession policySession = new PolicySession(name, this, kieSession); sessions.put(name, policySession); + + // notify features + for (PolicySessionFeatureAPI feature : + PolicySessionFeatureAPI.impl.getList()) + { + try + { + feature.newPolicySession(policySession); + } + catch (Exception e) + { + logger.error("ERROR: Feature API: " + + feature.getClass().getName(), e); + } + } return(policySession); } } @@ -590,9 +629,18 @@ public class PolicyContainer implements Startable session.getKieSession().dispose(); // notify features - for (PolicySessionFeatureAPI feature : PolicySessionFeatureAPI.impl.getList()) + for (PolicySessionFeatureAPI feature : + PolicySessionFeatureAPI.impl.getList()) { - feature.disposeKieSession(session); + try + { + feature.disposeKieSession(session); + } + catch (Exception e) + { + logger.error("ERROR: Feature API: " + + feature.getClass().getName(), e); + } } } isStarted = false; @@ -657,9 +705,18 @@ public class PolicyContainer implements Startable session.getKieSession().destroy(); // notify features - for (PolicySessionFeatureAPI feature : PolicySessionFeatureAPI.impl.getList()) + for (PolicySessionFeatureAPI feature : + PolicySessionFeatureAPI.impl.getList()) { - feature.destroyKieSession(session); + try + { + feature.destroyKieSession(session); + } + catch (Exception e) + { + logger.error("ERROR: Feature API: " + + feature.getClass().getName(), e); + } } } isStarted = false; @@ -750,9 +807,18 @@ public class PolicyContainer implements Startable logger.info("initlogger returned"); // invoke 'globalInit' on all of the features - for (PolicySessionFeatureAPI feature : PolicySessionFeatureAPI.impl.getList()) + for (PolicySessionFeatureAPI feature : + PolicySessionFeatureAPI.impl.getList()) { - feature.globalInit(args, configDir); + try + { + feature.globalInit(args, configDir); + } + catch (Exception e) + { + logger.error("ERROR: Feature API: " + + feature.getClass().getName(), e); + } } } diff --git a/policy-core/src/main/java/org/openecomp/policy/drools/core/PolicySession.java b/policy-core/src/main/java/org/openecomp/policy/drools/core/PolicySession.java index 2149735a..ae9dbc45 100644 --- a/policy-core/src/main/java/org/openecomp/policy/drools/core/PolicySession.java +++ b/policy-core/src/main/java/org/openecomp/policy/drools/core/PolicySession.java @@ -20,6 +20,8 @@ package org.openecomp.policy.drools.core; +import java.util.concurrent.ConcurrentHashMap; + import org.kie.api.event.rule.AfterMatchFiredEvent; import org.kie.api.event.rule.AgendaEventListener; import org.kie.api.event.rule.AgendaGroupPoppedEvent; @@ -58,6 +60,10 @@ public class PolicySession // 'PolicySession' instances in addition to this one private PolicyContainer container; + // maps feature objects to per-PolicyContainer data + private ConcurrentHashMap adjuncts = + new ConcurrentHashMap(); + // associated 'KieSession' instance private KieSession kieSession; @@ -223,6 +229,41 @@ public class PolicySession { return(policySession.get()); } + + /** + * Fetch the adjunct object associated with a given feature + * + * @param object this is typically the singleton feature object that is + * used as a key, but it might also be useful to use nested objects + * within the feature as keys. + * @return a feature-specific object associated with the key, or 'null' + * if it is not found. + */ + public Object getAdjunct(Object object) + { + return(adjuncts.get(object)); + } + + /** + * Store the adjunct object associated with a given feature + * + * @param object this is typically the singleton feature object that is + * used as a key, but it might also be useful to use nested objects + * within the feature as keys. + * @param value a feature-specific object associated with the key, or 'null' + * if the feature-specific object should be removed + */ + public void setAdjunct(Object object, Object value) + { + if (value == null) + { + adjuncts.remove(object); + } + else + { + adjuncts.put(object, value); + } + } /***********************************/ /* 'AgendaEventListener' interface */ diff --git a/policy-core/src/main/java/org/openecomp/policy/drools/core/PolicySessionFeatureAPI.java b/policy-core/src/main/java/org/openecomp/policy/drools/core/PolicySessionFeatureAPI.java index d5057696..da828db3 100644 --- a/policy-core/src/main/java/org/openecomp/policy/drools/core/PolicySessionFeatureAPI.java +++ b/policy-core/src/main/java/org/openecomp/policy/drools/core/PolicySessionFeatureAPI.java @@ -48,7 +48,7 @@ public interface PolicySessionFeatureAPI extends OrderedService * @param args standard 'main' arguments, which are currently ignored * @param configDir the relative directory containing configuration files */ - public void globalInit(String args[], String configDir); + default public void globalInit(String args[], String configDir) {} /** * This method is used to create a 'KieSession' as part of a @@ -65,30 +65,33 @@ public interface PolicySessionFeatureAPI extends OrderedService * (this depends on the capabilities and state of the object implementing * this interface) */ - public KieSession activatePolicySession - (PolicyContainer policyContainer, String name, String kieBaseName); + default public KieSession activatePolicySession + (PolicyContainer policyContainer, String name, String kieBaseName) + { + return(null); + } /** - * This method is called after 'KieSession.dispose()' is called + * This method is called after a new 'PolicySession' has been initialized, + * and linked to the 'PolicyContainer'. * - * @param policySession the 'PolicySession' object that wrapped the - * 'KieSession' + * @param policySession the new 'PolicySession' instance */ - public void disposeKieSession(PolicySession policySession); + default public void newPolicySession(PolicySession policySession) {} /** - * This method is called after 'KieSession.destroy()' is called + * This method is called after 'KieSession.dispose()' is called * * @param policySession the 'PolicySession' object that wrapped the * 'KieSession' */ - public void destroyKieSession(PolicySession policySession); + default public void disposeKieSession(PolicySession policySession) {} /** - * NOTE: this method is probably temporary + * This method is called after 'KieSession.destroy()' is called * - * @return 'true' if persistence is enabled, and 'false' if not, or if - * this feature is not related to persistence. + * @param policySession the 'PolicySession' object that wrapped the + * 'KieSession' */ - public boolean isPersistenceEnabled(); + default public void destroyKieSession(PolicySession policySession) {} } -- cgit 1.2.3-korg