First part of onap rename
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / openecomp / appc / oam / processor / BaseActionRunnable.java
index 5df2c80..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,17 +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;
@@ -65,32 +66,53 @@ abstract class BaseActionRunnable extends BaseCommon implements Runnable {
     BaseProcessor myParent;
     Map<String, Future<?>> bundleNameToFuture = new HashMap<>();
 
+    /**
+     * Constructor
+     *
+     * @param parent BaseProcessor who has called this constructor.
+     */
     BaseActionRunnable(BaseProcessor parent) {
         super(parent.logger, parent.configurationHelper, parent.stateHelper, parent.operationHelper);
 
         rpc = parent.rpc;
         commonHeader = parent.commonHeader;
-        startTime = parent.startTime;
         myParent = parent;
-
         setTimeoutValues();
     }
 
+    /**
+     * 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
@@ -98,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()) {
@@ -120,19 +142,29 @@ abstract class BaseActionRunnable extends BaseCommon implements Runnable {
         }
     }
 
+    /**
+     * Keep waiting to be override by children classes for different behaviors.
+     * Timeout is validated here.
+     */
     void keepWaiting() {
         logDebug(String.format("%s runnable waiting, current state is %s.",
-                actionName, currentState == null ? "null" : currentState.toString()));
+            actionName, stateHelper.getCurrentOamState()));
 
         isTimeout("keepWaiting");
     }
 
+    /**
+     * Check if the timeout milliseconds has reached.
+     *
+     * @param parentName String of the caller, for logging purpose.
+     * @return true if the timeout has reached, otherwise false.
+     */
     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);
@@ -142,14 +174,32 @@ abstract class BaseActionRunnable extends BaseCommon implements Runnable {
     }
 
     /**
-     * Set class <b>status</b> to REJECTED with abort message.
+     * Check if all bundle operations are successful through BundleHelper.
+     * If there's failed bundler operation, set error status and trigger postAction with Error state.
+     *
+     * @return true if bundler operations have failure, otherwise false.
+     */
+    boolean hasBundleOperationFailure() {
+        long failedTask = myParent.bundleHelper.getFailedMetrics(bundleNameToFuture);
+        if (failedTask == 0) {
+            return false;
+        }
+
+        setStatus(OAMCommandStatus.UNEXPECTED_ERROR, String.format(BUNDLE_OPERATION_FAILED_FORMAT, failedTask));
+        postAction(AppcOamStates.Error);
+        return true;
+    }
+
+    /**
+     * 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));
     }
 
     /**
      * Final handling. The thread is cancelled.
+     *
      * @param setState boolean to indicate if set OAM state or not
      */
     void postDoAction(boolean setState) {
@@ -157,7 +207,8 @@ abstract class BaseActionRunnable extends BaseCommon implements Runnable {
     }
 
     /**
-     * Handling for after doAction. does post notification,  issue audit log and set OAM state based on input
+     * Handling for after doAction. does post notification, issue audit log and set OAM state based on input.
+     *
      * @param state of AppcOamState to be set as OAM state when it is not null.
      */
     void postAction(AppcOamStates state) {
@@ -174,6 +225,7 @@ abstract class BaseActionRunnable extends BaseCommon implements Runnable {
 
     /**
      * Check state
+     *
      * @return true if final state reached, otherwise return false
      */
     boolean checkState() {
@@ -186,16 +238,11 @@ abstract class BaseActionRunnable extends BaseCommon implements Runnable {
             return false;
         }
 
-        long failedTask = myParent.bundleHelper.getFailedMetrics(bundleNameToFuture);
-        if (failedTask != 0) {
-            String errorMsg = failedTask + " bundle(s) failed, see logs for details.";
-            setStatus(OAMCommandStatus.UNEXPECTED_ERROR, errorMsg);
-            postAction(AppcOamStates.Error);
+        if (hasBundleOperationFailure()) {
             return true;
         }
 
-        currentState = stateHelper.getBundlesState();
-        if (currentState == finalState) {
+        if (stateHelper.getBundlesState() == finalState) {
             setStatus(OAMCommandStatus.SUCCESS);
             postDoAction(true);
             return true;