use of org.batfish.datamodel.Route in project batfish by batfish.
the class VirtualRouterTest method testOSPFPassiveInterfaceRejection.
/**
* Ensure no route propagation when the interfaces are disabled or passive
*/
@Test
public void testOSPFPassiveInterfaceRejection() {
// Setup
String testRouterName = "R1";
String exportingRouterName = "R2";
String exportingRouterInterfaceName = "Ethernet1";
Map<String, Node> nodes = makeIosRouters(testRouterName, exportingRouterName);
Map<String, VirtualRouter> routers = nodes.entrySet().stream().collect(ImmutableMap.toImmutableMap(Entry::getKey, e -> e.getValue()._virtualRouters.get(Configuration.DEFAULT_VRF_NAME)));
VirtualRouter testRouter = routers.get(testRouterName);
VirtualRouter exportingRouter = routers.get(exportingRouterName);
testRouter.initRibs();
exportingRouter.initRibs();
addInterfaces(testRouter._c, exampleInterfaceAddresses);
addInterfaces(exportingRouter._c, ImmutableMap.of(exportingRouterInterfaceName, new InterfaceAddress("10.4.0.0/16")));
int adminCost = RoutingProtocol.OSPF.getDefaultAdministrativeCost(testRouter._c.getConfigurationFormat());
Prefix prefix = Prefix.parse("7.7.7.0/24");
OspfIntraAreaRoute route = new OspfIntraAreaRoute(prefix, new Ip("7.7.1.1"), adminCost, 20, 1);
exportingRouter._ospfIntraAreaRib.mergeRoute(route);
// Set interaces on router 1 to be OSPF passive
testRouter._c.getInterfaces().forEach((name, iface) -> iface.setActive(false));
// Test 1
testRouter.propagateOspfInternalRoutesFromNeighbor(testRouter._vrf.getOspfProcess(), nodes.get("R2"), testRouter._c.getInterfaces().firstEntry().getValue(), exportingRouter._c.getInterfaces().get(exportingRouterInterfaceName), adminCost);
assertThat(testRouter._ospfInterAreaStagingRib.getRoutes(), is(emptyIterableOf(OspfInterAreaRoute.class)));
assertThat(testRouter._ospfIntraAreaStagingRib.getRoutes(), is(emptyIterableOf(OspfIntraAreaRoute.class)));
// Flip interfaces on router 2 to be passive now
testRouter._c.getInterfaces().forEach((name, iface) -> iface.setActive(true));
exportingRouter._c.getInterfaces().forEach((name, iface) -> iface.setActive(false));
// Test 2
testRouter.propagateOspfInternalRoutesFromNeighbor(testRouter._vrf.getOspfProcess(), nodes.get("R2"), testRouter._c.getInterfaces().firstEntry().getValue(), exportingRouter._c.getInterfaces().get(exportingRouterInterfaceName), adminCost);
assertThat(testRouter._ospfInterAreaStagingRib.getRoutes(), is(emptyIterableOf(OspfInterAreaRoute.class)));
assertThat(testRouter._ospfIntraAreaStagingRib.getRoutes(), is(emptyIterableOf(OspfIntraAreaRoute.class)));
}
use of org.batfish.datamodel.Route 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;
}
use of org.batfish.datamodel.Route in project batfish by batfish.
the class EosRoutingTableExtractor method exitRoute.
@Override
public void exitRoute(RouteContext ctx) {
if (_currentVrfName == null) {
_currentVrfName = Configuration.DEFAULT_VRF_NAME;
initVrf(_currentVrfName);
}
RoutingProtocol protocol = toProtocol(ctx.protocol());
Prefix prefix = Prefix.parse(ctx.IP_PREFIX().getText());
int admin;
int cost;
Ip nextHopIp;
for (int i = 0; i < ctx.nexthopifaces.size(); i++) {
String nextHopInterface = ctx.nexthopifaces.get(i).getText();
// TODO: support EOS route tags
int tag = Route.UNSET_ROUTE_TAG;
// TODO: support EOS next hop
if (ctx.IS_DIRECTLY_CONNECTED() != null) {
admin = 0;
cost = 0;
nextHopIp = Route.UNSET_ROUTE_NEXT_HOP_IP;
} else {
admin = toInteger(ctx.admin);
cost = toInteger(ctx.cost);
nextHopIp = new Ip(ctx.nexthops.get(i).getText());
}
RouteBuilder rb = new RouteBuilder();
rb.setNode(_hostname);
rb.setNetwork(prefix);
if (protocol == RoutingProtocol.CONNECTED || (protocol == RoutingProtocol.STATIC && nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) || Interface.NULL_INTERFACE_NAME.equals(nextHopInterface)) {
rb.setNextHop(Configuration.NODE_NONE_NAME);
}
if (!nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) {
rb.setNextHopIp(nextHopIp);
String nextHop = _ipOwners.get(nextHopIp);
if (nextHop != null) {
rb.setNextHop(nextHop);
}
}
if (nextHopInterface != null) {
rb.setNextHopInterface(nextHopInterface);
}
rb.setAdministrativeCost(admin);
rb.setCost(cost);
rb.setProtocol(protocol);
rb.setTag(tag);
rb.setVrf(_currentVrfName);
Route route = rb.build();
_currentVrfRoutes.add(route);
}
}
use of org.batfish.datamodel.Route in project batfish by batfish.
the class IosRoutingTableExtractor method exitRoute.
@Override
public void exitRoute(RouteContext ctx) {
if (_currentVrfName == null) {
_currentVrfName = Configuration.DEFAULT_VRF_NAME;
initVrf(_currentVrfName);
}
RoutingProtocol protocol = toProtocol(ctx.protocol());
if (protocol == RoutingProtocol.LOCAL) {
return;
}
Prefix prefix = Prefix.parse(ctx.IP_PREFIX().getText());
int admin;
int cost;
List<String> nextHopInterfaces = ctx.nexthopifaces.stream().map(nextHopIfaceCtx -> nextHopIfaceCtx.getText()).collect(Collectors.toList());
List<Ip> nextHopIps = ctx.nexthops.stream().map(nextHopIpCtx -> new Ip(nextHopIpCtx.getText())).collect(Collectors.toList());
int numIterations = Math.max(nextHopIps.size(), nextHopInterfaces.size());
for (int i = 0; i < numIterations; i++) {
String nextHopInterface;
Ip nextHopIp;
if (nextHopInterfaces.isEmpty()) {
nextHopInterface = Route.UNSET_NEXT_HOP_INTERFACE;
} else {
nextHopInterface = nextHopInterfaces.get(i);
}
if (ctx.IS_DIRECTLY_CONNECTED() != null) {
if (protocol == RoutingProtocol.STATIC) {
// static
if (ctx.admin != null) {
admin = toInteger(ctx.admin);
} else {
admin = 1;
}
if (ctx.cost != null) {
cost = toInteger(ctx.cost);
} else {
cost = 0;
}
} else {
// connected
admin = 0;
cost = 0;
}
nextHopIp = Route.UNSET_ROUTE_NEXT_HOP_IP;
} else {
admin = toInteger(ctx.admin);
cost = toInteger(ctx.cost);
nextHopIp = nextHopIps.get(i);
}
// TODO: support IOS route tags
int tag = Route.UNSET_ROUTE_TAG;
// TODO: support IOS next hop
if (protocol == RoutingProtocol.BGP && admin > RoutingProtocol.BGP.getDefaultAdministrativeCost(ConfigurationFormat.CISCO_IOS)) {
protocol = RoutingProtocol.IBGP;
}
RouteBuilder rb = new RouteBuilder();
rb.setNode(_hostname);
rb.setNetwork(prefix);
if (protocol == RoutingProtocol.CONNECTED || (protocol == RoutingProtocol.STATIC && nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) || Interface.NULL_INTERFACE_NAME.equals(nextHopInterface)) {
rb.setNextHop(Configuration.NODE_NONE_NAME);
}
if (!nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) {
rb.setNextHopIp(nextHopIp);
String nextHop = _ipOwners.get(nextHopIp);
if (nextHop != null) {
rb.setNextHop(nextHop);
}
}
if (nextHopInterface != null) {
rb.setNextHopInterface(nextHopInterface);
}
rb.setAdministrativeCost(admin);
rb.setCost(cost);
rb.setProtocol(protocol);
rb.setTag(tag);
rb.setVrf(_currentVrfName);
Route route = rb.build();
_currentVrfRoutes.add(route);
}
}
Aggregations