Search in sources :

Example 66 with Interface

use of org.batfish.datamodel.Interface in project batfish by batfish.

the class CommonUtil method computeIpOwners.

public static Map<Ip, Set<String>> computeIpOwners(boolean excludeInactive, Map<String, Map<String, Interface>> enabledInterfaces) {
    // TODO: confirm VRFs are handled correctly
    Map<Ip, Set<String>> ipOwners = new HashMap<>();
    Map<Pair<InterfaceAddress, Integer>, Set<Interface>> vrrpGroups = new HashMap<>();
    enabledInterfaces.forEach((hostname, currentEnabledInterfaces) -> {
        for (Interface i : currentEnabledInterfaces.values()) {
            if (!i.getActive() && (excludeInactive || !i.getBlacklisted())) {
                continue;
            }
            // collect vrrp info
            i.getVrrpGroups().forEach((groupNum, vrrpGroup) -> {
                InterfaceAddress address = vrrpGroup.getVirtualAddress();
                if (address == null) {
                    // never win the election, so is not a candidate.
                    return;
                }
                Pair<InterfaceAddress, Integer> key = new Pair<>(address, groupNum);
                Set<Interface> candidates = vrrpGroups.computeIfAbsent(key, k -> Collections.newSetFromMap(new IdentityHashMap<>()));
                candidates.add(i);
            });
            // collect prefixes
            i.getAllAddresses().stream().map(InterfaceAddress::getIp).forEach(ip -> {
                Set<String> owners = ipOwners.computeIfAbsent(ip, k -> new HashSet<>());
                owners.add(hostname);
            });
        }
    });
    vrrpGroups.forEach((p, candidates) -> {
        int groupNum = p.getSecond();
        InterfaceAddress address = p.getFirst();
        Ip ip = address.getIp();
        int lowestPriority = Integer.MAX_VALUE;
        String bestCandidate = null;
        SortedSet<String> bestCandidates = new TreeSet<>();
        for (Interface candidate : candidates) {
            VrrpGroup group = candidate.getVrrpGroups().get(groupNum);
            int currentPriority = group.getPriority();
            if (currentPriority < lowestPriority) {
                lowestPriority = currentPriority;
                bestCandidates.clear();
                bestCandidate = candidate.getOwner().getHostname();
            }
            if (currentPriority == lowestPriority) {
                bestCandidates.add(candidate.getOwner().getHostname());
            }
        }
        if (bestCandidates.size() != 1) {
            String deterministicBestCandidate = bestCandidates.first();
            bestCandidate = deterministicBestCandidate;
        // _logger.redflag(
        // "Arbitrarily choosing best vrrp candidate: '"
        // + deterministicBestCandidate
        // + " for prefix/groupNumber: '"
        // + p.toString()
        // + "' among multiple best candidates: "
        // + bestCandidates);
        }
        Set<String> owners = ipOwners.computeIfAbsent(ip, k -> new HashSet<>());
        owners.add(bestCandidate);
    });
    return ipOwners;
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) SortedSet(java.util.SortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) HashSet(java.util.HashSet) VrrpGroup(org.batfish.datamodel.VrrpGroup) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) Ip(org.batfish.datamodel.Ip) IdentityHashMap(java.util.IdentityHashMap) TreeSet(java.util.TreeSet) Interface(org.batfish.datamodel.Interface) Pair(org.batfish.common.Pair) NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair)

Example 67 with Interface

use of org.batfish.datamodel.Interface in project batfish by batfish.

the class CommonUtil method initPrivateIpsByPublicIp.

