Program: 105536
Project: 105536
Buggy source:
@Override @DB @ActionEvent(eventType=EventTypes.EVENT_GUEST_VLAN_RANGE_DEDICATE,eventDescription="String_Node_Str",async=false) public GuestVlan dedicateGuestVlanRange(DedicateGuestVlanRangeCmd cmd){
  String vlan=cmd.getVlan();
  String accountName=cmd.getAccountName();
  Long domainId=cmd.getDomainId();
  Long physicalNetworkId=cmd.getPhysicalNetworkId();
  Long projectId=cmd.getProjectId();
  int startVlan, endVlan;
  String updatedVlanRange=null;
  long guestVlanMapId=0;
  long guestVlanMapAccountId=0;
  long vlanOwnerId=0;
  Account vlanOwner=null;
  if (projectId != null) {
    if (accountName != null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    Project project=_projectMgr.getProject(projectId);
    if (project == null) {
      throw new InvalidParameterValueException("String_Node_Str" + projectId);
    }
    vlanOwner=_accountMgr.getAccount(project.getProjectAccountId());
  }
  if ((accountName != null) && (domainId != null)) {
    vlanOwner=_accountDao.findActiveAccount(accountName,domainId);
    if (vlanOwner == null) {
      throw new InvalidParameterValueException("String_Node_Str" + accountName);
    }
  }
  vlanOwnerId=vlanOwner.getAccountId();
  PhysicalNetworkVO physicalNetwork=_physicalNetworkDao.findById(physicalNetworkId);
  if (physicalNetwork == null) {
    throw new InvalidParameterValueException("String_Node_Str" + physicalNetworkId);
  }
 else   if (!physicalNetwork.getIsolationMethods().isEmpty() && !physicalNetwork.getIsolationMethods().contains("String_Node_Str")) {
    throw new InvalidParameterValueException("String_Node_Str" + "String_Node_Str" + physicalNetworkId + "String_Node_Str");
  }
  String[] vlanRange=vlan.split("String_Node_Str");
  if (vlanRange.length != 2) {
    throw new InvalidParameterValueException("String_Node_Str" + vlan + "String_Node_Str");
  }
  try {
    startVlan=Integer.parseInt(vlanRange[0]);
    endVlan=Integer.parseInt(vlanRange[1]);
  }
 catch (  NumberFormatException e) {
    s_logger.warn("String_Node_Str",e);
    throw new InvalidParameterValueException("String_Node_Str");
  }
  List<Pair<Integer,Integer>> existingRanges=physicalNetwork.getVnet();
  Boolean exists=false;
  if (!existingRanges.isEmpty()) {
    for (int i=0; i < existingRanges.size(); i++) {
      int existingStartVlan=existingRanges.get(i).first();
      int existingEndVlan=existingRanges.get(i).second();
      if (startVlan <= endVlan && startVlan >= existingStartVlan && endVlan <= existingEndVlan) {
        exists=true;
        break;
      }
    }
    if (!exists) {
      throw new InvalidParameterValueException("String_Node_Str" + vlan);
    }
  }
  for (int i=startVlan; i <= endVlan; i++) {
    List<DataCenterVnetVO> allocatedVlans=_datacneterVnet.listAllocatedVnetsInRange(physicalNetwork.getDataCenterId(),physicalNetwork.getId(),startVlan,endVlan);
    if (allocatedVlans != null && !allocatedVlans.isEmpty()) {
      for (      DataCenterVnetVO allocatedVlan : allocatedVlans) {
        if (allocatedVlan.getAccountId() != vlanOwner.getAccountId()) {
          throw new InvalidParameterValueException("String_Node_Str" + allocatedVlan.getVnet() + "String_Node_Str"+ "String_Node_Str");
        }
      }
    }
  }
  List<AccountGuestVlanMapVO> guestVlanMaps=_accountGuestVlanMapDao.listAccountGuestVlanMapsByPhysicalNetwork(physicalNetworkId);
  for (  AccountGuestVlanMapVO guestVlanMap : guestVlanMaps) {
    List<Integer> vlanTokens=getVlanFromRange(guestVlanMap.getGuestVlanRange());
    int dedicatedStartVlan=vlanTokens.get(0).intValue();
    int dedicatedEndVlan=vlanTokens.get(1).intValue();
    if ((startVlan < dedicatedStartVlan & endVlan >= dedicatedStartVlan) || (startVlan >= dedicatedStartVlan & startVlan <= dedicatedEndVlan)) {
      throw new InvalidParameterValueException("String_Node_Str" + "String_Node_Str" + vlan);
    }
  }
  Collections.sort(guestVlanMaps,new Comparator<AccountGuestVlanMapVO>(){
    @Override public int compare(    AccountGuestVlanMapVO obj1,    AccountGuestVlanMapVO obj2){
      List<Integer> vlanTokens1=getVlanFromRange(obj1.getGuestVlanRange());
      List<Integer> vlanTokens2=getVlanFromRange(obj2.getGuestVlanRange());
      return vlanTokens1.get(0).compareTo(vlanTokens2.get(0));
    }
  }
);
  for (int i=0; i < guestVlanMaps.size(); i++) {
    guestVlanMapId=guestVlanMaps.get(i).getId();
    guestVlanMapAccountId=guestVlanMaps.get(i).getAccountId();
    List<Integer> vlanTokens1=getVlanFromRange(guestVlanMaps.get(i).getGuestVlanRange());
    if (endVlan == (vlanTokens1.get(0).intValue() - 1)) {
      if (guestVlanMapAccountId == vlanOwnerId) {
        updatedVlanRange=startVlan + "String_Node_Str" + vlanTokens1.get(1).intValue();
      }
      break;
    }
    if (startVlan == (vlanTokens1.get(1).intValue() + 1) & guestVlanMapAccountId == vlanOwnerId) {
      if (i != (guestVlanMaps.size() - 1)) {
        List<Integer> vlanTokens2=getVlanFromRange(guestVlanMaps.get(i + 1).getGuestVlanRange());
        if (endVlan == (vlanTokens2.get(0).intValue() - 1) & guestVlanMaps.get(i + 1).getAccountId() == vlanOwnerId) {
          _datacneterVnet.releaseDedicatedGuestVlans(guestVlanMaps.get(i + 1).getId());
          _accountGuestVlanMapDao.remove(guestVlanMaps.get(i + 1).getId());
          updatedVlanRange=vlanTokens1.get(0).intValue() + "String_Node_Str" + vlanTokens2.get(1).intValue();
          break;
        }
      }
      updatedVlanRange=vlanTokens1.get(0).intValue() + "String_Node_Str" + endVlan;
      break;
    }
  }
  AccountGuestVlanMapVO accountGuestVlanMapVO;
  if (updatedVlanRange != null) {
    accountGuestVlanMapVO=_accountGuestVlanMapDao.findById(guestVlanMapId);
    accountGuestVlanMapVO.setGuestVlanRange(updatedVlanRange);
    _accountGuestVlanMapDao.update(guestVlanMapId,accountGuestVlanMapVO);
  }
 else {
    accountGuestVlanMapVO=new AccountGuestVlanMapVO(vlanOwner.getAccountId(),physicalNetworkId);
    accountGuestVlanMapVO.setGuestVlanRange(startVlan + "String_Node_Str" + endVlan);
    _accountGuestVlanMapDao.persist(accountGuestVlanMapVO);
  }
  List<Integer> finaVlanTokens=getVlanFromRange(accountGuestVlanMapVO.getGuestVlanRange());
  for (int i=finaVlanTokens.get(0).intValue(); i <= finaVlanTokens.get(1).intValue(); i++) {
    List<DataCenterVnetVO> dataCenterVnet=_datacneterVnet.findVnet(physicalNetwork.getDataCenterId(),physicalNetworkId,((Integer)i).toString());
    dataCenterVnet.get(0).setAccountGuestVlanMapId(accountGuestVlanMapVO.getId());
    _datacneterVnet.update(dataCenterVnet.get(0).getId(),dataCenterVnet.get(0));
  }
  return accountGuestVlanMapVO;
}

Fixed version (ground truth):
@Override @DB @ActionEvent(eventType=EventTypes.EVENT_GUEST_VLAN_RANGE_DEDICATE,eventDescription="String_Node_Str",async=false) public GuestVlan dedicateGuestVlanRange(DedicateGuestVlanRangeCmd cmd){
  String vlan=cmd.getVlan();
  String accountName=cmd.getAccountName();
  Long domainId=cmd.getDomainId();
  Long physicalNetworkId=cmd.getPhysicalNetworkId();
  Long projectId=cmd.getProjectId();
  int startVlan, endVlan;
  String updatedVlanRange=null;
  long guestVlanMapId=0;
  long guestVlanMapAccountId=0;
  long vlanOwnerId=0;
  Account vlanOwner=null;
  if (projectId != null) {
    if (accountName != null) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    Project project=_projectMgr.getProject(projectId);
    if (project == null) {
      throw new InvalidParameterValueException("String_Node_Str" + projectId);
    }
    vlanOwner=_accountMgr.getAccount(project.getProjectAccountId());
  }
  if ((accountName != null) && (domainId != null)) {
    vlanOwner=_accountDao.findActiveAccount(accountName,domainId);
  }
  if (vlanOwner == null) {
    throw new InvalidParameterValueException("String_Node_Str" + accountName);
  }
  vlanOwnerId=vlanOwner.getAccountId();
  PhysicalNetworkVO physicalNetwork=_physicalNetworkDao.findById(physicalNetworkId);
  if (physicalNetwork == null) {
    throw new InvalidParameterValueException("String_Node_Str" + physicalNetworkId);
  }
 else   if (!physicalNetwork.getIsolationMethods().isEmpty() && !physicalNetwork.getIsolationMethods().contains("String_Node_Str")) {
    throw new InvalidParameterValueException("String_Node_Str" + "String_Node_Str" + physicalNetworkId + "String_Node_Str");
  }
  String[] vlanRange=vlan.split("String_Node_Str");
  if (vlanRange.length != 2) {
    throw new InvalidParameterValueException("String_Node_Str" + vlan + "String_Node_Str");
  }
  try {
    startVlan=Integer.parseInt(vlanRange[0]);
    endVlan=Integer.parseInt(vlanRange[1]);
  }
 catch (  NumberFormatException e) {
    s_logger.warn("String_Node_Str",e);
    throw new InvalidParameterValueException("String_Node_Str");
  }
  List<Pair<Integer,Integer>> existingRanges=physicalNetwork.getVnet();
  Boolean exists=false;
  if (!existingRanges.isEmpty()) {
    for (int i=0; i < existingRanges.size(); i++) {
      int existingStartVlan=existingRanges.get(i).first();
      int existingEndVlan=existingRanges.get(i).second();
      if (startVlan <= endVlan && startVlan >= existingStartVlan && endVlan <= existingEndVlan) {
        exists=true;
        break;
      }
    }
    if (!exists) {
      throw new InvalidParameterValueException("String_Node_Str" + vlan);
    }
  }
  for (int i=startVlan; i <= endVlan; i++) {
    List<DataCenterVnetVO> allocatedVlans=_datacneterVnet.listAllocatedVnetsInRange(physicalNetwork.getDataCenterId(),physicalNetwork.getId(),startVlan,endVlan);
    if (allocatedVlans != null && !allocatedVlans.isEmpty()) {
      for (      DataCenterVnetVO allocatedVlan : allocatedVlans) {
        if (allocatedVlan.getAccountId() != vlanOwner.getAccountId()) {
          throw new InvalidParameterValueException("String_Node_Str" + allocatedVlan.getVnet() + "String_Node_Str"+ "String_Node_Str");
        }
      }
    }
  }
  List<AccountGuestVlanMapVO> guestVlanMaps=_accountGuestVlanMapDao.listAccountGuestVlanMapsByPhysicalNetwork(physicalNetworkId);
  for (  AccountGuestVlanMapVO guestVlanMap : guestVlanMaps) {
    List<Integer> vlanTokens=getVlanFromRange(guestVlanMap.getGuestVlanRange());
    int dedicatedStartVlan=vlanTokens.get(0).intValue();
    int dedicatedEndVlan=vlanTokens.get(1).intValue();
    if ((startVlan < dedicatedStartVlan & endVlan >= dedicatedStartVlan) || (startVlan >= dedicatedStartVlan & startVlan <= dedicatedEndVlan)) {
      throw new InvalidParameterValueException("String_Node_Str" + "String_Node_Str" + vlan);
    }
  }
  Collections.sort(guestVlanMaps,new Comparator<AccountGuestVlanMapVO>(){
    @Override public int compare(    AccountGuestVlanMapVO obj1,    AccountGuestVlanMapVO obj2){
      List<Integer> vlanTokens1=getVlanFromRange(obj1.getGuestVlanRange());
      List<Integer> vlanTokens2=getVlanFromRange(obj2.getGuestVlanRange());
      return vlanTokens1.get(0).compareTo(vlanTokens2.get(0));
    }
  }
);
  for (int i=0; i < guestVlanMaps.size(); i++) {
    guestVlanMapId=guestVlanMaps.get(i).getId();
    guestVlanMapAccountId=guestVlanMaps.get(i).getAccountId();
    List<Integer> vlanTokens1=getVlanFromRange(guestVlanMaps.get(i).getGuestVlanRange());
    if (endVlan == (vlanTokens1.get(0).intValue() - 1)) {
      if (guestVlanMapAccountId == vlanOwnerId) {
        updatedVlanRange=startVlan + "String_Node_Str" + vlanTokens1.get(1).intValue();
      }
      break;
    }
    if (startVlan == (vlanTokens1.get(1).intValue() + 1) & guestVlanMapAccountId == vlanOwnerId) {
      if (i != (guestVlanMaps.size() - 1)) {
        List<Integer> vlanTokens2=getVlanFromRange(guestVlanMaps.get(i + 1).getGuestVlanRange());
        if (endVlan == (vlanTokens2.get(0).intValue() - 1) & guestVlanMaps.get(i + 1).getAccountId() == vlanOwnerId) {
          _datacneterVnet.releaseDedicatedGuestVlans(guestVlanMaps.get(i + 1).getId());
          _accountGuestVlanMapDao.remove(guestVlanMaps.get(i + 1).getId());
          updatedVlanRange=vlanTokens1.get(0).intValue() + "String_Node_Str" + vlanTokens2.get(1).intValue();
          break;
        }
      }
      updatedVlanRange=vlanTokens1.get(0).intValue() + "String_Node_Str" + endVlan;
      break;
    }
  }
  AccountGuestVlanMapVO accountGuestVlanMapVO;
  if (updatedVlanRange != null) {
    accountGuestVlanMapVO=_accountGuestVlanMapDao.findById(guestVlanMapId);
    accountGuestVlanMapVO.setGuestVlanRange(updatedVlanRange);
    _accountGuestVlanMapDao.update(guestVlanMapId,accountGuestVlanMapVO);
  }
 else {
    accountGuestVlanMapVO=new AccountGuestVlanMapVO(vlanOwner.getAccountId(),physicalNetworkId);
    accountGuestVlanMapVO.setGuestVlanRange(startVlan + "String_Node_Str" + endVlan);
    _accountGuestVlanMapDao.persist(accountGuestVlanMapVO);
  }
  List<Integer> finaVlanTokens=getVlanFromRange(accountGuestVlanMapVO.getGuestVlanRange());
  for (int i=finaVlanTokens.get(0).intValue(); i <= finaVlanTokens.get(1).intValue(); i++) {
    List<DataCenterVnetVO> dataCenterVnet=_datacneterVnet.findVnet(physicalNetwork.getDataCenterId(),physicalNetworkId,((Integer)i).toString());
    dataCenterVnet.get(0).setAccountGuestVlanMapId(accountGuestVlanMapVO.getId());
    _datacneterVnet.update(dataCenterVnet.get(0).getId(),dataCenterVnet.get(0));
  }
  return accountGuestVlanMapVO;
}