Search in sources :

Example 1 with Network

use of com.google.common.graph.Network in project DaggerCodeParse-2.38.1base by hellogaod.

the class ExternalBindingGraphConverter method fromSpiModel.

private static ImmutableNetwork<Node, Edge> fromSpiModel(Network<dagger.spi.model.BindingGraph.Node, dagger.spi.model.BindingGraph.Edge> spiNetwork) {
    MutableNetwork<Node, Edge> network = NetworkBuilder.directed().allowsParallelEdges(true).allowsSelfLoops(true).build();
    ImmutableMap<dagger.spi.model.BindingGraph.Node, Node> fromSpiNodes = spiNetwork.nodes().stream().collect(toImmutableMap(spiNode -> spiNode, ExternalBindingGraphConverter::fromSpiModel));
    for (Node node : fromSpiNodes.values()) {
        network.addNode(node);
    }
    for (dagger.spi.model.BindingGraph.Edge edge : spiNetwork.edges()) {
        EndpointPair<dagger.spi.model.BindingGraph.Node> edgePair = spiNetwork.incidentNodes(edge);
        network.addEdge(fromSpiNodes.get(edgePair.source()), fromSpiNodes.get(edgePair.target()), fromSpiModel(edge));
    }
    return ImmutableNetwork.copyOf(network);
}
Also used : SubcomponentCreatorBindingEdge(dagger.model.BindingGraph.SubcomponentCreatorBindingEdge) DaggerElement(dagger.spi.model.DaggerElement) ComponentNode(dagger.model.BindingGraph.ComponentNode) Scope(dagger.model.Scope) BindingGraph(dagger.model.BindingGraph) EndpointPair(com.google.common.graph.EndpointPair) ComponentPath(dagger.model.ComponentPath) Diagnostic(javax.tools.Diagnostic) DaggerAnnotation(dagger.spi.model.DaggerAnnotation) ImmutableNetwork(com.google.common.graph.ImmutableNetwork) FormatMethod(com.google.errorprone.annotations.FormatMethod) MissingBinding(dagger.model.BindingGraph.MissingBinding) Binding(dagger.model.Binding) DaggerTypeElement(dagger.spi.model.DaggerTypeElement) Network(com.google.common.graph.Network) Key(dagger.model.Key) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) MaybeBinding(dagger.model.BindingGraph.MaybeBinding) ChildFactoryMethodEdge(dagger.model.BindingGraph.ChildFactoryMethodEdge) DaggerStreams.toImmutableMap(dagger.internal.codegen.extension.DaggerStreams.toImmutableMap) Memoized(com.google.auto.value.extension.memoized.Memoized) ImmutableMap(com.google.common.collect.ImmutableMap) NetworkBuilder(com.google.common.graph.NetworkBuilder) DaggerStreams.toImmutableSet(dagger.internal.codegen.extension.DaggerStreams.toImmutableSet) RequestKind(dagger.model.RequestKind) DaggerStreams.toImmutableList(dagger.internal.codegen.extension.DaggerStreams.toImmutableList) DependencyRequest(dagger.model.DependencyRequest) DependencyEdge(dagger.model.BindingGraph.DependencyEdge) Edge(dagger.model.BindingGraph.Edge) Node(dagger.model.BindingGraph.Node) DiagnosticReporter(dagger.spi.DiagnosticReporter) AutoValue(com.google.auto.value.AutoValue) MutableNetwork(com.google.common.graph.MutableNetwork) Optional(java.util.Optional) MultibindingContributionIdentifier(dagger.model.Key.MultibindingContributionIdentifier) BindingKind(dagger.model.BindingKind) BindingGraph(dagger.model.BindingGraph) ComponentNode(dagger.model.BindingGraph.ComponentNode) Node(dagger.model.BindingGraph.Node) SubcomponentCreatorBindingEdge(dagger.model.BindingGraph.SubcomponentCreatorBindingEdge) ChildFactoryMethodEdge(dagger.model.BindingGraph.ChildFactoryMethodEdge) DependencyEdge(dagger.model.BindingGraph.DependencyEdge) Edge(dagger.model.BindingGraph.Edge)

Example 2 with Network

use of com.google.common.graph.Network in project batfish by batfish.

the class BgpTopologyUtils method initBgpTopology.

/**
 * Compute the BGP topology -- a network of {@link BgpPeerConfigId}s connected by {@link
 * BgpSessionProperties}.
 *
 * @param configurations node configurations, keyed by hostname
 * @param ipVrfOwners network Ip owners (see {@link IpOwners#computeIpNodeOwners(Map, boolean)}
 *     for reference)
 * @param keepInvalid whether to keep improperly configured neighbors. If performing configuration
 *     checks, you probably want this set to {@code true}, otherwise (e.g., computing dataplane)
 *     you want this to be {@code false}.
 * @param checkReachability whether to perform dataplane-level checks to ensure that neighbors are
 *     reachable and sessions can be established correctly. <b>Note:</b> this is different from
 *     {@code keepInvalid=false}, which only does filters invalid neighbors at the control-plane
 *     level
 * @param tracerouteEngine an instance of {@link TracerouteEngine} for doing reachability checks.
 * @param l3Adjacencies {@link L3Adjacencies} of the network, for checking BGP unnumbered
 *     reachability.
 * @return A graph ({@link Network}) representing all BGP peerings.
 */
