@Override
    public FenceOperationResult createActionResult(List<FenceOperationResult> taskResults) {
      int skippedDueToPolicy = 0;
      FenceOperationResult skippedDueToPolicyResult = null;
      for (FenceOperationResult result : taskResults) {
        if (result.getStatus() == Status.SKIPPED_DUE_TO_POLICY) {
          skippedDueToPolicy++;
          skippedDueToPolicyResult = result;
        } else if (result.getStatus() != Status.SUCCESS) {
          // stop action on one agent failed, so the whole action has to fail also
          return result;
        }
      }

      if (skippedDueToPolicy == taskResults.size()) {
        // all agents reported skipped due to policy, return it as a whole operation result
        return skippedDueToPolicyResult;
      } else if (skippedDueToPolicy > 0) {
        // only some agents reported skipped due to policy, return error
        return new FenceOperationResult(
            Status.ERROR,
            PowerStatus.UNKNOWN,
            "Fence action was skipped due to fencing policy only on some of fence agents, but not all");
      }

      // all tasks returned status off, so the whole action is successful
      return new FenceOperationResult(Status.SUCCESS, PowerStatus.OFF);
    }
    @Override
    public FenceOperationResult createActionResult(List<FenceOperationResult> taskResults) {
      for (FenceOperationResult result : taskResults) {
        if (result.getPowerStatus() == PowerStatus.ON) {
          return result;
        }
      }

      // no task reported status ON, so whole start operation has to fail
      return new FenceOperationResult(Status.ERROR, PowerStatus.UNKNOWN);
    }
  /** Test execution of stop action when host is powered down */
  @Test
  public void stopHostWhichIsPoweredDown() {
    setupCommand(setupCommandParams(FenceActionType.STOP));
    setupBrokerResult(
        createBrokerResultMap(
            0, "", "off", null)); // result of STATUS action executed prior to STOP action

    command.execute();
    FenceOperationResult result =
        (FenceOperationResult) command.getVDSReturnValue().getReturnValue();

    assertEquals(Status.SKIPPED_ALREADY_IN_STATUS, result.getStatus());
    assertEquals(PowerStatus.UNKNOWN, result.getPowerStatus());
  }
  /**
   * Test execution of stop action when fencing policy forbids stopping host still connected to
   * storage, so stopping the host is skipped
   */
  @Test
  public void stopHostSkippedDueToFencingPolicy() {
    setupCommand(setupCommandParams(FenceActionType.STOP));
    setupBrokerResult(
        createBrokerResultMap(
            0, "", "on", null), // result of STATUS action executed prior to STOP action
        createBrokerResultMap(0, "", "unknown", "skipped")); // result of actual STOP action

    command.execute();
    FenceOperationResult result =
        (FenceOperationResult) command.getVDSReturnValue().getReturnValue();

    assertEquals(Status.SKIPPED_DUE_TO_POLICY, result.getStatus());
    assertEquals(PowerStatus.UNKNOWN, result.getPowerStatus());
  }
  /** Test execution of stop action when host power status cannot be determined */
  @Test
  public void stopHostWithUnknownPowerStatus() {
    setupCommand(setupCommandParams(FenceActionType.STOP));
    setupBrokerResult(
        createBrokerResultMap(
            1, "", "unknown", null), // result of STATUS action executed prior to STOP action
        createBrokerResultMap(0, "", "unknown", "initiated")); // result of actual STOP action

    command.execute();
    FenceOperationResult result =
        (FenceOperationResult) command.getVDSReturnValue().getReturnValue();

    assertEquals(Status.SUCCESS, result.getStatus());
    assertEquals(PowerStatus.UNKNOWN, result.getPowerStatus());
  }
  /** Tests result of failed get power status of host */
  @Test
  public void failedGetStatus() {
    String agentMessage = "Test failed, status unknown";

    setupCommand(setupCommandParams(FenceActionType.STATUS));
    setupBrokerResult(createBrokerResultMap(1, agentMessage, "unknown", null));

    command.execute();
    FenceOperationResult result =
        (FenceOperationResult) command.getVDSReturnValue().getReturnValue();

    assertEquals(Status.ERROR, result.getStatus());
    assertEquals(PowerStatus.UNKNOWN, result.getPowerStatus());
    assertEquals(agentMessage, result.getMessage());
  }
  /** Tests result of successful get power status of host */
  @Test
  public void successfulGetStatus() {
    String agentMessage = "Test succeeded: on";

    setupCommand(setupCommandParams(FenceActionType.STATUS));
    setupBrokerResult(createBrokerResultMap(0, agentMessage, "on", null));

    command.execute();
    FenceOperationResult result =
        (FenceOperationResult) command.getVDSReturnValue().getReturnValue();

    assertEquals(Status.SUCCESS, result.getStatus());
    assertEquals(PowerStatus.ON, result.getPowerStatus());
    assertEquals(agentMessage, result.getMessage());
  }
    @Override
    public FenceOperationResult createActionResult(List<FenceOperationResult> taskResults) {
      FenceOperationResult successfulResult = null;
      int statusOffReported = 0;
      int errorReported = 0;
      for (FenceOperationResult result : taskResults) {
        if (result.getPowerStatus() == PowerStatus.ON) {
          // one task reported power status on, so the host should be on
          return result;
        }

        if (result.getStatus() == Status.SUCCESS) {
          // save successful task result, so we know that at least one status attempt was successful
          // with
          // power status off, so we can return it
          successfulResult = result;
          if (result.getPowerStatus() == PowerStatus.OFF) {
            // note that we received off status
            statusOffReported++;
          }
        } else {
          errorReported++;
        }
      }

      if (statusOffReported > 0) {
        if (errorReported > 0) {
          // we received at least one error and at least one power off status reported, so we cannot
          // determine
          // if host is really powered off
          return new FenceOperationResult(
              Status.ERROR,
              PowerStatus.UNKNOWN,
              String.format(
                  "Unable to determine host '%s' power status: at least one agent failed to get"
                      + "status and at least one agent reported host is powered off.",
                  fencedHost));
        } else {
          // no errors received, report successful result
          return successfulResult;
        }
      }

      // all tasks returned error, so the whole status action failed
      return new FenceOperationResult(
          Status.ERROR, PowerStatus.UNKNOWN, "All agents failed to get host power status.");
    }
 /**
  * If at least one agent reports status {@code PowerStatus.ON}, the the host is on (so the goal
  * of start action is reached), otherwise and we have to continue with processing
  */
 @Override
 public boolean isGoalReached(FenceOperationResult result) {
   return result.getPowerStatus() == PowerStatus.ON;
 }