First part of onap rename
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / openecomp / appc / oam / processor / BaseActionRunnable.java
index 2ffad69..9273346 100644 (file)
  * ============LICENSE_END=========================================================
  */
 
-package org.openecomp.appc.oam.processor;
+package org.onap.appc.oam.processor;
 
-import org.openecomp.appc.i18n.Msg;
-import org.openecomp.appc.oam.OAMCommandStatus;
-import org.openecomp.appc.statemachine.impl.readers.AppcOamStates;
+import org.onap.appc.i18n.Msg;
+import org.onap.appc.oam.AppcOam;
+import org.onap.appc.oam.OAMCommandStatus;
+import org.onap.appc.statemachine.impl.readers.AppcOamStates;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -43,19 +44,17 @@ import java.util.concurrent.Future;
  *   <br>  - auditMsg
  *   <br>  - finalState
  */
-abstract class BaseActionRunnable extends BaseCommon implements Runnable {
-    final String OAM_OPERATION_TIMEOUT_SECOND = "appc.OAM.api.timeout";
-    /** Default operation tiemout set to 1 minute */
-    final int DEFAULT_OAM_OPERATION_TIMEOUT = 60;
-    /** Abort message format with flexible operation name */
-    final String ABORT_MESSAGE_FORMAT = "Aborting %s operation.";
+public abstract class BaseActionRunnable extends BaseCommon implements Runnable {
+    /** Abort due to rejection message format with flexible operation name */
+    final String ABORT_MESSAGE_FORMAT = "Aborting %s operation due to %s.";
     /** Timeout message format with flexible operation name */
     final String TIMEOUT_MESSAGE_FORMAT = "%s operation has reached timeout %d milliseconds.";
     /** Failure message format with flexible number of bundles */
     final String BUNDLE_OPERATION_FAILED_FORMAT = "%d bundle(s) failed, see logs for details.";
+    final String NEW_RPC_OPERATION_REQUEST = "new %s operation request";
+    final String DUE_TO_EXECUTION_ERROR = "due to execution error.";
 
     private boolean isWaiting = false;
-    private AppcOamStates currentState;
     long startTimeMs = 0;
     long timeoutMs = 0;
     boolean doTimeoutChecking = false;
@@ -77,30 +76,43 @@ abstract class BaseActionRunnable extends BaseCommon implements Runnable {
 
         rpc = parent.rpc;
         commonHeader = parent.commonHeader;
-        startTime = parent.startTime;
         myParent = parent;
-
         setTimeoutValues();
     }
 
     /**
-     * Set timeout in milliseconds
+     * Collect the timeout value for this {@link BaseActionRunnable}
      */
     void setTimeoutValues() {
-        Integer timeoutSeconds = myParent.timeoutSeconds;
-        if (timeoutSeconds == null) {
-            timeoutMs = configurationHelper.getConfig().getIntegerProperty(
-                    OAM_OPERATION_TIMEOUT_SECOND, DEFAULT_OAM_OPERATION_TIMEOUT) * 1000;
-        } else {
-            timeoutMs = timeoutSeconds.longValue() * 1000;
-        }
-
+        startTime = myParent.startTime;
+        timeoutMs = myParent.getTimeoutMilliseconds();
         doTimeoutChecking = timeoutMs != 0;
         if (doTimeoutChecking) {
             startTimeMs = startTime.getTime();
         }
         logDebug("%s action runnable check timeout (%s) with timeout (%d)ms, and startMs (%d)",
-                rpc.name(), Boolean.toString(doTimeoutChecking), timeoutMs, startTimeMs);
+            rpc.name(), Boolean.toString(doTimeoutChecking), timeoutMs, startTimeMs);
+    }
+
+
+    /**
+     * Abort operation handling due to outside interruption, does<br>
+     *     - set ABORT status<br>
+     *     - send notification message<br>
+     *     - add audit log
+     *
+     * @param newRpc of the new AppcOam.RPC operation.
+     */
+    void abortRunnable(final AppcOam.RPC newRpc) {
+        resetLogProperties(false);
+
+        String additionalMsg = String.format(NEW_RPC_OPERATION_REQUEST, newRpc);
+        logDebug("%s action aborted due to %s", rpc.name(), additionalMsg);
+        setStatus(OAMCommandStatus.ABORT, String.format(ABORT_MESSAGE_FORMAT, rpc.name(), additionalMsg));
+        operationHelper.sendNotificationMessage(rpc, commonHeader, status);
+        auditInfoLog(auditMsg);
+
+        resetLogProperties(true);
     }
 
     @Override
@@ -108,7 +120,7 @@ abstract class BaseActionRunnable extends BaseCommon implements Runnable {
         try {
             setInitialLogProperties();
             logDebug(String.format("===========in %s run (waiting: %s)=======",
-                    actionName, Boolean.toString(isWaiting)));
+                actionName, Boolean.toString(isWaiting)));
 
             if (isWaiting) {
                 if (!checkState()) {
@@ -136,7 +148,7 @@ abstract class BaseActionRunnable extends BaseCommon implements Runnable {
      */
     void keepWaiting() {
         logDebug(String.format("%s runnable waiting, current state is %s.",
-                actionName, currentState == null ? "null" : currentState.toString()));
+            actionName, stateHelper.getCurrentOamState()));
 
         isTimeout("keepWaiting");
     }
@@ -150,9 +162,9 @@ abstract class BaseActionRunnable extends BaseCommon implements Runnable {
     boolean isTimeout(String parentName) {
         logDebug(String.format("%s task isTimeout called from %s", actionName, parentName));
         if (doTimeoutChecking
-                && System.currentTimeMillis() - startTimeMs > timeoutMs) {
+            && System.currentTimeMillis() - startTimeMs > timeoutMs) {
             logger.error(String.format("%s operation timeout (%d) ms has reached, abort with error state.",
-                    actionName, timeoutMs));
+                actionName, timeoutMs));
 
             setStatus(OAMCommandStatus.TIMEOUT, String.format(TIMEOUT_MESSAGE_FORMAT, rpc.name(), timeoutMs));
             postAction(AppcOamStates.Error);
@@ -179,10 +191,10 @@ abstract class BaseActionRunnable extends BaseCommon implements Runnable {
     }
 
     /**
-     * Set class <b>status</b> to REJECTED with abort message.
+     * Set class <b>status</b> to ABORT with abort message.
      */
     void setAbortStatus() {
-        setStatus(OAMCommandStatus.REJECTED, String.format(ABORT_MESSAGE_FORMAT, rpc.name()));
+        setStatus(OAMCommandStatus.ABORT, String.format(ABORT_MESSAGE_FORMAT, rpc.name(), DUE_TO_EXECUTION_ERROR));
     }
 
     /**
@@ -230,8 +242,7 @@ abstract class BaseActionRunnable extends BaseCommon implements Runnable {
             return true;
         }
 
-        currentState = stateHelper.getBundlesState();
-        if (currentState == finalState) {
+        if (stateHelper.getBundlesState() == finalState) {
             setStatus(OAMCommandStatus.SUCCESS);
             postDoAction(true);
             return true;