Program: 106784
Project: 106784
Buggy source:
@Override public UserVm createAdvancedVirtualMachine(DataCenter zone,ServiceOffering serviceOffering,VirtualMachineTemplate template,List<Long> networkIdList,Account owner,String hostName,String displayName,Long diskOfferingId,Long diskSize,String group,HypervisorType hypervisor,String userData,String sshKeyPair,Map<Long,String> requestedIps,String defaultIp) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, StorageUnavailableException, ResourceAllocationException {
  Account caller=UserContext.current().getCaller();
  List<NetworkVO> networkList=new ArrayList<NetworkVO>();
  _accountMgr.checkAccess(caller,owner);
  if (networkIdList == null || networkIdList.isEmpty()) {
    NetworkVO defaultNetwork=null;
    List<NetworkOfferingVO> defaultVirtualOffering=_networkOfferingDao.listByTrafficTypeAndGuestType(false,TrafficType.Guest,GuestIpType.Virtual);
    if (defaultVirtualOffering.get(0).getAvailability() == Availability.Required) {
      List<NetworkVO> virtualNetworks=_networkMgr.listNetworksForAccount(owner.getId(),zone.getId(),GuestIpType.Virtual,true);
      if (virtualNetworks.isEmpty()) {
        s_logger.debug("String_Node_Str" + owner + "String_Node_Str");
        Network newNetwork=_networkMgr.createNetwork(defaultVirtualOffering.get(0).getId(),owner.getAccountName() + "String_Node_Str",owner.getAccountName() + "String_Node_Str",false,null,zone.getId(),null,null,null,null,owner,false,null,null);
        defaultNetwork=_networkDao.findById(newNetwork.getId());
      }
 else       if (virtualNetworks.size() > 1) {
        throw new InvalidParameterValueException("String_Node_Str" + owner + "String_Node_Str");
      }
 else {
        defaultNetwork=virtualNetworks.get(0);
      }
    }
 else {
      List<NetworkVO> defaultNetworks=_networkMgr.listNetworksForAccount(owner.getId(),zone.getId(),null,true);
      if (defaultNetworks.isEmpty()) {
        if (defaultVirtualOffering.get(0).getAvailability() == Availability.Optional) {
          s_logger.debug("String_Node_Str" + owner + "String_Node_Str");
          Network newNetwork=_networkMgr.createNetwork(defaultVirtualOffering.get(0).getId(),owner.getAccountName() + "String_Node_Str",owner.getAccountName() + "String_Node_Str",false,null,zone.getId(),null,null,null,null,owner,false,null,null);
          defaultNetwork=_networkDao.findById(newNetwork.getId());
        }
 else {
          throw new InvalidParameterValueException("String_Node_Str" + owner);
        }
      }
 else       if (defaultNetworks.size() > 1) {
        throw new InvalidParameterValueException("String_Node_Str" + owner);
      }
 else {
        defaultNetwork=defaultNetworks.get(0);
      }
    }
    NetworkOffering networkOffering=_configMgr.getNetworkOffering(defaultNetwork.getNetworkOfferingId());
    if (networkOffering.getAvailability() == Availability.Unavailable) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    networkList.add(defaultNetwork);
  }
 else {
    boolean requiredNetworkOfferingIsPresent=false;
    List<NetworkOfferingVO> requiredOfferings=_networkOfferingDao.listByAvailability(Availability.Required,false);
    Long requiredOfferingId=null;
    if (!requiredOfferings.isEmpty()) {
      requiredOfferingId=requiredOfferings.get(0).getId();
    }
    for (    Long networkId : networkIdList) {
      NetworkVO network=_networkDao.findById(networkId);
      if (network == null) {
        throw new InvalidParameterValueException("String_Node_Str" + networkIdList.get(0).longValue());
      }
      if (!network.getIsShared()) {
        List<NetworkVO> networkMap=_networkDao.listBy(owner.getId(),network.getId());
        if (networkMap == null || networkMap.isEmpty()) {
          throw new PermissionDeniedException("String_Node_Str" + network.getId() + "String_Node_Str");
        }
      }
 else {
        if (!_networkMgr.isNetworkAvailableInDomain(networkId,owner.getDomainId())) {
          throw new PermissionDeniedException("String_Node_Str" + networkId + "String_Node_Str"+ owner.getDomainId());
        }
      }
      NetworkOffering networkOffering=_configMgr.getNetworkOffering(network.getNetworkOfferingId());
      if (networkOffering.getAvailability() == Availability.Unavailable) {
        throw new InvalidParameterValueException("String_Node_Str" + network.getId() + "String_Node_Str"+ Availability.Unavailable);
      }
      if (networkOffering.isSystemOnly()) {
        throw new InvalidParameterValueException("String_Node_Str" + networkId + "String_Node_Str");
      }
      if (requiredOfferingId != null && network.getNetworkOfferingId() == requiredOfferingId.longValue()) {
        requiredNetworkOfferingIsPresent=true;
      }
      networkList.add(network);
    }
    if (requiredOfferingId != null && !requiredNetworkOfferingIsPresent) {
      throw new InvalidParameterValueException("String_Node_Str" + requiredOfferingId + "String_Node_Str");
    }
  }
  return createVirtualMachine(zone,serviceOffering,template,hostName,displayName,owner,diskOfferingId,diskSize,networkList,null,group,userData,sshKeyPair,hypervisor,caller,requestedIps,defaultIp);
}

