Fix soft stop and start of engine 41/66141/2
authorliamfallon <liam.fallon@ericsson.com>
Wed, 12 Sep 2018 15:12:14 +0000 (16:12 +0100)
committerliamfallon <liam.fallon@ericsson.com>
Wed, 12 Sep 2018 15:21:43 +0000 (16:21 +0100)
Stopping an apexc engine is a soft stop, allowing the engine
to be restarted again. However, when a model upgrade is carried out
the engine must be stopped and cleared to clear the previous model,
loaded with the new model and started again.

THis handling must work over the Web Socket admin interface and
also when apex is started as a service.

This change fixes the handling so that it is consistent and
works the same when apex is stopped/started/upgraded from
the command line or over the management web socket interface

Issue-ID: POLICY-1101
Change-Id: I1fe619641cb6ecd34d0669b4b7df105280e5e2c0
Signed-off-by: liamfallon <liam.fallon@ericsson.com>
core/core-engine/src/main/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImpl.java
services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexActivator.java
services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexEngineServiceHandler.java
services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/EngineService.java
services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineServiceImpl.java
services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorker.java
testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/apps/uservice/test/context/EventAlbumContextTest.java

index 3349b60..3497ed6 100644 (file)
@@ -247,8 +247,11 @@ public class ApexEngineImpl implements ApexEngine {
         // Clear everything
         stateMachineHandler = null;
         engineStats.clean();
-        internalContext.clear();
-        internalContext = null;
+
+        if (internalContext != null) {
+            internalContext.clear();
+            internalContext = null;
+        }
     }
 
     /*
index 20af314..aa82e1c 100644 (file)
@@ -102,7 +102,6 @@ public class ApexActivator {
                         .getTextFileAsString(apexParameters.getEngineServiceParameters().getPolicyModelFileName());
                 apexEngineService.updateModel(apexParameters.getEngineServiceParameters().getEngineKey(),
                         policyModelString, true);
-                apexEngineService.startAll();
             }
 
             // Producer parameters specify what event marshalers to handle events leaving Apex are
index ad7af94..516ea4f 100644 (file)
@@ -83,6 +83,7 @@ public class ApexEngineServiceHandler {
         // Shut down each engine instance
         if (apexEngineService != null) {
             apexEngineService.stop();
+            apexEngineService.clear();
         }
     }
 }
index d643135..1af0c9d 100644 (file)
@@ -137,6 +137,21 @@ public interface EngineService {
      */
     void stop(AxArtifactKey engineKey) throws ApexException;
 
