Program: 105803
Project: 105803
Buggy source:
@ActionEvent(eventType=EventTypes.EVENT_VOLUME_ATTACH,eventDescription="String_Node_Str",async=true) public Volume attachVolumeToVM(Long vmId,Long volumeId,Long deviceId){
  Account caller=CallContext.current().getCallingAccount();
  VolumeInfo volumeToAttach=volFactory.getVolume(volumeId);
  if (volumeToAttach == null || !(volumeToAttach.getVolumeType() == Volume.Type.DATADISK || volumeToAttach.getVolumeType() == Volume.Type.ROOT)) {
    throw new InvalidParameterValueException("String_Node_Str" + Volume.Type.ROOT.toString() + "String_Node_Str"+ Volume.Type.DATADISK.toString());
  }
  if (volumeToAttach.getInstanceId() != null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (volumeToAttach.getState() == Volume.State.Destroy) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  UserVmVO vm=_userVmDao.findById(vmId);
  if (vm == null || vm.getType() != VirtualMachine.Type.User) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (vm.getState() != State.Running && vm.getState() != State.Stopped) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (vm.getDataCenterId() != volumeToAttach.getDataCenterId()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  _accountMgr.checkAccess(caller,null,true,volumeToAttach,vm);
  if (deviceId != null) {
    if (deviceId.longValue() == 0) {
      validateRootVolumeDetachAttach(_volsDao.findById(volumeToAttach.getId()),vm);
      if (!_volsDao.findByInstanceAndDeviceId(vm.getId(),0).isEmpty()) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
      if (volumeToAttach.getState() == Volume.State.Uploaded) {
        throw new InvalidParameterValueException("String_Node_Str" + Volume.State.Uploaded);
      }
    }
  }
  if (deviceId == null || deviceId.longValue() != 0) {
    List<VolumeVO> existingDataVolumes=_volsDao.findByInstanceAndType(vmId,Volume.Type.DATADISK);
    int maxDataVolumesSupported=getMaxDataVolumesSupported(vm);
    if (existingDataVolumes.size() >= maxDataVolumesSupported) {
      throw new InvalidParameterValueException("String_Node_Str" + maxDataVolumesSupported + "String_Node_Str");
    }
  }
  deviceId=getDeviceId(vmId,deviceId);
  DataCenterVO dataCenter=_dcDao.findById(volumeToAttach.getDataCenterId());
  if (!dataCenter.isLocalStorageEnabled()) {
    DiskOfferingVO diskOffering=_diskOfferingDao.findById(volumeToAttach.getDiskOfferingId());
    if (diskOffering.getUseLocalStorage()) {
      throw new InvalidParameterValueException("String_Node_Str" + diskOffering.getName() + "String_Node_Str");
    }
  }
  List<VMSnapshotVO> vmSnapshots=_vmSnapshotDao.findByVm(vmId);
  if (vmSnapshots.size() > 0) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  _accountMgr.checkAccess(caller,AccessType.OperateEntry,volumeToAttach,vm);
  if (!(Volume.State.Allocated.equals(volumeToAttach.getState()) || Volume.State.Ready.equals(volumeToAttach.getState()) || Volume.State.Uploaded.equals(volumeToAttach.getState()))) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  VolumeVO exstingVolumeOfVm=null;
  List<VolumeVO> rootVolumesOfVm=_volsDao.findByInstanceAndType(vmId,Volume.Type.ROOT);
  if (rootVolumesOfVm.size() > 1) {
    throw new CloudRuntimeException("String_Node_Str" + vm.getHostName() + "String_Node_Str");
  }
 else {
    if (!rootVolumesOfVm.isEmpty()) {
      exstingVolumeOfVm=rootVolumesOfVm.get(0);
    }
 else {
      List<VolumeVO> diskVolumesOfVm=_volsDao.findByInstanceAndType(vmId,Volume.Type.DATADISK);
      for (      VolumeVO diskVolume : diskVolumesOfVm) {
        if (diskVolume.getState() != Volume.State.Allocated) {
          exstingVolumeOfVm=diskVolume;
          break;
        }
      }
    }
  }
  HypervisorType rootDiskHyperType=vm.getHypervisorType();
  HypervisorType volumeToAttachHyperType=_volsDao.getHypervisorType(volumeToAttach.getId());
  StoragePoolVO volumeToAttachStoragePool=_storagePoolDao.findById(volumeToAttach.getPoolId());
  if (volumeToAttachStoragePool != null && !volumeToAttachStoragePool.isManaged()) {
    if (volumeToAttachHyperType != HypervisorType.None && rootDiskHyperType != volumeToAttachHyperType) {
      throw new InvalidParameterValueException("String_Node_Str" + volumeToAttachHyperType + "String_Node_Str"+ rootDiskHyperType+ "String_Node_Str");
    }
  }
  VolumeInfo newVolumeOnPrimaryStorage=volumeToAttach;
  StoragePoolVO destPrimaryStorage=null;
  if (exstingVolumeOfVm != null && !exstingVolumeOfVm.getState().equals(Volume.State.Allocated)) {
    destPrimaryStorage=_storagePoolDao.findById(exstingVolumeOfVm.getPoolId());
  }
  if (destPrimaryStorage != null && (volumeToAttach.getState() == Volume.State.Allocated || volumeToAttach.getState() == Volume.State.Uploaded)) {
    try {
      newVolumeOnPrimaryStorage=_volumeMgr.createVolumeOnPrimaryStorage(vm,volumeToAttach,rootDiskHyperType,destPrimaryStorage);
    }
 catch (    NoTransitionException e) {
      s_logger.debug("String_Node_Str",e);
      throw new CloudRuntimeException("String_Node_Str",e);
    }
  }
  newVolumeOnPrimaryStorage=volFactory.getVolume(newVolumeOnPrimaryStorage.getId());
  boolean moveVolumeNeeded=needMoveVolume(exstingVolumeOfVm,newVolumeOnPrimaryStorage);
  if (moveVolumeNeeded) {
    PrimaryDataStoreInfo primaryStore=(PrimaryDataStoreInfo)newVolumeOnPrimaryStorage.getDataStore();
    if (primaryStore.isLocal()) {
      throw new CloudRuntimeException("String_Node_Str" + volumeToAttach.getName() + "String_Node_Str"+ vm.getDisplayName()+ "String_Node_Str");
    }
    StoragePoolVO vmRootVolumePool=_storagePoolDao.findById(exstingVolumeOfVm.getPoolId());
    try {
      newVolumeOnPrimaryStorage=_volumeMgr.moveVolume(newVolumeOnPrimaryStorage,vmRootVolumePool.getDataCenterId(),vmRootVolumePool.getPodId(),vmRootVolumePool.getClusterId(),volumeToAttachHyperType);
    }
 catch (    ConcurrentOperationException e) {
      s_logger.debug("String_Node_Str",e);
      throw new CloudRuntimeException("String_Node_Str",e);
    }
catch (    StorageUnavailableException e) {
      s_logger.debug("String_Node_Str",e);
      throw new CloudRuntimeException("String_Node_Str",e);
    }
  }
  AsyncJobExecutionContext asyncExecutionContext=AsyncJobExecutionContext.getCurrentExecutionContext();
  if (asyncExecutionContext != null) {
    AsyncJob job=asyncExecutionContext.getJob();
    if (s_logger.isInfoEnabled()) {
      s_logger.info("String_Node_Str" + volumeId + "String_Node_Str"+ vm.getId()+ "String_Node_Str"+ job.getId()+ "String_Node_Str");
    }
    _jobMgr.updateAsyncJobAttachment(job.getId(),"String_Node_Str",volumeId);
  }
  VolumeVO newVol=_volsDao.findById(newVolumeOnPrimaryStorage.getId());
  newVol=sendAttachVolumeCommand(vm,newVol,deviceId);
  return newVol;
}

Fixed version (ground truth):
@ActionEvent(eventType=EventTypes.EVENT_VOLUME_ATTACH,eventDescription="String_Node_Str",async=true) public Volume attachVolumeToVM(Long vmId,Long volumeId,Long deviceId){
  Account caller=CallContext.current().getCallingAccount();
  VolumeInfo volumeToAttach=volFactory.getVolume(volumeId);
  if (volumeToAttach == null || !(volumeToAttach.getVolumeType() == Volume.Type.DATADISK || volumeToAttach.getVolumeType() == Volume.Type.ROOT)) {
    throw new InvalidParameterValueException("String_Node_Str" + Volume.Type.ROOT.toString() + "String_Node_Str"+ Volume.Type.DATADISK.toString());
  }
  if (volumeToAttach.getInstanceId() != null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (volumeToAttach.getState() == Volume.State.Destroy) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  UserVmVO vm=_userVmDao.findById(vmId);
  if (vm == null || vm.getType() != VirtualMachine.Type.User) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (vm.getState() != State.Running && vm.getState() != State.Stopped) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (vm.getDataCenterId() != volumeToAttach.getDataCenterId()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (deviceId != null) {
    if (deviceId.longValue() == 0) {
      validateRootVolumeDetachAttach(_volsDao.findById(volumeToAttach.getId()),vm);
      if (!_volsDao.findByInstanceAndDeviceId(vm.getId(),0).isEmpty()) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
      if (volumeToAttach.getState() == Volume.State.Uploaded) {
        throw new InvalidParameterValueException("String_Node_Str" + Volume.State.Uploaded);
      }
    }
  }
  if (deviceId == null || deviceId.longValue() != 0) {
    List<VolumeVO> existingDataVolumes=_volsDao.findByInstanceAndType(vmId,Volume.Type.DATADISK);
    int maxDataVolumesSupported=getMaxDataVolumesSupported(vm);
    if (existingDataVolumes.size() >= maxDataVolumesSupported) {
      throw new InvalidParameterValueException("String_Node_Str" + maxDataVolumesSupported + "String_Node_Str");
    }
  }
  deviceId=getDeviceId(vmId,deviceId);
  DataCenterVO dataCenter=_dcDao.findById(volumeToAttach.getDataCenterId());
  if (!dataCenter.isLocalStorageEnabled()) {
    DiskOfferingVO diskOffering=_diskOfferingDao.findById(volumeToAttach.getDiskOfferingId());
    if (diskOffering.getUseLocalStorage()) {
      throw new InvalidParameterValueException("String_Node_Str" + diskOffering.getName() + "String_Node_Str");
    }
  }
  List<VMSnapshotVO> vmSnapshots=_vmSnapshotDao.findByVm(vmId);
  if (vmSnapshots.size() > 0) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  _accountMgr.checkAccess(caller,AccessType.OperateEntry,volumeToAttach,vm);
  if (!(Volume.State.Allocated.equals(volumeToAttach.getState()) || Volume.State.Ready.equals(volumeToAttach.getState()) || Volume.State.Uploaded.equals(volumeToAttach.getState()))) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  VolumeVO exstingVolumeOfVm=null;
  List<VolumeVO> rootVolumesOfVm=_volsDao.findByInstanceAndType(vmId,Volume.Type.ROOT);
  if (rootVolumesOfVm.size() > 1) {
    throw new CloudRuntimeException("String_Node_Str" + vm.getHostName() + "String_Node_Str");
  }
 else {
    if (!rootVolumesOfVm.isEmpty()) {
      exstingVolumeOfVm=rootVolumesOfVm.get(0);
    }
 else {
      List<VolumeVO> diskVolumesOfVm=_volsDao.findByInstanceAndType(vmId,Volume.Type.DATADISK);
      for (      VolumeVO diskVolume : diskVolumesOfVm) {
        if (diskVolume.getState() != Volume.State.Allocated) {
          exstingVolumeOfVm=diskVolume;
          break;
        }
      }
    }
  }
  HypervisorType rootDiskHyperType=vm.getHypervisorType();
  HypervisorType volumeToAttachHyperType=_volsDao.getHypervisorType(volumeToAttach.getId());
  StoragePoolVO volumeToAttachStoragePool=_storagePoolDao.findById(volumeToAttach.getPoolId());
  if (volumeToAttachStoragePool != null && !volumeToAttachStoragePool.isManaged()) {
    if (volumeToAttachHyperType != HypervisorType.None && rootDiskHyperType != volumeToAttachHyperType) {
      throw new InvalidParameterValueException("String_Node_Str" + volumeToAttachHyperType + "String_Node_Str"+ rootDiskHyperType+ "String_Node_Str");
    }
  }
  VolumeInfo newVolumeOnPrimaryStorage=volumeToAttach;
  StoragePoolVO destPrimaryStorage=null;
  if (exstingVolumeOfVm != null && !exstingVolumeOfVm.getState().equals(Volume.State.Allocated)) {
    destPrimaryStorage=_storagePoolDao.findById(exstingVolumeOfVm.getPoolId());
  }
  if (destPrimaryStorage != null && (volumeToAttach.getState() == Volume.State.Allocated || volumeToAttach.getState() == Volume.State.Uploaded)) {
    try {
      newVolumeOnPrimaryStorage=_volumeMgr.createVolumeOnPrimaryStorage(vm,volumeToAttach,rootDiskHyperType,destPrimaryStorage);
    }
 catch (    NoTransitionException e) {
      s_logger.debug("String_Node_Str",e);
      throw new CloudRuntimeException("String_Node_Str",e);
    }
  }
  newVolumeOnPrimaryStorage=volFactory.getVolume(newVolumeOnPrimaryStorage.getId());
  boolean moveVolumeNeeded=needMoveVolume(exstingVolumeOfVm,newVolumeOnPrimaryStorage);
  if (moveVolumeNeeded) {
    PrimaryDataStoreInfo primaryStore=(PrimaryDataStoreInfo)newVolumeOnPrimaryStorage.getDataStore();
    if (primaryStore.isLocal()) {
      throw new CloudRuntimeException("String_Node_Str" + volumeToAttach.getName() + "String_Node_Str"+ vm.getDisplayName()+ "String_Node_Str");
    }
    StoragePoolVO vmRootVolumePool=_storagePoolDao.findById(exstingVolumeOfVm.getPoolId());
    try {
      newVolumeOnPrimaryStorage=_volumeMgr.moveVolume(newVolumeOnPrimaryStorage,vmRootVolumePool.getDataCenterId(),vmRootVolumePool.getPodId(),vmRootVolumePool.getClusterId(),volumeToAttachHyperType);
    }
 catch (    ConcurrentOperationException e) {
      s_logger.debug("String_Node_Str",e);
      throw new CloudRuntimeException("String_Node_Str",e);
    }
catch (    StorageUnavailableException e) {
      s_logger.debug("String_Node_Str",e);
      throw new CloudRuntimeException("String_Node_Str",e);
    }
  }
  AsyncJobExecutionContext asyncExecutionContext=AsyncJobExecutionContext.getCurrentExecutionContext();
  if (asyncExecutionContext != null) {
    AsyncJob job=asyncExecutionContext.getJob();
    if (s_logger.isInfoEnabled()) {
      s_logger.info("String_Node_Str" + volumeId + "String_Node_Str"+ vm.getId()+ "String_Node_Str"+ job.getId()+ "String_Node_Str");
    }
    _jobMgr.updateAsyncJobAttachment(job.getId(),"String_Node_Str",volumeId);
  }
  VolumeVO newVol=_volsDao.findById(newVolumeOnPrimaryStorage.getId());
  newVol=sendAttachVolumeCommand(vm,newVol,deviceId);
  return newVol;
}