Fixed version (ground truth):
@Override public UserVm createAdvancedVirtualMachine(DataCenter zone,ServiceOffering serviceOffering,VirtualMachineTemplate template,List<Long> networkIdList,Account owner,String hostName,String displayName,Long diskOfferingId,Long diskSize,String group,HypervisorType hypervisor,String userData,String sshKeyPair,Map<Long,String> requestedIps,String defaultIp,String keyboard) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, StorageUnavailableException, ResourceAllocationException {
  Account caller=UserContext.current().getCaller();
  List<NetworkVO> networkList=new ArrayList<NetworkVO>();
  _accountMgr.checkAccess(caller,owner);
  if (networkIdList == null || networkIdList.isEmpty()) {
    NetworkVO defaultNetwork=null;
    List<NetworkOfferingVO> defaultVirtualOffering=_networkOfferingDao.listByTrafficTypeAndGuestType(false,TrafficType.Guest,GuestIpType.Virtual);
    if (defaultVirtualOffering.get(0).getAvailability() == Availability.Required) {
      List<NetworkVO> virtualNetworks=_networkMgr.listNetworksForAccount(owner.getId(),zone.getId(),GuestIpType.Virtual,true);
      if (virtualNetworks.isEmpty()) {
        s_logger.debug("String_Node_Str" + owner + "String_Node_Str");
        Network newNetwork=_networkMgr.createNetwork(defaultVirtualOffering.get(0).getId(),owner.getAccountName() + "String_Node_Str",owner.getAccountName() + "String_Node_Str",false,null,zone.getId(),null,null,null,null,owner,false,null,null);
        defaultNetwork=_networkDao.findById(newNetwork.getId());
      }
 else       if (virtualNetworks.size() > 1) {
        throw new InvalidParameterValueException("String_Node_Str" + owner + "String_Node_Str");
      }
 else {
        defaultNetwork=virtualNetworks.get(0);
      }
    }
 else {
      List<NetworkVO> defaultNetworks=_networkMgr.listNetworksForAccount(owner.getId(),zone.getId(),null,true);
      if (defaultNetworks.isEmpty()) {
        if (defaultVirtualOffering.get(0).getAvailability() == Availability.Optional) {
          s_logger.debug("String_Node_Str" + owner + "String_Node_Str");
          Network newNetwork=_networkMgr.createNetwork(defaultVirtualOffering.get(0).getId(),owner.getAccountName() + "String_Node_Str",owner.getAccountName() + "String_Node_Str",false,null,zone.getId(),null,null,null,null,owner,false,null,null);
          defaultNetwork=_networkDao.findById(newNetwork.getId());
        }
 else {
          throw new InvalidParameterValueException("String_Node_Str" + owner);
        }
      }
 else       if (defaultNetworks.size() > 1) {
        throw new InvalidParameterValueException("String_Node_Str" + owner);
      }
 else {
        defaultNetwork=defaultNetworks.get(0);
      }
    }
    NetworkOffering networkOffering=_configMgr.getNetworkOffering(defaultNetwork.getNetworkOfferingId());
    if (networkOffering.getAvailability() == Availability.Unavailable) {
      throw new InvalidParameterValueException("String_Node_Str");
    }
    networkList.add(defaultNetwork);
  }
 else {
    boolean requiredNetworkOfferingIsPresent=false;
    List<NetworkOfferingVO> requiredOfferings=_networkOfferingDao.listByAvailability(Availability.Required,false);
    Long requiredOfferingId=null;
    if (!requiredOfferings.isEmpty()) {
      requiredOfferingId=requiredOfferings.get(0).getId();
    }
    for (    Long networkId : networkIdList) {
      NetworkVO network=_networkDao.findById(networkId);
      if (network == null) {
        throw new InvalidParameterValueException("String_Node_Str" + networkIdList.get(0).longValue());
      }
      if (!network.getIsShared()) {
        List<NetworkVO> networkMap=_networkDao.listBy(owner.getId(),network.getId());
        if (networkMap == null || networkMap.isEmpty()) {
          throw new PermissionDeniedException("String_Node_Str" + network.getId() + "String_Node_Str");
        }
      }
 else {
        if (!_networkMgr.isNetworkAvailableInDomain(networkId,owner.getDomainId())) {
          throw new PermissionDeniedException("String_Node_Str" + networkId + "String_Node_Str"+ owner.getDomainId());
        }
      }
      NetworkOffering networkOffering=_configMgr.getNetworkOffering(network.getNetworkOfferingId());
      if (networkOffering.getAvailability() == Availability.Unavailable) {
        throw new InvalidParameterValueException("String_Node_Str" + network.getId() + "String_Node_Str"+ Availability.Unavailable);
      }
      if (networkOffering.isSystemOnly()) {
        throw new InvalidParameterValueException("String_Node_Str" + networkId + "String_Node_Str");
      }
      if (requiredOfferingId != null && network.getNetworkOfferingId() == requiredOfferingId.longValue()) {
        requiredNetworkOfferingIsPresent=true;
      }
      networkList.add(network);
    }
    if (requiredOfferingId != null && !requiredNetworkOfferingIsPresent) {
      throw new InvalidParameterValueException("String_Node_Str" + requiredOfferingId + "String_Node_Str");
    }
  }
  return createVirtualMachine(zone,serviceOffering,template,hostName,displayName,owner,diskOfferingId,diskSize,networkList,null,group,userData,sshKeyPair,hypervisor,caller,requestedIps,defaultIp,keyboard);
}