+    /**
+     * This method clears and uninitializes all Apex engines in the engine service.
+     *
+     * @throws ApexException on clear errors
+     */
+    void clear() throws ApexException;
+
+    /**
+     * This method clears and uninitializes an Apex engine in the engine service.
+     *
+     * @param engineKey The key of the Apex engine to clear
+     * @throws ApexException on clear errors
+     */
+    void clear(AxArtifactKey engineKey) throws ApexException;
+
     /**
      * This method checks if all Apex engines in the engine service are started.
      * 
index d5cb055..04fb8e3 100644 (file)
@@ -50,9 +50,9 @@ import org.slf4j.ext.XLogger;
 import org.slf4j.ext.XLoggerFactory;
 
 /**
- * The Class EngineServiceImpl controls a thread pool that runs a set of Apex engine workers, each
- * of which is running on an identical Apex model. This class handles the management of the engine
- * worker instances, their threads, and event forwarding to and from the engine workers.
+ * The Class EngineServiceImpl controls a thread pool that runs a set of Apex engine workers, each of which is running
+ * on an identical Apex model. This class handles the management of the engine worker instances, their threads, and
+ * event forwarding to and from the engine workers.
  *
  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
  * @author Liam Fallon (liam.fallon@ericsson.com)
@@ -72,8 +72,8 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     private AxArtifactKey engineServiceKey = null;
 
     // The Apex engine workers this engine service is handling
-    private final Map<AxArtifactKey, EngineService> engineWorkerMap =
-            Collections.synchronizedMap(new LinkedHashMap<AxArtifactKey, EngineService>());
+    private final Map<AxArtifactKey, EngineService> engineWorkerMap = Collections
+                    .synchronizedMap(new LinkedHashMap<AxArtifactKey, EngineService>());
 
     // Event queue for events being sent into the Apex engines, it used by all engines within a
     // group.
@@ -87,8 +87,8 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     private long periodicEventPeriod;
 
     /**
-     * This constructor instantiates engine workers and adds them to the set of engine workers to be
-     * managed. The constructor is private to prevent subclassing.
+     * This constructor instantiates engine workers and adds them to the set of engine workers to be managed. The
+     * constructor is private to prevent subclassing.
      *
      * @param engineServiceKey the engine service key
      * @param incomingThreadCount the thread count, the number of engine workers to start
@@ -96,7 +96,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
      * @throws ApexException on worker instantiation errors
      */
     private EngineServiceImpl(final AxArtifactKey engineServiceKey, final int incomingThreadCount,
-            final long periodicEventPeriod) throws ApexException {
+                    final long periodicEventPeriod) throws ApexException {
         LOGGER.entry(engineServiceKey, incomingThreadCount);
 
         this.engineServiceKey = engineServiceKey;
@@ -110,8 +110,8 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
 
         // Start engine workers
         for (int engineCounter = 0; engineCounter < threadCount; engineCounter++) {
-            final AxArtifactKey engineWorkerKey =
-                    new AxArtifactKey(engineServiceKey.getName() + '-' + engineCounter, engineServiceKey.getVersion());
+            final AxArtifactKey engineWorkerKey = new AxArtifactKey(engineServiceKey.getName() + '-' + engineCounter,
+                            engineServiceKey.getVersion());
             engineWorkerMap.put(engineWorkerKey, new EngineWorker(engineWorkerKey, queue, atFactory));
             LOGGER.info("Created apex engine {} .", engineWorkerKey.getId());
         }
@@ -121,8 +121,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     }
 
     /**
-     * Create an Apex Engine Service instance. This method is deprecated and will be removed in the
-     * next version.
+     * Create an Apex Engine Service instance. This method is deprecated and will be removed in the next version.
      *
      * @param engineServiceKey the engine service key
      * @param threadCount the thread count, the number of engine workers to start
@@ -132,7 +131,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
      */
     @Deprecated
     public static EngineServiceImpl create(final AxArtifactKey engineServiceKey, final int threadCount)
