/**
   * Returns the list of volumes as if they were fetched from glusterfs. Changes from existing
   * volumes are:<br>
   * - existingDistVol not fetched (means it was removed from gluster cli, and should be removed
   * from db<br>
   * - option 'auth.allow' removed from the existingReplVol<br>
   * - new option 'auth.reject' added to existingReplVol<br>
   * - value of option 'nfs.disable' changed from 'off' ot 'on' in existingReplVol<br>
   * - new volume test-new-vol fetched from gluster (means it was added from gluster cli, and should
   * be added to db<br>
   */
  private Map<Guid, GlusterVolumeEntity> getFetchedVolumesList() {
    Map<Guid, GlusterVolumeEntity> volumes = new HashMap<>();

    GlusterVolumeEntity fetchedReplVol = createReplVol();
    fetchedReplVol.removeOption(OPTION_AUTH_ALLOW); // option removed
    fetchedReplVol.setOption(OPTION_AUTH_REJECT, AUTH_REJECT_IP); // added
    fetchedReplVol.setOption(OPTION_NFS_DISABLE, OPTION_VALUE_ON); // changed

    // brick changes
    removedBrickIds.add(
        GlusterCoreUtil.findBrick(existingReplVol.getBricks(), SERVER_ID_1, REPL_BRICK_R1D1)
            .getId());
    removedBrickIds.add(
        GlusterCoreUtil.findBrick(existingReplVol.getBricks(), SERVER_ID_1, REPL_BRICK_R2D1)
            .getId());

    GlusterBrickEntity brickToReplace =
        GlusterCoreUtil.findBrick(fetchedReplVol.getBricks(), SERVER_ID_1, REPL_BRICK_R1D1);
    replaceBrick(brickToReplace, SERVER_ID_1, REPL_BRICK_R1D1_NEW);

    brickToReplace =
        GlusterCoreUtil.findBrick(fetchedReplVol.getBricks(), SERVER_ID_1, REPL_BRICK_R2D1);
    replaceBrick(brickToReplace, SERVER_ID_1, REPL_BRICK_R2D1_NEW);
    volumes.put(fetchedReplVol.getId(), fetchedReplVol);

    // add a new volume
    newVolume = getNewVolume();
    volumes.put(newVolume.getId(), newVolume);

    return volumes;
  }
  private GlusterVolumeAdvancedDetails getVolumeAdvancedDetails(GlusterVolumeEntity volume) {
    GlusterVolumeAdvancedDetails volDetails = new GlusterVolumeAdvancedDetails();
    GlusterVolumeSizeInfo capacityInfo = new GlusterVolumeSizeInfo();
    capacityInfo.setVolumeId(volume.getId());
    capacityInfo.setTotalSize(600000L);
    capacityInfo.setFreeSize(200000L);
    capacityInfo.setUsedSize(400000L);
    volDetails.setCapacityInfo(capacityInfo);

    List<BrickDetails> brickDetailsList = new ArrayList<>();
    for (GlusterBrickEntity brick : volume.getBricks()) {
      BrickDetails brickDetails = new BrickDetails();
      BrickProperties properties = new BrickProperties();
      properties.setBrickId(brick.getId());
      brickDetails.setBrickProperties(properties);
      properties.setStatus(brick.getStatus());
      if (volume == existingReplVol) {
        if (brick.getServerId().equals(SERVER_ID_1)
            && (brick.getBrickDirectory().equals(REPL_BRICK_R1D1)
                || brick.getBrickDirectory().equals(REPL_BRICK_R2D1))) {
          properties.setStatus(GlusterStatus.DOWN);
          bricksWithChangedStatus.add(brick);
        }
      }
      brickDetailsList.add(brickDetails);
    }
    volDetails.setBrickDetails(brickDetailsList);

    return volDetails;
  }
  private void onResetAllParameters() {
    ConfirmationModel model = (ConfirmationModel) getWindow();

    if (model.getProgress() != null) {
      return;
    }

    if (getEntity() == null) {
      return;
    }
    GlusterVolumeEntity volume = getEntity();

    ResetGlusterVolumeOptionsParameters parameters =
        new ResetGlusterVolumeOptionsParameters(volume.getId(), null, false);

    model.startProgress();

    Frontend.getInstance()
        .runAction(
            VdcActionType.ResetGlusterVolumeOptions,
            parameters,
            new IFrontendActionAsyncCallback() {

              @Override
              public void executed(FrontendActionAsyncResult result) {
                ConfirmationModel localModel = (ConfirmationModel) result.getState();
                localModel.stopProgress();
                cancel();
              }
            },
            model);
  }
  private void onSetParameter() {
    if (getEntity() == null) {
      return;
    }

    GlusterVolumeEntity volume = getEntity();

    VolumeParameterModel model = (VolumeParameterModel) getWindow();

    if (!model.validate()) {
      return;
    }

    GlusterVolumeOptionEntity option = new GlusterVolumeOptionEntity();
    option.setVolumeId(volume.getId());
    option.setKey(model.getSelectedKey().getEntity());
    option.setValue(model.getValue().getEntity());

    model.startProgress();

    Frontend.getInstance()
        .runAction(
            VdcActionType.SetGlusterVolumeOption,
            new GlusterVolumeOptionParameters(option),
            new IFrontendActionAsyncCallback() {

              @Override
              public void executed(FrontendActionAsyncResult result) {
                VolumeParameterListModel localModel = (VolumeParameterListModel) result.getState();
                localModel.postOnSetParameter(result.getReturnValue());
              }
            },
            this);
  }
  /**
   * Sets all options of a volume by invoking the action {@link
   * VdcActionType#SetGlusterVolumeOption} in a loop. <br>
   * Errors if any are collected and added to "executeFailedMessages"
   *
   * @param volume
   */
  private void setVolumeOptions(GlusterVolumeEntity volume) {
    List<String> errors = new ArrayList<String>();
    for (GlusterVolumeOptionEntity option : volume.getOptions()) {
      // make sure that volume id is set
      option.setVolumeId(volume.getId());
      VdcReturnValueBase setOptionReturnValue =
          runBllAction(
              VdcActionType.SetGlusterVolumeOption, new GlusterVolumeOptionParameters(option));
      if (!getSucceeded()) {
        errors.addAll(setOptionReturnValue.getCanDoActionMessages());
        errors.addAll(setOptionReturnValue.getExecuteFailedMessages());
      }
    }

    if (!errors.isEmpty()) {
      handleVdsErrors(AuditLogType.GLUSTER_VOLUME_OPTION_SET_FAILED, errors);
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see org.ovirt.engine.core.bll.CommandBase#executeCommand()
   */
  @Override
  protected void executeCommand() {
    // set the gluster volume name for audit purpose
    setGlusterVolumeName(volume.getName());

    if (volume.getTransportTypes() == null || volume.getTransportTypes().isEmpty()) {
      volume.addTransportType(TransportType.TCP);
    }

    // GLUSTER access protocol is enabled by default
    volume.addAccessProtocol(AccessProtocol.GLUSTER);
    if (!volume.getAccessProtocols().contains(AccessProtocol.NFS)) {
      volume.disableNFS();
    }

    VDSReturnValue returnValue =
        runVdsCommand(
            VDSCommandType.CreateGlusterVolume,
            new CreateGlusterVolumeVDSParameters(upServer.getId(), volume));
    setSucceeded(returnValue.getSucceeded());

    if (!getSucceeded()) {
      handleVdsError(
          AuditLogType.GLUSTER_VOLUME_CREATE_FAILED, returnValue.getVdsError().getMessage());
      return;
    }

    // Volume created successfully. Insert it to database.
    GlusterVolumeEntity createdVolume = (GlusterVolumeEntity) returnValue.getReturnValue();
    setVolumeType(createdVolume);
    setBrickOrder(createdVolume.getBricks());
    addVolumeToDb(createdVolume);

    // set all options of the volume
    setVolumeOptions(createdVolume);

    getReturnValue().setActionReturnValue(createdVolume.getId());
  }