Search in sources :

Example 6 with AsPath

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

the class ConfigurationBuilder method exitRoaa_path.

@Override
public void exitRoaa_path(Roaa_pathContext ctx) {
    AsPath asPath = toAsPath(ctx.path);
    _currentAggregateRoute.setAsPath(asPath);
}
Also used : PsFromAsPath(org.batfish.representation.juniper.PsFromAsPath) AsPath(org.batfish.datamodel.AsPath)

Example 7 with AsPath

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

the class VirtualRouter method propagateBgpRoutes.

int propagateBgpRoutes(Map<Ip, Set<String>> ipOwners, int dependentRoutesIterations, SortedSet<Prefix> oscillatingPrefixes, Map<String, Node> nodes) {
    int numRoutes = 0;
    _receivedBgpAdvertisements = new LinkedHashSet<>();
    _prevSentBgpAdvertisements = _sentBgpAdvertisements != null ? _sentBgpAdvertisements : new LinkedHashSet<>();
    _sentBgpAdvertisements = new LinkedHashSet<>();
    // If we have no BGP process, nothing to do
    if (_vrf.getBgpProcess() == null) {
        return numRoutes;
    }
    int ebgpAdminCost = RoutingProtocol.BGP.getDefaultAdministrativeCost(_c.getConfigurationFormat());
    int ibgpAdminCost = RoutingProtocol.IBGP.getDefaultAdministrativeCost(_c.getConfigurationFormat());
    for (BgpNeighbor neighbor : _vrf.getBgpProcess().getNeighbors().values()) {
        Ip localIp = neighbor.getLocalIp();
        Set<String> localIpOwners = ipOwners.get(localIp);
        String hostname = _c.getHostname();
        if (localIpOwners == null || !localIpOwners.contains(hostname)) {
            continue;
        }
        BgpNeighbor remoteBgpNeighbor = neighbor.getRemoteBgpNeighbor();
        if (remoteBgpNeighbor == null) {
            continue;
        }
        int localAs = neighbor.getLocalAs();
        int remoteAs = neighbor.getRemoteAs();
        Configuration remoteConfig = remoteBgpNeighbor.getOwner();
        String remoteHostname = remoteConfig.getHostname();
        String remoteVrfName = remoteBgpNeighbor.getVrf();
        Vrf remoteVrf = remoteConfig.getVrfs().get(remoteVrfName);
        VirtualRouter remoteVirtualRouter = nodes.get(remoteHostname)._virtualRouters.get(remoteVrfName);
        RoutingPolicy remoteExportPolicy = remoteConfig.getRoutingPolicies().get(remoteBgpNeighbor.getExportPolicy());
        boolean ebgpSession = localAs != remoteAs;
        BgpMultipathRib targetRib = ebgpSession ? _ebgpStagingRib : _ibgpStagingRib;
        RoutingProtocol targetProtocol = ebgpSession ? RoutingProtocol.BGP : RoutingProtocol.IBGP;
        Set<AbstractRoute> remoteCandidateRoutes = Collections.newSetFromMap(new IdentityHashMap<>());
        // Add IGP routes
        Set<AbstractRoute> activeRemoteRoutes = Collections.newSetFromMap(new IdentityHashMap<>());
        activeRemoteRoutes.addAll(remoteVirtualRouter._prevMainRib.getRoutes());
        for (AbstractRoute remoteCandidateRoute : activeRemoteRoutes) {
            if (remoteCandidateRoute.getProtocol() != RoutingProtocol.BGP && remoteCandidateRoute.getProtocol() != RoutingProtocol.IBGP) {
                remoteCandidateRoutes.add(remoteCandidateRoute);
            }
        }
        /*
       * bgp advertise-external
       *
       * When this is set, add best eBGP path independently of whether
       * it is preempted by an iBGP or IGP route. Only applicable to
       * iBGP sessions.
       */
        boolean advertiseExternal = !ebgpSession && remoteBgpNeighbor.getAdvertiseExternal();
        if (advertiseExternal) {
            remoteCandidateRoutes.addAll(remoteVirtualRouter._prevEbgpBestPathRib.getRoutes());
        }
        /*
       * bgp advertise-inactive
       *
       * When this is set, add best BGP path independently of whether
       * it is preempted by an IGP route. Only applicable to eBGP
       * sessions.
       */
        boolean advertiseInactive = ebgpSession && remoteBgpNeighbor.getAdvertiseInactive();
        /* Add best bgp paths if they are active, or if advertise-inactive */
        for (AbstractRoute remoteCandidateRoute : remoteVirtualRouter._prevBgpBestPathRib.getRoutes()) {
            if (advertiseInactive || activeRemoteRoutes.contains(remoteCandidateRoute)) {
                remoteCandidateRoutes.add(remoteCandidateRoute);
            }
        }
        /* Add all bgp paths if additional-paths active for this session */
        boolean additionalPaths = !ebgpSession && neighbor.getAdditionalPathsReceive() && remoteBgpNeighbor.getAdditionalPathsSend() && remoteBgpNeighbor.getAdditionalPathsSelectAll();
        if (additionalPaths) {
            remoteCandidateRoutes.addAll(remoteVirtualRouter._prevBgpMultipathRib.getRoutes());
        }
        for (AbstractRoute remoteRoute : remoteCandidateRoutes) {
            BgpRoute.Builder transformedOutgoingRouteBuilder = new BgpRoute.Builder();
            transformedOutgoingRouteBuilder.setReceivedFromIp(remoteBgpNeighbor.getLocalIp());
            RoutingProtocol remoteRouteProtocol = remoteRoute.getProtocol();
            boolean remoteRouteIsBgp = remoteRouteProtocol == RoutingProtocol.IBGP || remoteRouteProtocol == RoutingProtocol.BGP;
            // originatorIP
            Ip originatorIp;
            if (!ebgpSession && remoteRouteProtocol.equals(RoutingProtocol.IBGP)) {
                BgpRoute bgpRemoteRoute = (BgpRoute) remoteRoute;
                originatorIp = bgpRemoteRoute.getOriginatorIp();
            } else {
                originatorIp = remoteVrf.getBgpProcess().getRouterId();
            }
            transformedOutgoingRouteBuilder.setOriginatorIp(originatorIp);
            // note whether new route is received from route reflector client
            transformedOutgoingRouteBuilder.setReceivedFromRouteReflectorClient(!ebgpSession && neighbor.getRouteReflectorClient());
            // for bgp remote route)
            if (remoteRouteIsBgp) {
                BgpRoute bgpRemoteRoute = (BgpRoute) remoteRoute;
                transformedOutgoingRouteBuilder.setOriginType(bgpRemoteRoute.getOriginType());
                if (ebgpSession && bgpRemoteRoute.getAsPath().containsAs(remoteBgpNeighbor.getRemoteAs()) && !remoteBgpNeighbor.getAllowRemoteAsOut()) {
                    // disable-peer-as-check (getAllowRemoteAsOut) is set
                    continue;
                }
                /*
           * route reflection: reflect everything received from
           * clients to clients and non-clients. reflect everything
           * received from non-clients to clients. Do not reflect to
           * originator
           */
                Ip remoteOriginatorIp = bgpRemoteRoute.getOriginatorIp();
                /*
           *  iBGP speaker should not send out routes to iBGP neighbor whose router-id is
           *  same as originator id of advertisement
           */
                if (!ebgpSession && remoteOriginatorIp != null && _vrf.getBgpProcess().getRouterId().equals(remoteOriginatorIp)) {
                    continue;
                }
                if (remoteRouteProtocol.equals(RoutingProtocol.IBGP) && !ebgpSession) {
                    /*
             *  The remote route is iBGP. The session is iBGP. We consider whether to reflect, and
             *  modify the outgoing route as appropriate.
             */
                    boolean remoteRouteReceivedFromRouteReflectorClient = bgpRemoteRoute.getReceivedFromRouteReflectorClient();
                    boolean sendingToRouteReflectorClient = remoteBgpNeighbor.getRouteReflectorClient();
                    Ip remoteReceivedFromIp = bgpRemoteRoute.getReceivedFromIp();
                    boolean remoteRouteOriginatedByRemoteNeighbor = remoteReceivedFromIp.equals(Ip.ZERO);
                    if (!remoteRouteReceivedFromRouteReflectorClient && !sendingToRouteReflectorClient && !remoteRouteOriginatedByRemoteNeighbor) {
                        /*
               * Neither reflecting nor originating this iBGP route, so don't send
               */
                        continue;
                    }
                    transformedOutgoingRouteBuilder.getClusterList().addAll(bgpRemoteRoute.getClusterList());
                    if (!remoteRouteOriginatedByRemoteNeighbor) {
                        // we are reflecting, so we need to get the clusterid associated with the remoteRoute
                        BgpNeighbor remoteReceivedFromSession = remoteVrf.getBgpProcess().getNeighbors().get(new Prefix(remoteReceivedFromIp, Prefix.MAX_PREFIX_LENGTH));
                        long newClusterId = remoteReceivedFromSession.getClusterId();
                        transformedOutgoingRouteBuilder.getClusterList().add(newClusterId);
                    }
                    Set<Long> localClusterIds = _vrf.getBgpProcess().getClusterIds();
                    Set<Long> outgoingClusterList = transformedOutgoingRouteBuilder.getClusterList();
                    if (localClusterIds.stream().anyMatch(outgoingClusterList::contains)) {
                        /*
               *  receiver will reject new route if it contains any of its local cluster ids
               */
                        continue;
                    }
                }
            }
            // Outgoing communities
            if (remoteRouteIsBgp) {
                BgpRoute bgpRemoteRoute = (BgpRoute) remoteRoute;
                transformedOutgoingRouteBuilder.setAsPath(bgpRemoteRoute.getAsPath().getAsSets());
                if (remoteBgpNeighbor.getSendCommunity()) {
                    transformedOutgoingRouteBuilder.getCommunities().addAll(bgpRemoteRoute.getCommunities());
                }
            }
            if (ebgpSession) {
                SortedSet<Integer> newAsPathElement = new TreeSet<>();
                newAsPathElement.add(remoteAs);
                transformedOutgoingRouteBuilder.getAsPath().add(0, newAsPathElement);
            }
            // Outgoing protocol
            transformedOutgoingRouteBuilder.setProtocol(targetProtocol);
            transformedOutgoingRouteBuilder.setNetwork(remoteRoute.getNetwork());
            // Outgoing metric
            if (remoteRouteIsBgp) {
                transformedOutgoingRouteBuilder.setMetric(remoteRoute.getMetric());
            }
            // Outgoing nextHopIp
            // Outgoing localPreference
            Ip nextHopIp;
            int localPreference;
            if (ebgpSession || !remoteRouteIsBgp) {
                nextHopIp = remoteBgpNeighbor.getLocalIp();
                localPreference = BgpRoute.DEFAULT_LOCAL_PREFERENCE;
            } else {
                nextHopIp = remoteRoute.getNextHopIp();
                BgpRoute remoteIbgpRoute = (BgpRoute) remoteRoute;
                localPreference = remoteIbgpRoute.getLocalPreference();
            }
            if (nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) {
                // should only happen for ibgp
                String nextHopInterface = remoteRoute.getNextHopInterface();
                InterfaceAddress nextHopAddress = remoteVrf.getInterfaces().get(nextHopInterface).getAddress();
                if (nextHopAddress == null) {
                    throw new BatfishException("remote route's nextHopInterface has no address");
                }
                nextHopIp = nextHopAddress.getIp();
            }
            transformedOutgoingRouteBuilder.setNextHopIp(nextHopIp);
            transformedOutgoingRouteBuilder.setLocalPreference(localPreference);
            // Outgoing srcProtocol
            transformedOutgoingRouteBuilder.setSrcProtocol(remoteRoute.getProtocol());
            /*
         * CREATE OUTGOING ROUTE
         */
            boolean acceptOutgoing = remoteExportPolicy.process(remoteRoute, transformedOutgoingRouteBuilder, localIp, remoteVrfName, Direction.OUT);
            if (acceptOutgoing) {
                BgpRoute transformedOutgoingRoute = transformedOutgoingRouteBuilder.build();
                // Record sent advertisement
                BgpAdvertisementType sentType = ebgpSession ? BgpAdvertisementType.EBGP_SENT : BgpAdvertisementType.IBGP_SENT;
                Ip sentReceivedFromIp = transformedOutgoingRoute.getReceivedFromIp();
                Ip sentOriginatorIp = transformedOutgoingRoute.getOriginatorIp();
                SortedSet<Long> sentClusterList = transformedOutgoingRoute.getClusterList();
                boolean sentReceivedFromRouteReflectorClient = transformedOutgoingRoute.getReceivedFromRouteReflectorClient();
                AsPath sentAsPath = transformedOutgoingRoute.getAsPath();
                SortedSet<Long> sentCommunities = transformedOutgoingRoute.getCommunities();
                Prefix sentNetwork = remoteRoute.getNetwork();
                Ip sentNextHopIp;
                String sentSrcNode = remoteHostname;
                String sentSrcVrf = remoteVrfName;
                Ip sentSrcIp = remoteBgpNeighbor.getLocalIp();
                String sentDstNode = hostname;
                String sentDstVrf = _vrf.getName();
                Ip sentDstIp = neighbor.getLocalIp();
                int sentWeight = -1;
                if (ebgpSession) {
                    sentNextHopIp = nextHopIp;
                } else {
                    sentNextHopIp = transformedOutgoingRoute.getNextHopIp();
                }
                int sentLocalPreference = transformedOutgoingRoute.getLocalPreference();
                long sentMed = transformedOutgoingRoute.getMetric();
                OriginType sentOriginType = transformedOutgoingRoute.getOriginType();
                RoutingProtocol sentSrcProtocol = targetProtocol;
                BgpRoute.Builder transformedIncomingRouteBuilder = new BgpRoute.Builder();
                // Incoming originatorIp
                transformedIncomingRouteBuilder.setOriginatorIp(sentOriginatorIp);
                // Incoming receivedFromIp
                transformedIncomingRouteBuilder.setReceivedFromIp(sentReceivedFromIp);
                // Incoming clusterList
                transformedIncomingRouteBuilder.getClusterList().addAll(sentClusterList);
                // Incoming receivedFromRouteReflectorClient
                transformedIncomingRouteBuilder.setReceivedFromRouteReflectorClient(sentReceivedFromRouteReflectorClient);
                // Incoming asPath
                transformedIncomingRouteBuilder.setAsPath(sentAsPath.getAsSets());
                // Incoming communities
                transformedIncomingRouteBuilder.getCommunities().addAll(sentCommunities);
                // Incoming protocol
                transformedIncomingRouteBuilder.setProtocol(targetProtocol);
                // Incoming network
                transformedIncomingRouteBuilder.setNetwork(sentNetwork);
                // Incoming nextHopIp
                transformedIncomingRouteBuilder.setNextHopIp(sentNextHopIp);
                // Incoming localPreference
                transformedIncomingRouteBuilder.setLocalPreference(sentLocalPreference);
                // Incoming admin
                int admin = ebgpSession ? ebgpAdminCost : ibgpAdminCost;
                transformedIncomingRouteBuilder.setAdmin(admin);
                // Incoming metric
                transformedIncomingRouteBuilder.setMetric(sentMed);
                // Incoming originType
                transformedIncomingRouteBuilder.setOriginType(sentOriginType);
                // Incoming srcProtocol
                transformedIncomingRouteBuilder.setSrcProtocol(sentSrcProtocol);
                String importPolicyName = neighbor.getImportPolicy();
                if (transformedOutgoingRoute.getAsPath().containsAs(neighbor.getLocalAs()) && !neighbor.getAllowLocalAsIn()) {
                    // disable-peer-as-check (getAllowRemoteAsOut) is set
                    continue;
                }
                BgpAdvertisement sentAdvert = new BgpAdvertisement(sentType, sentNetwork, sentNextHopIp, sentSrcNode, sentSrcVrf, sentSrcIp, sentDstNode, sentDstVrf, sentDstIp, sentSrcProtocol, sentOriginType, sentLocalPreference, sentMed, sentOriginatorIp, sentAsPath, sentCommunities, sentClusterList, sentWeight);
                Prefix prefix = remoteRoute.getNetwork();
                boolean isOscillatingPrefix = oscillatingPrefixes.contains(prefix);
                boolean hasAdvertisementPriorityDuringRecovery = hasAdvertisementPriorityDuringRecovery(remoteRoute, dependentRoutesIterations, oscillatingPrefixes, neighbor, remoteBgpNeighbor);
                if (isOscillatingPrefix && !hasAdvertisementPriorityDuringRecovery && !_prevSentBgpAdvertisements.contains(sentAdvert)) {
                    continue;
                }
                _sentBgpAdvertisements.add(sentAdvert);
                /*
           * CREATE INCOMING ROUTE
           */
                boolean acceptIncoming = true;
                if (importPolicyName != null) {
                    RoutingPolicy importPolicy = _c.getRoutingPolicies().get(importPolicyName);
                    if (importPolicy != null) {
                        acceptIncoming = importPolicy.process(transformedOutgoingRoute, transformedIncomingRouteBuilder, remoteBgpNeighbor.getLocalIp(), _key, Direction.IN);
                    }
                }
                if (acceptIncoming) {
                    BgpRoute transformedIncomingRoute = transformedIncomingRouteBuilder.build();
                    BgpAdvertisementType receivedType = ebgpSession ? BgpAdvertisementType.EBGP_RECEIVED : BgpAdvertisementType.IBGP_RECEIVED;
                    Prefix receivedNetwork = sentNetwork;
                    Ip receivedNextHopIp = sentNextHopIp;
                    String receivedSrcNode = sentSrcNode;
                    String receivedSrcVrf = sentSrcVrf;
                    Ip receivedSrcIp = sentSrcIp;
                    String receivedDstNode = sentDstNode;
                    String receivedDstVrf = sentDstVrf;
                    Ip receivedDstIp = sentDstIp;
                    RoutingProtocol receivedSrcProtocol = sentSrcProtocol;
                    OriginType receivedOriginType = transformedIncomingRoute.getOriginType();
                    int receivedLocalPreference = transformedIncomingRoute.getLocalPreference();
                    long receivedMed = transformedIncomingRoute.getMetric();
                    Ip receivedOriginatorIp = sentOriginatorIp;
                    AsPath receivedAsPath = transformedIncomingRoute.getAsPath();
                    SortedSet<Long> receivedCommunities = transformedIncomingRoute.getCommunities();
                    SortedSet<Long> receivedClusterList = sentClusterList;
                    int receivedWeight = transformedIncomingRoute.getWeight();
                    BgpAdvertisement receivedAdvert = new BgpAdvertisement(receivedType, receivedNetwork, receivedNextHopIp, receivedSrcNode, receivedSrcVrf, receivedSrcIp, receivedDstNode, receivedDstVrf, receivedDstIp, receivedSrcProtocol, receivedOriginType, receivedLocalPreference, receivedMed, receivedOriginatorIp, receivedAsPath, receivedCommunities, receivedClusterList, receivedWeight);
                    if (targetRib.mergeRoute(transformedIncomingRoute)) {
                        numRoutes++;
                    }
                    _receivedBgpAdvertisements.add(receivedAdvert);
                }
            }
        }
    }
    return numRoutes;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RoutingProtocol(org.batfish.datamodel.RoutingProtocol) Configuration(org.batfish.datamodel.Configuration) BgpAdvertisementType(org.batfish.datamodel.BgpAdvertisement.BgpAdvertisementType) Ip(org.batfish.datamodel.Ip) Vrf(org.batfish.datamodel.Vrf) Prefix(org.batfish.datamodel.Prefix) BgpNeighbor(org.batfish.datamodel.BgpNeighbor) TreeSet(java.util.TreeSet) BgpRoute(org.batfish.datamodel.BgpRoute) AbstractRoute(org.batfish.datamodel.AbstractRoute) BatfishException(org.batfish.common.BatfishException) OriginType(org.batfish.datamodel.OriginType) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) AsPath(org.batfish.datamodel.AsPath) BgpAdvertisement(org.batfish.datamodel.BgpAdvertisement)

