Search in sources :

Example 16 with AbstractRoute

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;
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) StaticRoute(org.batfish.datamodel.StaticRoute) Prefix(org.batfish.datamodel.Prefix)

Example 17 with AbstractRoute

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);
        }
    }
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) OspfProcess(org.batfish.datamodel.OspfProcess) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) OspfExternalRoute(org.batfish.datamodel.OspfExternalRoute)

Example 18 with AbstractRoute

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;
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) GeneratedRoute(org.batfish.datamodel.GeneratedRoute) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy)

Example 19 with AbstractRoute

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();
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) LinkedHashSet(java.util.LinkedHashSet) SortedSet(java.util.SortedSet) Set(java.util.Set) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) TreeMap(java.util.TreeMap) SortedSet(java.util.SortedSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Entry(java.util.Map.Entry) TreeSet(java.util.TreeSet) SortedMap(java.util.SortedMap)

Example 20 with AbstractRoute

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;
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) LinkedHashSet(java.util.LinkedHashSet) SortedSet(java.util.SortedSet) Set(java.util.Set) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Prefix(org.batfish.datamodel.Prefix) TreeMap(java.util.TreeMap) SortedSet(java.util.SortedSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Entry(java.util.Map.Entry) TreeSet(java.util.TreeSet) SortedMap(java.util.SortedMap) AbstractRoute(org.batfish.datamodel.AbstractRoute) Route(org.batfish.datamodel.Route)

Aggregations

AbstractRoute (org.batfish.datamodel.AbstractRoute)27 Prefix (org.batfish.datamodel.Prefix)18 SortedMap (java.util.SortedMap)16 SortedSet (java.util.SortedSet)13 TreeMap (java.util.TreeMap)12 Set (java.util.Set)11 Ip (org.batfish.datamodel.Ip)10 Batfish (org.batfish.main.Batfish)10 TreeSet (java.util.TreeSet)9 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)9 Test (org.junit.Test)9 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)8 Map (java.util.Map)8 Configuration (org.batfish.datamodel.Configuration)8 Interface (org.batfish.datamodel.Interface)8 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)7 StaticRoute (org.batfish.datamodel.StaticRoute)7 Vrf (org.batfish.datamodel.Vrf)7 ImmutableList (com.google.common.collect.ImmutableList)6 IOException (java.io.IOException)6