Search in sources :

Example 11 with Edge

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

the class Batfish method writeJsonTopology.

private void writeJsonTopology() {
    try {
        Map<String, Configuration> configs = loadConfigurations();
        SortedSet<Edge> textEdges = CommonUtil.synthesizeTopology(configs).getEdges();
        JSONArray jEdges = new JSONArray();
        for (Edge textEdge : textEdges) {
            Configuration node1 = configs.get(textEdge.getNode1());
            Configuration node2 = configs.get(textEdge.getNode2());
            Interface interface1 = node1.getInterfaces().get(textEdge.getInt1());
            Interface interface2 = node2.getInterfaces().get(textEdge.getInt2());
            JSONObject jEdge = new JSONObject();
            jEdge.put("interface1", interface1.toJSONObject());
            jEdge.put("interface2", interface2.toJSONObject());
            jEdges.put(jEdge);
        }
        JSONObject master = new JSONObject();
        JSONObject topology = new JSONObject();
        topology.put("edges", jEdges);
        master.put("topology", topology);
        String text = master.toString(3);
        _logger.output(text);
    } catch (JSONException e) {
        throw new BatfishException("Failed to synthesize JSON topology", e);
    }
}
Also used : CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) HostConfiguration(org.batfish.representation.host.HostConfiguration) Configuration(org.batfish.datamodel.Configuration) ImmutableConfiguration(org.apache.commons.configuration2.ImmutableConfiguration) AwsConfiguration(org.batfish.representation.aws.AwsConfiguration) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) VendorConfiguration(org.batfish.vendor.VendorConfiguration) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) JSONException(org.codehaus.jettison.json.JSONException) Edge(org.batfish.datamodel.Edge) Interface(org.batfish.datamodel.Interface)

Example 12 with Edge

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

the class Graph method initGraph.

/*
   * Initialize the topology by inferring interface pairs and
   * create the opposite edge mapping.
   */
private void initGraph(Topology topology) {
    Map<NodeInterfacePair, Interface> ifaceMap = new HashMap<>();
    Map<String, Set<NodeInterfacePair>> routerIfaceMap = new HashMap<>();
    for (Entry<String, Configuration> entry : _configurations.entrySet()) {
        String router = entry.getKey();
        Configuration conf = entry.getValue();
        Set<NodeInterfacePair> ifacePairs = new HashSet<>();
        for (Entry<String, Interface> entry2 : conf.getInterfaces().entrySet()) {
            String name = entry2.getKey();
            Interface iface = entry2.getValue();
            NodeInterfacePair nip = new NodeInterfacePair(router, name);
            ifacePairs.add(nip);
            ifaceMap.put(nip, iface);
        }
        routerIfaceMap.put(router, ifacePairs);
    }
    Map<NodeInterfacePair, SortedSet<Edge>> ifaceEdges = topology.getInterfaceEdges();
    _neighbors = new HashMap<>();
    for (Entry<String, Set<NodeInterfacePair>> entry : routerIfaceMap.entrySet()) {
        String router = entry.getKey();
        Set<NodeInterfacePair> nips = entry.getValue();
        Set<GraphEdge> graphEdges = new HashSet<>();
        Set<String> neighs = new HashSet<>();
        for (NodeInterfacePair nip : nips) {
            SortedSet<Edge> es = ifaceEdges.get(nip);
            Interface i1 = ifaceMap.get(nip);
            boolean hasNoOtherEnd = (es == null && i1.getAddress() != null);
            if (hasNoOtherEnd) {
                GraphEdge ge = new GraphEdge(i1, null, router, null, false, false);
                graphEdges.add(ge);
            }
            if (es != null) {
                boolean hasMultipleEnds = (es.size() > 2);
                if (hasMultipleEnds) {
                    GraphEdge ge = new GraphEdge(i1, null, router, null, false, false);
                    graphEdges.add(ge);
                } else {
                    for (Edge e : es) {
                        // Weird inference behavior from Batfish here with a self-loop
                        if (router.equals(e.getNode1()) && router.equals(e.getNode2())) {
                            GraphEdge ge = new GraphEdge(i1, null, router, null, false, false);
                            graphEdges.add(ge);
                        }
                        // Only look at the first pair
                        if (!router.equals(e.getNode2())) {
                            Interface i2 = ifaceMap.get(e.getInterface2());
                            String neighbor = e.getNode2();
                            GraphEdge ge1 = new GraphEdge(i1, i2, router, neighbor, false, false);
                            GraphEdge ge2 = new GraphEdge(i2, i1, neighbor, router, false, false);
                            _otherEnd.put(ge1, ge2);
                            graphEdges.add(ge1);
                            neighs.add(neighbor);
                        }
                    }
                }
            }
        }
        _allRealEdges.addAll(graphEdges);
        _allEdges.addAll(graphEdges);
        _edgeMap.put(router, new ArrayList<>(graphEdges));
        _neighbors.put(router, neighs);
    }
}
Also used : SortedSet(java.util.SortedSet) MatchCommunitySet(org.batfish.datamodel.routing_policy.expr.MatchCommunitySet) InlineCommunitySet(org.batfish.datamodel.routing_policy.expr.InlineCommunitySet) Set(java.util.Set) HashSet(java.util.HashSet) ExplicitPrefixSet(org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) NamedCommunitySet(org.batfish.datamodel.routing_policy.expr.NamedCommunitySet) Configuration(org.batfish.datamodel.Configuration) HashMap(java.util.HashMap) NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair) SortedSet(java.util.SortedSet) Edge(org.batfish.datamodel.Edge) Interface(org.batfish.datamodel.Interface) HashSet(java.util.HashSet)

