use of org.batfish.datamodel.Interface in project batfish by batfish.
the class Graph method initEbgpNeighbors.
/*
* Initialize external eBGP neighbors by looking for BGP neighbors
* where their is no neighbor in the configurations, and the IPs align.
*/
private void initEbgpNeighbors() {
Map<String, List<Ip>> ips = new HashMap<>();
Map<String, List<BgpNeighbor>> neighbors = new HashMap<>();
for (Entry<String, Configuration> entry : _configurations.entrySet()) {
String router = entry.getKey();
Configuration conf = entry.getValue();
List<Ip> ipList = new ArrayList<>();
List<BgpNeighbor> ns = new ArrayList<>();
ips.put(router, ipList);
neighbors.put(router, ns);
if (conf.getDefaultVrf().getBgpProcess() != null) {
BgpProcess bgp = conf.getDefaultVrf().getBgpProcess();
for (BgpNeighbor neighbor : bgp.getNeighbors().values()) {
ipList.add(neighbor.getAddress());
ns.add(neighbor);
}
}
}
for (Entry<String, Configuration> entry : _configurations.entrySet()) {
String router = entry.getKey();
Configuration conf = entry.getValue();
List<Ip> ipList = ips.get(router);
List<BgpNeighbor> ns = neighbors.get(router);
if (conf.getDefaultVrf().getBgpProcess() != null) {
List<GraphEdge> edges = _edgeMap.get(router);
for (GraphEdge ge : edges) {
for (int i = 0; i < ipList.size(); i++) {
Ip ip = ipList.get(i);
BgpNeighbor n = ns.get(i);
Interface iface = ge.getStart();
if (ip != null && iface.getAddress().getPrefix().containsIp(ip)) {
_ebgpNeighbors.put(ge, n);
}
}
}
}
}
}
use of org.batfish.datamodel.Interface in project batfish by batfish.
the class Graph method isEdgeUsed.
/*
* Check if a topology edge is used in a particular protocol.
*/
public boolean isEdgeUsed(Configuration conf, Protocol proto, GraphEdge ge) {
Interface iface = ge.getStart();
// Use a null routed edge, but only for the static protocol
if (ge.isNullEdge()) {
return proto.isStatic();
}
// Don't use if interface is not active
if (!isInterfaceActive(proto, iface)) {
return false;
}
// Exclude abstract iBGP edges from all protocols except BGP
if (iface.getName().startsWith("iBGP-")) {
return proto.isBgp();
}
// Never use Loopbacks for any protocol except connected
if (ge.getStart().isLoopback(conf.getConfigurationFormat())) {
return proto.isConnected();
}
// Don't use ospf over edges to hosts / external
if ((ge.getPeer() == null || isHost(ge.getPeer())) && proto.isOspf()) {
return false;
}
// Only use specified edges from static routes
if (proto.isStatic()) {
List<StaticRoute> srs = getStaticRoutes().get(conf.getName(), iface.getName());
return iface.getActive() && srs != null && srs.size() > 0;
}
// Only use an edge in BGP if there is an explicit peering
if (proto.isBgp()) {
BgpNeighbor n1 = _ebgpNeighbors.get(ge);
BgpNeighbor n2 = _ibgpNeighbors.get(ge);
return n1 != null || n2 != null;
}
return true;
}
use of org.batfish.datamodel.Interface 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.Interface in project batfish by batfish.
the class Graph method addNullRouteEdges.
/*
* Add graph edges to represent the null interface when used by a static route
*/
private void addNullRouteEdges() {
for (Entry<String, List<StaticRoute>> entry : _nullStaticRoutes.entrySet()) {
String router = entry.getKey();
List<StaticRoute> srs = entry.getValue();
for (StaticRoute sr : srs) {
String name = sr.getNextHopInterface();
// Create null route interface
Interface iface = new Interface(name);
iface.setActive(true);
iface.setAddress(new InterfaceAddress(sr.getNetwork().getStartIp(), sr.getNextHopIp().numSubnetBits()));
iface.setBandwidth(0.);
// Add static route to all static routes list
Map<String, List<StaticRoute>> map = _staticRoutes.get(router);
List<StaticRoute> routes = map.computeIfAbsent(name, k -> new ArrayList<>());
routes.add(sr);
// Create and add graph edge for null route
GraphEdge ge = new GraphEdge(iface, null, router, null, false, true);
_allRealEdges.add(ge);
_allEdges.add(ge);
List<GraphEdge> edges = _edgeMap.computeIfAbsent(router, k -> new ArrayList<>());
edges.add(ge);
}
}
}
use of org.batfish.datamodel.Interface in project batfish by batfish.
the class Graph method initStaticRoutes.
/*
* Collect all static routes after inferring which interface they indicate
* should be used for the next-hop.
*/
private void initStaticRoutes() {
for (Entry<String, Configuration> entry : _configurations.entrySet()) {
String router = entry.getKey();
Configuration conf = entry.getValue();
Map<String, List<StaticRoute>> map = new HashMap<>();
_staticRoutes.put(router, map);
for (StaticRoute sr : conf.getDefaultVrf().getStaticRoutes()) {
boolean someIface = false;
for (GraphEdge ge : _edgeMap.get(router)) {
Interface here = ge.getStart();
Interface there = ge.getEnd();
// Check if next-hop interface is specified
String hereName = here.getName();
someIface = true;
if (hereName.equals(sr.getNextHopInterface())) {
List<StaticRoute> srs = map.computeIfAbsent(hereName, k -> new ArrayList<>());
srs.add(sr);
map.put(hereName, srs);
}
// Check if next-hop ip corresponds to direct interface
Ip nhIp = sr.getNextHopIp();
boolean isNextHop = there != null && there.getAddress() != null && there.getAddress().getIp().equals(nhIp);
if (isNextHop) {
someIface = true;
List<StaticRoute> srs = map.computeIfAbsent(hereName, k -> new ArrayList<>());
srs.add(sr);
map.put(here.getName(), srs);
}
}
if (Graph.isNullRouted(sr)) {
List<StaticRoute> nulls = _nullStaticRoutes.computeIfAbsent(router, k -> new ArrayList<>());
nulls.add(sr);
}
if (!someIface && !Graph.isNullRouted(sr)) {
_hasStaticRouteWithDynamicNextHop = true;
}
}
}
}
Aggregations