use of org.batfish.datamodel.Configuration 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.Configuration in project batfish by batfish.
the class Graph method findAllCommunities.
public Set<CommunityVar> findAllCommunities(String router) {
Set<CommunityVar> comms = new HashSet<>();
Configuration conf = getConfigurations().get(router);
for (RoutingPolicy pol : conf.getRoutingPolicies().values()) {
AstVisitor v = new AstVisitor();
v.visit(conf, pol.getStatements(), stmt -> {
if (stmt instanceof SetCommunity) {
SetCommunity sc = (SetCommunity) stmt;
comms.addAll(findAllCommunities(conf, sc.getExpr()));
}
if (stmt instanceof AddCommunity) {
AddCommunity ac = (AddCommunity) stmt;
comms.addAll(findAllCommunities(conf, ac.getExpr()));
}
if (stmt instanceof DeleteCommunity) {
DeleteCommunity dc = (DeleteCommunity) stmt;
comms.addAll(findAllCommunities(conf, dc.getExpr()));
}
if (stmt instanceof RetainCommunity) {
RetainCommunity rc = (RetainCommunity) stmt;
comms.addAll(findAllCommunities(conf, rc.getExpr()));
}
}, expr -> {
if (expr instanceof MatchCommunitySet) {
MatchCommunitySet m = (MatchCommunitySet) expr;
CommunitySetExpr ce = m.getExpr();
comms.addAll(findAllCommunities(conf, ce));
}
});
}
return comms;
}
use of org.batfish.datamodel.Configuration in project batfish by batfish.
the class Graph method findRouterId.
/*
* Find the router Id for the neighbor corresponding to a logical edge.
*/
public long findRouterId(GraphEdge ge, Protocol proto) {
GraphEdge eOther = _otherEnd.get(ge);
if (proto.isOspf() || proto.isConnected() || proto.isStatic()) {
return 0L;
}
if (eOther != null) {
String peer = eOther.getRouter();
Configuration peerConf = getConfigurations().get(peer);
return routerId(peerConf, proto);
}
BgpNeighbor n = findBgpNeighbor(ge);
if (n != null && n.getAddress() != null) {
return n.getAddress().asLong();
}
throw new BatfishException("Unable to find router id for " + ge + "," + proto.name());
}
use of org.batfish.datamodel.Configuration in project batfish by batfish.
the class Graph method isHost.
/*
* Determine if an edge is potentially attached to a host
*/
public boolean isHost(String router) {
Configuration peerConf = _configurations.get(router);
String vendor = peerConf.getConfigurationFormat().getVendorString();
return "host".equals(vendor);
}
use of org.batfish.datamodel.Configuration 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);
}
}
Aggregations