/*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modification Copyright (C) 2019 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- * 
+ *
  * SPDX-License-Identifier: Apache-2.0
  * ============LICENSE_END=========================================================
  */
 import java.util.Arrays;
 import java.util.Base64;
 import java.util.Map.Entry;
-
+import lombok.Getter;
+import lombok.Setter;
 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
 import org.onap.policy.apex.service.parameters.ApexParameterHandler;
 import org.onap.policy.apex.service.parameters.ApexParameters;
     // The parameters read in from JSON
     private ApexParameters parameters;
 
+    @Getter
+    @Setter(lombok.AccessLevel.PRIVATE)
+    private volatile boolean alive = false;
+
     /**
      * Instantiates the Apex service.
      *
         // Start the activator
         try {
             activator.initialize();
+            setAlive(true);
         } catch (final ApexActivatorException e) {
             LOGGER.error("start of Apex service failed, used parameters are " + Arrays.toString(args), e);
             return;
         if (activator != null) {
             activator.terminate();
         }
+        setAlive(false);
     }
 
     /**
             try {
                 // Shutdown the Apex engine and wait for everything to stop
                 activator.terminate();
+                setAlive(false);
             } catch (final ApexException e) {
                 LOGGER.warn("error occured during shut down of the Apex service", e);
             }
 
 package org.onap.policy.apex.services.onappf.handler;
 
 import java.util.List;
-
 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
 import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher;
 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
                     final ApexEngineHandler apexEngineHandler =
                             new ApexEngineHandler(policies.get(0).getProperties().get("content"));
                     Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
-                    pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
+                    if (apexEngineHandler.isApexEngineRunning()) {
+                        pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
                             PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active.");
-                    pdpStatusContext.setState(PdpState.ACTIVE);
+                        pdpStatusContext.setState(PdpState.ACTIVE);
+                    } else {
+                        pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
+                            PdpResponseStatus.FAIL, "Apex engine failed to start. State cannot be changed to active.");
+                    }
                 } catch (final ApexStarterException e) {
                     LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e);
                     pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(),
 
 package org.onap.policy.apex.services.onappf.handler;
 
 import java.util.List;
-
 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
 import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher;
 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
             LOGGER.debug("ApenEngineHandler not in registry.", e);
         }
         if (pdpUpdateMsg.getPolicies().isEmpty()) {
-            if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
-                try {
-                    apexEngineHandler.shutdown();
-                } catch (final ApexStarterException e) {
-                    LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e);
-                    pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
-                            PdpResponseStatus.FAIL, "Pdp update failed as the policies couldn't be undeployed.");
-                }
-            }
+            pdpResponseDetails = stopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
         } else {
+            pdpResponseDetails = startApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
+        }
+        return pdpResponseDetails;
+    }
+
+    private PdpResponseDetails stopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
+        final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
+        PdpResponseDetails pdpResponseDetails = null;
+        if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
             try {
-                if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
-                    apexEngineHandler.shutdown();
-                }
-                apexEngineHandler =
-                        new ApexEngineHandler(pdpUpdateMsg.getPolicies().get(0).getProperties().get("content"));
-                Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
-                pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
-                        PdpResponseStatus.SUCCESS, "Apex engine started and policies are running.");
+                apexEngineHandler.shutdown();
             } catch (final ApexStarterException e) {
-                LOGGER.error("Apex engine service running failed. ", e);
+                LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e);
+                pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
+                        PdpResponseStatus.FAIL, "Pdp update failed as the policies couldn't be undeployed.");
+            }
+        }
+        return pdpResponseDetails;
+    }
+
+    private PdpResponseDetails startApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg,
+        final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) {
+        PdpResponseDetails pdpResponseDetails = null;
+        try {
+            if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
+                apexEngineHandler.shutdown();
+            }
+            apexEngineHandler =
+                    new ApexEngineHandler(pdpUpdateMsg.getPolicies().get(0).getProperties().get("content"));
+            Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
+            if (apexEngineHandler.isApexEngineRunning()) {
+                pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
+                    PdpResponseStatus.SUCCESS, "Apex engine started and policies are running.");
+            } else {
                 pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
-                        PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
+                    PdpResponseStatus.FAIL, "Apex engine failed to start.");
             }
+        } catch (final ApexStarterException e) {
+            LOGGER.error("Apex engine service running failed. ", e);
+            pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
+                    PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage());
         }
         return pdpResponseDetails;
     }