-            throws ApexException {
+                    throws ApexException {
         // Check if the Apex model specified is sane
         if (engineServiceKey == null) {
             LOGGER.warn("engine service key is null");
@@ -144,9 +143,8 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /**
      * Create an Apex Engine Service instance. This method does not load the policy so
      * {@link #updateModel(AxArtifactKey, AxPolicyModel, boolean)} or
-     * {@link #updateModel(AxArtifactKey, AxPolicyModel, boolean)} must be used to load a model.
-     * This method does not start the Engine Service so {@link #start(AxArtifactKey)} or
-     * {@link #startAll()} must be used.
+     * {@link #updateModel(AxArtifactKey, AxPolicyModel, boolean)} must be used to load a model. This method does not
+     * start the Engine Service so {@link #start(AxArtifactKey)} or {@link #startAll()} must be used.
      *
      * @param config the configuration for this Apex Engine Service.
      * @return the Engine Service instance
@@ -177,8 +175,8 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      * 
-     * @see com.ericsson.apex.service.engine.runtime.EngineService#registerActionListener(java.lang.
-     * String, com.ericsson.apex.service.engine.runtime.ApexEventListener)
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#registerActionListener(java.lang. String,
+     * org.onap.policy.apex.service.engine.runtime.ApexEventListener)
      */
     @Override
     public void registerActionListener(final String listenerName, final ApexEventListener apexEventListener) {
@@ -197,9 +195,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      * 
-     * @see
-     * com.ericsson.apex.service.engine.runtime.EngineService#deregisterActionListener(java.lang.
-     * String)
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#deregisterActionListener(java.lang. String)
      */
     @Override
     public void deregisterActionListener(final String listenerName) {
@@ -218,7 +214,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see com.ericsson.apex.service.engine.runtime.EngineService#getEngineServiceEventInterface()
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#getEngineServiceEventInterface()
      */
     @Override
     public EngineServiceEventInterface getEngineServiceEventInterface() {
@@ -228,7 +224,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see com.ericsson.apex.service.engine.runtime.EngineService#getKey()
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#getKey()
      */
     @Override
     public AxArtifactKey getKey() {
@@ -238,7 +234,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see com.ericsson.apex.service.engine.runtime.EngineService#getInfo()
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#getInfo()
      */
     @Override
     public Collection<AxArtifactKey> getEngineKeys() {
@@ -248,7 +244,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see com.ericsson.apex.service.engine.runtime.EngineService#getApexModelKey()
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#getApexModelKey()
      */
     @Override
     public AxArtifactKey getApexModelKey() {
@@ -262,19 +258,18 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see
-     * com.ericsson.apex.service.engine.runtime.EngineService#updateModel(com.ericsson.apex.model.
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#updateModel(org.onap.policy.apex.model.
      * basicmodel.concepts.AxArtifactKey, java.lang.String, boolean)
      */
     @Override
     public void updateModel(final AxArtifactKey incomingEngineServiceKey, final String apexModelString,
-            final boolean forceFlag) throws ApexException {
+                    final boolean forceFlag) throws ApexException {
         // Check if the Apex model specified is sane
         if (apexModelString == null || apexModelString.trim().length() == 0) {
-            LOGGER.warn(
-                    "model for updating on engine service with key " + incomingEngineServiceKey.getId() + " is empty");
-            throw new ApexException(
-                    "model for updating on engine service with key " + incomingEngineServiceKey.getId() + " is empty");
+            LOGGER.warn("model for updating on engine service with key " + incomingEngineServiceKey.getId()
+                            + " is empty");
+            throw new ApexException("model for updating on engine service with key " + incomingEngineServiceKey.getId()
+                            + " is empty");
         }
 
         // Read the Apex model into memory using the Apex Model Reader
@@ -285,7 +280,8 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
         } catch (final ApexModelException e) {
             LOGGER.error("failed to unmarshal the apex model on engine service " + incomingEngineServiceKey.getId(), e);
             throw new ApexException(
-                    "failed to unmarshal the apex model on engine service " + incomingEngineServiceKey.getId(), e);
+                            "failed to unmarshal the apex model on engine service " + incomingEngineServiceKey.getId(),
+                            e);
         }
 
         if (apexPolicyModel == null) {
@@ -302,30 +298,28 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see
-     * com.ericsson.apex.service.engine.runtime.EngineService#updateModel(com.ericsson.apex.model.
-     * basicmodel.concepts.AxArtifactKey,
-     * com.ericsson.apex.model.policymodel.concepts.AxPolicyModel, boolean)
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#updateModel(org.onap.policy.apex.model.
+     * basicmodel.concepts.AxArtifactKey, org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel, boolean)
      */
     @Override
     public void updateModel(final AxArtifactKey incomingEngineServiceKey, final AxPolicyModel apexModel,
-            final boolean forceFlag) throws ApexException {
+                    final boolean forceFlag) throws ApexException {
         LOGGER.entry(incomingEngineServiceKey);
 
         // Check if the Apex model specified is sane
         if (apexModel == null) {
-            LOGGER.warn(
-                    "model for updating on engine service with key " + incomingEngineServiceKey.getId() + " is null");
-            throw new ApexException(
-                    "model for updating on engine service with key " + incomingEngineServiceKey.getId() + " is null");
+            LOGGER.warn("model for updating on engine service with key " + incomingEngineServiceKey.getId()
+                            + " is null");
+            throw new ApexException("model for updating on engine service with key " + incomingEngineServiceKey.getId()
+                            + " is null");
         }
 
         // Check if the key on the update request is correct
         if (!this.engineServiceKey.equals(incomingEngineServiceKey)) {
             LOGGER.warn("engine service key " + incomingEngineServiceKey.getId() + " does not match the key"
-                    + engineServiceKey.getId() + " of this engine service");
+                            + engineServiceKey.getId() + " of this engine service");
             throw new ApexException("engine service key " + incomingEngineServiceKey.getId() + " does not match the key"
-                    + engineServiceKey.getId() + " of this engine service");
+                            + engineServiceKey.getId() + " of this engine service");
         }
 
         // Check model compatibility
@@ -335,20 +329,18 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
             if (!currentModel.getKey().isCompatible(apexModel.getKey())) {
                 if (forceFlag) {
                     LOGGER.warn("apex model update forced, supplied model with key \"" + apexModel.getKey().getId()
-                            + "\" is not a compatible model update from the existing engine model with key \""
-                            + currentModel.getKey().getId() + "\"");
+                                    + "\" is not a compatible model update from the existing engine model with key \""
+                                    + currentModel.getKey().getId() + "\"");
                 } else {
-                    throw new ContextException(
-                            "apex model update failed, supplied model with key \"" + apexModel.getKey().getId()
+                    throw new ContextException("apex model update failed, supplied model with key \""
+                                    + apexModel.getKey().getId()
                                     + "\" is not a compatible model update from the existing engine model with key \""
                                     + currentModel.getKey().getId() + "\"");
                 }
             }
         }
 
-        final boolean wasstopped = isStopped();
-
-        if (!wasstopped) {
+        if (!isStopped()) {
             // Stop all engines on this engine service
             stop();
             final long stoptime = System.currentTimeMillis();
@@ -356,19 +348,19 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
                 ThreadUtilities.sleep(ENGINE_SERVICE_STOP_START_WAIT_INTERVAL);
             }
             // Check if all engines are stopped
-            final StringBuilder notStoppedEngineIDBuilder = new StringBuilder();
+            final StringBuilder notStoppedEngineIdBuilder = new StringBuilder();
             for (final Entry<AxArtifactKey, EngineService> engineWorkerEntry : engineWorkerMap.entrySet()) {
                 if (engineWorkerEntry.getValue().getState() != AxEngineState.STOPPED) {
-                    notStoppedEngineIDBuilder.append(engineWorkerEntry.getKey().getId());
-                    notStoppedEngineIDBuilder.append('(');
-                    notStoppedEngineIDBuilder.append(engineWorkerEntry.getValue().getState());
-                    notStoppedEngineIDBuilder.append(") ");
+                    notStoppedEngineIdBuilder.append(engineWorkerEntry.getKey().getId());
+                    notStoppedEngineIdBuilder.append('(');
+                    notStoppedEngineIdBuilder.append(engineWorkerEntry.getValue().getState());
+                    notStoppedEngineIdBuilder.append(") ");
                 }
             }
-            if (notStoppedEngineIDBuilder.length() > 0) {
+            if (notStoppedEngineIdBuilder.length() > 0) {
                 final String errorString = "cannot update model on engine service with key "
-                        + incomingEngineServiceKey.getId() + ", engines not stopped after " + MAX_STOP_WAIT_TIME
-                        + "ms are: " + notStoppedEngineIDBuilder.toString().trim();
+                                + incomingEngineServiceKey.getId() + ", engines not stopped after " + MAX_STOP_WAIT_TIME
+                                + "ms are: " + notStoppedEngineIdBuilder.toString().trim();
                 LOGGER.warn(errorString);
                 throw new ApexException(errorString);
             }
@@ -380,32 +372,30 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
             engineWorkerEntry.getValue().updateModel(engineWorkerEntry.getKey(), apexModel, forceFlag);
         }
 
-        if (!wasstopped) {
-            // start all engines on this engine service if it was not stopped before the update
-            startAll();
-            final long starttime = System.currentTimeMillis();
-            while (!isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT_TIME) {
-                ThreadUtilities.sleep(ENGINE_SERVICE_STOP_START_WAIT_INTERVAL);
-            }
-            // Check if all engines are running
-            final StringBuilder notRunningEngineIDBuilder = new StringBuilder();
-            for (final Entry<AxArtifactKey, EngineService> engineWorkerEntry : engineWorkerMap.entrySet()) {
-                if (engineWorkerEntry.getValue().getState() != AxEngineState.READY
-                        && engineWorkerEntry.getValue().getState() != AxEngineState.EXECUTING) {
-                    notRunningEngineIDBuilder.append(engineWorkerEntry.getKey().getId());
-                    notRunningEngineIDBuilder.append('(');
-                    notRunningEngineIDBuilder.append(engineWorkerEntry.getValue().getState());
-                    notRunningEngineIDBuilder.append(") ");
-                }
-            }
-            if (notRunningEngineIDBuilder.length() > 0) {
-                final String errorString = "engine start error on model update on engine service with key "
-                        + incomingEngineServiceKey.getId() + ", engines not running are: "
-                        + notRunningEngineIDBuilder.toString().trim();
-                LOGGER.warn(errorString);
-                throw new ApexException(errorString);
+        // start all engines on this engine service if it was not stopped before the update
+        startAll();
+        final long starttime = System.currentTimeMillis();
+        while (!isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT_TIME) {
+            ThreadUtilities.sleep(ENGINE_SERVICE_STOP_START_WAIT_INTERVAL);
+        }
+        // Check if all engines are running
+        final StringBuilder notRunningEngineIdBuilder = new StringBuilder();
+        for (final Entry<AxArtifactKey, EngineService> engineWorkerEntry : engineWorkerMap.entrySet()) {
+            if (engineWorkerEntry.getValue().getState() != AxEngineState.READY
+                            && engineWorkerEntry.getValue().getState() != AxEngineState.EXECUTING) {
+                notRunningEngineIdBuilder.append(engineWorkerEntry.getKey().getId());
+                notRunningEngineIdBuilder.append('(');
+                notRunningEngineIdBuilder.append(engineWorkerEntry.getValue().getState());
+                notRunningEngineIdBuilder.append(") ");
             }
         }
+        if (notRunningEngineIdBuilder.length() > 0) {
+            final String errorString = "engine start error on model update on engine service with key "
+                            + incomingEngineServiceKey.getId() + ", engines not running are: "
+                            + notRunningEngineIdBuilder.toString().trim();
+            LOGGER.warn(errorString);
+            throw new ApexException(errorString);
+        }
 
         LOGGER.exit();
     }
@@ -413,7 +403,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see com.ericsson.apex.service.engine.runtime.EngineService#getState()
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#getState()
      */
     @Override
     public AxEngineState getState() {
@@ -430,7 +420,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see com.ericsson.apex.service.engine.runtime.EngineService#startAll()
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#startAll()
      */
     @Override
     public void startAll() throws ApexException {
@@ -447,8 +437,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see
-     * com.ericsson.apex.service.engine.runtime.EngineService#start(com.ericsson.apex.core.model.
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#start(org.onap.policy.apex.core.model.
      * concepts.AxArtifactKey)
      */
     @Override
@@ -470,7 +459,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see com.ericsson.apex.service.engine.runtime.EngineService#stop()
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#stop()
      */
     @Override
     public void stop() throws ApexException {
@@ -489,8 +478,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see
-     * com.ericsson.apex.service.engine.runtime.EngineService#stop(com.ericsson.apex.core.model.
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#stop(org.onap.policy.apex.core.model.
      * concepts.AxArtifactKey)
      */
     @Override
@@ -509,6 +497,49 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
         LOGGER.exit(engineKey);
     }
 
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#clear()
+     */
+    @Override
+    public void clear() throws ApexException {
+        LOGGER.entry();
+
+        // Stop each engine
+        for (final EngineService engine : engineWorkerMap.values()) {
+            if (engine.getState() == AxEngineState.STOPPED) {
+                engine.clear();
+            }
+        }
+
+        LOGGER.exit();
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#clear(org.onap.policy.apex.core.model.
+     * concepts.AxArtifactKey)
+     */
+    @Override
+    public void clear(final AxArtifactKey engineKey) throws ApexException {
+        LOGGER.entry(engineKey);
+
+        // Check if we have this key on our map
+        if (!engineWorkerMap.containsKey(engineKey)) {
+            LOGGER.warn("engine with key " + engineKey.getId() + " not found in engine service");
+            throw new ApexException("engine with key " + engineKey.getId() + " not found in engine service");
+        }
+
+        // Clear the engine
+        if (engineWorkerMap.get(engineKey).getState() == AxEngineState.STOPPED) {
+            engineWorkerMap.get(engineKey).stop(engineKey);
+        }
+
+        LOGGER.exit(engineKey);
+    }
+
     /**
      * Check all engines are started.
      *
@@ -528,8 +559,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see
-     * com.ericsson.apex.service.engine.runtime.EngineService#isStarted(com.ericsson.apex.model.
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#isStarted(org.onap.policy.apex.model.
      * basicmodel.concepts.AxArtifactKey)
      */
     @Override
@@ -560,8 +590,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see
-     * com.ericsson.apex.service.engine.runtime.EngineService#isStopped(com.ericsson.apex.model.
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#isStopped(org.onap.policy.apex.model.
      * basicmodel.concepts.AxArtifactKey)
      */
     @Override
@@ -576,16 +605,16 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see com.ericsson.apex.service.engine.runtime.EngineService#startPeriodicEvents(long)
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#startPeriodicEvents(long)
      */
     @Override
     public void startPeriodicEvents(final long period) throws ApexException {
         // Check if periodic events are already started
         if (periodicEventGenerator != null) {
             LOGGER.warn("Peiodic event geneation already running on engine " + engineServiceKey.getId() + ", "
-                    + periodicEventGenerator.toString());
+                            + periodicEventGenerator.toString());
             throw new ApexException("Peiodic event geneation already running on engine " + engineServiceKey.getId()
-                    + ", " + periodicEventGenerator.toString());
+                            + ", " + periodicEventGenerator.toString());
         }
 
         // Set up periodic event execution, its a Java Timer/TimerTask
@@ -599,7 +628,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see com.ericsson.apex.service.engine.runtime.EngineService#stopPeriodicEvents()
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#stopPeriodicEvents()
      */
     @Override
     public void stopPeriodicEvents() throws ApexException {
@@ -617,8 +646,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see
-     * com.ericsson.apex.service.engine.runtime.EngineService#getStatus(com.ericsson.apex.core.model
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#getStatus(org.onap.policy.apex.core.model
      * .concepts.AxArtifactKey)
      */
     @Override
@@ -636,8 +664,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see
-     * com.ericsson.apex.service.engine.runtime.EngineService#getRuntimeInfo(com.ericsson.apex.core.
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#getRuntimeInfo(org.onap.policy.apex.core.
      * model.concepts.AxArtifactKey)
      */
     @Override
@@ -655,8 +682,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
     /*
      * (non-Javadoc)
      *
-     * @see
-     * com.ericsson.apex.service.engine.runtime.EngineServiceEventInterface#sendEvent(com.ericsson.
+     * @see org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface#sendEvent(org.onap.policy.
      * apex.service.engine.event.ApexEvent)
      */
     @Override
@@ -664,7 +690,7 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven
         // Check if we have this key on our map
         if (getState() == AxEngineState.STOPPED) {
             LOGGER.warn("event " + event.getName() + " not processed, no engines on engine service "
-                    + engineServiceKey.getId() + " are running");
+                            + engineServiceKey.getId() + " are running");
             return;
         }
 
index be294aa..b9a405b 100644 (file)
 
 package org.onap.policy.apex.service.engine.runtime.impl;
 
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
+
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.util.Arrays;
@@ -55,11 +60,6 @@ import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface;
 import org.slf4j.ext.XLogger;
 import org.slf4j.ext.XLoggerFactory;
 
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonParser;
-
 /**
  * The Class EngineWorker encapsulates a core {@link ApexEngine} instance, which runs policies
  * defined in the {@link org.onap.policy.apex.model.basicmodel.concepts.AxModelAxModel}. Each policy
@@ -283,6 +283,7 @@ final class EngineWorker implements EngineService {
         }
 
         // Update the Apex model in the Apex engine
+        engine.clear();
         engine.updateModel(apexModel);
 
         LOGGER.debug("engine model {} added to the engine-{}", apexModel.getKey().getId(), engineWorkerKey);
@@ -394,9 +395,54 @@ final class EngineWorker implements EngineService {
 
         // Interrupt the thread that is handling events toward the engine
         processorThread.interrupt();
+        processorThread = null;
 
         // Stop the engine
         engine.stop();
+
+        LOGGER.exit(engineKey);
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see org.onap.policy.apex.service.engine.runtime.EngineService#clear()
+     */
+    @Override
+    public void clear() throws ApexException {
+        clear(this.getKey());
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see
+     * org.onap.policy.apex.service.engine.runtime.EngineService#clear(org.onap.policy.apex.core.
+     * model. concepts.AxArtifactKey)
+     */
+    @Override
+    public void clear(final AxArtifactKey engineKey) throws ApexException {
+        // Check if the key on the start request is correct
+        if (!engineWorkerKey.equals(engineKey)) {
+            LOGGER.warn("engine key " + engineKey.getId() + " does not match the key" + engineWorkerKey.getId()
+                    + " of this engine");
+            throw new ApexException("engine key " + engineKey.getId() + " does not match the key"
+                    + engineWorkerKey.getId() + " of this engine");
+        }
+
+        if (engine == null) {
+            LOGGER.error("apex engine for engine key" + engineWorkerKey.getId() + " null");
+            throw new ApexException("apex engine for engine key" + engineWorkerKey.getId() + " null");
+        }
+
+        // Interrupt the worker to stop its thread
+        if (processorThread != null && !processorThread.isAlive()) {
+            LOGGER.warn("apex engine for engine key" + engineWorkerKey.getId() + " is not stopped with state "
+                    + getState());
+            return;
+        }
+
+        // Clear the engine
         engine.clear();
 
         LOGGER.exit(engineKey);
index d68576d..aa7b561 100644 (file)
@@ -86,12 +86,8 @@ public class EventAlbumContextTest {
         final String[] cliArgs = new String[] { "-c", tempCommandFile.getCanonicalPath(), "-l",
                         tempLogFile.getAbsolutePath(), "-o", tempModelFile.getAbsolutePath() };
 
-        ModelService.clear();
-
         new ApexCLIEditorMain(cliArgs);
 
-        ModelService.clear();
-
         final String[] args = new String[] { "-m", tempModelFile.getAbsolutePath(), "-c", configFile };
         final ApexMain apexMain = new ApexMain(args);
 
@@ -111,7 +107,6 @@ public class EventAlbumContextTest {
 
         // Shut down Apex
         apexMain.shutdown();
-        ParameterService.clear();
 
         assertTrue("Test failed, the output event file was not created", outputEventFile.exists());
         outputEventFile.delete();