Program: 89151
Project: 89151
Buggy source:
@DB protected List<DomainRouterVO> findOrDeployVirtualRouters(Network guestNetwork,DeployDestination dest,Account owner,boolean isRedundant,Map<Param,Object> params) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
  Network network=_networkDao.acquireInLockTable(guestNetwork.getId());
  if (network == null) {
    throw new ConcurrentOperationException("String_Node_Str" + guestNetwork.getId());
  }
  long dcId=dest.getDataCenter().getId();
  DataCenterDeployment plan=new DataCenterDeployment(dcId);
  boolean isPodBased=(dest.getDataCenter().getNetworkType() == NetworkType.Basic || _networkMgr.areServicesSupportedInNetwork(guestNetwork.getId(),Service.SecurityGroup)) && guestNetwork.getTrafficType() == TrafficType.Guest;
  boolean publicNetwork=false;
  if (_networkMgr.areServicesSupportedInNetwork(guestNetwork.getId(),Service.SourceNat)) {
    publicNetwork=true;
  }
  if (isRedundant && !publicNetwork) {
    s_logger.error("String_Node_Str");
    return null;
  }
  List<DomainRouterVO> routers;
  Long podId=null;
  if (isPodBased) {
    Pod pod=dest.getPod();
    if (pod != null) {
      podId=pod.getId();
    }
  }
  if (publicNetwork) {
    routers=_routerDao.listByNetworkAndRole(guestNetwork.getId(),Role.VIRTUAL_ROUTER);
  }
 else {
    if (isPodBased && podId != null) {
      routers=_routerDao.listByNetworkAndPodAndRole(guestNetwork.getId(),podId,Role.VIRTUAL_ROUTER);
      plan=new DataCenterDeployment(dcId,podId,null,null,null,null);
    }
 else {
      routers=_routerDao.listByNetworkAndRole(guestNetwork.getId(),Role.VIRTUAL_ROUTER);
      plan=new DataCenterDeployment(dcId);
    }
  }
  try {
    int routerCount=1;
    if (isRedundant) {
      routerCount=2;
    }
    for (    DomainRouterVO router : routers) {
      if (!router.getIsRedundantRouter()) {
        routerCount=1;
      }
    }
    if (routers.size() >= routerCount || (isPodBased && podId == null)) {
      return routers;
    }
    if (routers.size() >= 5) {
      s_logger.error("String_Node_Str");
    }
    NicProfile defaultNic=new NicProfile();
    if (publicNetwork) {
      PublicIp sourceNatIp=_networkMgr.assignSourceNatIpAddress(owner,guestNetwork,_accountMgr.getSystemUser().getId());
      defaultNic.setDefaultNic(true);
      defaultNic.setIp4Address(sourceNatIp.getAddress().addr());
      defaultNic.setGateway(sourceNatIp.getGateway());
      defaultNic.setNetmask(sourceNatIp.getNetmask());
      defaultNic.setMacAddress(sourceNatIp.getMacAddress());
      defaultNic.setBroadcastType(BroadcastDomainType.Vlan);
      defaultNic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(sourceNatIp.getVlanTag()));
      defaultNic.setIsolationUri(IsolationType.Vlan.toUri(sourceNatIp.getVlanTag()));
      defaultNic.setDeviceId(2);
    }
    int count=routerCount - routers.size();
    for (int i=0; i < count; i++) {
      long id=_routerDao.getNextInSequence(Long.class,"String_Node_Str");
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + id);
      }
      DomainRouterVO router=null;
      List<NetworkOfferingVO> offerings=_networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork);
      NetworkOfferingVO controlOffering=offerings.get(0);
      NetworkVO controlConfig=_networkMgr.setupNetwork(_systemAcct,controlOffering,plan,null,null,false).get(0);
      List<Pair<NetworkVO,NicProfile>> networks=new ArrayList<Pair<NetworkVO,NicProfile>>(3);
      if (publicNetwork) {
        NetworkOfferingVO publicOffering=_networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemPublicNetwork).get(0);
        List<NetworkVO> publicNetworks=_networkMgr.setupNetwork(_systemAcct,publicOffering,plan,null,null,false);
        networks.add(new Pair<NetworkVO,NicProfile>(publicNetworks.get(0),defaultNic));
      }
      String defaultNetworkStartIp=null;
      if (guestNetwork.getCidr() != null && !publicNetwork) {
        String startIp=_networkMgr.getStartIpAddress(guestNetwork.getId());
        if (startIp != null && _ipAddressDao.findByIpAndSourceNetworkId(guestNetwork.getId(),startIp).getAllocatedTime() == null) {
          defaultNetworkStartIp=startIp;
        }
 else         if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + startIp + "String_Node_Str"+ guestNetwork.getId()+ "String_Node_Str");
        }
      }
      NicProfile gatewayNic=new NicProfile(defaultNetworkStartIp);
      if (publicNetwork) {
        if (isRedundant) {
          gatewayNic.setIp4Address(_networkMgr.acquireGuestIpAddress(guestNetwork,null));
        }
 else {
          gatewayNic.setIp4Address(guestNetwork.getGateway());
        }
        gatewayNic.setBroadcastUri(guestNetwork.getBroadcastUri());
        gatewayNic.setBroadcastType(guestNetwork.getBroadcastDomainType());
        gatewayNic.setIsolationUri(guestNetwork.getBroadcastUri());
        gatewayNic.setMode(guestNetwork.getMode());
        String gatewayCidr=guestNetwork.getCidr();
        gatewayNic.setNetmask(NetUtils.getCidrNetmask(gatewayCidr));
      }
 else {
        gatewayNic.setDefaultNic(true);
      }
      networks.add(new Pair<NetworkVO,NicProfile>((NetworkVO)guestNetwork,gatewayNic));
      networks.add(new Pair<NetworkVO,NicProfile>(controlConfig,null));
      Long offering_id=_networkOfferingDao.findById(guestNetwork.getNetworkOfferingId()).getServiceOfferingId();
      if (offering_id == null) {
        offering_id=_offering.getId();
      }
      VirtualRouterProviderType type=VirtualRouterProviderType.VirtualRouter;
      Long physicalNetworkId=_networkMgr.getPhysicalNetworkId(network);
      PhysicalNetworkServiceProvider provider=_physicalProviderDao.findByServiceProvider(physicalNetworkId,type.toString());
      if (provider == null) {
        throw new CloudRuntimeException("String_Node_Str" + type.toString() + "String_Node_Str"+ physicalNetworkId);
      }
      VirtualRouterProvider vrProvider=_vrProviderDao.findByNspIdAndType(provider.getId(),type);
      if (vrProvider == null) {
        throw new CloudRuntimeException("String_Node_Str" + type.toString() + "String_Node_Str"+ provider.getId());
      }
      ServiceOfferingVO routerOffering=_serviceOfferingDao.findById(offering_id);
      List<HypervisorType> supportedHypervisors=new ArrayList<HypervisorType>();
      HypervisorType defaults=_resourceMgr.getDefaultHypervisor(dest.getDataCenter().getId());
      if (defaults != HypervisorType.None) {
        supportedHypervisors.add(defaults);
      }
      if (dest.getCluster() != null) {
        if (dest.getCluster().getHypervisorType() == HypervisorType.Ovm) {
          supportedHypervisors.add(getClusterToStartDomainRouterForOvm(dest.getCluster().getPodId()));
        }
 else {
          supportedHypervisors.add(dest.getCluster().getHypervisorType());
        }
      }
 else {
        supportedHypervisors=_resourceMgr.getSupportedHypervisorTypes(dest.getDataCenter().getId(),true,podId);
      }
      if (supportedHypervisors.isEmpty()) {
        if (podId != null) {
          throw new InsufficientServerCapacityException("String_Node_Str",Pod.class,podId);
        }
        throw new InsufficientServerCapacityException("String_Node_Str",DataCenter.class,dest.getDataCenter().getId());
      }
      int allocateRetry=0;
      int startRetry=0;
      for (Iterator<HypervisorType> iter=supportedHypervisors.iterator(); iter.hasNext(); ) {
        HypervisorType hType=iter.next();
        try {
          s_logger.debug("String_Node_Str" + hType);
          VMTemplateVO template=_templateDao.findRoutingTemplate(hType);
          if (template == null) {
            s_logger.debug(hType + "String_Node_Str");
            continue;
          }
          boolean offerHA=routerOffering.getOfferHA();
          if (isRedundant) {
            offerHA=false;
          }
          router=new DomainRouterVO(id,routerOffering.getId(),vrProvider.getId(),VirtualMachineName.getRouterName(id,_instance),template.getId(),template.getHypervisorType(),template.getGuestOSId(),owner.getDomainId(),owner.getId(),guestNetwork.getId(),isRedundant,0,false,RedundantState.UNKNOWN,offerHA,false);
          router.setRole(Role.VIRTUAL_ROUTER);
          router=_itMgr.allocate(router,template,routerOffering,networks,plan,null,owner);
        }
 catch (        InsufficientCapacityException ex) {
          if (allocateRetry < 2 && iter.hasNext()) {
            s_logger.debug("String_Node_Str" + hType + "String_Node_Str");
            continue;
          }
 else {
            throw ex;
          }
        }
 finally {
          allocateRetry++;
        }
        try {
          router=startVirtualRouter(router,_accountMgr.getSystemUser(),_accountMgr.getSystemAccount(),params);
          break;
        }
 catch (        InsufficientCapacityException ex) {
          if (startRetry < 2 && iter.hasNext()) {
            s_logger.debug("String_Node_Str" + router + "String_Node_Str"+ hType+ "String_Node_Str");
            destroyRouter(router.getId());
            continue;
          }
 else {
            throw ex;
          }
        }
 finally {
          startRetry++;
        }
      }
      routers.add(router);
      UserStatisticsVO stats=_userStatsDao.findBy(owner.getId(),dcId,router.getNetworkId(),null,router.getId(),router.getType().toString());
      if (stats == null) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + owner.getId() + "String_Node_Str"+ router.getId());
        }
        stats=new UserStatisticsVO(owner.getId(),dcId,null,router.getId(),router.getType().toString(),guestNetwork.getId());
        _userStatsDao.persist(stats);
      }
    }
  }
  finally {
    if (network != null) {
      _networkDao.releaseFromLockTable(network.getId());
    }
  }
  return routers;
}

