use of org.batfish.datamodel.BgpNeighbor in project batfish by batfish.
the class EncoderSlice method addEnvironmentVariables.
/*
* Initialize all environment symbolic records for BGP.
*/
private void addEnvironmentVariables() {
// If not the main slice, just use the main slice
if (!isMainSlice()) {
Map<LogicalEdge, SymbolicRoute> envs = _logicalGraph.getEnvironmentVars();
EncoderSlice main = _encoder.getMainSlice();
LogicalGraph lg = main.getLogicalGraph();
Map<LogicalEdge, SymbolicRoute> existing = lg.getEnvironmentVars();
envs.putAll(existing);
return;
}
// Otherwise create it anew
for (String router : getGraph().getRouters()) {
for (Protocol proto : getProtocols().get(router)) {
if (proto.isBgp()) {
List<ArrayList<LogicalEdge>> les = _logicalGraph.getLogicalEdges().get(router, proto);
assert (les != null);
for (ArrayList<LogicalEdge> eList : les) {
for (LogicalEdge e : eList) {
if (e.getEdgeType() == EdgeType.IMPORT) {
GraphEdge ge = e.getEdge();
BgpNeighbor n = getGraph().getEbgpNeighbors().get(ge);
if (n != null && ge.getEnd() == null) {
if (!isMainSlice()) {
LogicalGraph lg = _encoder.getMainSlice().getLogicalGraph();
SymbolicRoute r = lg.getEnvironmentVars().get(e);
_logicalGraph.getEnvironmentVars().put(e, r);
} else {
String address;
if (n.getAddress() == null) {
address = "null";
} else {
address = n.getAddress().toString();
}
String ifaceName = "ENV-" + address;
String name = String.format("%d_%s%s_%s_%s_%s", _encoder.getId(), _sliceName, router, proto.name(), "EXPORT", ifaceName);
SymbolicRoute vars = new SymbolicRoute(this, name, router, proto, _optimizations, null, ge.isAbstract());
getAllSymbolicRecords().add(vars);
_logicalGraph.getEnvironmentVars().put(e, vars);
}
}
}
}
}
}
}
}
}
use of org.batfish.datamodel.BgpNeighbor in project batfish by batfish.
the class Graph method initIbgpNeighbors.
// TODO: very inefficient
/*
* Initialize iBGP neighbors by looking for nieghbors
* with the same AS number.
*/
private void initIbgpNeighbors() {
Map<String, Ip> ips = new HashMap<>();
Table2<String, String, BgpNeighbor> neighbors = new Table2<>();
// Match iBGP sessions with pairs of routers and BgpNeighbor
for (Entry<String, Configuration> entry : _configurations.entrySet()) {
String router = entry.getKey();
Configuration conf = entry.getValue();
BgpProcess p = conf.getDefaultVrf().getBgpProcess();
if (p != null) {
for (BgpNeighbor n : p.getNeighbors().values()) {
if (n.getLocalAs().equals(n.getRemoteAs())) {
ips.put(router, n.getLocalIp());
}
}
}
}
for (Entry<String, Configuration> entry : _configurations.entrySet()) {
String router = entry.getKey();
Configuration conf = entry.getValue();
BgpProcess p = conf.getDefaultVrf().getBgpProcess();
if (p != null) {
for (Entry<Prefix, BgpNeighbor> entry2 : p.getNeighbors().entrySet()) {
Prefix pfx = entry2.getKey();
BgpNeighbor n = entry2.getValue();
if (n.getLocalAs().equals(n.getRemoteAs())) {
for (Entry<String, Ip> ipEntry : ips.entrySet()) {
String r = ipEntry.getKey();
Ip ip = ipEntry.getValue();
if (!router.equals(r) && pfx.containsIp(ip)) {
neighbors.put(router, r, n);
}
}
}
}
}
}
// Add abstract graph edges for iBGP sessions
Table2<String, String, GraphEdge> reverse = new Table2<>();
neighbors.forEach((r1, r2, n1) -> {
Interface iface1 = createIbgpInterface(n1, r2);
BgpNeighbor n2 = neighbors.get(r2, r1);
GraphEdge ge;
if (n2 != null) {
Interface iface2 = createIbgpInterface(n2, r1);
ge = new GraphEdge(iface1, iface2, r1, r2, true, false);
} else {
ge = new GraphEdge(iface1, null, r1, null, true, false);
}
_allEdges.add(ge);
_ibgpNeighbors.put(ge, n1);
reverse.put(r1, r2, ge);
List<GraphEdge> edges = _edgeMap.get(r1);
if (edges != null) {
edges.add(ge);
} else {
edges = new ArrayList<>();
edges.add(ge);
_edgeMap.put(r1, edges);
}
});
// Add other end to ibgp edges
reverse.forEach((r1, r2, ge1) -> {
GraphEdge ge2 = reverse.get(r2, r1);
_otherEnd.put(ge1, ge2);
});
// Configure Route Reflector information
Integer[] id = new Integer[1];
id[0] = 1;
neighbors.forEach((r1, ns) -> {
if (!_originatorId.containsKey(r1)) {
_originatorId.put(r1, id[0]);
id[0]++;
}
Set<String> clients = new HashSet<>();
ns.forEach((r2, n) -> {
if (n.getRouteReflectorClient()) {
clients.add(r2);
_routeReflectorParent.put(r2, r1);
}
});
_routeReflectorClients.put(r1, clients);
});
}
use of org.batfish.datamodel.BgpNeighbor in project batfish by batfish.
the class FlatJuniperGrammarTest method testBgpClusterId.
@Test
public void testBgpClusterId() throws IOException {
String testrigName = "rr";
String configName = "rr";
Ip neighbor1Ip = new Ip("2.2.2.2");
Ip neighbor2Ip = new Ip("4.4.4.4");
List<String> configurationNames = ImmutableList.of(configName);
Batfish batfish = BatfishTestUtils.getBatfishFromTestrigText(TestrigText.builder().setConfigurationText(TESTRIGS_PREFIX + testrigName, configurationNames).build(), _folder);
Map<String, Configuration> configurations = batfish.loadConfigurations();
Configuration rr = configurations.get(configName);
BgpProcess proc = rr.getDefaultVrf().getBgpProcess();
BgpNeighbor neighbor1 = proc.getNeighbors().get(new Prefix(neighbor1Ip, Prefix.MAX_PREFIX_LENGTH));
BgpNeighbor neighbor2 = proc.getNeighbors().get(new Prefix(neighbor2Ip, Prefix.MAX_PREFIX_LENGTH));
assertThat(neighbor1, hasClusterId(new Ip("3.3.3.3").asLong()));
assertThat(neighbor2, hasClusterId(new Ip("1.1.1.1").asLong()));
}
Aggregations