@VisibleForTesting
static SetMultimap<Ip, IpWildcardSetIpSpace> initPrivateIpsByPublicIp(Map<String, Configuration> configurations) {
    /*
     * Very hacky mapping from public IP to set of spaces of possible natted private IPs.
     * Does not currently support source-nat acl.
     *
     * The current implementation just considers every IP in every prefix on a non-masquerading
     * interface (except the local address in each such prefix) to be a possible private IP
     * match for every public IP referred to by every source-nat pool on a masquerading interface.
     */
    ImmutableSetMultimap.Builder<Ip, IpWildcardSetIpSpace> builder = ImmutableSetMultimap.builder();
    for (Configuration c : configurations.values()) {
        Collection<Interface> interfaces = c.getInterfaces().values();
        Set<InterfaceAddress> nonNattedInterfaceAddresses = interfaces.stream().filter(i -> i.getSourceNats().isEmpty()).flatMap(i -> i.getAllAddresses().stream()).collect(ImmutableSet.toImmutableSet());
        Set<IpWildcard> blacklist = nonNattedInterfaceAddresses.stream().map(address -> new IpWildcard(address.getIp(), Ip.ZERO)).collect(ImmutableSet.toImmutableSet());
        Set<IpWildcard> whitelist = nonNattedInterfaceAddresses.stream().map(address -> new IpWildcard(address.getPrefix())).collect(ImmutableSet.toImmutableSet());
        IpWildcardSetIpSpace ipSpace = IpWildcardSetIpSpace.builder().including(whitelist).excluding(blacklist).build();
        interfaces.stream().flatMap(i -> i.getSourceNats().stream()).forEach(sourceNat -> {
            for (long ipAsLong = sourceNat.getPoolIpFirst().asLong(); ipAsLong <= sourceNat.getPoolIpLast().asLong(); ipAsLong++) {
                Ip currentPoolIp = new Ip(ipAsLong);
                builder.put(currentPoolIp, ipSpace);
            }
        });
    }
    return builder.build();
}
Also used : SSLEngineConfigurator(org.glassfish.grizzly.ssl.SSLEngineConfigurator) SSLContext(javax.net.ssl.SSLContext) FileTime(java.nio.file.attribute.FileTime) StringUtils(org.apache.commons.lang3.StringUtils) Configurations(org.apache.commons.configuration2.builder.fluent.Configurations) Interface(org.batfish.datamodel.Interface) DirectoryStream(java.nio.file.DirectoryStream) BfConsts(org.batfish.common.BfConsts) Flow(org.batfish.datamodel.Flow) Topology(org.batfish.datamodel.Topology) Map(java.util.Map) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Pair(org.batfish.common.Pair) Path(java.nio.file.Path) DataPlane(org.batfish.datamodel.DataPlane) VrrpGroup(org.batfish.datamodel.VrrpGroup) ClientTracingFeature(io.opentracing.contrib.jaxrs2.client.ClientTracingFeature) Set(java.util.Set) FileAttribute(java.nio.file.attribute.FileAttribute) StandardCharsets(java.nio.charset.StandardCharsets) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) IOUtils(org.apache.commons.io.IOUtils) Stream(java.util.stream.Stream) Supplier(java.util.function.Supplier) TreeSet(java.util.TreeSet) JSONAssert(org.skyscreamer.jsonassert.JSONAssert) MustBeClosed(com.google.errorprone.annotations.MustBeClosed) SSLSession(javax.net.ssl.SSLSession) FlowProcessor(org.batfish.common.plugin.FlowProcessor) BiConsumer(java.util.function.BiConsumer) SSLContextConfigurator(org.glassfish.grizzly.ssl.SSLContextConfigurator) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Nullable(javax.annotation.Nullable) Files(java.nio.file.Files) Route(org.batfish.datamodel.Route) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) KeyManager(javax.net.ssl.KeyManager) TreeMap(java.util.TreeMap) Paths(java.nio.file.Paths) X509TrustManager(javax.net.ssl.X509TrustManager) BufferedReader(java.io.BufferedReader) X509Certificate(java.security.cert.X509Certificate) IpsecVpn(org.batfish.datamodel.IpsecVpn) NoSuchFileException(java.nio.file.NoSuchFileException) IpProtocol(org.batfish.datamodel.IpProtocol) SortedSet(java.util.SortedSet) URL(java.net.URL) TrustManager(javax.net.ssl.TrustManager) FlowTrace(org.batfish.datamodel.FlowTrace) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) OspfNeighbor(org.batfish.datamodel.OspfNeighbor) Edge(org.batfish.datamodel.Edge) IpWildcardSetIpSpace(org.batfish.datamodel.IpWildcardSetIpSpace) OspfProcess(org.batfish.datamodel.OspfProcess) URI(java.net.URI) HostnameVerifier(javax.net.ssl.HostnameVerifier) NamedPort(org.batfish.datamodel.NamedPort) Vrf(org.batfish.datamodel.Vrf) OspfArea(org.batfish.datamodel.OspfArea) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) ImmutableSet(com.google.common.collect.ImmutableSet) IdentityHashMap(java.util.IdentityHashMap) PatternSyntaxException(java.util.regex.PatternSyntaxException) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) FlowDisposition(org.batfish.datamodel.FlowDisposition) KeyStore(java.security.KeyStore) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) Entry(java.util.Map.Entry) Pattern(java.util.regex.Pattern) BgpNeighbor(org.batfish.datamodel.BgpNeighbor) SortedMap(java.util.SortedMap) IpWildcard(org.batfish.datamodel.IpWildcard) Ip(org.batfish.datamodel.Ip) NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair) Hashing(com.google.common.hash.Hashing) HashMap(java.util.HashMap) BatfishException(org.batfish.common.BatfishException) BgpProcess(org.batfish.datamodel.BgpProcess) Function(java.util.function.Function) HashSet(java.util.HashSet) ClientBuilder(javax.ws.rs.client.ClientBuilder) Configuration(org.batfish.datamodel.Configuration) OutputStreamWriter(java.io.OutputStreamWriter) OutputStream(java.io.OutputStream) IpLink(org.batfish.datamodel.IpLink) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) GlobalTracer(io.opentracing.util.GlobalTracer) FileInputStream(java.io.FileInputStream) SetMultimap(com.google.common.collect.SetMultimap) Consumer(java.util.function.Consumer) GrizzlyHttpServerFactory(org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) InputStream(java.io.InputStream) Prefix(org.batfish.datamodel.Prefix) Configuration(org.batfish.datamodel.Configuration) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) Ip(org.batfish.datamodel.Ip) IpWildcard(org.batfish.datamodel.IpWildcard) IpWildcardSetIpSpace(org.batfish.datamodel.IpWildcardSetIpSpace) Interface(org.batfish.datamodel.Interface) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 68 with Interface

