/** * Computes a mapping of primary {@link Ip}s to the names of interfaces owning them. Filters out * the interfaces having no primary {@link InterfaceAddress} */ private static Map<Ip, String> computeIpToIfaceNameMap(Map<String, Interface> interfaces) { Map<Ip, String> ipToIfaceNameMap = new HashMap<>(); for (Entry<String, Interface> interfaceNameToInterface : interfaces.entrySet()) { interfaceNameToInterface .getValue() .getAllAddresses() .forEach( interfaceAddress -> { ipToIfaceNameMap.put(interfaceAddress.getIp(), interfaceNameToInterface.getKey()); }); } return ipToIfaceNameMap; }
/** * Computes a mapping of interface names to the primary {@link Ip} owned by each of the interface. * Filters out the interfaces having no primary {@link InterfaceAddress} */ private static Map<String, Ip> computeInterfaceOwnedPrimaryIp(Map<String, Interface> interfaces) { return interfaces.entrySet().stream() .filter(e -> Objects.nonNull(e.getValue().getAddress())) .collect( ImmutableMap.toImmutableMap(Entry::getKey, e -> e.getValue().getAddress().getIp())); }
/** Returns {@code true} if any {@link Ip IP address} is owned by both devices. */ private static boolean haveIpInCommon(Interface i1, Interface i2) { for (InterfaceAddress ia : i1.getAllAddresses()) { for (InterfaceAddress ia2 : i2.getAllAddresses()) { if (ia.getIp().equals(ia2.getIp())) { return true; } } } return false; }
@VisibleForTesting public static Set<String> getIps(Map<String, Configuration> configurations) { ImmutableSet.Builder<String> ips = ImmutableSet.builder(); configurations .values() .forEach( configuration -> configuration .getAllInterfaces() .values() .forEach( iface -> iface.getAllAddresses().stream() .map(interfaceAddress -> interfaceAddress.getIp().toString()) .forEach(ips::add))); return ips.build(); }
/** * Returns an active interface with the specified IP address for configuration. * * @param ipAddress The IP address to check * @param c The configuration object in which to check * @return Any Interface that matches the condition */ public static Optional<Interface> getActiveInterfaceWithIp(Ip ipAddress, Configuration c) { return c.getAllInterfaces().values().stream() .filter( iface -> iface.getActive() && iface.getAllAddresses().stream() .anyMatch(ifAddr -> Objects.equals(ifAddr.getIp(), ipAddress))) .findAny(); }
private void generateAristaDynamicSourceNats( org.batfish.datamodel.Interface newIface, List<AristaDynamicSourceNat> aristaDynamicSourceNats) { Ip interfaceIp = newIface.getAddress().getIp(); Transformation next = null; for (AristaDynamicSourceNat nat : Lists.reverse(aristaDynamicSourceNats)) { next = nat.toTransformation(interfaceIp, _natPools, next).orElse(next); } newIface.setOutgoingTransformation(next); }
public LocalRoute(InterfaceAddress interfaceAddress, String nextHopInterface) { this( Prefix.create(interfaceAddress.getIp(), Prefix.MAX_PREFIX_LENGTH), nextHopInterface, interfaceAddress.getNetworkBits(), 0); }
@VisibleForTesting static boolean interfaceRepliesToArpRequestForIp(Interface iface, Fib ifaceFib, Ip arpIp) { // interfaces without addresses never reply if (iface.getAllAddresses().isEmpty()) { return false; } // the interface that owns the arpIp always replies if (iface.getAllAddresses().stream().anyMatch(addr -> addr.getIp().equals(arpIp))) { return true; } /* * iface does not own arpIp, so it replies if and only if: * 1. proxy-arp is enabled * 2. the interface's vrf has a route to the destination * 3. the destination is not on the incoming edge. */ Set<String> nextHopInterfaces = ifaceFib.getNextHopInterfaces(arpIp); return iface.getProxyArp() && !nextHopInterfaces.isEmpty() && nextHopInterfaces.stream().noneMatch(iface.getName()::equals); }
String buildRoute(Prefix pfx, Protocol proto, GraphEdge graphEdge) { String type; String nextHopIP; String nextHopInt; if (proto.isConnected()) { type = "ConnectedRoute"; nextHopIP = "AUTO/NONE(-1l)"; nextHopInt = graphEdge.getStart().getName(); } else { type = StringUtils.capitalize(proto.name().toLowerCase()) + "Route"; nextHopIP = graphEdge.getStart().getAddress().getIp().toString(); nextHopInt = "dynamic"; } return String.format("%s<%s,nhip:%s,nhint:%s>", type, pfx, nextHopIP, nextHopInt); }
/** * Test that VRRP tie breaking works correctly by picking higher IP in case of equal vrrp group * priority */ @Test public void testVrrpPriorityTieBreaking() { Map<String, Configuration> configs = setupVrrpTestCase(true); Map<Ip, Set<String>> owners = computeIpNodeOwners(configs, false); // Ensure node that has higher interface IP wins assertThat(owners.get(_virtInterfaceAddr.getIp()), contains("n2")); }
/** Test that higher priority router wins */ @Test public void testVrrpPriority() { Map<String, Configuration> configs = setupVrrpTestCase(false); Map<Ip, Set<String>> owners = computeIpNodeOwners(configs, false); assertThat(owners.get(_virtInterfaceAddr.getIp()), contains("n2")); }
@Override public void exitNatp_address(Natp_addressContext ctx) { if (ctx.TO() != null) { if (ctx.IP_ADDRESS().isEmpty()) { // from IP_PREFIX to IP_PREFIX // Juniper will treat IP_PREFIX as IP ADDRESS _currentNatPool.setFromAddress(new InterfaceAddress(ctx.from.getText()).getIp()); _currentNatPool.setToAddress(new InterfaceAddress(ctx.to.getText()).getIp()); } else { // from IP_ADDRESS to IP_ADDRESS _currentNatPool.setFromAddress(Ip.parse(ctx.from.getText())); _currentNatPool.setToAddress(Ip.parse(ctx.to.getText())); } } else { Prefix prefix = Prefix.parse(ctx.prefix.getText()); _currentNatPool.setFromAddress(prefix.getFirstHostIp()); _currentNatPool.setToAddress(prefix.getLastHostIp()); } }
/** * Generate IS-IS from a given interface for a given level (with a given metric/admin cost) and * merge them into the appropriate RIB. */ private static Set<IsisRoute> generateIsisInterfaceRoutesPerLevel( int adminCost, IsisRoute.Builder routeBuilder, Interface iface, IsisLevel level) { IsisInterfaceLevelSettings ifaceLevelSettings = level == IsisLevel.LEVEL_1 ? iface.getIsis().getLevel1() : iface.getIsis().getLevel2(); RoutingProtocol isisProtocol = level == IsisLevel.LEVEL_1 ? RoutingProtocol.ISIS_L1 : RoutingProtocol.ISIS_L2; long metric = ifaceLevelSettings.getMode() == IsisInterfaceMode.PASSIVE ? 0L : firstNonNull(ifaceLevelSettings.getCost(), IsisRoute.DEFAULT_METRIC); routeBuilder.setAdmin(adminCost).setLevel(level).setMetric(metric).setProtocol(isisProtocol); return iface.getAllAddresses().stream() .map( address -> routeBuilder.setNetwork(address.getPrefix()).setNextHopIp(address.getIp()).build()) .collect(ImmutableSet.toImmutableSet()); }
private boolean propagateOspfInterAreaRouteFromIntraAreaRoute( Configuration neighbor, OspfProcess neighborProc, OspfIntraAreaRoute neighborRoute, long incrementalCost, Interface neighborInterface, int adminCost, long linkAreaNum) { return OspfProtocolHelper.isOspfInterAreaFromIntraAreaPropagationAllowed( linkAreaNum, neighbor, neighborProc, neighborRoute, neighborInterface.getOspfArea()) && stageOspfInterAreaRoute( neighborRoute, neighborInterface.getVrf().getOspfProcess().getMaxMetricSummaryNetworks(), neighborInterface.getAddress().getIp(), incrementalCost, adminCost, linkAreaNum); }
private org.batfish.datamodel.Interface toInterface(Interface iface) { String name = iface.getName(); org.batfish.datamodel.Interface newIface = new org.batfish.datamodel.Interface(name, _c); newIface.setDeclaredNames(ImmutableSortedSet.of(name)); newIface.setActive(true); // TODO: may have to change newIface.setBandwidth(iface.getBandwidth()); newIface.setDescription(iface.getDescription()); InterfaceAddress address = iface.getAddress(); if (address != null) { newIface.setAddress(iface.getAddress()); } newIface.getAllAddresses().addAll(iface.getAllAddresses()); for (InterfaceAddress p : newIface.getAllAddresses()) { _ipToInterfaceMap.put(p.getIp(), newIface); } return newIface; }
@Test public void resolveIpSpaceOfLocationTestDefault() { SpecifiersQuestion question = new SpecifiersQuestion(QueryType.IP_SPACE); assertThat( resolveIpSpaceOfLocation(question, _context).getRows().getData(), equalTo( ImmutableMultiset.of( Row.of( COL_LOCATIONS, ImmutableSet.of(new InterfaceLocation(_c1.getHostname(), _iface1.getName())) .toString(), COL_IP_SPACE, _iface1.getAddress().getIp().toIpSpace().toString()), Row.of( COL_LOCATIONS, ImmutableSet.of(new InterfaceLocation(_c2.getHostname(), _iface2.getName())) .toString(), COL_IP_SPACE, _iface2.getAddress().getIp().toIpSpace().toString())))); }
boolean propagateOspfIntraAreaRoute( OspfIntraAreaRoute neighborRoute, long incrementalCost, Interface neighborInterface, int adminCost, long linkAreaNum) { long newCost = neighborRoute.getMetric() + incrementalCost; Ip nextHopIp = neighborInterface.getAddress().getIp(); OspfIntraAreaRoute newRoute = (OspfIntraAreaRoute) OspfIntraAreaRoute.builder() .setProtocol(RoutingProtocol.OSPF) .setNetwork(neighborRoute.getNetwork()) .setNextHopIp(nextHopIp) .setAdmin(adminCost) .setMetric(newCost) .setArea(linkAreaNum) .build(); return neighborRoute.getArea() == linkAreaNum && (_ospfIntraAreaStagingRib.mergeRoute(newRoute)); }
@Test public void resolveIpSpaceOfLocationTest() { SpecifiersQuestion questionWithLocation = new SpecifiersQuestion(QueryType.IP_SPACE_OF_LOCATION); questionWithLocation.setLocationSpecifierInput(_c1.getHostname()); // only c1:iface1 should be present assertThat( resolveIpSpaceOfLocation(questionWithLocation, _context).getRows().getData(), equalTo( ImmutableMultiset.of( Row.of( COL_LOCATIONS, ImmutableSet.of(new InterfaceLocation(_c1.getHostname(), _iface1.getName())) .toString(), COL_IP_SPACE, _iface1.getAddress().getIp().toIpSpace().toString())))); }
public Interface toInterface(Configuration configuration, Warnings warnings) { String name = _canonicalName != null ? _canonicalName : _name; Interface.Builder iface = Interface.builder() .setName(name) .setOwner(configuration) .setAddresses(_address, _otherAddresses) .setBandwidth(_bandwidth) .setDeclaredNames(ImmutableSortedSet.of(_name)) .setProxyArp(false) .setVrf(configuration.getDefaultVrf()); if (_shared) { Ip publicIp = _address.getIp(); iface.setOutgoingTransformation(always().apply(assignSourceIp(publicIp, publicIp)).build()); } return iface.build(); } }