Example 13 with Edge

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

the class CounterExample method buildFailedLinks.

SortedSet<Edge> buildFailedLinks(Encoder enc) {
    Set<GraphEdge> failed = new HashSet<>();
    Graph g = enc.getMainSlice().getGraph();
    for (List<GraphEdge> edges : g.getEdgeMap().values()) {
        for (GraphEdge ge : edges) {
            ArithExpr e = enc.getSymbolicFailures().getFailedVariable(ge);
            assert e != null;
            if (intVal(e) != 0) {
                // Don't add both directions?
                GraphEdge other = g.getOtherEnd().get(ge);
                if (other == null || !failed.contains(other)) {
                    failed.add(ge);
                }
            }
        }
    }
    // Convert to Batfish Edge type
    SortedSet<Edge> failedEdges = new TreeSet<>();
    for (GraphEdge ge : failed) {
        failedEdges.add(fromGraphEdge(ge));
    }
    return failedEdges;
}
Also used : ArithExpr(com.microsoft.z3.ArithExpr) Graph(org.batfish.symbolic.Graph) TreeSet(java.util.TreeSet) GraphEdge(org.batfish.symbolic.GraphEdge) Edge(org.batfish.datamodel.Edge) GraphEdge(org.batfish.symbolic.GraphEdge) HashSet(java.util.HashSet)

Example 14 with Edge

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

the class CounterExample method buildFlowHistoryDiff.

/*
   * Create a trace-based counterexample demonstrating
   * the difference between two networks on a single packet.
   */
