Program: 106522
Project: 106522
Buggy source:
@Override @DB public boolean associateIpAddressListToAccount(long userId,long accountId,long zoneId,Long vlanId,Network network) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, ResourceAllocationException {
  Account owner=_accountMgr.getActiveAccountById(accountId);
  boolean createNetwork=false;
  Transaction txn=Transaction.currentTxn();
  txn.start();
  if (network == null) {
    List<? extends Network> networks=getIsolatedNetworksOwnedByAccountInZone(zoneId,owner);
    if (networks.size() == 0) {
      createNetwork=true;
    }
 else {
      network=networks.get(0);
    }
  }
  if (createNetwork) {
    List<? extends NetworkOffering> offerings=_configMgr.listNetworkOfferings(TrafficType.Guest,false);
    PhysicalNetwork physicalNetwork=translateZoneIdToPhysicalNetwork(zoneId);
    network=createGuestNetwork(offerings.get(0).getId(),owner.getAccountName() + "String_Node_Str",owner.getAccountName() + "String_Node_Str",null,null,null,null,owner,false,null,physicalNetwork,zoneId,ACLType.Account,null);
    if (network == null) {
      s_logger.warn("String_Node_Str" + accountId + "String_Node_Str"+ zoneId);
      return false;
    }
  }
  boolean allocateSourceNat=false;
  List<IPAddressVO> sourceNat=_ipAddressDao.listByAssociatedNetwork(network.getId(),true);
  if (sourceNat.isEmpty()) {
    allocateSourceNat=true;
  }
  List<IPAddressVO> ips=_ipAddressDao.listByVlanId(vlanId);
  boolean isSourceNatAllocated=false;
  for (  IPAddressVO addr : ips) {
    if (addr.getState() != State.Allocated) {
      if (!isSourceNatAllocated && allocateSourceNat) {
        addr.setSourceNat(true);
        isSourceNatAllocated=true;
      }
 else {
        addr.setSourceNat(false);
      }
      addr.setAssociatedWithNetworkId(network.getId());
      addr.setAllocatedTime(new Date());
      addr.setAllocatedInDomainId(owner.getDomainId());
      addr.setAllocatedToAccountId(owner.getId());
      addr.setSystem(false);
      addr.setState(IpAddress.State.Allocating);
      markPublicIpAsAllocated(addr);
    }
  }
  txn.commit();
  return true;
}

Fixed version (ground truth):
@Override @DB public boolean associateIpAddressListToAccount(long userId,long accountId,long zoneId,Long vlanId,Network network) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, ResourceAllocationException {
  Account owner=_accountMgr.getActiveAccountById(accountId);
  boolean createNetwork=false;
  Transaction txn=Transaction.currentTxn();
  txn.start();
  if (network == null) {
    List<? extends Network> networks=getIsolatedNetworksWithSourceNATOwnedByAccountInZone(zoneId,owner);
    if (networks.size() == 0) {
      createNetwork=true;
    }
 else     if (networks.size() == 1) {
      network=networks.get(0);
    }
 else {
      throw new InvalidParameterValueException("String_Node_Str");
    }
  }
  if (createNetwork) {
    List<NetworkOfferingVO> requiredOfferings=_networkOfferingDao.listByAvailability(Availability.Required,false);
    if (requiredOfferings.size() < 1) {
      throw new CloudRuntimeException("String_Node_Str" + Availability.Required + "String_Node_Str");
    }
    PhysicalNetwork physicalNetwork=translateZoneIdToPhysicalNetwork(zoneId);
    if (requiredOfferings.get(0).getState() == NetworkOffering.State.Enabled) {
      s_logger.debug("String_Node_Str" + owner + "String_Node_Str"+ requiredOfferings.get(0).getId()+ "String_Node_Str");
      network=createGuestNetwork(requiredOfferings.get(0).getId(),owner.getAccountName() + "String_Node_Str",owner.getAccountName() + "String_Node_Str",null,null,null,null,owner,false,null,physicalNetwork,zoneId,ACLType.Account,null);
      if (network == null) {
        s_logger.warn("String_Node_Str" + accountId + "String_Node_Str"+ zoneId);
        throw new CloudRuntimeException("String_Node_Str" + accountId + "String_Node_Str"+ zoneId);
      }
    }
 else {
      throw new CloudRuntimeException("String_Node_Str" + requiredOfferings.get(0).getId() + "String_Node_Str"+ NetworkOffering.State.Enabled);
    }
  }
  boolean allocateSourceNat=false;
  List<IPAddressVO> sourceNat=_ipAddressDao.listByAssociatedNetwork(network.getId(),true);
  if (sourceNat.isEmpty()) {
    allocateSourceNat=true;
  }
  List<IPAddressVO> ips=_ipAddressDao.listByVlanId(vlanId);
  boolean isSourceNatAllocated=false;
  for (  IPAddressVO addr : ips) {
    if (addr.getState() != State.Allocated) {
      if (!isSourceNatAllocated && allocateSourceNat) {
        addr.setSourceNat(true);
        isSourceNatAllocated=true;
      }
 else {
        addr.setSourceNat(false);
      }
      addr.setAssociatedWithNetworkId(network.getId());
      addr.setAllocatedTime(new Date());
      addr.setAllocatedInDomainId(owner.getDomainId());
      addr.setAllocatedToAccountId(owner.getId());
      addr.setSystem(false);
      addr.setState(IpAddress.State.Allocating);
      markPublicIpAsAllocated(addr);
    }
  }
  txn.commit();
  return true;
}