Search in sources :

Example 1 with OspfProcess

use of org.batfish.datamodel.OspfProcess in project batfish by batfish.

the class VirtualRouter method propagateOspfInternalRoutes.

/**
 * Propagate OSPF internal routes from every valid OSPF neighbor
 *
 * @param nodes mapping of node names to instances.
 * @param topology network topology
 * @return true if new routes have been added to the staging RIB
 */
boolean propagateOspfInternalRoutes(Map<String, Node> nodes, Topology topology) {
    OspfProcess proc = _vrf.getOspfProcess();
    if (proc == null) {
        // nothing to do
        return false;
    }
    boolean changed = false;
    String node = _c.getHostname();
    // Default OSPF admin cost for constructing new routes
    int adminCost = RoutingProtocol.OSPF.getDefaultAdministrativeCost(_c.getConfigurationFormat());
    SortedSet<Edge> edges = topology.getNodeEdges().get(node);
    if (edges == null) {
        // there are no edges, so OSPF won't produce anything
        return false;
    }
    for (Edge edge : edges) {
        if (!edge.getNode1().equals(node)) {
            continue;
        }
        String connectingInterfaceName = edge.getInt1();
        Interface connectingInterface = _vrf.getInterfaces().get(connectingInterfaceName);
        if (connectingInterface == null) {
            // wrong vrf, so skip
            continue;
        }
        String neighborName = edge.getNode2();
        Node neighbor = nodes.get(neighborName);
        Interface neighborInterface = neighbor._c.getInterfaces().get(edge.getInt2());
        changed |= propagateOspfInternalRoutesFromNeighbor(proc, neighbor, connectingInterface, neighborInterface, adminCost);
    }
    return changed;
}
Also used : OspfProcess(org.batfish.datamodel.OspfProcess) Edge(org.batfish.datamodel.Edge) Interface(org.batfish.datamodel.Interface)

Example 2 with OspfProcess

use of org.batfish.datamodel.OspfProcess in project batfish by batfish.

the class VirtualRouter method initIntraAreaOspfRoutes.

/**
 * Initialize Intra-area OSPF routes from the interface prefixes
 */
private void initIntraAreaOspfRoutes() {
    OspfProcess proc = _vrf.getOspfProcess();
    if (proc == null) {
        // nothing to do
        return;
    }
    // init intra-area routes from connected routes
    // For each interface within an OSPF area and each interface prefix,
    // construct a new OSPF-IA route. Put it in the IA RIB.
    proc.getAreas().forEach((areaNum, area) -> {
        for (String ifaceName : area.getInterfaces()) {
            Interface iface = _c.getInterfaces().get(ifaceName);
            if (iface.getActive()) {
                Set<Prefix> allNetworkPrefixes = iface.getAllAddresses().stream().map(InterfaceAddress::getPrefix).collect(Collectors.toSet());
                int interfaceOspfCost = iface.getOspfCost();
                for (Prefix prefix : allNetworkPrefixes) {
                    long cost = interfaceOspfCost;
                    boolean stubNetwork = iface.getOspfPassive() || iface.getOspfPointToPoint();
                    if (stubNetwork) {
                        if (proc.getMaxMetricStubNetworks() != null) {
                            cost = proc.getMaxMetricStubNetworks();
                        }
                    } else if (proc.getMaxMetricTransitLinks() != null) {
                        cost = proc.getMaxMetricTransitLinks();
                    }
                    OspfIntraAreaRoute route = new OspfIntraAreaRoute(prefix, null, RoutingProtocol.OSPF.getDefaultAdministrativeCost(_c.getConfigurationFormat()), cost, areaNum);
                    _ospfIntraAreaRib.mergeRoute(route);
                }
            }
        }
    });
}
Also used : OspfIntraAreaRoute(org.batfish.datamodel.OspfIntraAreaRoute) OspfProcess(org.batfish.datamodel.OspfProcess) Prefix(org.batfish.datamodel.Prefix) Interface(org.batfish.datamodel.Interface)

Example 3 with OspfProcess

use of org.batfish.datamodel.OspfProcess in project batfish by batfish.

the class Graph method initAreaIds.

/*
   * Initialize each routers set of area IDs for OSPF
   */
private void initAreaIds() {
    for (Entry<String, Configuration> entry : _configurations.entrySet()) {
        String router = entry.getKey();
        Configuration conf = entry.getValue();
        Set<Long> areaIds = new HashSet<>();
        OspfProcess p = conf.getDefaultVrf().getOspfProcess();
        if (p != null) {
            p.getAreas().forEach((id, area) -> areaIds.add(id));
        }
        _areaIds.put(router, areaIds);
    }
}
Also used : Configuration(org.batfish.datamodel.Configuration) OspfProcess(org.batfish.datamodel.OspfProcess) HashSet(java.util.HashSet)

Example 4 with OspfProcess

use of org.batfish.datamodel.OspfProcess in project batfish by batfish.

the class Graph method getOriginatedNetworks.

/*
   * Collects and returns all originated prefixes for the given
   * router as well as the protocol. Static routes and connected
   * routes are treated as originating the prefix.
   */