Example 8 with AsPath

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

the class BgpMultipathRib method comparePreference.

@Override
public int comparePreference(BgpRoute lhs, BgpRoute rhs) {
    int res;
    /*
     * first compare local preference
     */
    res = Integer.compare(lhs.getLocalPreference(), rhs.getLocalPreference());
    if (res != 0) {
        return res;
    }
    /*
     * on non-juniper, prefer aggregates (these routes won't appear on
     * juniper)
     */
    res = Integer.compare(getAggregatePreference(lhs.getProtocol()), getAggregatePreference(rhs.getProtocol()));
    if (res != 0) {
        return res;
    }
    /*
     * then compare as path size (shorter is better, hence reversal)
     */
    res = Integer.compare(rhs.getAsPath().size(), lhs.getAsPath().size());
    if (res != 0) {
        return res;
    }
    /*
     * AS path size is same. Now compare to best asPath (if available). Note we do not necessarily
     * guarantee existing rhs route is better than best path, since rhs may have been merged before
     * best as paths map was supplied.
     */
    if (_bestAsPaths != null) {
        AsPath bestAsPath = _bestAsPaths.get(lhs.getNetwork());
        AsPath lhsAsPath = lhs.getAsPath();
        AsPath rhsAsPath = rhs.getAsPath();
        switch(_multipathEquivalentAsPathMatchMode) {
            case EXACT_PATH:
                if (bestAsPath.equals(lhsAsPath)) {
                    if (!bestAsPath.equals(rhsAsPath)) {
                        return 1;
                    }
                } else if (bestAsPath.equals(rhsAsPath)) {
                    return -1;
                }
                break;
            case FIRST_AS:
                SortedSet<Integer> lhsFirstAsSet = lhsAsPath.getAsSets().isEmpty() ? Collections.emptySortedSet() : lhsAsPath.getAsSets().get(0);
                SortedSet<Integer> rhsFirstAsSet = rhsAsPath.getAsSets().isEmpty() ? Collections.emptySortedSet() : rhsAsPath.getAsSets().get(0);
                SortedSet<Integer> bestFirstAsSet = bestAsPath.getAsSets().isEmpty() ? Collections.emptySortedSet() : bestAsPath.getAsSets().get(0);
                if (bestFirstAsSet.equals(lhsFirstAsSet)) {
                    if (!bestFirstAsSet.equals(rhsFirstAsSet)) {
                        return 1;
                    }
                } else if (bestFirstAsSet.equals(rhsFirstAsSet)) {
                    return -1;
                }
                break;
            case PATH_LENGTH:
                // Skip since already compared
                break;
            default:
                throw new BatfishException(String.format("Unsupported %s: %s", MultipathEquivalentAsPathMatchMode.class.getName(), _multipathEquivalentAsPathMatchMode));
        }
    }
    /*
     * origin type (IGP better than EGP, which is better than INCOMPLETE)
     */
    res = Integer.compare(lhs.getOriginType().getPreference(), rhs.getOriginType().getPreference());
    if (res != 0) {
        return res;
    }
    /*
     * then compare MED
     *
     * TODO: handle presence/absence of always-compare-med, noting that
     * normally we only do this comparison if the first AS is the same in the
     * paths for both routes
     */
    res = Long.compare(rhs.getMetric(), lhs.getMetric());
    if (res != 0) {
        return res;
    }
    /*
     * next prefer eBGP over iBGP
     */
    res = Integer.compare(getTypeCost(rhs.getProtocol()), getTypeCost(lhs.getProtocol()));
    if (res != 0) {
        return res;
    }
    /*
     * The remaining criteria only apply in non-multipath environments. So we
     * end here.
     */
    return res;
}
Also used : BatfishException(org.batfish.common.BatfishException) AsPath(org.batfish.datamodel.AsPath)

