Program: 107439
Project: 107439
Buggy source:
@Override public VolumeVO allocVolume(CreateVolumeCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, ResourceAllocationException {
  Account account=(Account)UserContext.current().getAccount();
  String accountName=cmd.getAccountName();
  Long domainId=cmd.getDomainId();
  Account targetAccount=null;
  if ((account == null) || isAdmin(account.getType())) {
    if ((domainId != null) && (accountName != null)) {
      if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(),domainId)) {
        throw new PermissionDeniedException("String_Node_Str" + domainId + "String_Node_Str");
      }
      targetAccount=_accountDao.findActiveAccount(accountName,domainId);
    }
 else {
      targetAccount=account;
    }
    if (targetAccount == null) {
      throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str"+ domainId);
    }
  }
 else {
    targetAccount=account;
  }
  if (_accountMgr.resourceLimitExceeded((Account)targetAccount,ResourceType.volume)) {
    ResourceAllocationException rae=new ResourceAllocationException("String_Node_Str" + targetAccount.getAccountName() + "String_Node_Str");
    rae.setResourceType("String_Node_Str");
    throw rae;
  }
  if (cmd.getSnapshotId() == null) {
    Long zoneId=cmd.getZoneId();
    if ((zoneId == null)) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    Long diskOfferingId=cmd.getDiskOfferingId();
    Long size=cmd.getSize();
    if ((diskOfferingId == null) && (size == null)) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
 else     if ((diskOfferingId == null) && (size != null)) {
      boolean ok=validateCustomVolumeSizeRange(size);
      if (!ok) {
        throw new InvalidParameterValueException("String_Node_Str" + size);
      }
      List<DiskOfferingVO> privateTemplateList=_diskOfferingDao.findPrivateDiskOffering();
      diskOfferingId=privateTemplateList.get(0).getId();
    }
 else {
      DiskOfferingVO diskOffering=_diskOfferingDao.findById(diskOfferingId);
      if ((diskOffering == null) || !DiskOfferingVO.Type.Disk.equals(diskOffering.getType())) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
    }
  }
 else {
    Long snapshotId=cmd.getSnapshotId();
    Snapshot snapshotCheck=_snapshotDao.findById(snapshotId);
    if (snapshotCheck == null) {
      throw new ServerApiException(BaseCmd.SNAPSHOT_INVALID_PARAM_ERROR,"String_Node_Str" + snapshotId);
    }
    if (account != null) {
      if (isAdmin(account.getType())) {
        Account snapshotOwner=_accountDao.findById(snapshotCheck.getAccountId());
        if (!_domainDao.isChildDomain(account.getDomainId(),snapshotOwner.getDomainId())) {
          throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + snapshotId + "String_Node_Str");
        }
      }
 else       if (account.getId() != snapshotCheck.getAccountId()) {
        throw new ServerApiException(BaseCmd.SNAPSHOT_INVALID_PARAM_ERROR,"String_Node_Str" + snapshotId + "String_Node_Str");
      }
    }
  }
  Long zoneId=cmd.getZoneId();
  DataCenterVO zone=_dcDao.findById(zoneId);
  if (zone == null) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  List<StoragePoolVO> storagePools=_storagePoolDao.listByDataCenterId(zoneId);
  boolean sharedPoolExists=false;
  for (  StoragePoolVO storagePool : storagePools) {
    if (storagePool.isShared()) {
      sharedPoolExists=true;
    }
  }
  List<HostVO> hosts=_hostDao.listByDataCenter(zoneId);
  if (hosts.isEmpty()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (!sharedPoolExists) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  String userSpecifiedName=cmd.getVolumeName();
  if (userSpecifiedName == null) {
    userSpecifiedName=getRandomVolumeName();
  }
  VolumeVO volume=new VolumeVO(userSpecifiedName,-1,-1,-1,-1,new Long(-1),null,null,0,Volume.VolumeType.DATADISK);
  volume.setPoolId(null);
  volume.setDataCenterId(zoneId);
  volume.setPodId(null);
  volume.setAccountId(targetAccount.getId());
  volume.setDomainId(((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId()));
  volume.setMirrorState(MirrorState.NOT_MIRRORED);
  volume.setDiskOfferingId(cmd.getDiskOfferingId());
  volume.setStorageResourceType(StorageResourceType.STORAGE_POOL);
  volume.setInstanceId(null);
  volume.setUpdated(new Date());
  volume.setStatus(AsyncInstanceCreateStatus.Creating);
  volume.setDomainId((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId());
  volume.setState(Volume.State.Allocated);
  volume=_volsDao.persist(volume);
  return volume;
}

Fixed version (ground truth):
@Override public VolumeVO allocVolume(CreateVolumeCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, ResourceAllocationException {
  Account account=(Account)UserContext.current().getAccount();
  String accountName=cmd.getAccountName();
  Long domainId=cmd.getDomainId();
  Account targetAccount=null;
  if ((account == null) || isAdmin(account.getType())) {
    if ((domainId != null) && (accountName != null)) {
      if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(),domainId)) {
        throw new PermissionDeniedException("String_Node_Str" + domainId + "String_Node_Str");
      }
      targetAccount=_accountDao.findActiveAccount(accountName,domainId);
    }
 else {
      targetAccount=account;
    }
    if (targetAccount == null) {
      throw new InvalidParameterValueException("String_Node_Str" + accountName + "String_Node_Str"+ domainId);
    }
  }
 else {
    targetAccount=account;
  }
  if (_accountMgr.resourceLimitExceeded((Account)targetAccount,ResourceType.volume)) {
    ResourceAllocationException rae=new ResourceAllocationException("String_Node_Str" + targetAccount.getAccountName() + "String_Node_Str");
    rae.setResourceType("String_Node_Str");
    throw rae;
  }
  Long zoneId=null;
  Long diskOfferingId=null;
  Long size=null;
  if (cmd.getSnapshotId() == null) {
    zoneId=cmd.getZoneId();
    if ((zoneId == null)) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    diskOfferingId=cmd.getDiskOfferingId();
    size=cmd.getSize();
    if ((diskOfferingId == null) && (size == null)) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
 else     if ((diskOfferingId == null) && (size != null)) {
      boolean ok=validateCustomVolumeSizeRange(size);
      if (!ok) {
        throw new InvalidParameterValueException("String_Node_Str" + size);
      }
      List<DiskOfferingVO> privateTemplateList=_diskOfferingDao.findPrivateDiskOffering();
      diskOfferingId=privateTemplateList.get(0).getId();
    }
 else {
      DiskOfferingVO diskOffering=_diskOfferingDao.findById(diskOfferingId);
      if ((diskOffering == null) || !DiskOfferingVO.Type.Disk.equals(diskOffering.getType())) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
      size=diskOffering.getDiskSize();
    }
  }
 else {
    Long snapshotId=cmd.getSnapshotId();
    Snapshot snapshotCheck=_snapshotDao.findById(snapshotId);
    if (snapshotCheck == null) {
      throw new ServerApiException(BaseCmd.SNAPSHOT_INVALID_PARAM_ERROR,"String_Node_Str" + snapshotId);
    }
    VolumeVO vol=_volsDao.findById(snapshotCheck.getVolumeId());
    zoneId=vol.getDataCenterId();
    diskOfferingId=vol.getDiskOfferingId();
    size=vol.getSize();
    if (account != null) {
      if (isAdmin(account.getType())) {
        Account snapshotOwner=_accountDao.findById(snapshotCheck.getAccountId());
        if (!_domainDao.isChildDomain(account.getDomainId(),snapshotOwner.getDomainId())) {
          throw new ServerApiException(BaseCmd.ACCOUNT_ERROR,"String_Node_Str" + snapshotId + "String_Node_Str");
        }
      }
 else       if (account.getId() != snapshotCheck.getAccountId()) {
        throw new ServerApiException(BaseCmd.SNAPSHOT_INVALID_PARAM_ERROR,"String_Node_Str" + snapshotId + "String_Node_Str");
      }
    }
  }
  List<StoragePoolVO> storagePools=_storagePoolDao.listByDataCenterId(zoneId);
  boolean sharedPoolExists=false;
  for (  StoragePoolVO storagePool : storagePools) {
    if (storagePool.isShared()) {
      sharedPoolExists=true;
    }
  }
  List<HostVO> hosts=_hostDao.listByDataCenter(zoneId);
  if (hosts.isEmpty()) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (!sharedPoolExists) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  String userSpecifiedName=cmd.getVolumeName();
  if (userSpecifiedName == null) {
    userSpecifiedName=getRandomVolumeName();
  }
  VolumeVO volume=new VolumeVO(userSpecifiedName,-1,-1,-1,-1,new Long(-1),null,null,0,Volume.VolumeType.DATADISK);
  volume.setPoolId(null);
  volume.setDataCenterId(zoneId);
  volume.setPodId(null);
  volume.setAccountId(targetAccount.getId());
  volume.setDomainId(((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId()));
  volume.setMirrorState(MirrorState.NOT_MIRRORED);
  volume.setDiskOfferingId(diskOfferingId);
  volume.setSize(size);
  volume.setStorageResourceType(StorageResourceType.STORAGE_POOL);
  volume.setInstanceId(null);
  volume.setUpdated(new Date());
  volume.setStatus(AsyncInstanceCreateStatus.Creating);
  volume.setDomainId((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId());
  volume.setState(Volume.State.Allocated);
  volume=_volsDao.persist(volume);
  return volume;
}