Program: 89829
Project: 89829
Buggy source:
@Override public Vlan createVlanAndPublicIpRange(Long userId,Long zoneId,Long podId,String startIP,String endIP,String vlanGateway,String vlanNetmask,boolean forVirtualNetwork,String vlanId,Account account,Long networkId) throws InsufficientCapacityException, ConcurrentOperationException, InvalidParameterValueException {
  if (podId != null && ((_podDao.findById(podId)) == null)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (podId != null && _podDao.findById(podId).getDataCenterId() != zoneId) {
    throw new InvalidParameterValueException("String_Node_Str" + podId + "String_Node_Str"+ zoneId);
  }
  if (vlanId == null) {
    vlanId=Vlan.UNTAGGED;
  }
  DataCenterVO zone;
  if (zoneId == null || ((zone=_zoneDao.findById(zoneId)) == null)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (zone.getNetworkType() == NetworkType.Advanced && vlanId.equals(Vlan.UNTAGGED) && !forVirtualNetwork) {
    throw new InvalidParameterValueException("String_Node_Str" + zone.getId() + "String_Node_Str"+ zone.getNetworkType());
  }
 else   if (zone.getNetworkType() == NetworkType.Basic && !(vlanId.equals(Vlan.UNTAGGED) && !forVirtualNetwork)) {
    throw new InvalidParameterValueException("String_Node_Str" + zone.getId() + "String_Node_Str"+ zone.getNetworkType());
  }
  if (zone.getVnet() == null && forVirtualNetwork) {
    throw new InvalidParameterValueException("String_Node_Str" + zone.getId() + "String_Node_Str");
  }
  VlanType vlanType=forVirtualNetwork ? VlanType.VirtualNetwork : VlanType.DirectAttached;
  String hypervisorType=_configDao.getValue("String_Node_Str");
  if (hypervisorType.equalsIgnoreCase("String_Node_Str")) {
    if (vlanType.toString().equalsIgnoreCase("String_Node_Str") && vlanId.equalsIgnoreCase(Vlan.UNTAGGED)) {
      if (_configDao.getValue("String_Node_Str") == null || _configDao.getValue("String_Node_Str").equals("String_Node_Str")) {
        throw new CloudRuntimeException("String_Node_Str");
      }
    }
  }
  checkAccess(account,zone);
  boolean associateIpRangeToAccount=false;
  if (vlanType.equals(VlanType.VirtualNetwork)) {
    if (account != null) {
      long ipResourceLimit=_accountMgr.findCorrectResourceLimit((AccountVO)account,ResourceType.public_ip);
      long accountIpRange=NetUtils.ip2Long(endIP) - NetUtils.ip2Long(startIP) + 1;
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + ipResourceLimit + "String_Node_Str"+ accountIpRange);
      }
      if (ipResourceLimit != -1 && accountIpRange > ipResourceLimit) {
        throw new InvalidParameterValueException("String_Node_Str" + ipResourceLimit + "String_Node_Str"+ accountIpRange+ "String_Node_Str");
      }
      associateIpRangeToAccount=true;
    }
  }
 else   if (vlanType.equals(VlanType.DirectAttached)) {
    if (account != null) {
      if (vlanId.equals(Vlan.UNTAGGED)) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
      List<HostPodVO> podsInZone=_podDao.listByDataCenterId(zone.getId());
      for (      HostPodVO pod : podsInZone) {
        if (_podVlanMapDao.listPodVlanMapsByPod(pod.getId()).size() > 0) {
          throw new InvalidParameterValueException("String_Node_Str" + zone.getName() + "String_Node_Str");
        }
      }
    }
 else     if (podId != null) {
      if (!vlanId.equals(Vlan.UNTAGGED)) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
      List<AccountVlanMapVO> accountVlanMaps=_accountVlanMapDao.listAllIncludingRemoved();
      for (      AccountVlanMapVO accountVlanMap : accountVlanMaps) {
        VlanVO vlan=_vlanDao.findById(accountVlanMap.getVlanDbId());
        if (vlan.getDataCenterId() == zone.getId()) {
          throw new InvalidParameterValueException("String_Node_Str" + zone.getName() + "String_Node_Str");
        }
      }
    }
  }
 else {
    throw new InvalidParameterValueException("String_Node_Str" + VlanType.values().toString());
  }
  if (!NetUtils.isValidIp(vlanGateway)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (!NetUtils.isValidIp(vlanNetmask)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  String newVlanSubnet=NetUtils.getSubNet(vlanGateway,vlanNetmask);
  String guestNetworkCidr=zone.getGuestNetworkCidr();
  if (guestNetworkCidr != null) {
    String[] cidrPair=guestNetworkCidr.split("String_Node_Str");
    String guestIpNetwork=NetUtils.getIpRangeStartIpFromCidr(cidrPair[0],Long.parseLong(cidrPair[1]));
    long guestCidrSize=Long.parseLong(cidrPair[1]);
    long vlanCidrSize=NetUtils.getCidrSize(vlanNetmask);
    long cidrSizeToUse=-1;
    if (vlanCidrSize < guestCidrSize) {
      cidrSizeToUse=vlanCidrSize;
    }
 else {
      cidrSizeToUse=guestCidrSize;
    }
    String guestSubnet=NetUtils.getCidrSubNet(guestIpNetwork,cidrSizeToUse);
    if (newVlanSubnet.equals(guestSubnet)) {
      throw new InvalidParameterValueException("String_Node_Str" + zone.getName() + "String_Node_Str");
    }
  }
  checkPublicIpRangeErrors(zoneId,vlanId,vlanGateway,vlanNetmask,startIP,endIP);
  List<VlanVO> vlans=_vlanDao.listByZone(zone.getId());
  for (  VlanVO vlan : vlans) {
    String otherVlanGateway=vlan.getVlanGateway();
    String otherVlanSubnet=NetUtils.getSubNet(vlan.getVlanGateway(),vlan.getVlanNetmask());
    String[] otherVlanIpRange=vlan.getIpRange().split("String_Node_Str");
    String otherVlanStartIP=otherVlanIpRange[0];
    String otherVlanEndIP=null;
    if (otherVlanIpRange.length > 1) {
      otherVlanEndIP=otherVlanIpRange[1];
    }
    if (!vlanId.equals(vlan.getVlanTag()) && newVlanSubnet.equals(otherVlanSubnet)) {
      throw new InvalidParameterValueException("String_Node_Str" + vlan.getVlanTag() + "String_Node_Str"+ zone.getName()+ "String_Node_Str");
    }
    if (vlanId.equals(vlan.getVlanTag()) && newVlanSubnet.equals(otherVlanSubnet)) {
      if (NetUtils.ipRangesOverlap(startIP,endIP,otherVlanStartIP,otherVlanEndIP)) {
        throw new InvalidParameterValueException("String_Node_Str" + vlan.getVlanTag() + "String_Node_Str");
      }
      if (!vlanGateway.equals(otherVlanGateway)) {
        throw new InvalidParameterValueException("String_Node_Str" + vlan.getVlanTag() + "String_Node_Str"+ otherVlanGateway+ "String_Node_Str");
      }
    }
  }
  if (_zoneDao.findVnet(zoneId,vlanId).size() > 0) {
    throw new InvalidParameterValueException("String_Node_Str" + vlanId + "String_Node_Str"+ zone.getName());
  }
  if (zone.getNetworkType() == NetworkType.Basic && vlanId.equalsIgnoreCase(Vlan.UNTAGGED) && podId != null) {
    List<VlanVO> podVlans=_vlanDao.listVlansForPodByType(podId,VlanType.DirectAttached);
    if (podVlans != null && !podVlans.isEmpty()) {
      VlanVO podVlan=podVlans.get(0);
      if (!podVlan.getVlanNetmask().equals(vlanNetmask)) {
        throw new InvalidParameterValueException("String_Node_Str" + podVlan.getId() + "String_Node_Str"+ podId);
      }
 else       if (!podVlan.getVlanGateway().equals(vlanGateway)) {
        throw new InvalidParameterValueException("String_Node_Str" + podVlan.getId() + "String_Node_Str"+ podId);
      }
    }
  }
  String ipRange=startIP;
  if (endIP != null) {
    ipRange+="String_Node_Str" + endIP;
  }
  VlanVO vlan=new VlanVO(vlanType,vlanId,vlanGateway,vlanNetmask,zone.getId(),ipRange,networkId);
  vlan=_vlanDao.persist(vlan);
  if (account != null && vlanType.equals(VlanType.VirtualNetwork)) {
    if (!savePublicIPRangeForAccount(startIP,endIP,zoneId,vlan.getId(),account.getId(),account.getDomainId())) {
      deletePublicIPRange(vlan.getId());
      _vlanDao.expunge(vlan.getId());
      throw new CloudRuntimeException("String_Node_Str");
    }
  }
 else   if (!savePublicIPRange(startIP,endIP,zoneId,vlan.getId())) {
    deletePublicIPRange(vlan.getId());
    _vlanDao.expunge(vlan.getId());
    throw new CloudRuntimeException("String_Node_Str");
  }
  if (account != null) {
    AccountVlanMapVO accountVlanMapVO=new AccountVlanMapVO(account.getId(),vlan.getId());
    _accountVlanMapDao.persist(accountVlanMapVO);
  }
 else   if (podId != null) {
    PodVlanMapVO podVlanMapVO=new PodVlanMapVO(podId,vlan.getId());
    _podVlanMapDao.persist(podVlanMapVO);
  }
  String eventMsg="String_Node_Str" + vlanId + "String_Node_Str"+ vlanGateway+ "String_Node_Str"+ vlanNetmask+ "String_Node_Str"+ startIP;
  if (endIP != null) {
    eventMsg+="String_Node_Str" + endIP;
  }
  eventMsg+="String_Node_Str";
  if (associateIpRangeToAccount) {
    associateIpAddressListToAccount(userId,account.getId(),zoneId,vlan.getId());
  }
  return vlan;
}

Fixed version (ground truth):
@Override public Vlan createVlanAndPublicIpRange(Long userId,Long zoneId,Long podId,String startIP,String endIP,String vlanGateway,String vlanNetmask,boolean forVirtualNetwork,String vlanId,Account account,Long networkId) throws InsufficientCapacityException, ConcurrentOperationException, InvalidParameterValueException, ResourceUnavailableException {
  if (podId != null && ((_podDao.findById(podId)) == null)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (podId != null && _podDao.findById(podId).getDataCenterId() != zoneId) {
    throw new InvalidParameterValueException("String_Node_Str" + podId + "String_Node_Str"+ zoneId);
  }
  if (vlanId == null) {
    vlanId=Vlan.UNTAGGED;
  }
  DataCenterVO zone;
  if (zoneId == null || ((zone=_zoneDao.findById(zoneId)) == null)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (zone.getNetworkType() == NetworkType.Advanced && vlanId.equals(Vlan.UNTAGGED) && !forVirtualNetwork) {
    throw new InvalidParameterValueException("String_Node_Str" + zone.getId() + "String_Node_Str"+ zone.getNetworkType());
  }
 else   if (zone.getNetworkType() == NetworkType.Basic && !(vlanId.equals(Vlan.UNTAGGED) && !forVirtualNetwork)) {
    throw new InvalidParameterValueException("String_Node_Str" + zone.getId() + "String_Node_Str"+ zone.getNetworkType());
  }
  if (zone.getVnet() == null && forVirtualNetwork) {
    throw new InvalidParameterValueException("String_Node_Str" + zone.getId() + "String_Node_Str");
  }
  VlanType vlanType=forVirtualNetwork ? VlanType.VirtualNetwork : VlanType.DirectAttached;
  String hypervisorType=_configDao.getValue("String_Node_Str");
  if (hypervisorType.equalsIgnoreCase("String_Node_Str")) {
    if (vlanType.toString().equalsIgnoreCase("String_Node_Str") && vlanId.equalsIgnoreCase(Vlan.UNTAGGED)) {
      if (_configDao.getValue("String_Node_Str") == null || _configDao.getValue("String_Node_Str").equals("String_Node_Str")) {
        throw new CloudRuntimeException("String_Node_Str");
      }
    }
  }
  checkAccess(account,zone);
  boolean associateIpRangeToAccount=false;
  if (vlanType.equals(VlanType.VirtualNetwork)) {
    if (account != null) {
      long ipResourceLimit=_accountMgr.findCorrectResourceLimit((AccountVO)account,ResourceType.public_ip);
      long accountIpRange=NetUtils.ip2Long(endIP) - NetUtils.ip2Long(startIP) + 1;
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + ipResourceLimit + "String_Node_Str"+ accountIpRange);
      }
      if (ipResourceLimit != -1 && accountIpRange > ipResourceLimit) {
        throw new InvalidParameterValueException("String_Node_Str" + ipResourceLimit + "String_Node_Str"+ accountIpRange+ "String_Node_Str");
      }
      associateIpRangeToAccount=true;
    }
  }
 else   if (vlanType.equals(VlanType.DirectAttached)) {
    if (account != null) {
      if (vlanId.equals(Vlan.UNTAGGED)) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
      List<HostPodVO> podsInZone=_podDao.listByDataCenterId(zone.getId());
      for (      HostPodVO pod : podsInZone) {
        if (_podVlanMapDao.listPodVlanMapsByPod(pod.getId()).size() > 0) {
          throw new InvalidParameterValueException("String_Node_Str" + zone.getName() + "String_Node_Str");
        }
      }
    }
 else     if (podId != null) {
      if (!vlanId.equals(Vlan.UNTAGGED)) {
        throw new InvalidParameterValueException("String_Node_Str");
      }
      List<AccountVlanMapVO> accountVlanMaps=_accountVlanMapDao.listAllIncludingRemoved();
      for (      AccountVlanMapVO accountVlanMap : accountVlanMaps) {
        VlanVO vlan=_vlanDao.findById(accountVlanMap.getVlanDbId());
        if (vlan.getDataCenterId() == zone.getId()) {
          throw new InvalidParameterValueException("String_Node_Str" + zone.getName() + "String_Node_Str");
        }
      }
    }
  }
 else {
    throw new InvalidParameterValueException("String_Node_Str" + VlanType.values().toString());
  }
  if (!NetUtils.isValidIp(vlanGateway)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  if (!NetUtils.isValidIp(vlanNetmask)) {
    throw new InvalidParameterValueException("String_Node_Str");
  }
  String newVlanSubnet=NetUtils.getSubNet(vlanGateway,vlanNetmask);
  String guestNetworkCidr=zone.getGuestNetworkCidr();
  if (guestNetworkCidr != null) {
    String[] cidrPair=guestNetworkCidr.split("String_Node_Str");
    String guestIpNetwork=NetUtils.getIpRangeStartIpFromCidr(cidrPair[0],Long.parseLong(cidrPair[1]));
    long guestCidrSize=Long.parseLong(cidrPair[1]);
    long vlanCidrSize=NetUtils.getCidrSize(vlanNetmask);
    long cidrSizeToUse=-1;
    if (vlanCidrSize < guestCidrSize) {
      cidrSizeToUse=vlanCidrSize;
    }
 else {
      cidrSizeToUse=guestCidrSize;
    }
    String guestSubnet=NetUtils.getCidrSubNet(guestIpNetwork,cidrSizeToUse);
    if (newVlanSubnet.equals(guestSubnet)) {
      throw new InvalidParameterValueException("String_Node_Str" + zone.getName() + "String_Node_Str");
    }
  }
  checkPublicIpRangeErrors(zoneId,vlanId,vlanGateway,vlanNetmask,startIP,endIP);
  List<VlanVO> vlans=_vlanDao.listByZone(zone.getId());
  for (  VlanVO vlan : vlans) {
    String otherVlanGateway=vlan.getVlanGateway();
    String otherVlanSubnet=NetUtils.getSubNet(vlan.getVlanGateway(),vlan.getVlanNetmask());
    String[] otherVlanIpRange=vlan.getIpRange().split("String_Node_Str");
    String otherVlanStartIP=otherVlanIpRange[0];
    String otherVlanEndIP=null;
    if (otherVlanIpRange.length > 1) {
      otherVlanEndIP=otherVlanIpRange[1];
    }
    if (!vlanId.equals(vlan.getVlanTag()) && newVlanSubnet.equals(otherVlanSubnet)) {
      throw new InvalidParameterValueException("String_Node_Str" + vlan.getVlanTag() + "String_Node_Str"+ zone.getName()+ "String_Node_Str");
    }
    if (vlanId.equals(vlan.getVlanTag()) && newVlanSubnet.equals(otherVlanSubnet)) {
      if (NetUtils.ipRangesOverlap(startIP,endIP,otherVlanStartIP,otherVlanEndIP)) {
        throw new InvalidParameterValueException("String_Node_Str" + vlan.getVlanTag() + "String_Node_Str");
      }
      if (!vlanGateway.equals(otherVlanGateway)) {
        throw new InvalidParameterValueException("String_Node_Str" + vlan.getVlanTag() + "String_Node_Str"+ otherVlanGateway+ "String_Node_Str");
      }
    }
  }
  if (_zoneDao.findVnet(zoneId,vlanId).size() > 0) {
    throw new InvalidParameterValueException("String_Node_Str" + vlanId + "String_Node_Str"+ zone.getName());
  }
  if (zone.getNetworkType() == NetworkType.Basic && vlanId.equalsIgnoreCase(Vlan.UNTAGGED) && podId != null) {
    List<VlanVO> podVlans=_vlanDao.listVlansForPodByType(podId,VlanType.DirectAttached);
    if (podVlans != null && !podVlans.isEmpty()) {
      VlanVO podVlan=podVlans.get(0);
      if (!podVlan.getVlanNetmask().equals(vlanNetmask)) {
        throw new InvalidParameterValueException("String_Node_Str" + podVlan.getId() + "String_Node_Str"+ podId);
      }
 else       if (!podVlan.getVlanGateway().equals(vlanGateway)) {
        throw new InvalidParameterValueException("String_Node_Str" + podVlan.getId() + "String_Node_Str"+ podId);
      }
    }
  }
  String ipRange=startIP;
  if (endIP != null) {
    ipRange+="String_Node_Str" + endIP;
  }
  VlanVO vlan=new VlanVO(vlanType,vlanId,vlanGateway,vlanNetmask,zone.getId(),ipRange,networkId);
  vlan=_vlanDao.persist(vlan);
  if (!savePublicIPRange(startIP,endIP,zoneId,vlan.getId())) {
    deletePublicIPRange(vlan.getId());
    _vlanDao.expunge(vlan.getId());
    throw new CloudRuntimeException("String_Node_Str");
  }
  if (account != null) {
    AccountVlanMapVO accountVlanMapVO=new AccountVlanMapVO(account.getId(),vlan.getId());
    _accountVlanMapDao.persist(accountVlanMapVO);
  }
 else   if (podId != null) {
    PodVlanMapVO podVlanMapVO=new PodVlanMapVO(podId,vlan.getId());
    _podVlanMapDao.persist(podVlanMapVO);
  }
  String eventMsg="String_Node_Str" + vlanId + "String_Node_Str"+ vlanGateway+ "String_Node_Str"+ vlanNetmask+ "String_Node_Str"+ startIP;
  if (endIP != null) {
    eventMsg+="String_Node_Str" + endIP;
  }
  eventMsg+="String_Node_Str";
  if (associateIpRangeToAccount) {
    long ipCount=_publicIpAddressDao.countIPs(zoneId,vlan.getId(),false);
    _accountMgr.incrementResourceCount(account.getId(),ResourceType.public_ip,ipCount);
    s_logger.trace("String_Node_Str" + ResourceType.public_ip + "String_Node_Str"+ ipCount+ "String_Node_Str"+ account);
    _networkMgr.associateIpAddressListToAccount(userId,account.getId(),zoneId,vlan.getId());
  }
  return vlan;
}