/** @param network */
  private void mockDAOs(final NetworkVO network, final NetworkOfferingVO offering) {
    when(_networkDao.acquireInLockTable(
            network.getId(), NetworkOrchestrationService.NetworkLockTimeout.value()))
        .thenReturn(network);
    when(_networksDao.acquireInLockTable(
            network.getId(), NetworkOrchestrationService.NetworkLockTimeout.value()))
        .thenReturn(network);
    when(_physicalProviderDao.findByServiceProvider(0L, "VirtualRouter"))
        .thenReturn(new PhysicalNetworkServiceProviderVO());
    when(_vrProviderDao.findByNspIdAndType(0L, Type.VirtualRouter))
        .thenReturn(new VirtualRouterProviderVO());
    when(_networkOfferingDao.findById(0L)).thenReturn(offering);
    // watchit: (in this test) there can be only one
    when(_routerDao.getNextInSequence(Long.class, "id")).thenReturn(0L);
    final ServiceOfferingVO svcoff =
        new ServiceOfferingVO(
            "name",
            /* cpu */ 1,
            /* ramsize */ 1024 * 1024,
            /* (clock?)speed */ 1024 * 1024 * 1024,
            /* rateMbps */ 1,
            /* multicastRateMbps */ 0,
            /* offerHA */ false,
            "displayText",
            ProvisioningType.THIN,
            /* useLocalStorage */ false,
            /* recreatable */ false,
            "tags",
            /* systemUse */ false,
            VirtualMachine.Type.DomainRouter,
            /* defaultUse */ false);
    when(_serviceOfferingDao.findById(0L)).thenReturn(svcoff);
    final DomainRouterVO router =
        new DomainRouterVO(
            /* id */ 1L,
            /* serviceOfferingId */ 1L,
            /* elementId */ 0L,
            "name",
            /* templateId */ 0L,
            HypervisorType.XenServer,
            /* guestOSId */ 0L,
            /* domainId */ 0L,
            /* accountId */ 1L,
            /* userId */ 1L,
            /* isRedundantRouter */ false,
            RedundantState.UNKNOWN,
            /* haEnabled */ false,
            /* stopPending */ false,
            /* vpcId */ null);

    when(_routerDao.getNextInSequence(Long.class, "id")).thenReturn(1L);
    when(_templateDao.findRoutingTemplate(
            HypervisorType.XenServer, "SystemVM Template (XenServer)"))
        .thenReturn(new VMTemplateVO());
    when(_routerDao.persist(any(DomainRouterVO.class))).thenReturn(router);
    when(_routerDao.findById(router.getId())).thenReturn(router);
  }
 @Override
 public NetworkVO getPrivateNetwork(
     String broadcastUri, String cidr, long accountId, long zoneId, Long networkOfferingId) {
   if (networkOfferingId == null) {
     networkOfferingId =
         _ntwkOffDao.findByUniqueName(NetworkOffering.SystemPrivateGatewayNetworkOffering).getId();
   }
   SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
   sc.setParameters("datacenter", zoneId);
   sc.setParameters("broadcastUri", broadcastUri);
   sc.setParameters("cidr", cidr);
   sc.setParameters("account", accountId);
   sc.setParameters("offering", networkOfferingId);
   return findOneBy(sc);
 }
 @Override
 @DB
 public void addRouterToGuestNetwork(VirtualRouter router, Network guestNetwork) {
   if (_routerNetworkDao.findByRouterAndNetwork(router.getId(), guestNetwork.getId()) == null) {
     NetworkOffering off = _offDao.findById(guestNetwork.getNetworkOfferingId());
     if (!(off.getName().equalsIgnoreCase(NetworkOffering.SystemPrivateGatewayNetworkOffering))) {
       TransactionLegacy txn = TransactionLegacy.currentTxn();
       txn.start();
       // 1) add router to network
       RouterNetworkVO routerNtwkMap =
           new RouterNetworkVO(router.getId(), guestNetwork.getId(), guestNetwork.getGuestType());
       _routerNetworkDao.persist(routerNtwkMap);
       // 2) create user stats entry for the network
       UserStatisticsVO stats =
           _userStatsDao.findBy(
               router.getAccountId(),
               router.getDataCenterId(),
               guestNetwork.getId(),
               null,
               router.getId(),
               router.getType().toString());
       if (stats == null) {
         stats =
             new UserStatisticsVO(
                 router.getAccountId(),
                 router.getDataCenterId(),
                 null,
                 router.getId(),
                 router.getType().toString(),
                 guestNetwork.getId());
         _userStatsDao.persist(stats);
       }
       txn.commit();
     }
   }
 }
  @PostConstruct
  protected void init() {
    AllFieldsSearch = createSearchBuilder();
    AllFieldsSearch.and("trafficType", AllFieldsSearch.entity().getTrafficType(), Op.EQ);
    AllFieldsSearch.and("cidr", AllFieldsSearch.entity().getCidr(), Op.EQ);
    AllFieldsSearch.and("broadcastType", AllFieldsSearch.entity().getBroadcastDomainType(), Op.EQ);
    AllFieldsSearch.and("offering", AllFieldsSearch.entity().getNetworkOfferingId(), Op.EQ);
    AllFieldsSearch.and("datacenter", AllFieldsSearch.entity().getDataCenterId(), Op.EQ);
    AllFieldsSearch.and("account", AllFieldsSearch.entity().getAccountId(), Op.EQ);
    AllFieldsSearch.and("related", AllFieldsSearch.entity().getRelated(), Op.EQ);
    AllFieldsSearch.and("guestType", AllFieldsSearch.entity().getGuestType(), Op.EQ);
    AllFieldsSearch.and("physicalNetwork", AllFieldsSearch.entity().getPhysicalNetworkId(), Op.EQ);
    AllFieldsSearch.and("broadcastUri", AllFieldsSearch.entity().getBroadcastUri(), Op.EQ);
    AllFieldsSearch.and("vpcId", AllFieldsSearch.entity().getVpcId(), Op.EQ);
    AllFieldsSearch.and("aclId", AllFieldsSearch.entity().getNetworkACLId(), Op.EQ);
    SearchBuilder<NetworkOfferingVO> join1 = _ntwkOffDao.createSearchBuilder();
    join1.and("isSystem", join1.entity().isSystemOnly(), Op.EQ);
    join1.and("isRedundant", join1.entity().getRedundantRouter(), Op.EQ);
    AllFieldsSearch.join(
        "offerings",
        join1,
        AllFieldsSearch.entity().getNetworkOfferingId(),
        join1.entity().getId(),
        JoinBuilder.JoinType.INNER);
    AllFieldsSearch.done();

    AccountSearch = createSearchBuilder();
    AccountSearch.and("offering", AccountSearch.entity().getNetworkOfferingId(), Op.EQ);
    SearchBuilder<NetworkAccountVO> join = _accountsDao.createSearchBuilder();
    join.and("account", join.entity().getAccountId(), Op.EQ);
    AccountSearch.join(
        "accounts",
        join,
        AccountSearch.entity().getId(),
        join.entity().getNetworkId(),
        JoinBuilder.JoinType.INNER);
    AccountSearch.and("datacenter", AccountSearch.entity().getDataCenterId(), Op.EQ);
    AccountSearch.and("cidr", AccountSearch.entity().getCidr(), Op.EQ);
    AccountSearch.and("vpcId", AccountSearch.entity().getVpcId(), Op.EQ);
    AccountSearch.done();

    RelatedConfigSearch = createSearchBuilder();
    RelatedConfigSearch.and("offering", RelatedConfigSearch.entity().getNetworkOfferingId(), Op.EQ);
    RelatedConfigSearch.and("datacenter", RelatedConfigSearch.entity().getDataCenterId(), Op.EQ);
    SearchBuilder<NetworkAccountVO> join2 = _accountsDao.createSearchBuilder();
    join2.and("account", join2.entity().getAccountId(), Op.EQ);
    RelatedConfigSearch.join(
        "account",
        join2,
        join2.entity().getNetworkId(),
        RelatedConfigSearch.entity().getId(),
        JoinType.INNER);
    RelatedConfigSearch.done();

    AccountNetworkSearch = createSearchBuilder();
    AccountNetworkSearch.and("networkId", AccountNetworkSearch.entity().getId(), Op.EQ);
    SearchBuilder<NetworkAccountVO> mapJoin = _accountsDao.createSearchBuilder();
    mapJoin.and("accountId", mapJoin.entity().getAccountId(), Op.EQ);
    AccountNetworkSearch.join(
        "networkSearch",
        mapJoin,
        AccountNetworkSearch.entity().getId(),
        mapJoin.entity().getNetworkId(),
        JoinBuilder.JoinType.INNER);
    AccountNetworkSearch.done();

    ZoneBroadcastUriSearch = createSearchBuilder();
    ZoneBroadcastUriSearch.and(
        "dataCenterId", ZoneBroadcastUriSearch.entity().getDataCenterId(), Op.EQ);
    ZoneBroadcastUriSearch.and(
        "broadcastUri", ZoneBroadcastUriSearch.entity().getBroadcastUri(), Op.EQ);
    ZoneBroadcastUriSearch.and("guestType", ZoneBroadcastUriSearch.entity().getGuestType(), Op.EQ);
    ZoneBroadcastUriSearch.done();

    CountByZoneAndURI = createSearchBuilder(Long.class);
    CountByZoneAndURI.select(null, Func.COUNT, null);
    CountByZoneAndURI.and("dataCenterId", CountByZoneAndURI.entity().getDataCenterId(), Op.EQ);
    CountByZoneAndURI.and("broadcastUri", CountByZoneAndURI.entity().getBroadcastUri(), Op.EQ);
    CountByZoneAndURI.and("guestType", CountByZoneAndURI.entity().getGuestType(), Op.EQ);

    CountByZoneAndURI.done();

    ZoneSecurityGroupSearch = createSearchBuilder();
    ZoneSecurityGroupSearch.and(
        "dataCenterId", ZoneSecurityGroupSearch.entity().getDataCenterId(), Op.EQ);
    SearchBuilder<NetworkServiceMapVO> offJoin = _ntwkSvcMap.createSearchBuilder();
    offJoin.and("service", offJoin.entity().getService(), Op.EQ);
    ZoneSecurityGroupSearch.join(
        "services",
        offJoin,
        ZoneSecurityGroupSearch.entity().getId(),
        offJoin.entity().getNetworkId(),
        JoinBuilder.JoinType.INNER);
    ZoneSecurityGroupSearch.done();

    CountBy = createSearchBuilder(Integer.class);
    CountBy.select(null, Func.COUNT, CountBy.entity().getId());
    CountBy.and("offeringId", CountBy.entity().getNetworkOfferingId(), Op.EQ);
    CountBy.and("vpcId", CountBy.entity().getVpcId(), Op.EQ);
    CountBy.and("removed", CountBy.entity().getRemoved(), Op.NULL);
    SearchBuilder<NetworkOfferingVO> ntwkOffJoin = _ntwkOffDao.createSearchBuilder();
    ntwkOffJoin.and("isSystem", ntwkOffJoin.entity().isSystemOnly(), Op.EQ);
    CountBy.join(
        "offerings",
        ntwkOffJoin,
        CountBy.entity().getNetworkOfferingId(),
        ntwkOffJoin.entity().getId(),
        JoinBuilder.JoinType.INNER);
    CountBy.done();

    PhysicalNetworkSearch = createSearchBuilder();
    PhysicalNetworkSearch.and(
        "physicalNetworkId", PhysicalNetworkSearch.entity().getPhysicalNetworkId(), Op.EQ);
    PhysicalNetworkSearch.done();

    SecurityGroupSearch = createSearchBuilder();
    SearchBuilder<NetworkServiceMapVO> join3 = _ntwkSvcMap.createSearchBuilder();
    join3.and("service", join3.entity().getService(), Op.EQ);
    SecurityGroupSearch.join(
        "services",
        join3,
        SecurityGroupSearch.entity().getId(),
        join3.entity().getNetworkId(),
        JoinBuilder.JoinType.INNER);
    SecurityGroupSearch.done();

    NetworksCount = createSearchBuilder(Integer.class);
    NetworksCount.select(null, Func.COUNT, NetworksCount.entity().getId());
    NetworksCount.and(
        "networkOfferingId", NetworksCount.entity().getNetworkOfferingId(), SearchCriteria.Op.EQ);
    NetworksCount.done();

    NetworksRegularUserCanCreateSearch = createSearchBuilder(Long.class);
    NetworksRegularUserCanCreateSearch.and(
        "aclType", NetworksRegularUserCanCreateSearch.entity().getAclType(), Op.EQ);
    NetworksRegularUserCanCreateSearch.select(
        null, Func.COUNT, NetworksRegularUserCanCreateSearch.entity().getId());
    SearchBuilder<NetworkAccountVO> join4 = _accountsDao.createSearchBuilder();
    join4.and("account", join4.entity().getAccountId(), Op.EQ);
    join4.and("isOwner", join4.entity().isOwner(), Op.EQ);
    NetworksRegularUserCanCreateSearch.join(
        "accounts",
        join4,
        NetworksRegularUserCanCreateSearch.entity().getId(),
        join4.entity().getNetworkId(),
        JoinBuilder.JoinType.INNER);
    SearchBuilder<NetworkOfferingVO> join5 = _ntwkOffDao.createSearchBuilder();
    join5.and("specifyVlan", join5.entity().getSpecifyVlan(), Op.EQ);
    NetworksRegularUserCanCreateSearch.join(
        "ntwkOff",
        join5,
        NetworksRegularUserCanCreateSearch.entity().getNetworkOfferingId(),
        join5.entity().getId(),
        JoinBuilder.JoinType.INNER);
    NetworksRegularUserCanCreateSearch.done();

    _tgMacAddress = _tgs.get("macAddress");

    SourceNATSearch = createSearchBuilder();
    SourceNATSearch.and("account", SourceNATSearch.entity().getAccountId(), Op.EQ);
    SourceNATSearch.and("datacenter", SourceNATSearch.entity().getDataCenterId(), Op.EQ);
    SourceNATSearch.and("guestType", SourceNATSearch.entity().getGuestType(), Op.EQ);
    SearchBuilder<NetworkServiceMapVO> join6 = _ntwkSvcMap.createSearchBuilder();
    join6.and("service", join6.entity().getService(), Op.EQ);
    SourceNATSearch.join(
        "services",
        join6,
        SourceNATSearch.entity().getId(),
        join6.entity().getNetworkId(),
        JoinBuilder.JoinType.INNER);
    SourceNATSearch.done();

    VpcNetworksCount = createSearchBuilder(Long.class);
    VpcNetworksCount.and("vpcId", VpcNetworksCount.entity().getVpcId(), Op.EQ);
    VpcNetworksCount.select(null, Func.COUNT, VpcNetworksCount.entity().getId());
    SearchBuilder<NetworkOfferingVO> join9 = _ntwkOffDao.createSearchBuilder();
    join9.and("isSystem", join9.entity().isSystemOnly(), Op.EQ);
    VpcNetworksCount.join(
        "offerings",
        join9,
        VpcNetworksCount.entity().getNetworkOfferingId(),
        join9.entity().getId(),
        JoinBuilder.JoinType.INNER);
    VpcNetworksCount.done();

    OfferingAccountNetworkSearch = createSearchBuilder();
    OfferingAccountNetworkSearch.select(
        null, Func.DISTINCT, OfferingAccountNetworkSearch.entity().getId());
    SearchBuilder<NetworkOfferingVO> ntwkOfferingJoin = _ntwkOffDao.createSearchBuilder();
    ntwkOfferingJoin.and("isSystem", ntwkOfferingJoin.entity().isSystemOnly(), Op.EQ);
    OfferingAccountNetworkSearch.join(
        "ntwkOfferingSearch",
        ntwkOfferingJoin,
        OfferingAccountNetworkSearch.entity().getNetworkOfferingId(),
        ntwkOfferingJoin.entity().getId(),
        JoinBuilder.JoinType.LEFT);
    SearchBuilder<NetworkAccountVO> ntwkAccountJoin = _accountsDao.createSearchBuilder();
    ntwkAccountJoin.and("accountId", ntwkAccountJoin.entity().getAccountId(), Op.EQ);
    OfferingAccountNetworkSearch.join(
        "ntwkAccountSearch",
        ntwkAccountJoin,
        OfferingAccountNetworkSearch.entity().getId(),
        ntwkAccountJoin.entity().getNetworkId(),
        JoinBuilder.JoinType.INNER);
    OfferingAccountNetworkSearch.and(
        "zoneId", OfferingAccountNetworkSearch.entity().getDataCenterId(), Op.EQ);
    OfferingAccountNetworkSearch.and(
        "type", OfferingAccountNetworkSearch.entity().getGuestType(), Op.EQ);
    OfferingAccountNetworkSearch.done();

    GarbageCollectedSearch = createSearchBuilder(Long.class);
    GarbageCollectedSearch.selectFields(GarbageCollectedSearch.entity().getId());
    SearchBuilder<NetworkOpVO> join7 = _ntwkOpDao.createSearchBuilder();
    join7.and("activenics", join7.entity().getActiveNicsCount(), Op.EQ);
    join7.and("gc", join7.entity().isGarbageCollected(), Op.EQ);
    join7.and("check", join7.entity().isCheckForGc(), Op.EQ);
    GarbageCollectedSearch.join(
        "ntwkOpGC",
        join7,
        GarbageCollectedSearch.entity().getId(),
        join7.entity().getId(),
        JoinBuilder.JoinType.INNER);
    SearchBuilder<NetworkOfferingVO> join8 = _ntwkOffDao.createSearchBuilder();
    join8.and("isPersistent", join8.entity().getIsPersistent(), Op.EQ);
    GarbageCollectedSearch.join(
        "ntwkOffGC",
        join8,
        GarbageCollectedSearch.entity().getNetworkOfferingId(),
        join8.entity().getId(),
        JoinBuilder.JoinType.INNER);
    GarbageCollectedSearch.done();
  }