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);
}
}
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);
}
}
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;
}
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;
}
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))));
}
Aggregations