Program: 105150
Project: 105150
Buggy source:
private VolumeVO sendAttachVolumeCommand(UserVmVO vm,VolumeVO volumeToAttach,Long deviceId){
  String errorMsg="String_Node_Str" + volumeToAttach.getName() + "String_Node_Str"+ vm.getHostName();
  boolean sendCommand=vm.getState() == State.Running;
  AttachAnswer answer=null;
  Long hostId=vm.getHostId();
  if (hostId == null) {
    hostId=vm.getLastHostId();
    HostVO host=_hostDao.findById(hostId);
    if (host != null && host.getHypervisorType() == HypervisorType.VMware) {
      sendCommand=true;
    }
  }
  HostVO host=null;
  StoragePoolVO volumeToAttachStoragePool=_storagePoolDao.findById(volumeToAttach.getPoolId());
  if (hostId != null) {
    host=_hostDao.findById(hostId);
    if (host != null && host.getHypervisorType() == HypervisorType.XenServer && volumeToAttachStoragePool != null && volumeToAttachStoragePool.isManaged()) {
      sendCommand=true;
    }
  }
  DataStore dataStore=volumeToAttachStoragePool != null ? dataStoreMgr.getDataStore(volumeToAttachStoragePool.getId(),DataStoreRole.Primary) : null;
  if (host != null) {
    try {
      volService.grantAccess(volFactory.getVolume(volumeToAttach.getId()),host,dataStore);
    }
 catch (    Exception e) {
      volService.revokeAccess(volFactory.getVolume(volumeToAttach.getId()),host,dataStore);
      throw new CloudRuntimeException(e.getMessage());
    }
  }
  if (sendCommand) {
    if (host != null && host.getHypervisorType() == HypervisorType.KVM && volumeToAttachStoragePool.isManaged() && volumeToAttach.getPath() == null) {
      volumeToAttach.setPath(volumeToAttach.get_iScsiName());
      _volsDao.update(volumeToAttach.getId(),volumeToAttach);
    }
    DataTO volTO=volFactory.getVolume(volumeToAttach.getId()).getTO();
    deviceId=getDeviceId(vm.getId(),deviceId);
    DiskTO disk=new DiskTO(volTO,deviceId,volumeToAttach.getPath(),volumeToAttach.getVolumeType());
    AttachCommand cmd=new AttachCommand(disk,vm.getInstanceName());
    ChapInfo chapInfo=volService.getChapInfo(volFactory.getVolume(volumeToAttach.getId()),dataStore);
    Map<String,String> details=new HashMap<String,String>();
    disk.setDetails(details);
    details.put(DiskTO.MANAGED,String.valueOf(volumeToAttachStoragePool.isManaged()));
    details.put(DiskTO.STORAGE_HOST,volumeToAttachStoragePool.getHostAddress());
    details.put(DiskTO.STORAGE_PORT,String.valueOf(volumeToAttachStoragePool.getPort()));
    details.put(DiskTO.VOLUME_SIZE,String.valueOf(volumeToAttach.getSize()));
    details.put(DiskTO.IQN,volumeToAttach.get_iScsiName());
    details.put(DiskTO.MOUNT_POINT,volumeToAttach.get_iScsiName());
    details.put(DiskTO.PROTOCOL_TYPE,(volumeToAttach.getPoolType() != null) ? volumeToAttach.getPoolType().toString() : null);
    if (chapInfo != null) {
      details.put(DiskTO.CHAP_INITIATOR_USERNAME,chapInfo.getInitiatorUsername());
      details.put(DiskTO.CHAP_INITIATOR_SECRET,chapInfo.getInitiatorSecret());
      details.put(DiskTO.CHAP_TARGET_USERNAME,chapInfo.getTargetUsername());
      details.put(DiskTO.CHAP_TARGET_SECRET,chapInfo.getTargetSecret());
    }
    _userVmDao.loadDetails(vm);
    Map<String,String> controllerInfo=new HashMap<String,String>();
    controllerInfo.put(VmDetailConstants.ROOT_DISK_CONTROLLER,vm.getDetail(VmDetailConstants.ROOT_DISK_CONTROLLER));
    controllerInfo.put(VmDetailConstants.DATA_DISK_CONTROLLER,vm.getDetail(VmDetailConstants.DATA_DISK_CONTROLLER));
    cmd.setControllerInfo(controllerInfo);
    s_logger.debug("String_Node_Str" + volumeToAttach.getId() + "String_Node_Str"+ vm.getId()+ "String_Node_Str"+ controllerInfo);
    try {
      answer=(AttachAnswer)_agentMgr.send(hostId,cmd);
    }
 catch (    Exception e) {
      if (host != null) {
        volService.revokeAccess(volFactory.getVolume(volumeToAttach.getId()),host,dataStore);
      }
      throw new CloudRuntimeException(errorMsg + "String_Node_Str" + e.getMessage());
    }
  }
  if (!sendCommand || (answer != null && answer.getResult())) {
    if (sendCommand) {
      DiskTO disk=answer.getDisk();
      _volsDao.attachVolume(volumeToAttach.getId(),vm.getId(),disk.getDiskSeq());
      volumeToAttach=_volsDao.findById(volumeToAttach.getId());
      if (volumeToAttachStoragePool.isManaged() && volumeToAttach.getPath() == null) {
        volumeToAttach.setPath(answer.getDisk().getPath());
        _volsDao.update(volumeToAttach.getId(),volumeToAttach);
      }
    }
 else {
      deviceId=getDeviceId(vm.getId(),deviceId);
      _volsDao.attachVolume(volumeToAttach.getId(),vm.getId(),deviceId);
    }
    VmDiskStatisticsVO diskstats=_vmDiskStatsDao.findBy(vm.getAccountId(),vm.getDataCenterId(),vm.getId(),volumeToAttach.getId());
    if (diskstats == null) {
      diskstats=new VmDiskStatisticsVO(vm.getAccountId(),vm.getDataCenterId(),vm.getId(),volumeToAttach.getId());
      _vmDiskStatsDao.persist(diskstats);
    }
    return _volsDao.findById(volumeToAttach.getId());
  }
 else {
    if (answer != null) {
      String details=answer.getDetails();
      if (details != null && !details.isEmpty()) {
        errorMsg+="String_Node_Str" + details;
      }
    }
    if (host != null) {
      volService.revokeAccess(volFactory.getVolume(volumeToAttach.getId()),host,dataStore);
    }
    throw new CloudRuntimeException(errorMsg);
  }
}