use of org.batfish.datamodel.Interface in project batfish by batfish.

the class CommonUtil method initRemoteOspfNeighbors.

public static void initRemoteOspfNeighbors(Map<String, Configuration> configurations, Map<Ip, Set<String>> ipOwners, Topology topology) {
    for (Entry<String, Configuration> e : configurations.entrySet()) {
        String hostname = e.getKey();
        Configuration c = e.getValue();
        for (Entry<String, Vrf> e2 : c.getVrfs().entrySet()) {
            Vrf vrf = e2.getValue();
            OspfProcess proc = vrf.getOspfProcess();
            if (proc != null) {
                proc.setOspfNeighbors(new TreeMap<>());
                String vrfName = e2.getKey();
                for (Entry<Long, OspfArea> e3 : proc.getAreas().entrySet()) {
                    long areaNum = e3.getKey();
                    OspfArea area = e3.getValue();
                    for (String ifaceName : area.getInterfaces()) {
                        Interface iface = c.getInterfaces().get(ifaceName);
                        if (iface.getOspfPassive()) {
                            continue;
                        }
                        SortedSet<Edge> ifaceEdges = topology.getInterfaceEdges().get(new NodeInterfacePair(hostname, ifaceName));
                        boolean hasNeighbor = false;
                        Ip localIp = iface.getAddress().getIp();
                        if (ifaceEdges != null) {
                            for (Edge edge : ifaceEdges) {
                                if (edge.getNode1().equals(hostname)) {
                                    String remoteHostname = edge.getNode2();
                                    String remoteIfaceName = edge.getInt2();
                                    Configuration remoteNode = configurations.get(remoteHostname);
                                    Interface remoteIface = remoteNode.getInterfaces().get(remoteIfaceName);
                                    if (remoteIface.getOspfPassive()) {
                                        continue;
                                    }
                                    Vrf remoteVrf = remoteIface.getVrf();
                                    String remoteVrfName = remoteVrf.getName();
                                    OspfProcess remoteProc = remoteVrf.getOspfProcess();
                                    if (remoteProc != null) {
                                        if (remoteProc.getOspfNeighbors() == null) {
                                            remoteProc.setOspfNeighbors(new TreeMap<>());
                                        }
                                        OspfArea remoteArea = remoteProc.getAreas().get(areaNum);
                                        if (remoteArea != null && remoteArea.getInterfaces().contains(remoteIfaceName)) {
                                            Ip remoteIp = remoteIface.getAddress().getIp();
                                            IpLink localKey = new IpLink(localIp, remoteIp);
                                            OspfNeighbor neighbor = proc.getOspfNeighbors().get(localKey);
                                            if (neighbor == null) {
                                                hasNeighbor = true;
                                                // initialize local neighbor
                                                neighbor = new OspfNeighbor(localKey);
                                                neighbor.setArea(areaNum);
                                                neighbor.setVrf(vrfName);
                                                neighbor.setOwner(c);
                                                neighbor.setInterface(iface);
                                                proc.getOspfNeighbors().put(localKey, neighbor);
                                                // initialize remote neighbor
                                                IpLink remoteKey = new IpLink(remoteIp, localIp);
                                                OspfNeighbor remoteNeighbor = new OspfNeighbor(remoteKey);
                                                remoteNeighbor.setArea(areaNum);
                                                remoteNeighbor.setVrf(remoteVrfName);
                                                remoteNeighbor.setOwner(remoteNode);
                                                remoteNeighbor.setInterface(remoteIface);
                                                remoteProc.getOspfNeighbors().put(remoteKey, remoteNeighbor);
                                                // link neighbors
                                                neighbor.setRemoteOspfNeighbor(remoteNeighbor);
                                                remoteNeighbor.setRemoteOspfNeighbor(neighbor);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (!hasNeighbor) {
                            IpLink key = new IpLink(localIp, Ip.ZERO);
                            OspfNeighbor neighbor = new OspfNeighbor(key);
                            neighbor.setArea(areaNum);
                            neighbor.setVrf(vrfName);
                            neighbor.setOwner(c);
                            neighbor.setInterface(iface);
                            proc.getOspfNeighbors().put(key, neighbor);
                        }
                    }
                }
            }
        }
    }
}
Also used : IpLink(org.batfish.datamodel.IpLink) OspfNeighbor(org.batfish.datamodel.OspfNeighbor) OspfArea(org.batfish.datamodel.OspfArea) Configuration(org.batfish.datamodel.Configuration) NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair) Ip(org.batfish.datamodel.Ip) OspfProcess(org.batfish.datamodel.OspfProcess) Vrf(org.batfish.datamodel.Vrf) Edge(org.batfish.datamodel.Edge) Interface(org.batfish.datamodel.Interface)

Aggregations

Interface (org.batfish.datamodel.Interface)68 Configuration (org.batfish.datamodel.Configuration)42 Ip (org.batfish.datamodel.Ip)26 Edge (org.batfish.datamodel.Edge)21 Prefix (org.batfish.datamodel.Prefix)20 Test (org.junit.Test)19 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)18 Vrf (org.batfish.datamodel.Vrf)18 HashMap (java.util.HashMap)17 IpAccessList (org.batfish.datamodel.IpAccessList)16 Topology (org.batfish.datamodel.Topology)14 ArrayList (java.util.ArrayList)13 List (java.util.List)13 StaticRoute (org.batfish.datamodel.StaticRoute)13 HashSet (java.util.HashSet)12 Set (java.util.Set)12 BatfishException (org.batfish.common.BatfishException)12 Map (java.util.Map)11 TreeSet (java.util.TreeSet)10 SortedSet (java.util.SortedSet)9