FlowHistory buildFlowHistoryDiff(String testRigName, Collection<String> sourceRouters, Encoder enc, Encoder enc2, Map<String, BoolExpr> reach, Map<String, BoolExpr> reach2) {
    FlowHistory fh = new FlowHistory();
    assert (reach2 != null);
    for (String source : sourceRouters) {
        BoolExpr sourceVar1 = reach.get(source);
        BoolExpr sourceVar2 = reach2.get(source);
        String val1 = evaluate(sourceVar1);
        String val2 = evaluate(sourceVar2);
        if (!Objects.equals(val1, val2)) {
            Tuple<Flow, FlowTrace> diff = buildFlowTrace(enc, source);
            Tuple<Flow, FlowTrace> base = buildFlowTrace(enc2, source);
            SortedSet<Edge> failedLinksDiff = buildFailedLinks(enc);
            SortedSet<Edge> failedLinksBase = buildFailedLinks(enc2);
            SortedSet<BgpAdvertisement> envRoutesDiff = buildEnvRoutingTable(enc);
            SortedSet<BgpAdvertisement> envRoutesBase = buildEnvRoutingTable(enc2);
            Environment baseEnv = new Environment("BASE", testRigName, failedLinksBase, null, null, null, null, envRoutesBase);
            Environment failedEnv = new Environment("DELTA", testRigName, failedLinksDiff, null, null, null, null, envRoutesDiff);
            fh.addFlowTrace(base.getFirst(), "BASE", baseEnv, base.getSecond());
            fh.addFlowTrace(diff.getFirst(), "DELTA", failedEnv, diff.getSecond());
        }
    }
    return fh;
}
Also used : BoolExpr(com.microsoft.z3.BoolExpr) BgpAdvertisement(org.batfish.datamodel.BgpAdvertisement) FlowHistory(org.batfish.datamodel.FlowHistory) FlowTrace(org.batfish.datamodel.FlowTrace) Environment(org.batfish.datamodel.pojo.Environment) Edge(org.batfish.datamodel.Edge) GraphEdge(org.batfish.symbolic.GraphEdge) Flow(org.batfish.datamodel.Flow)

Example 15 with Edge

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

the class DefaultTransitionGeneratorTest method testVisitPreInInterface.

