Program: 106908
Project: 106908
Buggy source:
@DB protected DomainRouterVO findOrCreateVirtualRouter(Network guestNetwork,DataCenterDeployment plan,HypervisorType type,Account owner) throws ConcurrentOperationException, InsufficientCapacityException {
  DomainRouterVO router=_routerDao.findByNetwork(guestNetwork.getId());
  if (router != null) {
    return router;
  }
  VMTemplateVO template=_templateDao.findRoutingTemplate(type);
  List<NetworkOfferingVO> offerings=_networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork);
  NetworkOfferingVO controlOffering=offerings.get(0);
  NetworkVO controlConfig=_networkMgr.setupNetwork(_systemAcct,controlOffering,plan,null,null,false,false).get(0);
  List<Pair<NetworkVO,NicProfile>> networks=new ArrayList<Pair<NetworkVO,NicProfile>>(3);
  NetworkOfferingVO publicOffering=_networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemPublicNetwork).get(0);
  List<NetworkVO> publicNetworks=_networkMgr.setupNetwork(_systemAcct,publicOffering,plan,null,null,false,false);
  Transaction txn=Transaction.currentTxn();
  txn.start();
  Network network=_networkDao.lockRow(guestNetwork.getId(),true);
  if (network == null) {
    throw new ConcurrentOperationException("String_Node_Str" + guestNetwork.getId());
  }
  router=_routerDao.findByNetwork(guestNetwork.getId());
  if (router != null) {
    return router;
  }
  long id=_routerDao.getNextInSequence(Long.class,"String_Node_Str");
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + id);
  }
  PublicIp sourceNatIp=_networkMgr.assignSourceNatIpAddress(owner,guestNetwork,_accountService.getSystemUser().getId());
  NicProfile defaultNic=new NicProfile();
  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);
  networks.add(new Pair<NetworkVO,NicProfile>(publicNetworks.get(0),defaultNic));
  NicProfile gatewayNic=new NicProfile();
  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));
  networks.add(new Pair<NetworkVO,NicProfile>((NetworkVO)guestNetwork,gatewayNic));
  networks.add(new Pair<NetworkVO,NicProfile>(controlConfig,null));
  router=new DomainRouterVO(id,_offering.getId(),VirtualMachineName.getRouterName(id,_instance),template.getId(),template.getHypervisorType(),template.getGuestOSId(),owner.getDomainId(),owner.getId(),guestNetwork.getId(),_offering.getOfferHA());
  router=_itMgr.allocate(router,template,_offering,networks,plan,null,owner);
  UserStatisticsVO stats=_userStatsDao.findBy(owner.getId(),plan.getDataCenterId(),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(),plan.getDataCenterId(),null,router.getId(),router.getType().toString(),guestNetwork.getId());
    _userStatsDao.persist(stats);
  }
  txn.commit();
  return router;
}

Fixed version (ground truth):
@DB protected DomainRouterVO findOrCreateVirtualRouter(Network guestNetwork,DataCenterDeployment plan,HypervisorType type,Account owner) throws ConcurrentOperationException, InsufficientCapacityException {
  DomainRouterVO router=_routerDao.findByNetwork(guestNetwork.getId());
  if (router != null) {
    return router;
  }
  VMTemplateVO template=_templateDao.findRoutingTemplate(type);
  List<NetworkOfferingVO> offerings=_networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork);
  NetworkOfferingVO controlOffering=offerings.get(0);
  NetworkVO controlConfig=_networkMgr.setupNetwork(_systemAcct,controlOffering,plan,null,null,false,false).get(0);
  List<Pair<NetworkVO,NicProfile>> networks=new ArrayList<Pair<NetworkVO,NicProfile>>(3);
  NetworkOfferingVO publicOffering=_networkMgr.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemPublicNetwork).get(0);
  List<NetworkVO> publicNetworks=_networkMgr.setupNetwork(_systemAcct,publicOffering,plan,null,null,false,false);
  Transaction txn=Transaction.currentTxn();
  Network network=_networkDao.acquireInLockTable(guestNetwork.getId());
  if (network == null) {
    throw new ConcurrentOperationException("String_Node_Str" + guestNetwork.getId());
  }
  try {
    txn.start();
    router=_routerDao.findByNetwork(guestNetwork.getId());
    if (router != null) {
      return router;
    }
    long id=_routerDao.getNextInSequence(Long.class,"String_Node_Str");
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + id);
    }
    PublicIp sourceNatIp=_networkMgr.assignSourceNatIpAddress(owner,guestNetwork,_accountService.getSystemUser().getId());
    NicProfile defaultNic=new NicProfile();
    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);
    networks.add(new Pair<NetworkVO,NicProfile>(publicNetworks.get(0),defaultNic));
    NicProfile gatewayNic=new NicProfile();
    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));
    networks.add(new Pair<NetworkVO,NicProfile>((NetworkVO)guestNetwork,gatewayNic));
    networks.add(new Pair<NetworkVO,NicProfile>(controlConfig,null));
    router=new DomainRouterVO(id,_offering.getId(),VirtualMachineName.getRouterName(id,_instance),template.getId(),template.getHypervisorType(),template.getGuestOSId(),owner.getDomainId(),owner.getId(),guestNetwork.getId(),_offering.getOfferHA());
    router=_itMgr.allocate(router,template,_offering,networks,plan,null,owner);
    UserStatisticsVO stats=_userStatsDao.findBy(owner.getId(),plan.getDataCenterId(),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(),plan.getDataCenterId(),null,router.getId(),router.getType().toString(),guestNetwork.getId());
      _userStatsDao.persist(stats);
    }
    txn.commit();
  }
  finally {
    if (network != null) {
      _networkDao.releaseFromLockTable(network.getId());
    }
  }
  return router;
}