Fixed version (ground truth):
private VolumeVO sendAttachVolumeCommand(UserVmVO vm,VolumeVO volumeToAttach,Long deviceId){
  String errorMsg="String_Node_Str" + volumeToAttach.getName() + "String_Node_Str"+ vm.getHostName();
  boolean sendCommand=vm.getState() == State.Running;
  AttachAnswer answer=null;
  Long hostId=vm.getHostId();
  if (hostId == null) {
    hostId=vm.getLastHostId();
    HostVO host=_hostDao.findById(hostId);
    if (host != null && host.getHypervisorType() == HypervisorType.VMware) {
      sendCommand=true;
    }
  }
  HostVO host=null;
  StoragePoolVO volumeToAttachStoragePool=_storagePoolDao.findById(volumeToAttach.getPoolId());
  if (hostId != null) {
    host=_hostDao.findById(hostId);
    if (host != null && host.getHypervisorType() == HypervisorType.XenServer && volumeToAttachStoragePool != null && volumeToAttachStoragePool.isManaged()) {
      sendCommand=true;
    }
  }
  DataStore dataStore=volumeToAttachStoragePool != null ? dataStoreMgr.getDataStore(volumeToAttachStoragePool.getId(),DataStoreRole.Primary) : null;
  if (host != null) {
    try {
      volService.grantAccess(volFactory.getVolume(volumeToAttach.getId()),host,dataStore);
    }
 catch (    Exception e) {
      volService.revokeAccess(volFactory.getVolume(volumeToAttach.getId()),host,dataStore);
      throw new CloudRuntimeException(e.getMessage());
    }
  }
  if (sendCommand) {
    if (host != null && host.getHypervisorType() == HypervisorType.KVM && volumeToAttachStoragePool.isManaged() && volumeToAttach.getPath() == null) {
      volumeToAttach.setPath(volumeToAttach.get_iScsiName());
      _volsDao.update(volumeToAttach.getId(),volumeToAttach);
    }
    DataTO volTO=volFactory.getVolume(volumeToAttach.getId()).getTO();
    deviceId=getDeviceId(vm.getId(),deviceId);
    DiskTO disk=storageMgr.getDiskWithThrottling(volTO,volumeToAttach.getVolumeType(),deviceId,volumeToAttach.getPath(),vm.getServiceOfferingId(),volumeToAttach.getDiskOfferingId());
    AttachCommand cmd=new AttachCommand(disk,vm.getInstanceName());
    ChapInfo chapInfo=volService.getChapInfo(volFactory.getVolume(volumeToAttach.getId()),dataStore);
    Map<String,String> details=new HashMap<String,String>();
    disk.setDetails(details);
    details.put(DiskTO.MANAGED,String.valueOf(volumeToAttachStoragePool.isManaged()));
    details.put(DiskTO.STORAGE_HOST,volumeToAttachStoragePool.getHostAddress());
    details.put(DiskTO.STORAGE_PORT,String.valueOf(volumeToAttachStoragePool.getPort()));
    details.put(DiskTO.VOLUME_SIZE,String.valueOf(volumeToAttach.getSize()));
    details.put(DiskTO.IQN,volumeToAttach.get_iScsiName());
    details.put(DiskTO.MOUNT_POINT,volumeToAttach.get_iScsiName());
    details.put(DiskTO.PROTOCOL_TYPE,(volumeToAttach.getPoolType() != null) ? volumeToAttach.getPoolType().toString() : null);
    if (chapInfo != null) {
      details.put(DiskTO.CHAP_INITIATOR_USERNAME,chapInfo.getInitiatorUsername());
      details.put(DiskTO.CHAP_INITIATOR_SECRET,chapInfo.getInitiatorSecret());
      details.put(DiskTO.CHAP_TARGET_USERNAME,chapInfo.getTargetUsername());
      details.put(DiskTO.CHAP_TARGET_SECRET,chapInfo.getTargetSecret());
    }
    _userVmDao.loadDetails(vm);
    Map<String,String> controllerInfo=new HashMap<String,String>();
    controllerInfo.put(VmDetailConstants.ROOT_DISK_CONTROLLER,vm.getDetail(VmDetailConstants.ROOT_DISK_CONTROLLER));
    controllerInfo.put(VmDetailConstants.DATA_DISK_CONTROLLER,vm.getDetail(VmDetailConstants.DATA_DISK_CONTROLLER));
    cmd.setControllerInfo(controllerInfo);
    s_logger.debug("String_Node_Str" + volumeToAttach.getId() + "String_Node_Str"+ vm.getId()+ "String_Node_Str"+ controllerInfo);
    try {
      answer=(AttachAnswer)_agentMgr.send(hostId,cmd);
    }
 catch (    Exception e) {
      if (host != null) {
        volService.revokeAccess(volFactory.getVolume(volumeToAttach.getId()),host,dataStore);
      }
      throw new CloudRuntimeException(errorMsg + "String_Node_Str" + e.getMessage());
    }
  }
  if (!sendCommand || (answer != null && answer.getResult())) {
    if (sendCommand) {
      DiskTO disk=answer.getDisk();
      _volsDao.attachVolume(volumeToAttach.getId(),vm.getId(),disk.getDiskSeq());
      volumeToAttach=_volsDao.findById(volumeToAttach.getId());
      if (volumeToAttachStoragePool.isManaged() && volumeToAttach.getPath() == null) {
        volumeToAttach.setPath(answer.getDisk().getPath());
        _volsDao.update(volumeToAttach.getId(),volumeToAttach);
      }
    }
 else {
      deviceId=getDeviceId(vm.getId(),deviceId);
      _volsDao.attachVolume(volumeToAttach.getId(),vm.getId(),deviceId);
    }
    VmDiskStatisticsVO diskstats=_vmDiskStatsDao.findBy(vm.getAccountId(),vm.getDataCenterId(),vm.getId(),volumeToAttach.getId());
    if (diskstats == null) {
      diskstats=new VmDiskStatisticsVO(vm.getAccountId(),vm.getDataCenterId(),vm.getId(),volumeToAttach.getId());
      _vmDiskStatsDao.persist(diskstats);
    }
    return _volsDao.findById(volumeToAttach.getId());
  }
 else {
    if (answer != null) {
      String details=answer.getDetails();
      if (details != null && !details.isEmpty()) {
        errorMsg+="String_Node_Str" + details;
      }
    }
    if (host != null) {
      volService.revokeAccess(volFactory.getVolume(volumeToAttach.getId()),host,dataStore);
    }
    throw new CloudRuntimeException(errorMsg);
  }
}