public static Set<Prefix> getOriginatedNetworks(Configuration conf, Protocol proto) {
    Set<Prefix> acc = new HashSet<>();
    if (proto.isOspf()) {
        OspfProcess ospf = conf.getDefaultVrf().getOspfProcess();
        for (OspfArea area : ospf.getAreas().values()) {
            for (String ifaceName : area.getInterfaces()) {
                Interface iface = conf.getInterfaces().get(ifaceName);
                if (iface.getActive() && iface.getOspfEnabled()) {
                    acc.add(iface.getAddress().getPrefix());
                }
            }
        }
        return acc;
    }
    if (proto.isBgp()) {
        RoutingPolicy defaultPol = findCommonRoutingPolicy(conf, Protocol.BGP);
        if (defaultPol != null) {
            AstVisitor v = new AstVisitor();
            v.visit(conf, defaultPol.getStatements(), stmt -> {
            }, expr -> {
                if (expr instanceof Conjunction) {
                    Conjunction c = (Conjunction) expr;
                    if (c.getConjuncts().size() >= 2) {
                        BooleanExpr be1 = c.getConjuncts().get(0);
                        BooleanExpr be2 = c.getConjuncts().get(1);
                        if (be1 instanceof MatchPrefixSet && be2 instanceof Not) {
                            MatchPrefixSet mps = (MatchPrefixSet) be1;
                            Not n = (Not) be2;
                            if (n.getExpr() instanceof MatchProtocol) {
                                MatchProtocol mp = (MatchProtocol) n.getExpr();
                                if (mp.getProtocol() == RoutingProtocol.BGP) {
                                    PrefixSetExpr e = mps.getPrefixSet();
                                    if (e instanceof ExplicitPrefixSet) {
                                        ExplicitPrefixSet eps = (ExplicitPrefixSet) e;
                                        Set<PrefixRange> ranges = eps.getPrefixSpace().getPrefixRanges();
                                        for (PrefixRange r : ranges) {
                                            acc.add(r.getPrefix());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
        return acc;
    }
    if (proto.isConnected()) {
        for (Interface iface : conf.getInterfaces().values()) {
            InterfaceAddress address = iface.getAddress();
            if (address != null) {
                acc.add(address.getPrefix());
            }
        }
        return acc;
    }
    if (proto.isStatic()) {
        for (StaticRoute sr : conf.getDefaultVrf().getStaticRoutes()) {
            if (sr.getNetwork() != null) {
                acc.add(sr.getNetwork());
            }
        }
        return acc;
    }
    throw new BatfishException("ERROR: getOriginatedNetworks: " + proto.name());
}
Also used : BatfishException(org.batfish.common.BatfishException) PrefixRange(org.batfish.datamodel.PrefixRange) StaticRoute(org.batfish.datamodel.StaticRoute) OspfArea(org.batfish.datamodel.OspfArea) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) PrefixSetExpr(org.batfish.datamodel.routing_policy.expr.PrefixSetExpr) OspfProcess(org.batfish.datamodel.OspfProcess) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) Prefix(org.batfish.datamodel.Prefix) MatchProtocol(org.batfish.datamodel.routing_policy.expr.MatchProtocol) Not(org.batfish.datamodel.routing_policy.expr.Not) ExplicitPrefixSet(org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet) Conjunction(org.batfish.datamodel.routing_policy.expr.Conjunction) Interface(org.batfish.datamodel.Interface) BooleanExpr(org.batfish.datamodel.routing_policy.expr.BooleanExpr) HashSet(java.util.HashSet)

Example 5 with OspfProcess

use of org.batfish.datamodel.OspfProcess in project batfish by batfish.

the class Graph method findExportRoutingPolicy.

/*
   * Find the export routing policy for a given edge
   */
@Nullable
public RoutingPolicy findExportRoutingPolicy(String router, Protocol proto, GraphEdge ge) {
    Configuration conf = _configurations.get(router);
    if (proto.isConnected()) {
        return null;
    }
    if (proto.isStatic()) {
        return null;
    }
    if (proto.isOspf()) {
        OspfProcess p = conf.getDefaultVrf().getOspfProcess();
        if (p == null) {
            return null;
        }
        String exp = p.getExportPolicy();
        return conf.getRoutingPolicies().get(exp);
    }
    if (proto.isBgp()) {
        BgpNeighbor n = findBgpNeighbor(ge);
        // if no neighbor (e.g., loopback), or no export policy
        if (n == null || n.getExportPolicy() == null) {
            return null;
        }
        return conf.getRoutingPolicies().get(n.getExportPolicy());
    }
    throw new BatfishException("TODO: findExportRoutingPolicy for " + proto.name());
}
Also used : BgpNeighbor(org.batfish.datamodel.BgpNeighbor) BatfishException(org.batfish.common.BatfishException) Configuration(org.batfish.datamodel.Configuration) OspfProcess(org.batfish.datamodel.OspfProcess) Nullable(javax.annotation.Nullable)

Aggregations

OspfProcess (org.batfish.datamodel.OspfProcess)15 Configuration (org.batfish.datamodel.Configuration)9 OspfArea (org.batfish.datamodel.OspfArea)7 Interface (org.batfish.datamodel.Interface)6 Prefix (org.batfish.datamodel.Prefix)6 Vrf (org.batfish.datamodel.Vrf)5 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)5 Ip (org.batfish.datamodel.Ip)4 HashSet (java.util.HashSet)3 Edge (org.batfish.datamodel.Edge)3 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)3 IpLink (org.batfish.datamodel.IpLink)3 OspfNeighbor (org.batfish.datamodel.OspfNeighbor)3 BatfishException (org.batfish.common.BatfishException)2 BgpNeighbor (org.batfish.datamodel.BgpNeighbor)2 BgpProcess (org.batfish.datamodel.BgpProcess)2 OspfIntraAreaRoute (org.batfish.datamodel.OspfIntraAreaRoute)2 Topology (org.batfish.datamodel.Topology)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1