Change 'policy-core' to support app persistence 15/4615/3
authorRalph Straubs <rs8887@att.com>
Fri, 2 Jun 2017 16:10:04 +0000 (11:10 -0500)
committerRalph Straubs <rs8887@att.com>
Fri, 2 Jun 2017 16:44:57 +0000 (11:44 -0500)
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 <rs8887@att.com>
policy-core/src/main/java/org/openecomp/policy/drools/core/PolicyContainer.java
policy-core/src/main/java/org/openecomp/policy/drools/core/PolicySession.java
policy-core/src/main/java/org/openecomp/policy/drools/core/PolicySessionFeatureAPI.java
policy-persistence/src/main/java/org/openecomp/policy/drools/persistence/PersistenceFeature.java

index d47ed61..fca30e0 100644 (file)
@@ -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);
+                 }
          }
   }
 
index 2149735..ae9dbc4 100644 (file)
@@ -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<Object, Object> adjuncts =
+       new ConcurrentHashMap<Object, Object>();
+
   // 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 */
index d505769..da828db 100644 (file)
@@ -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) {}
 }
index 3883aca..4d9c08a 100644 (file)
@@ -204,15 +204,6 @@ public class PersistenceFeature implements PolicySessionFeatureAPI, PolicyEngine
        getContainerAdjunct(policySession.getPolicyContainer())
          .destroyKieSession();
   }
-
-  /**
-   * {@inheritDoc}
-   */
-  @Override
-       public boolean isPersistenceEnabled()
-  {
-       return(!persistenceDisabled);
-  }
        
   /**
    * {@inheritDoc}