use of org.batfish.datamodel.AbstractRoute in project batfish by batfish.
the class VirtualRouter method activateStaticRoutes.
/**
* Re-activate static routes at the beginning of an iteration. Directly adds a static route to the
* main RIB if the route's next-hop-ip matches routes from the previous iterations.
*/
boolean activateStaticRoutes() {
boolean changed = false;
for (StaticRoute sr : _staticRib.getRoutes()) {
// See if we had (in the previous RIB) any routes matching this route's next hop IP
Set<AbstractRoute> matchingRoutes = _prevMainRib.longestPrefixMatch(sr.getNextHopIp());
Prefix staticRoutePrefix = sr.getNetwork();
for (AbstractRoute matchingRoute : matchingRoutes) {
Prefix matchingRoutePrefix = matchingRoute.getNetwork();
// contain this static route's prefix
if (!matchingRoutePrefix.containsPrefix(staticRoutePrefix)) {
changed |= _mainRib.mergeRoute(sr);
// break out of the inner loop but continue processing static routes
break;
}
}
}
return changed;
}
use of org.batfish.datamodel.AbstractRoute in project batfish by batfish.
the class VirtualRouter method initOspfExports.
void initOspfExports() {
OspfProcess proc = _vrf.getOspfProcess();
// Nothing to do
if (proc == null) {
return;
}
// get OSPF export policy name
String exportPolicyName = _vrf.getOspfProcess().getExportPolicy();
if (exportPolicyName == null) {
// nothing to export
return;
}
RoutingPolicy exportPolicy = _c.getRoutingPolicies().get(exportPolicyName);
if (exportPolicy == null) {
// nothing to export
return;
}
// RIB.
for (AbstractRoute potentialExport : _prevMainRib.getRoutes()) {
OspfExternalRoute outputRoute = computeOspfExportRoute(potentialExport, exportPolicy, proc);
if (outputRoute == null) {
// no need to export
continue;
}
if (outputRoute.getOspfMetricType() == OspfMetricType.E1) {
_ospfExternalType1Rib.mergeRoute((OspfExternalType1Route) outputRoute);
} else {
// assuming here that MetricType exists. Or E2 is the default
_ospfExternalType2Rib.mergeRoute((OspfExternalType2Route) outputRoute);
}
}
}
use of org.batfish.datamodel.AbstractRoute in project batfish by batfish.
the class VirtualRouter method activateGeneratedRoutes.
boolean activateGeneratedRoutes() {
boolean changed = false;
for (GeneratedRoute gr : _vrf.getGeneratedRoutes()) {
boolean active = true;
String generationPolicyName = gr.getGenerationPolicy();
GeneratedRoute.Builder grb = new GeneratedRoute.Builder();
grb.setNetwork(gr.getNetwork());
grb.setAdmin(gr.getAdministrativeCost());
grb.setMetric(gr.getMetric() != null ? gr.getMetric() : 0);
grb.setAttributePolicy(gr.getAttributePolicy());
grb.setGenerationPolicy(gr.getGenerationPolicy());
boolean discard = gr.getDiscard();
grb.setDiscard(discard);
if (discard) {
grb.setNextHopInterface(Interface.NULL_INTERFACE_NAME);
}
if (generationPolicyName != null) {
RoutingPolicy generationPolicy = _c.getRoutingPolicies().get(generationPolicyName);
if (generationPolicy != null) {
active = false;
for (AbstractRoute contributingRoute : _prevMainRib.getRoutes()) {
boolean accept = generationPolicy.process(contributingRoute, grb, null, _key, Direction.OUT);
if (accept) {
if (!discard) {
grb.setNextHopIp(contributingRoute.getNextHopIp());
}
active = true;
break;
}
}
}
}
if (active) {
GeneratedRoute newGr = grb.build();
if (_generatedRib.mergeRoute(newGr)) {
changed = true;
}
}
}
return changed;
}
use of org.batfish.datamodel.AbstractRoute in project batfish by batfish.
the class BdpEngine method debugAbstractRoutesIterations.
private String debugAbstractRoutesIterations(String msg, Map<Integer, SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>>> iterationAbsRoutes, int first, int last) {
StringBuilder sb = new StringBuilder();
sb.append(msg);
sb.append("\n");
SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> initialRoutes;
initialRoutes = iterationAbsRoutes.get(first);
sb.append("Initial routes (iteration ").append(first).append("):\n");
initialRoutes.forEach((hostname, routesByVrf) -> {
routesByVrf.forEach((vrfName, routes) -> {
for (AbstractRoute route : routes) {
sb.append(String.format("%s\n", route.fullString()));
}
});
});
for (int i = first + 1; i <= last; i++) {
SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> baseRoutesByHostname = iterationAbsRoutes.get(i - 1);
SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> deltaRoutesByHostname = iterationAbsRoutes.get(i);
SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> routesByHostname = new TreeMap<>();
sb.append("Changed routes (iteration ").append(i - 1).append(" ==> ").append(i).append("):\n");
Set<String> hosts = new LinkedHashSet<>();
hosts.addAll(baseRoutesByHostname.keySet());
hosts.addAll(deltaRoutesByHostname.keySet());
for (String hostname : hosts) {
SortedMap<String, SortedSet<AbstractRoute>> routesByVrf = new TreeMap<>();
routesByHostname.put(hostname, routesByVrf);
SortedMap<String, SortedSet<AbstractRoute>> baseRoutesByVrf = baseRoutesByHostname.get(hostname);
SortedMap<String, SortedSet<AbstractRoute>> deltaRoutesByVrf = deltaRoutesByHostname.get(hostname);
if (baseRoutesByVrf == null) {
for (Entry<String, SortedSet<AbstractRoute>> e : deltaRoutesByVrf.entrySet()) {
String vrfName = e.getKey();
SortedSet<AbstractRoute> deltaRoutes = e.getValue();
SortedSet<AbstractRoute> routes = new TreeSet<>();
routesByVrf.put(vrfName, routes);
for (AbstractRoute deltaRoute : deltaRoutes) {
sb.append(String.format("+ %s\n", deltaRoute.fullString()));
}
}
} else if (deltaRoutesByVrf == null) {
for (Entry<String, SortedSet<AbstractRoute>> e : baseRoutesByVrf.entrySet()) {
String vrfName = e.getKey();
SortedSet<AbstractRoute> baseRoutes = e.getValue();
SortedSet<AbstractRoute> routes = new TreeSet<>();
routesByVrf.put(vrfName, routes);
for (AbstractRoute baseRoute : baseRoutes) {
sb.append(String.format("- %s\n", baseRoute.fullString()));
}
}
} else {
Set<String> vrfNames = new LinkedHashSet<>();
vrfNames.addAll(baseRoutesByVrf.keySet());
vrfNames.addAll(deltaRoutesByVrf.keySet());
for (String vrfName : vrfNames) {
SortedSet<AbstractRoute> baseRoutes = baseRoutesByVrf.get(vrfName);
SortedSet<AbstractRoute> deltaRoutes = deltaRoutesByVrf.get(vrfName);
if (baseRoutes == null) {
for (AbstractRoute deltaRoute : deltaRoutes) {
sb.append(String.format("+ %s\n", deltaRoute.fullString()));
}
} else if (deltaRoutes == null) {
for (AbstractRoute baseRoute : baseRoutes) {
sb.append(String.format("- %s\n", baseRoute.fullString()));
}
} else {
SortedSet<AbstractRoute> prunedBaseRoutes = CommonUtil.difference(baseRoutes, deltaRoutes, TreeSet::new);
SortedSet<AbstractRoute> prunedDeltaRoutes = CommonUtil.difference(deltaRoutes, baseRoutes, TreeSet::new);
for (AbstractRoute baseRoute : prunedBaseRoutes) {
sb.append(String.format("- %s\n", baseRoute.fullString()));
}
for (AbstractRoute deltaRoute : prunedDeltaRoutes) {
sb.append(String.format("+ %s\n", deltaRoute.fullString()));
}
}
}
}
}
}
return sb.toString();
}
use of org.batfish.datamodel.AbstractRoute in project batfish by batfish.
the class BdpEngine method collectOscillatingPrefixes.
private SortedSet<Prefix> collectOscillatingPrefixes(Map<Integer, SortedSet<Route>> iterationRoutes, Map<Integer, SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>>> iterationAbsRoutes, int first, int last) {
SortedSet<Prefix> oscillatingPrefixes = new TreeSet<>();
if (_settings.getBdpDetail()) {
for (int i = first + 1; i <= last; i++) {
SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> baseRoutesByHostname = iterationAbsRoutes.get(i - 1);
SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> deltaRoutesByHostname = iterationAbsRoutes.get(i);
SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> routesByHostname = new TreeMap<>();
Set<String> hosts = new LinkedHashSet<>();
hosts.addAll(baseRoutesByHostname.keySet());
hosts.addAll(deltaRoutesByHostname.keySet());
for (String hostname : hosts) {
SortedMap<String, SortedSet<AbstractRoute>> routesByVrf = new TreeMap<>();
routesByHostname.put(hostname, routesByVrf);
SortedMap<String, SortedSet<AbstractRoute>> baseRoutesByVrf = baseRoutesByHostname.get(hostname);
SortedMap<String, SortedSet<AbstractRoute>> deltaRoutesByVrf = deltaRoutesByHostname.get(hostname);
if (baseRoutesByVrf == null) {
for (Entry<String, SortedSet<AbstractRoute>> e : deltaRoutesByVrf.entrySet()) {
String vrfName = e.getKey();
SortedSet<AbstractRoute> deltaRoutes = e.getValue();
SortedSet<AbstractRoute> routes = new TreeSet<>();
routesByVrf.put(vrfName, routes);
for (AbstractRoute deltaRoute : deltaRoutes) {
oscillatingPrefixes.add(deltaRoute.getNetwork());
}
}
} else if (deltaRoutesByVrf == null) {
for (Entry<String, SortedSet<AbstractRoute>> e : baseRoutesByVrf.entrySet()) {
String vrfName = e.getKey();
SortedSet<AbstractRoute> baseRoutes = e.getValue();
SortedSet<AbstractRoute> routes = new TreeSet<>();
routesByVrf.put(vrfName, routes);
for (AbstractRoute baseRoute : baseRoutes) {
oscillatingPrefixes.add(baseRoute.getNetwork());
}
}
} else {
Set<String> vrfNames = new LinkedHashSet<>();
vrfNames.addAll(baseRoutesByVrf.keySet());
vrfNames.addAll(deltaRoutesByVrf.keySet());
for (String vrfName : vrfNames) {
SortedSet<AbstractRoute> baseRoutes = baseRoutesByVrf.get(vrfName);
SortedSet<AbstractRoute> deltaRoutes = deltaRoutesByVrf.get(vrfName);
if (baseRoutes == null) {
for (AbstractRoute deltaRoute : deltaRoutes) {
oscillatingPrefixes.add(deltaRoute.getNetwork());
}
} else if (deltaRoutes == null) {
for (AbstractRoute baseRoute : baseRoutes) {
oscillatingPrefixes.add(baseRoute.getNetwork());
}
} else {
SortedSet<AbstractRoute> prunedBaseRoutes = CommonUtil.difference(baseRoutes, deltaRoutes, TreeSet::new);
SortedSet<AbstractRoute> prunedDeltaRoutes = CommonUtil.difference(deltaRoutes, baseRoutes, TreeSet::new);
for (AbstractRoute baseRoute : prunedBaseRoutes) {
oscillatingPrefixes.add(baseRoute.getNetwork());
}
for (AbstractRoute deltaRoute : prunedDeltaRoutes) {
oscillatingPrefixes.add(deltaRoute.getNetwork());
}
}
}
}
}
}
} else {
for (int i = first + 1; i <= last; i++) {
SortedSet<Route> baseRoutes = iterationRoutes.get(i - 1);
SortedSet<Route> deltaRoutes = iterationRoutes.get(i);
SortedSet<Route> added = CommonUtil.difference(deltaRoutes, baseRoutes, TreeSet<Route>::new);
SortedSet<Route> removed = CommonUtil.difference(baseRoutes, deltaRoutes, TreeSet<Route>::new);
SortedSet<Route> changed = CommonUtil.union(added, removed, TreeSet<Route>::new);
for (Route route : changed) {
oscillatingPrefixes.add(route.getNetwork());
}
}
}
return oscillatingPrefixes;
}
Aggregations