protected LinkedHashMap<Network, List<? extends NicProfile>> configurePublicNic(
      final RouterDeploymentDefinition routerDeploymentDefinition, final boolean hasGuestNic) {
    final LinkedHashMap<Network, List<? extends NicProfile>> publicConfig =
        new LinkedHashMap<Network, List<? extends NicProfile>>(3);

    if (routerDeploymentDefinition.isPublicNetwork()) {
      s_logger.debug("Adding nic for Virtual Router in Public network ");
      // if source nat service is supported by the network, get the source
      // nat ip address
      final NicProfile defaultNic = new NicProfile();
      defaultNic.setDefaultNic(true);
      final PublicIp sourceNatIp = routerDeploymentDefinition.getSourceNatIP();
      defaultNic.setIPv4Address(sourceNatIp.getAddress().addr());
      defaultNic.setIPv4Gateway(sourceNatIp.getGateway());
      defaultNic.setIPv4Netmask(sourceNatIp.getNetmask());
      defaultNic.setMacAddress(sourceNatIp.getMacAddress());
      // get broadcast from public network
      final Network pubNet = _networkDao.findById(sourceNatIp.getNetworkId());
      if (pubNet.getBroadcastDomainType() == BroadcastDomainType.Vxlan) {
        defaultNic.setBroadcastType(BroadcastDomainType.Vxlan);
        defaultNic.setBroadcastUri(BroadcastDomainType.Vxlan.toUri(sourceNatIp.getVlanTag()));
        defaultNic.setIsolationUri(BroadcastDomainType.Vxlan.toUri(sourceNatIp.getVlanTag()));
      } else {
        defaultNic.setBroadcastType(BroadcastDomainType.Vlan);
        defaultNic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(sourceNatIp.getVlanTag()));
        defaultNic.setIsolationUri(IsolationType.Vlan.toUri(sourceNatIp.getVlanTag()));
      }

      // If guest nic has already been added we will have 2 devices in the list.
      if (hasGuestNic) {
        defaultNic.setDeviceId(2);
      }

      final NetworkOffering publicOffering =
          _networkModel
              .getSystemAccountNetworkOfferings(NetworkOffering.SystemPublicNetwork)
              .get(0);
      final List<? extends Network> publicNetworks =
          _networkMgr.setupNetwork(
              s_systemAccount,
              publicOffering,
              routerDeploymentDefinition.getPlan(),
              null,
              null,
              false);
      final String publicIp = defaultNic.getIPv4Address();
      // We want to use the identical MAC address for RvR on public
      // interface if possible
      final NicVO peerNic =
          _nicDao.findByIp4AddressAndNetworkId(publicIp, publicNetworks.get(0).getId());
      if (peerNic != null) {
        s_logger.info("Use same MAC as previous RvR, the MAC is " + peerNic.getMacAddress());
        defaultNic.setMacAddress(peerNic.getMacAddress());
      }
      publicConfig.put(publicNetworks.get(0), new ArrayList<NicProfile>(Arrays.asList(defaultNic)));
    }

    return publicConfig;
  }
  // we use context.reservationId for dedup of guru & element operations.
  public boolean createNicEnv(
      Network network, NicProfile nic, DeployDestination dest, ReservationContext context) {
    String tenantNetworkUuid = _sspUuidDao.findUuidByNetwork(network);
    if (tenantNetworkUuid == null) {
      s_logger.debug("Skipping #createNicEnv() for nic on " + network.toString());
      return true;
    }

    String reservationId = context.getReservationId();
    List<SspUuidVO> tenantPortUuidVos = _sspUuidDao.listUUidVoByNicProfile(nic);
    for (SspUuidVO tenantPortUuidVo : tenantPortUuidVos) {
      if (reservationId.equals(tenantPortUuidVo.getReservationId())) {
        s_logger.info("Skipping because reservation found " + reservationId);
        return true;
      }
    }

    String tenantPortUuid = null;
    for (SspClient client :
        fetchSspClients(network.getPhysicalNetworkId(), network.getDataCenterId(), true)) {
      SspClient.TenantPort sspPort = client.createTenantPort(tenantNetworkUuid);
      if (sspPort != null) {
        tenantPortUuid = sspPort.uuid;
        nic.setReservationId(reservationId);

        SspUuidVO uuid = new SspUuidVO();
        uuid.setUuid(tenantPortUuid);
        uuid.setObjClass(SspUuidVO.objClassNicProfile);
        uuid.setObjId(nic.getId());
        uuid.setReservationId(reservationId);
        _sspUuidDao.persist(uuid);
        break;
      }
    }
    if (tenantPortUuid == null) {
      s_logger.debug("#createNicEnv() failed for nic on " + network.toString());
      return false;
    }

    for (SspClient client :
        fetchSspClients(network.getPhysicalNetworkId(), network.getDataCenterId(), true)) {
      SspClient.TenantPort sspPort =
          client.updateTenantVifBinding(tenantPortUuid, dest.getHost().getPrivateIpAddress());
      if (sspPort != null) {
        if (sspPort.vlanId != null) {
          nic.setBroadcastType(BroadcastDomainType.Vlan);
          nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(String.valueOf(sspPort.vlanId)));
        }
        return true;
      }
    }
    s_logger.error("Updating vif failed " + nic.toString());
    return false;
  }
  protected void getIp(
      NicProfile nic,
      DataCenter dc,
      VirtualMachineProfile<? extends VirtualMachine> vm,
      Network network)
      throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException,
          ConcurrentOperationException {
    if (nic.getIp4Address() == null) {
      PublicIp ip =
          _networkMgr.assignPublicIpAddress(
              dc.getId(),
              vm.getOwner(),
              dc.getNetworkType().equals(NetworkType.Basic)
                  ? VlanType.DirectAttached
                  : VlanType.VirtualNetwork,
              null);
      nic.setIp4Address(ip.getAddress().toString());
      nic.setGateway(ip.getGateway());
      nic.setNetmask(ip.getNetmask());
      if (ip.getVlanTag() != null && ip.getVlanTag().equalsIgnoreCase("untagged")) {
        nic.setIsolationUri(URI.create("vlan://untagged"));
        nic.setBroadcastUri(URI.create("vlan://untagged"));
        nic.setBroadcastType(BroadcastDomainType.Native);
      } else if (ip.getVlanTag() != null) {
        nic.setIsolationUri(IsolationType.Vlan.toUri(ip.getVlanTag()));
        nic.setBroadcastUri(IsolationType.Vlan.toUri(ip.getVlanTag()));
        nic.setBroadcastType(BroadcastDomainType.Vlan);
      }

      nic.setFormat(AddressFormat.Ip4);
      nic.setReservationId(String.valueOf(ip.getVlanTag()));
      nic.setMacAddress(ip.getMacAddress());
    }
    nic.setDns1(dc.getDns1());
    nic.setDns2(dc.getDns2());
  }
  @Override
  public NicProfile createGuestNicProfileForVpcRouter(
      final RouterDeploymentDefinition vpcRouterDeploymentDefinition, final Network guestNetwork) {
    final NicProfile guestNic = new NicProfile();

    if (vpcRouterDeploymentDefinition.isRedundant()) {
      guestNic.setIPv4Address(_ipAddrMgr.acquireGuestIpAddress(guestNetwork, null));
    } else {
      guestNic.setIPv4Address(guestNetwork.getGateway());
    }

    guestNic.setBroadcastUri(guestNetwork.getBroadcastUri());
    guestNic.setBroadcastType(guestNetwork.getBroadcastDomainType());
    guestNic.setIsolationUri(guestNetwork.getBroadcastUri());
    guestNic.setMode(guestNetwork.getMode());
    final String gatewayCidr = guestNetwork.getCidr();
    guestNic.setIPv4Netmask(NetUtils.getCidrNetmask(gatewayCidr));

    return guestNic;
  }
  @Override
  public LinkedHashMap<Network, List<? extends NicProfile>> configureGuestNic(
      final RouterDeploymentDefinition routerDeploymentDefinition)
      throws ConcurrentOperationException, InsufficientAddressCapacityException {

    // Form networks
    final LinkedHashMap<Network, List<? extends NicProfile>> networks =
        new LinkedHashMap<Network, List<? extends NicProfile>>(3);
    // 1) Guest network
    final Network guestNetwork = routerDeploymentDefinition.getGuestNetwork();

    if (guestNetwork != null) {
      s_logger.debug("Adding nic for Virtual Router in Guest network " + guestNetwork);
      String defaultNetworkStartIp = null, defaultNetworkStartIpv6 = null;
      if (!routerDeploymentDefinition.isPublicNetwork()) {
        final Nic placeholder =
            _networkModel.getPlaceholderNicForRouter(
                guestNetwork, routerDeploymentDefinition.getPodId());
        if (guestNetwork.getCidr() != null) {
          if (placeholder != null && placeholder.getIPv4Address() != null) {
            s_logger.debug(
                "Requesting ipv4 address "
                    + placeholder.getIPv4Address()
                    + " stored in placeholder nic for the network "
                    + guestNetwork);
            defaultNetworkStartIp = placeholder.getIPv4Address();
          } else {
            final String startIp = _networkModel.getStartIpAddress(guestNetwork.getId());
            if (startIp != null
                && _ipAddressDao
                        .findByIpAndSourceNetworkId(guestNetwork.getId(), startIp)
                        .getAllocatedTime()
                    == null) {
              defaultNetworkStartIp = startIp;
            } else if (s_logger.isDebugEnabled()) {
              s_logger.debug(
                  "First ipv4 "
                      + startIp
                      + " in network id="
                      + guestNetwork.getId()
                      + " is already allocated, can't use it for domain router; will get random ip address from the range");
            }
          }
        }

        if (guestNetwork.getIp6Cidr() != null) {
          if (placeholder != null && placeholder.getIPv6Address() != null) {
            s_logger.debug(
                "Requesting ipv6 address "
                    + placeholder.getIPv6Address()
                    + " stored in placeholder nic for the network "
                    + guestNetwork);
            defaultNetworkStartIpv6 = placeholder.getIPv6Address();
          } else {
            final String startIpv6 = _networkModel.getStartIpv6Address(guestNetwork.getId());
            if (startIpv6 != null
                && _ipv6Dao.findByNetworkIdAndIp(guestNetwork.getId(), startIpv6) == null) {
              defaultNetworkStartIpv6 = startIpv6;
            } else if (s_logger.isDebugEnabled()) {
              s_logger.debug(
                  "First ipv6 "
                      + startIpv6
                      + " in network id="
                      + guestNetwork.getId()
                      + " is already allocated, can't use it for domain router; will get random ipv6 address from the range");
            }
          }
        }
      }

      final NicProfile gatewayNic = new NicProfile(defaultNetworkStartIp, defaultNetworkStartIpv6);
      if (routerDeploymentDefinition.isPublicNetwork()) {
        if (routerDeploymentDefinition.isRedundant()) {
          gatewayNic.setIPv4Address(_ipAddrMgr.acquireGuestIpAddress(guestNetwork, null));
        } else {
          gatewayNic.setIPv4Address(guestNetwork.getGateway());
        }
        gatewayNic.setBroadcastUri(guestNetwork.getBroadcastUri());
        gatewayNic.setBroadcastType(guestNetwork.getBroadcastDomainType());
        gatewayNic.setIsolationUri(guestNetwork.getBroadcastUri());
        gatewayNic.setMode(guestNetwork.getMode());
        final String gatewayCidr = guestNetwork.getCidr();
        gatewayNic.setIPv4Netmask(NetUtils.getCidrNetmask(gatewayCidr));
      } else {
        gatewayNic.setDefaultNic(true);
      }

      networks.put(guestNetwork, new ArrayList<NicProfile>(Arrays.asList(gatewayNic)));
    }
    return networks;
  }
  @Override
  @DB
  public NicProfile createPrivateNicProfileForGateway(
      final VpcGateway privateGateway, final VirtualRouter router) {
    final Network privateNetwork = _networkModel.getNetwork(privateGateway.getNetworkId());

    PrivateIpVO ipVO =
        _privateIpDao.allocateIpAddress(
            privateNetwork.getDataCenterId(),
            privateNetwork.getId(),
            privateGateway.getIp4Address());

    final Long vpcId = privateGateway.getVpcId();
    final Vpc activeVpc = _vpcMgr.getActiveVpc(vpcId);
    if (activeVpc.isRedundant() && ipVO == null) {
      ipVO = _privateIpDao.findByIpAndVpcId(vpcId, privateGateway.getIp4Address());
    }

    Nic privateNic = null;

    if (ipVO != null) {
      privateNic =
          _nicDao.findByIp4AddressAndNetworkId(ipVO.getIpAddress(), privateNetwork.getId());
    }

    NicProfile privateNicProfile = new NicProfile();

    if (privateNic != null) {
      privateNicProfile =
          new NicProfile(
              privateNic,
              privateNetwork,
              privateNic.getBroadcastUri(),
              privateNic.getIsolationUri(),
              _networkModel.getNetworkRate(privateNetwork.getId(), router.getId()),
              _networkModel.isSecurityGroupSupportedInNetwork(privateNetwork),
              _networkModel.getNetworkTag(router.getHypervisorType(), privateNetwork));

      if (router.getIsRedundantRouter()) {
        String newMacAddress =
            NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(ipVO.getMacAddress()));
        privateNicProfile.setMacAddress(newMacAddress);
      }
    } else {
      final String netmask = NetUtils.getCidrNetmask(privateNetwork.getCidr());
      final PrivateIpAddress ip =
          new PrivateIpAddress(
              ipVO,
              privateNetwork.getBroadcastUri().toString(),
              privateNetwork.getGateway(),
              netmask,
              NetUtils.long2Mac(NetUtils.createSequenceBasedMacAddress(ipVO.getMacAddress())));

      final URI netUri = BroadcastDomainType.fromString(ip.getBroadcastUri());
      privateNicProfile.setIPv4Address(ip.getIpAddress());
      privateNicProfile.setIPv4Gateway(ip.getGateway());
      privateNicProfile.setIPv4Netmask(ip.getNetmask());
      privateNicProfile.setIsolationUri(netUri);
      privateNicProfile.setBroadcastUri(netUri);
      // can we solve this in setBroadcastUri()???
      // or more plugable construct is desirable
      privateNicProfile.setBroadcastType(BroadcastDomainType.getSchemeValue(netUri));
      privateNicProfile.setFormat(AddressFormat.Ip4);
      privateNicProfile.setReservationId(String.valueOf(ip.getBroadcastUri()));
      privateNicProfile.setMacAddress(ip.getMacAddress());
    }

    return privateNicProfile;
  }