Example 9 with AsPath

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

the class ConfigurationBuilder method toAsPath.

private AsPath toAsPath(As_path_exprContext path) {
    List<SortedSet<Integer>> asPath = new ArrayList<>();
    for (As_unitContext ctx : path.items) {
        SortedSet<Integer> asSet = new TreeSet<>();
        if (ctx.DEC() != null) {
            asSet.add(toInt(ctx.DEC()));
        } else {
            for (Token token : ctx.as_set().items) {
                asSet.add(toInt(token));
            }
        }
        asPath.add(asSet);
    }
    return new AsPath(asPath);
}
Also used : PsFromAsPath(org.batfish.representation.juniper.PsFromAsPath) AsPath(org.batfish.datamodel.AsPath) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) As_unitContext(org.batfish.grammar.flatjuniper.FlatJuniperParser.As_unitContext) Token(org.antlr.v4.runtime.Token) SortedSet(java.util.SortedSet)

Example 10 with AsPath

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

the class BdpDataPlanePluginTest method testBgpAsPathMultipathHelper.

private void testBgpAsPathMultipathHelper(MultipathEquivalentAsPathMatchMode multipathEquivalentAsPathMatchMode, boolean primeBestPathInMultipathBgpRib, boolean expectRoute2, boolean expectRoute3a, boolean expectRoute3b, boolean expectRoute3c, boolean expectRoute3d) {
    /*
     * Properties of the routes
     */
    // Should appear only for path-length match
    List<SortedSet<Integer>> asPath2 = AsPath.ofSingletonAsSets(2, 4, 6).getAsSets();
    // Should appear only for first-as match and path-length match
    List<SortedSet<Integer>> asPath3a = AsPath.ofSingletonAsSets(3, 5, 6).getAsSets();
    // Should never appear
    List<SortedSet<Integer>> asPath3b = AsPath.ofSingletonAsSets(3, 4, 4, 6).getAsSets();
    // Should always appear
    AsPath bestAsPath = AsPath.ofSingletonAsSets(3, 4, 6);
    List<SortedSet<Integer>> asPath3c = bestAsPath.getAsSets();
    List<SortedSet<Integer>> asPath3d = bestAsPath.getAsSets();
    Ip nextHop2 = new Ip("2.0.0.0");
    Ip nextHop3a = new Ip("3.0.0.1");
    Ip nextHop3b = new Ip("3.0.0.2");
    Ip nextHop3c = new Ip("3.0.0.3");
    Ip nextHop3d = new Ip("3.0.0.4");
    /*
     * Common attributes for all routes
     */
    Prefix p = Prefix.ZERO;
    BgpRoute.Builder b = new BgpRoute.Builder().setNetwork(p).setProtocol(RoutingProtocol.BGP).setOriginType(OriginType.INCOMPLETE);
    /*
     * Boilerplate virtual-router setup
     */
    String hostname = "r1";
    Configuration c = BatfishTestUtils.createTestConfiguration(hostname, ConfigurationFormat.CISCO_IOS);
    BgpProcess proc = new BgpProcess();
    c.getVrfs().computeIfAbsent(DEFAULT_VRF_NAME, Vrf::new).setBgpProcess(proc);
    Map<String, Node> nodes = new HashMap<String, Node>();
    Node node = new Node(c);
    nodes.put(hostname, node);
    VirtualRouter vr = new VirtualRouter(DEFAULT_VRF_NAME, c);
    /*
     * Instantiate routes
     */
    BgpRoute route2 = b.setAsPath(asPath2).setNextHopIp(nextHop2).setOriginatorIp(nextHop2).setReceivedFromIp(nextHop2).build();
    BgpRoute route3a = b.setAsPath(asPath3a).setNextHopIp(nextHop3a).setOriginatorIp(nextHop3a).setReceivedFromIp(nextHop3a).build();
    BgpRoute route3b = b.setAsPath(asPath3b).setNextHopIp(nextHop3b).setOriginatorIp(nextHop3b).setReceivedFromIp(nextHop3b).build();
    BgpRoute route3c = b.setAsPath(asPath3c).setNextHopIp(nextHop3c).setOriginatorIp(nextHop3c).setReceivedFromIp(nextHop3c).build();
    BgpRoute route3d = b.setAsPath(asPath3d).setNextHopIp(nextHop3d).setOriginatorIp(nextHop3d).setReceivedFromIp(nextHop3d).build();
    /*
     * Set the as-path match mode prior to instantiating bgp multipath RIB
     */
    proc.setMultipathEquivalentAsPathMatchMode(multipathEquivalentAsPathMatchMode);
    BgpMultipathRib bmr = new BgpMultipathRib(vr);
    /*
     * Prime bgp multipath RIB with best path for the prefix
     */
    if (primeBestPathInMultipathBgpRib) {
        bmr.setBestAsPaths(Collections.singletonMap(p, bestAsPath));
    }
    /*
     * Add routes to multipath RIB.
     */
    bmr.mergeRoute(route2);
    bmr.mergeRoute(route3a);
    bmr.mergeRoute(route3b);
    bmr.mergeRoute(route3c);
    bmr.mergeRoute(route3d);
    /*
     * Initialize the matchers with respect to the output route set
     */
    Set<BgpRoute> postMergeRoutes = bmr.getRoutes();
    Matcher<BgpRoute> present = isIn(postMergeRoutes);
    Matcher<BgpRoute> absent = not(present);
    /*
     * ASSERTIONS:
     * Only the expected routes for the given match mode should be present at end
     */
    assertThat(route2, expectRoute2 ? present : absent);
    assertThat(route3a, expectRoute3a ? present : absent);
    assertThat(route3b, expectRoute3b ? present : absent);
    assertThat(route3c, expectRoute3c ? present : absent);
    assertThat(route3c, expectRoute3d ? present : absent);
}
Also used : Configuration(org.batfish.datamodel.Configuration) BgpProcess(org.batfish.datamodel.BgpProcess) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) Matchers.containsString(org.hamcrest.Matchers.containsString) SortedSet(java.util.SortedSet) AsPath(org.batfish.datamodel.AsPath) BgpRoute(org.batfish.datamodel.BgpRoute)

Aggregations

AsPath (org.batfish.datamodel.AsPath)12 Ip (org.batfish.datamodel.Ip)7 BgpAdvertisement (org.batfish.datamodel.BgpAdvertisement)6 Prefix (org.batfish.datamodel.Prefix)6 TreeSet (java.util.TreeSet)5 BgpRoute (org.batfish.datamodel.BgpRoute)5 SortedSet (java.util.SortedSet)4 BatfishException (org.batfish.common.BatfishException)4 BgpNeighbor (org.batfish.datamodel.BgpNeighbor)4 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)4 AbstractRoute (org.batfish.datamodel.AbstractRoute)3 BgpAdvertisementType (org.batfish.datamodel.BgpAdvertisement.BgpAdvertisementType)3 Configuration (org.batfish.datamodel.Configuration)3 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 OriginType (org.batfish.datamodel.OriginType)2 RoutingProtocol (org.batfish.datamodel.RoutingProtocol)2 Vrf (org.batfish.datamodel.Vrf)2