@Test
public void testVisitPreInInterface() {
    SynthesizerInput input = MockSynthesizerInput.builder().setEnabledEdges(ImmutableSet.of(new Edge(NODE1, INTERFACE1, NODE2, INTERFACE1), new Edge(NODE1, INTERFACE1, NODE2, INTERFACE2), new Edge(NODE1, INTERFACE1, NODE2, INTERFACE3), new Edge(NODE1, INTERFACE2, NODE2, INTERFACE1), new Edge(NODE1, INTERFACE2, NODE2, INTERFACE2), new Edge(NODE1, INTERFACE2, NODE2, INTERFACE3), new Edge(NODE1, INTERFACE3, NODE2, INTERFACE1), new Edge(NODE1, INTERFACE3, NODE2, INTERFACE2), new Edge(NODE1, INTERFACE3, NODE2, INTERFACE3), new Edge(NODE2, INTERFACE1, NODE1, INTERFACE1), new Edge(NODE2, INTERFACE1, NODE1, INTERFACE2), new Edge(NODE2, INTERFACE1, NODE1, INTERFACE3), new Edge(NODE2, INTERFACE2, NODE1, INTERFACE1), new Edge(NODE2, INTERFACE2, NODE1, INTERFACE2), new Edge(NODE2, INTERFACE2, NODE1, INTERFACE3), new Edge(NODE2, INTERFACE3, NODE1, INTERFACE1), new Edge(NODE2, INTERFACE3, NODE1, INTERFACE2), new Edge(NODE2, INTERFACE3, NODE1, INTERFACE3))).build();
    Set<RuleStatement> rules = ImmutableSet.copyOf(DefaultTransitionGenerator.generateTransitions(input, ImmutableSet.of(PreInInterface.State.INSTANCE)));
    // PostOutNeighbor
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE1, INTERFACE1, NODE2, INTERFACE1)), new PreInInterface(NODE2, INTERFACE1))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE1, INTERFACE1, NODE2, INTERFACE2)), new PreInInterface(NODE2, INTERFACE2))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE1, INTERFACE1, NODE2, INTERFACE3)), new PreInInterface(NODE2, INTERFACE3))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE1, INTERFACE2, NODE2, INTERFACE1)), new PreInInterface(NODE2, INTERFACE1))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE1, INTERFACE2, NODE2, INTERFACE2)), new PreInInterface(NODE2, INTERFACE2))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE1, INTERFACE2, NODE2, INTERFACE3)), new PreInInterface(NODE2, INTERFACE3))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE1, INTERFACE3, NODE2, INTERFACE1)), new PreInInterface(NODE2, INTERFACE1))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE1, INTERFACE3, NODE2, INTERFACE2)), new PreInInterface(NODE2, INTERFACE2))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE1, INTERFACE3, NODE2, INTERFACE3)), new PreInInterface(NODE2, INTERFACE3))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE2, INTERFACE1, NODE1, INTERFACE1)), new PreInInterface(NODE1, INTERFACE1))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE2, INTERFACE1, NODE1, INTERFACE2)), new PreInInterface(NODE1, INTERFACE2))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE2, INTERFACE1, NODE1, INTERFACE3)), new PreInInterface(NODE1, INTERFACE3))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE2, INTERFACE2, NODE1, INTERFACE1)), new PreInInterface(NODE1, INTERFACE1))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE2, INTERFACE2, NODE1, INTERFACE2)), new PreInInterface(NODE1, INTERFACE2))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE2, INTERFACE2, NODE1, INTERFACE3)), new PreInInterface(NODE1, INTERFACE3))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE2, INTERFACE3, NODE1, INTERFACE1)), new PreInInterface(NODE1, INTERFACE1))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE2, INTERFACE3, NODE1, INTERFACE2)), new PreInInterface(NODE1, INTERFACE2))));
    assertThat(rules, hasItem(new BasicRuleStatement(TrueExpr.INSTANCE, ImmutableSet.of(new PostOutEdge(NODE2, INTERFACE3, NODE1, INTERFACE3)), new PreInInterface(NODE1, INTERFACE3))));
}
Also used : MockSynthesizerInput(org.batfish.z3.MockSynthesizerInput) SynthesizerInput(org.batfish.z3.SynthesizerInput) TransformationRuleStatement(org.batfish.z3.expr.TransformationRuleStatement) RuleStatement(org.batfish.z3.expr.RuleStatement) BasicRuleStatement(org.batfish.z3.expr.BasicRuleStatement) PostOutEdge(org.batfish.z3.state.PostOutEdge) PreInInterface(org.batfish.z3.state.PreInInterface) Edge(org.batfish.datamodel.Edge) PostOutEdge(org.batfish.z3.state.PostOutEdge) PreOutEdge(org.batfish.z3.state.PreOutEdge) BasicRuleStatement(org.batfish.z3.expr.BasicRuleStatement) Test(org.junit.Test)

Aggregations

Edge (org.batfish.datamodel.Edge)46 Configuration (org.batfish.datamodel.Configuration)23 Interface (org.batfish.datamodel.Interface)19 Topology (org.batfish.datamodel.Topology)17 Test (org.junit.Test)17 Ip (org.batfish.datamodel.Ip)12 NodeInterfacePair (org.batfish.datamodel.collections.NodeInterfacePair)12 Vrf (org.batfish.datamodel.Vrf)10 BatfishException (org.batfish.common.BatfishException)9 FlowTrace (org.batfish.datamodel.FlowTrace)8 ArrayList (java.util.ArrayList)7 Set (java.util.Set)7 TreeSet (java.util.TreeSet)7 IpAccessList (org.batfish.datamodel.IpAccessList)7 HostConfiguration (org.batfish.representation.host.HostConfiguration)7 VendorConfiguration (org.batfish.vendor.VendorConfiguration)7 SynthesizerInputMatchers.hasArpTrueEdge (org.batfish.z3.matchers.SynthesizerInputMatchers.hasArpTrueEdge)7 HashMap (java.util.HashMap)6 FlowTraceHop (org.batfish.datamodel.FlowTraceHop)6 AclPermit (org.batfish.z3.state.AclPermit)6