@Override protected Set<InterfaceAddress> featureValueOf(Interface actual) { return actual.getAllAddresses(); } }
public void updateConfigIps(Configuration configuration) { configuration.getAllInterfaces().values().stream() .flatMap(iface -> iface.getAllAddresses().stream()) .map(InterfaceAddress::getIp) .map(IpWildcard::new) .forEach(ipWildcard -> getUsersIpSpace().add(ipWildcard)); }
/** Returns the lowest IP across all interfaces for now. We'll improve it later. */ @JsonIgnore @Nullable public Ip getCanonicalIp() { return getVrfs().values().stream() .flatMap(v -> v.getInterfaces().values().stream()) .flatMap(i -> i.getAllAddresses().stream()) .map(InterfaceAddress::getIp) .min(Ip::compareTo) .orElse(null); }
@Override public Set<Interface> resolve(Set<String> nodes, SpecifierContext ctxt) { return ctxt.getConfigs().values().stream() .filter(c -> nodes.contains(c.getHostname())) .flatMap(c -> c.getAllInterfaces().values().stream().filter(Interface::getActive)) .filter(i -> i.getAllAddresses().stream().anyMatch(this::interfaceAddressMatchesIpSpace)) .collect(Collectors.toSet()); }
/** 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; }
private Set<InterfaceAddress> interfaceAddresses(String node, String iface) { return _specifierContext .getConfigs() .get(node) .getAllInterfaces() .get(iface) .getAllAddresses(); }
@VisibleForTesting public static Set<String> getPrefixes(Map<String, Configuration> configurations) { ImmutableSet.Builder<String> prefixes = ImmutableSet.builder(); configurations .values() .forEach( configuration -> configuration .getAllInterfaces() .values() .forEach( iface -> iface.getAllAddresses().stream() .map(interfaceAddress -> interfaceAddress.getPrefix().toString()) .forEach(prefixes::add))); return prefixes.build(); }
@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(); }
@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); }
iface.getAllAddresses().stream() .map( ifaceAdrr ->
@VisibleForTesting static Row layer3EdgeToRow(Map<String, Configuration> configurations, Edge edge) { Interface interface1 = configurations.get(edge.getNode1()).getAllInterfaces().get(edge.getInt1()); Interface interface2 = configurations.get(edge.getNode2()).getAllInterfaces().get(edge.getInt2()); Set<Ip> ips1 = interface1.getAllAddresses().stream() .filter(Objects::nonNull) .map(InterfaceAddress::getIp) .collect(Collectors.toSet()); Set<Ip> ips2 = interface2.getAllAddresses().stream() .filter(Objects::nonNull) .map(InterfaceAddress::getIp) .collect(Collectors.toSet()); RowBuilder row = Row.builder(); row.put(COL_INTERFACE, new NodeInterfacePair(edge.getNode1(), edge.getInt1())) .put(COL_IPS, ips1) .put(COL_REMOTE_INTERFACE, new NodeInterfacePair(edge.getNode2(), edge.getInt2())) .put(COL_REMOTE_IPS, ips2); return row.build(); }
@VisibleForTesting static Map<String, Map<String, Map<String, IpSpace>>> computeInterfaceHostSubnetIps( Map<String, Configuration> configs) { try (ActiveSpan span = GlobalTracer.get() .buildSpan("ForwardingAnalysisImpl.computeInterfaceHostSubnetIps") .startActive()) { assert span != null; // avoid unused warning return toImmutableMap( configs, Entry::getKey, /* hostname */ nodeEntry -> toImmutableMap( nodeEntry.getValue().getVrfs(), Entry::getKey, /* vrf */ vrfEntry -> toImmutableMap( vrfEntry.getValue().getInterfaces(), Entry::getKey, /* interface */ ifaceEntry -> firstNonNull( AclIpSpace.union( ifaceEntry.getValue().getAllAddresses().stream() .map(InterfaceAddress::getPrefix) .map(Prefix::toHostIpSpace) .collect(ImmutableList.toImmutableList())), EmptyIpSpace.INSTANCE)))); } }
/** * Initialize the connected RIB -- a RIB containing connected routes (i.e., direct connections to * neighbors). */ @VisibleForTesting void initConnectedRib() { // Look at all connected interfaces for (Interface i : _vrf.getInterfaces().values()) { if (i.getActive()) { // Make sure the interface is active // Create a route for each interface prefix for (InterfaceAddress ifaceAddress : i.getAllAddresses()) { Prefix prefix = ifaceAddress.getPrefix(); ConnectedRoute cr = new ConnectedRoute(prefix, i.getName()); _connectedRib.mergeRoute(cr); } } } }
/** Initialize RIP routes from the interface prefixes */ @VisibleForTesting void initBaseRipRoutes() { if (_vrf.getRipProcess() == null) { return; // nothing to do } // init internal routes from connected routes for (String ifaceName : _vrf.getRipProcess().getInterfaces()) { Interface iface = _vrf.getInterfaces().get(ifaceName); if (iface.getActive()) { Set<Prefix> allNetworkPrefixes = iface.getAllAddresses().stream() .map(InterfaceAddress::getPrefix) .collect(Collectors.toSet()); long cost = RipProcess.DEFAULT_RIP_COST; for (Prefix prefix : allNetworkPrefixes) { RipInternalRoute route = new RipInternalRoute( prefix, Route.UNSET_ROUTE_NEXT_HOP_IP, RoutingProtocol.RIP.getDefaultAdministrativeCost(_c.getConfigurationFormat()), cost); _ripInternalRib.mergeRouteGetDelta(route); } } } }
/** * Initialize the local RIB -- a RIB containing non-forwarding /32 routes for exact addresses of * interfaces */ @VisibleForTesting void initLocalRib() { // Look at all connected interfaces for (Interface i : _vrf.getInterfaces().values()) { if (i.getActive()) { // Make sure the interface is active // Create a route for each interface prefix for (InterfaceAddress ifaceAddress : i.getAllAddresses()) { if (ifaceAddress.getNetworkBits() < Prefix.MAX_PREFIX_LENGTH) { LocalRoute lr = new LocalRoute(ifaceAddress, i.getName()); _localRib.mergeRoute(lr); } } } } }
iface -> { iface .getAllAddresses() .forEach( address -> {
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; }
@Override public BooleanExpr toBooleanExpr(JuniperConfiguration jc, Configuration c, Warnings warnings) { Interface iface = c.getAllInterfaces().get(_name); if (iface == null) { // No such interface, won't match anything return BooleanExprs.FALSE; } // Convert to conjunction of connected protocol and matching at least one of the interface // prefixes return new Conjunction( ImmutableList.of( new MatchProtocol(RoutingProtocol.CONNECTED), new MatchPrefixSet( DestinationNetwork.instance(), new ExplicitPrefixSet( new PrefixSpace( c.getAllInterfaces().get(_name).getAllAddresses().stream() .map(InterfaceAddress::getPrefix) .map(PrefixRange::fromPrefix) .collect(ImmutableSet.toImmutableSet())))))); } }
/** * 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()); }