Fixed version (ground truth):
@DB protected List<DomainRouterVO> findOrDeployVirtualRouters(Network guestNetwork,DeployDestination dest,Account owner,boolean isRedundant,Map<Param,Object> params) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
  Network network=_networkDao.acquireInLockTable(guestNetwork.getId());
  if (network == null) {
    throw new ConcurrentOperationException("String_Node_Str" + guestNetwork.getId());
  }
  long dcId=dest.getDataCenter().getId();
  DataCenterDeployment plan=new DataCenterDeployment(dcId);
  boolean isPodBased=(dest.getDataCenter().getNetworkType() == NetworkType.Basic || _networkMgr.areServicesSupportedInNetwork(guestNetwork.getId(),Service.SecurityGroup)) && guestNetwork.getTrafficType() == TrafficType.Guest;
  boolean publicNetwork=false;
  if (_networkMgr.isProviderSupportServiceInNetwork(guestNetwork.getId(),Service.SourceNat,Provider.VirtualRouter)) {
    publicNetwork=true;
  }
  if (isRedundant && !publicNetwork) {
    s_logger.error("String_Node_Str");
    return null;
  }
  List<DomainRouterVO> routers;
  Long podId=null;
  if (isPodBased) {
    Pod pod=dest.getPod();
    if (pod != null) {
      podId=pod.getId();
    }
  }
  if (publicNetwork) {
    routers=_routerDao.listByNetworkAndRole(guestNetwork.getId(),Role.VIRTUAL_ROUTER);
  }
 else {
    if (isPodBased && podId != null) {
      routers=_routerDao.listByNetworkAndPodAndRole(guestNetwork.getId(),podId,Role.VIRTUAL_ROUTER);
      plan=new DataCenterDeployment(dcId,podId,null,null,null,null);
    }
 else {
      routers=_routerDao.listByNetworkAndRole(guestNetwork.getId(),Role.VIRTUAL_ROUTER);
      plan=new DataCenterDeployment(dcId);
    }
  }
  try {
    int routerCount=1;
    if (isRedundant) {
      routerCount=2;
    }
    for (    DomainRouterVO router : routers) {
      if (!router.getIsRedundantRouter()) {
        routerCount=1;
      }
    }
    if (routers.size() >= routerCount || (isPodBased && podId == null)) {
      return routers;
    }
    if (routers.size() >= 5) {
      s_logger.error("String_Node_Str");
    }
    NicProfile defaultNic=new NicProfile();
    if (publicNetwork) {
      PublicIp sourceNatIp=_networkMgr.assignSourceNatIpAddress(owner,guestNetwork,_accountMgr.getSystemUser().getId());
      defaultNic.setDefaultNic(true);
      defaultNic.setIp4Address(sourceNatIp.getAddress().addr());
      defaultNic.setGateway(sourceNatIp.getGateway());
      defaultNic.setNetmask(sourceNatIp.getNetmask());
      defaultNic.setMacAddress(sourceNatIp.getMacAddress());
      defaultNic.setBroadcastType(BroadcastDomainType.Vlan);
      defaultNic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(sourceNatIp.getVlanTag()));
      defaultNic.setIsolationUri(IsolationType.Vlan.toUri(sourceNatIp.getVlanTag()));
      defaultNic.setDeviceId(2);
    }
    int count=routerCount - routers.size();
    for (int i=0; i < count; i++) {
      long id=_routerDao.getNextInSequence(Long.class,"String_Node_Str");
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("String_Node_Str" + id);
      }
      DomainRouterVO router=null;
      List<NetworkOfferingVO> offerings=_networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork);
      NetworkOfferingVO controlOffering=offerings.get(0);
      NetworkVO controlConfig=_networkMgr.setupNetwork(_systemAcct,controlOffering,plan,null,null,false).get(0);
      List<Pair<NetworkVO,NicProfile>> networks=new ArrayList<Pair<NetworkVO,NicProfile>>(3);
      if (publicNetwork) {
        NetworkOfferingVO publicOffering=_networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemPublicNetwork).get(0);
        List<NetworkVO> publicNetworks=_networkMgr.setupNetwork(_systemAcct,publicOffering,plan,null,null,false);
        networks.add(new Pair<NetworkVO,NicProfile>(publicNetworks.get(0),defaultNic));
      }
      String defaultNetworkStartIp=null;
      if (guestNetwork.getCidr() != null && !publicNetwork) {
        String startIp=_networkMgr.getStartIpAddress(guestNetwork.getId());
        if (startIp != null && _ipAddressDao.findByIpAndSourceNetworkId(guestNetwork.getId(),startIp).getAllocatedTime() == null) {
          defaultNetworkStartIp=startIp;
        }
 else         if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + startIp + "String_Node_Str"+ guestNetwork.getId()+ "String_Node_Str");
        }
      }
      NicProfile gatewayNic=new NicProfile(defaultNetworkStartIp);
      if (publicNetwork) {
        if (isRedundant) {
          gatewayNic.setIp4Address(_networkMgr.acquireGuestIpAddress(guestNetwork,null));
        }
 else {
          gatewayNic.setIp4Address(guestNetwork.getGateway());
        }
        gatewayNic.setBroadcastUri(guestNetwork.getBroadcastUri());
        gatewayNic.setBroadcastType(guestNetwork.getBroadcastDomainType());
        gatewayNic.setIsolationUri(guestNetwork.getBroadcastUri());
        gatewayNic.setMode(guestNetwork.getMode());
        String gatewayCidr=guestNetwork.getCidr();
        gatewayNic.setNetmask(NetUtils.getCidrNetmask(gatewayCidr));
      }
 else {
        gatewayNic.setDefaultNic(true);
      }
      networks.add(new Pair<NetworkVO,NicProfile>((NetworkVO)guestNetwork,gatewayNic));
      networks.add(new Pair<NetworkVO,NicProfile>(controlConfig,null));
      Long offering_id=_networkOfferingDao.findById(guestNetwork.getNetworkOfferingId()).getServiceOfferingId();
      if (offering_id == null) {
        offering_id=_offering.getId();
      }
      VirtualRouterProviderType type=VirtualRouterProviderType.VirtualRouter;
      Long physicalNetworkId=_networkMgr.getPhysicalNetworkId(network);
      PhysicalNetworkServiceProvider provider=_physicalProviderDao.findByServiceProvider(physicalNetworkId,type.toString());
      if (provider == null) {
        throw new CloudRuntimeException("String_Node_Str" + type.toString() + "String_Node_Str"+ physicalNetworkId);
      }
      VirtualRouterProvider vrProvider=_vrProviderDao.findByNspIdAndType(provider.getId(),type);
      if (vrProvider == null) {
        throw new CloudRuntimeException("String_Node_Str" + type.toString() + "String_Node_Str"+ provider.getId());
      }
      ServiceOfferingVO routerOffering=_serviceOfferingDao.findById(offering_id);
      List<HypervisorType> supportedHypervisors=new ArrayList<HypervisorType>();
      HypervisorType defaults=_resourceMgr.getDefaultHypervisor(dest.getDataCenter().getId());
      if (defaults != HypervisorType.None) {
        supportedHypervisors.add(defaults);
      }
      if (dest.getCluster() != null) {
        if (dest.getCluster().getHypervisorType() == HypervisorType.Ovm) {
          supportedHypervisors.add(getClusterToStartDomainRouterForOvm(dest.getCluster().getPodId()));
        }
 else {
          supportedHypervisors.add(dest.getCluster().getHypervisorType());
        }
      }
 else {
        supportedHypervisors=_resourceMgr.getSupportedHypervisorTypes(dest.getDataCenter().getId(),true,podId);
      }
      if (supportedHypervisors.isEmpty()) {
        if (podId != null) {
          throw new InsufficientServerCapacityException("String_Node_Str",Pod.class,podId);
        }
        throw new InsufficientServerCapacityException("String_Node_Str",DataCenter.class,dest.getDataCenter().getId());
      }
      int allocateRetry=0;
      int startRetry=0;
      for (Iterator<HypervisorType> iter=supportedHypervisors.iterator(); iter.hasNext(); ) {
        HypervisorType hType=iter.next();
        try {
          s_logger.debug("String_Node_Str" + hType);
          VMTemplateVO template=_templateDao.findRoutingTemplate(hType);
          if (template == null) {
            s_logger.debug(hType + "String_Node_Str");
            continue;
          }
          boolean offerHA=routerOffering.getOfferHA();
          if (isRedundant) {
            offerHA=false;
          }
          router=new DomainRouterVO(id,routerOffering.getId(),vrProvider.getId(),VirtualMachineName.getRouterName(id,_instance),template.getId(),template.getHypervisorType(),template.getGuestOSId(),owner.getDomainId(),owner.getId(),guestNetwork.getId(),isRedundant,0,false,RedundantState.UNKNOWN,offerHA,false);
          router.setRole(Role.VIRTUAL_ROUTER);
          router=_itMgr.allocate(router,template,routerOffering,networks,plan,null,owner);
        }
 catch (        InsufficientCapacityException ex) {
          if (allocateRetry < 2 && iter.hasNext()) {
            s_logger.debug("String_Node_Str" + hType + "String_Node_Str");
            continue;
          }
 else {
            throw ex;
          }
        }
 finally {
          allocateRetry++;
        }
        try {
          router=startVirtualRouter(router,_accountMgr.getSystemUser(),_accountMgr.getSystemAccount(),params);
          break;
        }
 catch (        InsufficientCapacityException ex) {
          if (startRetry < 2 && iter.hasNext()) {
            s_logger.debug("String_Node_Str" + router + "String_Node_Str"+ hType+ "String_Node_Str");
            destroyRouter(router.getId());
            continue;
          }
 else {
            throw ex;
          }
        }
 finally {
          startRetry++;
        }
      }
      routers.add(router);
      UserStatisticsVO stats=_userStatsDao.findBy(owner.getId(),dcId,router.getNetworkId(),null,router.getId(),router.getType().toString());
      if (stats == null) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug("String_Node_Str" + owner.getId() + "String_Node_Str"+ router.getId());
        }
        stats=new UserStatisticsVO(owner.getId(),dcId,null,router.getId(),router.getType().toString(),guestNetwork.getId());
        _userStatsDao.persist(stats);
      }
    }
  }
  finally {
    if (network != null) {
      _networkDao.releaseFromLockTable(network.getId());
    }
  }
  return routers;
}