@Nonnull
public static BgpTopology initBgpTopology(Map<String, Configuration> configurations, Map<Ip, Map<String, Set<String>>> ipVrfOwners, boolean keepInvalid, boolean checkReachability, @Nullable TracerouteEngine tracerouteEngine, Map<String, Map<String, Fib>> fibs, L3Adjacencies l3Adjacencies) {
    checkArgument(!checkReachability || !keepInvalid, "Cannot check reachability while keeping invalid peers");
    checkArgument(!checkReachability || tracerouteEngine != null, "Cannot check reachability without a traceroute engine");
    // TODO: handle duplicate ips on different vrfs
    NetworkConfigurations networkConfigurations = NetworkConfigurations.of(configurations);
    /*
     * First pass: identify all addresses "owned" by BgpNeighbors, add neighbor ids as vertices to
     * the graph; dynamically determine local IPs as needed
     */
    MutableValueGraph<BgpPeerConfigId, BgpSessionProperties> graph = ValueGraphBuilder.directed().allowsSelfLoops(false).build();
    /*
     * Multimap of active peers' BgpPeerConfigIds to all IPs that each peer may use as local IP
     * when initiating a session. For a peer with an explicitly configured local IP, that IP is
     * the only value associated with the peer in this map. Otherwise:
     * - If FIBs are provided, the map contains all local IPs with which the peer may initiate,
     *   as inferred by getPotentialSrcIps().
     * - Else no IPs are associated with the peer.
     */
    ImmutableSetMultimap.Builder<BgpPeerConfigId, Ip> localIpsBuilder = ImmutableSetMultimap.builder();
    for (Configuration node : configurations.values()) {
        String hostname = node.getHostname();
        for (Vrf vrf : node.getVrfs().values()) {
            String vrfName = vrf.getName();
            BgpProcess proc = vrf.getBgpProcess();
            if (proc == null) {
                // nothing to do if no bgp process on this VRF
                continue;
            }
            Fib fib = fibs.getOrDefault(hostname, ImmutableMap.of()).get(vrfName);
            for (Entry<Ip, BgpActivePeerConfig> e : proc.getActiveNeighbors().entrySet()) {
                Ip peerAddress = e.getKey();
                BgpActivePeerConfig config = e.getValue();
                if (!keepInvalid && !bgpConfigPassesSanityChecks(config, hostname, vrfName, ipVrfOwners)) {
                    continue;
                }
                BgpPeerConfigId neighborId = new BgpPeerConfigId(hostname, vrfName, peerAddress.toPrefix(), false);
                graph.addNode(neighborId);
                if (config.getLocalIp() != null) {
                    localIpsBuilder.put(neighborId, config.getLocalIp());
                } else if (fib != null) {
                    // No explicitly configured local IP. Check for dynamically resolvable local IPs.
                    localIpsBuilder.putAll(neighborId, getPotentialSrcIps(peerAddress, fib, node));
                }
            }
            // Dynamic peers: map of prefix to BgpPassivePeerConfig
            proc.getPassiveNeighbors().entrySet().stream().filter(entry -> keepInvalid || bgpConfigPassesSanityChecks(entry.getValue(), hostname, vrfName, ipVrfOwners)).forEach(entry -> graph.addNode(new BgpPeerConfigId(hostname, vrfName, entry.getKey(), true)));
            // Unnumbered BGP peers: map of interface name to BgpUnnumberedPeerConfig
            proc.getInterfaceNeighbors().entrySet().stream().filter(e -> keepInvalid || bgpConfigPassesSanityChecks(e.getValue(), hostname, vrfName, ipVrfOwners)).forEach(e -> graph.addNode(new BgpPeerConfigId(hostname, vrf.getName(), e.getKey())));
        }
    }
    // Second pass: add edges to the graph. Note, these are directed edges.
    Map<String, Multimap<String, BgpPeerConfigId>> receivers = new HashMap<>();
    for (BgpPeerConfigId peer : graph.nodes()) {
        if (peer.getType() == BgpPeerConfigType.UNNUMBERED) {
            // Unnumbered configs only form sessions with each other
            continue;
        }
        Multimap<String, BgpPeerConfigId> vrf = receivers.computeIfAbsent(peer.getHostname(), name -> LinkedListMultimap.create());
        vrf.put(peer.getVrfName(), peer);
    }
    SetMultimap<BgpPeerConfigId, Ip> localIps = localIpsBuilder.build();
    for (BgpPeerConfigId neighborId : graph.nodes()) {
        switch(neighborId.getType()) {
            case DYNAMIC:
                // Passive end of the peering cannot initiate a connection
                continue;
            case ACTIVE:
                addActivePeerEdges(neighborId, graph, networkConfigurations, ipVrfOwners, receivers, localIps.get(neighborId), checkReachability, tracerouteEngine);
                break;
            case UNNUMBERED:
                addUnnumberedPeerEdges(neighborId, graph, networkConfigurations, l3Adjacencies);
                break;
            default:
                throw new IllegalArgumentException(String.format("Unrecognized peer type: %s", neighborId));
        }
    }
    return new BgpTopology(graph);
}
Also used : BgpPeerConfigType(org.batfish.datamodel.BgpPeerConfigId.BgpPeerConfigType) IpProtocol(org.batfish.datamodel.IpProtocol) Hop(org.batfish.datamodel.flow.Hop) FibForward(org.batfish.datamodel.FibForward) FibNextVrf(org.batfish.datamodel.FibNextVrf) Trace(org.batfish.datamodel.flow.Trace) Interface(org.batfish.datamodel.Interface) ValueGraphBuilder(com.google.common.graph.ValueGraphBuilder) Flow(org.batfish.datamodel.Flow) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) NetworkConfigurations(org.batfish.datamodel.NetworkConfigurations) Map(java.util.Map) Network(com.google.common.graph.Network) NamedPort(org.batfish.datamodel.NamedPort) Vrf(org.batfish.datamodel.Vrf) IpOwners(org.batfish.common.topology.IpOwners) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) ImmutableSet(com.google.common.collect.ImmutableSet) BgpPeerConfig(org.batfish.datamodel.BgpPeerConfig) ImmutableMap(com.google.common.collect.ImmutableMap) FlowDisposition(org.batfish.datamodel.FlowDisposition) FibActionVisitor(org.batfish.datamodel.visitors.FibActionVisitor) Set(java.util.Set) TraceAndReverseFlow(org.batfish.datamodel.flow.TraceAndReverseFlow) Objects(java.util.Objects) List(java.util.List) BgpPassivePeerConfig(org.batfish.datamodel.BgpPassivePeerConfig) L3Adjacencies(org.batfish.common.topology.L3Adjacencies) Entry(java.util.Map.Entry) Ip(org.batfish.datamodel.Ip) LongSpace(org.batfish.datamodel.LongSpace) NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair) TracerouteEngine(org.batfish.common.plugin.TracerouteEngine) BgpActivePeerConfig(org.batfish.datamodel.BgpActivePeerConfig) HashMap(java.util.HashMap) BgpProcess(org.batfish.datamodel.BgpProcess) Multimap(com.google.common.collect.Multimap) BgpPeerConfigId(org.batfish.datamodel.BgpPeerConfigId) ImmutableList(com.google.common.collect.ImmutableList) Configuration(org.batfish.datamodel.Configuration) FibNullRoute(org.batfish.datamodel.FibNullRoute) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ConcreteInterfaceAddress(org.batfish.datamodel.ConcreteInterfaceAddress) Fib(org.batfish.datamodel.Fib) MoreObjects(com.google.common.base.MoreObjects) SetMultimap(com.google.common.collect.SetMultimap) SessionType(org.batfish.datamodel.BgpSessionProperties.SessionType) MutableValueGraph(com.google.common.graph.MutableValueGraph) BgpSessionProperties(org.batfish.datamodel.BgpSessionProperties) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FibEntry(org.batfish.datamodel.FibEntry) BgpUnnumberedPeerConfig(org.batfish.datamodel.BgpUnnumberedPeerConfig) Configuration(org.batfish.datamodel.Configuration) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) BgpProcess(org.batfish.datamodel.BgpProcess) HashMap(java.util.HashMap) NetworkConfigurations(org.batfish.datamodel.NetworkConfigurations) Ip(org.batfish.datamodel.Ip) FibNextVrf(org.batfish.datamodel.FibNextVrf) Vrf(org.batfish.datamodel.Vrf) BgpSessionProperties(org.batfish.datamodel.BgpSessionProperties) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) Multimap(com.google.common.collect.Multimap) SetMultimap(com.google.common.collect.SetMultimap) BgpActivePeerConfig(org.batfish.datamodel.BgpActivePeerConfig) BgpPeerConfigId(org.batfish.datamodel.BgpPeerConfigId) Fib(org.batfish.datamodel.Fib) Nonnull(javax.annotation.Nonnull)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)2 Network (com.google.common.graph.Network)2 AutoValue (com.google.auto.value.AutoValue)1 Memoized (com.google.auto.value.extension.memoized.Memoized)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 MoreObjects (com.google.common.base.MoreObjects)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 LinkedListMultimap (com.google.common.collect.LinkedListMultimap)1 Multimap (com.google.common.collect.Multimap)1 SetMultimap (com.google.common.collect.SetMultimap)1 EndpointPair (com.google.common.graph.EndpointPair)1 ImmutableNetwork (com.google.common.graph.ImmutableNetwork)1 MutableNetwork (com.google.common.graph.MutableNetwork)1 MutableValueGraph (com.google.common.graph.MutableValueGraph)1 NetworkBuilder (com.google.common.graph.NetworkBuilder)1 ValueGraphBuilder (com.google.common.graph.ValueGraphBuilder)1 FormatMethod (com.google.errorprone.annotations.FormatMethod)1