Program: 89640
Project: 89640
Buggy source:
@Override @ActionEvent(eventType=EventTypes.EVENT_VOLUME_CREATE,eventDescription="String_Node_Str",create=true) public VolumeVO allocVolume(CreateVolumeCmd cmd) throws ResourceAllocationException {
  Account account=UserContext.current().getCaller();
  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(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 && cmd.getDiskOfferingId() == null) || (cmd.getSnapshotId() != null && cmd.getDiskOfferingId() != null)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (cmd.getSnapshotId() == null) {
    zoneId=cmd.getZoneId();
    if ((zoneId == null)) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    diskOfferingId=cmd.getDiskOfferingId();
    size=cmd.getSize() * 1024 * 1024* 1024;
    if (diskOfferingId == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    DiskOfferingVO diskOffering=_diskOfferingDao.findById(diskOfferingId);
    if ((diskOffering == null) || !DiskOfferingVO.Type.Disk.equals(diskOffering.getType())) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if ((diskOffering.isCustomized() && size == null)) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (!diskOffering.isCustomized() && size != null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (diskOffering.getDomainId() == null) {
    }
 else {
      _configMgr.checkDiskOfferingAccess(account,diskOffering);
    }
    if (diskOffering.getDiskSize() > 0) {
      size=diskOffering.getDiskSize();
    }
    if (!validateVolumeSizeRange(size)) {
      throw new InvalidParameterValueException("String_Node_Str" + size + "String_Node_Str"+ _maxVolumeSizeInGb);
    }
  }
 else {
    Long snapshotId=cmd.getSnapshotId();
    SnapshotVO snapshotCheck=_snapshotDao.findById(snapshotId);
    if (snapshotCheck == null) {
      throw new InvalidParameterValueException("String_Node_Str" + snapshotId);
    }
    if (snapshotCheck.getStatus() != Snapshot.Status.BackedUp) {
      throw new InvalidParameterValueException("String_Node_Str" + snapshotId + "String_Node_Str"+ Snapshot.Status.BackedUp+ "String_Node_Str");
    }
    diskOfferingId=(cmd.getDiskOfferingId() != null) ? cmd.getDiskOfferingId() : snapshotCheck.getDiskOfferingId();
    zoneId=snapshotCheck.getDataCenterId();
    size=snapshotCheck.getSize();
    if (account != null) {
      if (isAdmin(account.getType())) {
        Account snapshotOwner=_accountDao.findById(snapshotCheck.getAccountId());
        if (!_domainDao.isChildDomain(account.getDomainId(),snapshotOwner.getDomainId())) {
          throw new PermissionDeniedException("String_Node_Str" + snapshotId + "String_Node_Str");
        }
      }
 else       if (account.getId() != snapshotCheck.getAccountId()) {
        throw new InvalidParameterValueException("String_Node_Str" + snapshotId + "String_Node_Str");
      }
    }
  }
  DataCenterVO zone=_dcDao.findById(zoneId);
  if (zone == null) {
    throw new InvalidParameterValueException("String_Node_Str" + zoneId);
  }
  if (Grouping.AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(account.getType())) {
    throw new PermissionDeniedException("String_Node_Str" + zoneId);
  }
  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.Type.DATADISK);
  volume.setPoolId(null);
  volume.setDataCenterId(zoneId);
  volume.setPodId(null);
  volume.setAccountId(targetAccount.getId());
  volume.setDomainId(((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId()));
  volume.setDiskOfferingId(diskOfferingId);
  volume.setSize(size);
  volume.setInstanceId(null);
  volume.setUpdated(new Date());
  volume.setDomainId((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId());
  volume.setState(Volume.State.Allocated);
  volume=_volsDao.persist(volume);
  UserContext.current().setEventDetails("String_Node_Str" + volume.getId());
  UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_VOLUME_CREATE,volume.getAccountId(),volume.getDataCenterId(),volume.getId(),volume.getName(),diskOfferingId,null,size);
  _usageEventDao.persist(usageEvent);
  _accountMgr.incrementResourceCount(volume.getAccountId(),ResourceType.volume);
  return volume;
}

Fixed version (ground truth):
@Override @ActionEvent(eventType=EventTypes.EVENT_VOLUME_CREATE,eventDescription="String_Node_Str",create=true) public VolumeVO allocVolume(CreateVolumeCmd cmd) throws ResourceAllocationException {
  Account account=UserContext.current().getCaller();
  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(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 && cmd.getDiskOfferingId() == null) || (cmd.getSnapshotId() != null && cmd.getDiskOfferingId() != null)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (cmd.getSnapshotId() == null) {
    zoneId=cmd.getZoneId();
    if ((zoneId == null)) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    diskOfferingId=cmd.getDiskOfferingId();
    size=cmd.getSize();
    if (size != null) {
      if (size > 0) {
        size=size * 1024 * 1024* 1024;
      }
 else {
        throw new InvalidParameterValueException("String_Node_Str");
      }
    }
    if (diskOfferingId == null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    DiskOfferingVO diskOffering=_diskOfferingDao.findById(diskOfferingId);
    if ((diskOffering == null) || !DiskOfferingVO.Type.Disk.equals(diskOffering.getType())) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if ((diskOffering.isCustomized() && size == null)) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (!diskOffering.isCustomized() && size != null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    if (diskOffering.getDomainId() == null) {
    }
 else {
      _configMgr.checkDiskOfferingAccess(account,diskOffering);
    }
    if (diskOffering.getDiskSize() > 0) {
      size=diskOffering.getDiskSize();
    }
    if (!validateVolumeSizeRange(size)) {
      throw new InvalidParameterValueException("String_Node_Str" + size + "String_Node_Str"+ _maxVolumeSizeInGb);
    }
  }
 else {
    Long snapshotId=cmd.getSnapshotId();
    SnapshotVO snapshotCheck=_snapshotDao.findById(snapshotId);
    if (snapshotCheck == null) {
      throw new InvalidParameterValueException("String_Node_Str" + snapshotId);
    }
    if (snapshotCheck.getStatus() != Snapshot.Status.BackedUp) {
      throw new InvalidParameterValueException("String_Node_Str" + snapshotId + "String_Node_Str"+ Snapshot.Status.BackedUp+ "String_Node_Str");
    }
    diskOfferingId=(cmd.getDiskOfferingId() != null) ? cmd.getDiskOfferingId() : snapshotCheck.getDiskOfferingId();
    zoneId=snapshotCheck.getDataCenterId();
    size=snapshotCheck.getSize();
    if (account != null) {
      if (isAdmin(account.getType())) {
        Account snapshotOwner=_accountDao.findById(snapshotCheck.getAccountId());
        if (!_domainDao.isChildDomain(account.getDomainId(),snapshotOwner.getDomainId())) {
          throw new PermissionDeniedException("String_Node_Str" + snapshotId + "String_Node_Str");
        }
      }
 else       if (account.getId() != snapshotCheck.getAccountId()) {
        throw new InvalidParameterValueException("String_Node_Str" + snapshotId + "String_Node_Str");
      }
    }
  }
  DataCenterVO zone=_dcDao.findById(zoneId);
  if (zone == null) {
    throw new InvalidParameterValueException("String_Node_Str" + zoneId);
  }
  if (Grouping.AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(account.getType())) {
    throw new PermissionDeniedException("String_Node_Str" + zoneId);
  }
  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.Type.DATADISK);
  volume.setPoolId(null);
  volume.setDataCenterId(zoneId);
  volume.setPodId(null);
  volume.setAccountId(targetAccount.getId());
  volume.setDomainId(((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId()));
  volume.setDiskOfferingId(diskOfferingId);
  volume.setSize(size);
  volume.setInstanceId(null);
  volume.setUpdated(new Date());
  volume.setDomainId((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId());
  volume.setState(Volume.State.Allocated);
  volume=_volsDao.persist(volume);
  UserContext.current().setEventDetails("String_Node_Str" + volume.getId());
  UsageEventVO usageEvent=new UsageEventVO(EventTypes.EVENT_VOLUME_CREATE,volume.getAccountId(),volume.getDataCenterId(),volume.getId(),volume.getName(),diskOfferingId,null,size);
  _usageEventDao.persist(usageEvent);
  _accountMgr.incrementResourceCount(volume.getAccountId(),ResourceType.volume);
  return volume;
}