private static <N> ImmutableNetwork<N, N> networkWithNode(N node) { MutableNetwork<N, N> network = NetworkBuilder.directed().build(); network.addNode(node); return ImmutableNetwork.copyOf(network); } }
@Test public void immutableNetwork() { MutableNetwork<String, Integer> mutableNetwork = NetworkBuilder.directed().build(); mutableNetwork.addNode("A"); ImmutableNetwork<String, Integer> immutableNetwork = ImmutableNetwork.copyOf(mutableNetwork); assertThat(immutableNetwork.asGraph()).isInstanceOf(ImmutableGraph.class); assertThat(immutableNetwork).isNotInstanceOf(MutableNetwork.class); assertThat(immutableNetwork).isEqualTo(mutableNetwork); mutableNetwork.addNode("B"); assertThat(immutableNetwork).isNotEqualTo(mutableNetwork); }
@Test public void edgesConnecting_directed() { MutableNetwork<String, String> mutableNetwork = NetworkBuilder.directed().allowsSelfLoops(true).build(); mutableNetwork.addEdge("A", "A", "AA"); mutableNetwork.addEdge("A", "B", "AB"); Network<String, String> network = ImmutableNetwork.copyOf(mutableNetwork); assertThat(network.edgesConnecting("A", "A")).containsExactly("AA"); assertThat(network.edgesConnecting("A", "B")).containsExactly("AB"); assertThat(network.edgesConnecting("B", "A")).isEmpty(); }
@Test public void edgesConnecting_undirected() { MutableNetwork<String, String> mutableNetwork = NetworkBuilder.undirected().allowsSelfLoops(true).build(); mutableNetwork.addEdge("A", "A", "AA"); mutableNetwork.addEdge("A", "B", "AB"); Network<String, String> network = ImmutableNetwork.copyOf(mutableNetwork); assertThat(network.edgesConnecting("A", "A")).containsExactly("AA"); assertThat(network.edgesConnecting("A", "B")).containsExactly("AB"); assertThat(network.edgesConnecting("B", "A")).containsExactly("AB"); } }
static <N, E> void validateNetwork(Network<N, E> network) { assertStronglyEquivalent(network, Graphs.copyOf(network)); assertStronglyEquivalent(network, ImmutableNetwork.copyOf(network));
/** * Returns a shallow copy of this graph instance. Neither edges nor vertices are cloned. * * @return a shallow copy of this graph. * * @throws RuntimeException in case the clone is not supported * * @see java.lang.Object#clone() */ @Override public Object clone() { try { ImmutableNetworkAdapter<V, E> newGraph = TypeUtil.uncheckedCast(super.clone()); newGraph.vertexSupplier = this.vertexSupplier; newGraph.edgeSupplier = this.edgeSupplier; newGraph.unmodifiableVertexSet = null; newGraph.unmodifiableEdgeSet = null; newGraph.network = ImmutableNetwork.copyOf(Graphs.copyOf(this.network)); return newGraph; } catch (CloneNotSupportedException e) { e.printStackTrace(); throw new RuntimeException(); } }
this.network = ImmutableNetwork.copyOf(mutableNetwork);
/** Initialize the EIGRP topology as a directed graph. */ public static Network<EigrpInterface, EigrpEdge> initEigrpTopology( Map<String, Configuration> configurations, Topology topology) { Set<EigrpEdge> edges = topology.getEdges().stream() .map(edge -> EigrpEdge.edgeIfAdjacent(edge, configurations)) .filter(Optional::isPresent) .map(Optional::get) .collect(ImmutableSet.toImmutableSet()); MutableNetwork<EigrpInterface, EigrpEdge> graph = NetworkBuilder.directed().allowsParallelEdges(false).allowsSelfLoops(false).build(); ImmutableSet.Builder<EigrpInterface> nodes = ImmutableSet.builder(); edges.forEach( edge -> { nodes.add(edge.getNode1()); nodes.add(edge.getNode2()); }); nodes.build().forEach(graph::addNode); edges.forEach(edge -> graph.addEdge(edge.getNode1(), edge.getNode2(), edge)); return ImmutableNetwork.copyOf(graph); } }
public VxlanTopology(Map<String, Configuration> configurations) { MutableNetwork<VxlanNode, VxlanEdge> graph = NetworkBuilder.directed().allowsParallelEdges(false).allowsSelfLoops(false).build(); Map<Integer, List<Vrf>> vrfsByVni = initVniVrfAssociations(configurations); Map<Vrf, String> vrfHostnames = initVrfHostnameMap(configurations); vrfsByVni.forEach((vni, vrfs) -> addVniEdges(graph, vrfHostnames, vni, vrfs)); _graph = ImmutableNetwork.copyOf(graph); }
/** Initialize the ISIS topology as a directed graph. */ public static Network<IsisNode, IsisEdge> initIsisTopology( Map<String, Configuration> configurations, Topology topology) { try (ActiveSpan span = GlobalTracer.get().buildSpan("IsisTopology.initIsisTopology").startActive()) { assert span != null; // avoid unused warning Set<IsisEdge> edges = topology.getEdges().stream() .map(edge -> IsisEdge.edgeIfCircuit(edge, configurations)) .filter(Optional::isPresent) .map(Optional::get) .collect(ImmutableSet.toImmutableSet()); MutableNetwork<IsisNode, IsisEdge> graph = NetworkBuilder.directed().allowsParallelEdges(false).allowsSelfLoops(false).build(); ImmutableSet.Builder<IsisNode> nodes = ImmutableSet.builder(); edges.forEach( edge -> { nodes.add(edge.getNode1()); nodes.add(edge.getNode2()); }); nodes.build().forEach(graph::addNode); edges.forEach(edge -> graph.addEdge(edge.getNode1(), edge.getNode2(), edge)); return ImmutableNetwork.copyOf(graph); } } }
public Layer1Topology(@Nonnull Iterable<Layer1Edge> edges) { MutableNetwork<Layer1Node, Layer1Edge> graph = NetworkBuilder.directed().allowsParallelEdges(false).allowsSelfLoops(false).build(); edges.forEach( edge -> { graph.addNode(edge.getNode1()); graph.addNode(edge.getNode2()); graph.addEdge(edge.getNode1(), edge.getNode2(), edge); }); _graph = ImmutableNetwork.copyOf(graph); }
public Layer3Topology(@Nonnull Iterable<Layer3Edge> edges) { MutableNetwork<Layer3Node, Layer3Edge> graph = NetworkBuilder.directed().allowsParallelEdges(false).allowsSelfLoops(false).build(); edges.forEach( edge -> { graph.addNode(edge.getNode1()); graph.addNode(edge.getNode2()); graph.addEdge(edge.getNode1(), edge.getNode2(), edge); }); _graph = ImmutableNetwork.copyOf(graph); }
private static <N> ImmutableNetwork<N, N> networkWithNode(N node) { MutableNetwork<N, N> network = NetworkBuilder.directed().build(); network.addNode(node); return ImmutableNetwork.copyOf(network); } }
/** Returns the subgraph containing only {@link DependencyEdge}s that would not break a cycle. */ // TODO(dpb): Return a network containing only Binding nodes. private ImmutableNetwork<Node, DependencyEdge> nonCycleBreakingDependencyGraph( BindingGraph bindingGraph) { MutableNetwork<Node, DependencyEdge> dependencyNetwork = NetworkBuilder.from(bindingGraph.network()) .expectedNodeCount(bindingGraph.network().nodes().size()) .expectedEdgeCount(bindingGraph.dependencyEdges().size()) .build(); bindingGraph.dependencyEdges().stream() .filter(edge -> !breaksCycle(edge, bindingGraph)) .forEach( edge -> { EndpointPair<Node> endpoints = bindingGraph.network().incidentNodes(edge); dependencyNetwork.addEdge(endpoints.source(), endpoints.target(), edge); }); return ImmutableNetwork.copyOf(dependencyNetwork); }
@Test public void immutableNetwork() { MutableNetwork<String, Integer> mutableNetwork = NetworkBuilder.directed().build(); mutableNetwork.addNode("A"); ImmutableNetwork<String, Integer> immutableNetwork = ImmutableNetwork.copyOf(mutableNetwork); assertThat(immutableNetwork.asGraph()).isInstanceOf(ImmutableGraph.class); assertThat(immutableNetwork).isNotInstanceOf(MutableNetwork.class); assertThat(immutableNetwork).isEqualTo(mutableNetwork); mutableNetwork.addNode("B"); assertThat(immutableNetwork).isNotEqualTo(mutableNetwork); }
@Test public void edgesConnecting_directed() { MutableNetwork<String, String> mutableNetwork = NetworkBuilder.directed().allowsSelfLoops(true).build(); mutableNetwork.addEdge("A", "A", "AA"); mutableNetwork.addEdge("A", "B", "AB"); Network<String, String> network = ImmutableNetwork.copyOf(mutableNetwork); assertThat(network.edgesConnecting("A", "A")).containsExactly("AA"); assertThat(network.edgesConnecting("A", "B")).containsExactly("AB"); assertThat(network.edgesConnecting("B", "A")).isEmpty(); }
@Test public void edgesConnecting_undirected() { MutableNetwork<String, String> mutableNetwork = NetworkBuilder.undirected().allowsSelfLoops(true).build(); mutableNetwork.addEdge("A", "A", "AA"); mutableNetwork.addEdge("A", "B", "AB"); Network<String, String> network = ImmutableNetwork.copyOf(mutableNetwork); assertThat(network.edgesConnecting("A", "A")).containsExactly("AA"); assertThat(network.edgesConnecting("A", "B")).containsExactly("AB"); assertThat(network.edgesConnecting("B", "A")).containsExactly("AB"); } }
static <N, E> void validateNetwork(Network<N, E> network) { assertStronglyEquivalent(network, Graphs.copyOf(network)); assertStronglyEquivalent(network, ImmutableNetwork.copyOf(network));