Search in sources :

Example 11 with DestinationNetwork

use of org.batfish.datamodel.routing_policy.expr.DestinationNetwork in project batfish by batfish.

the class BatfishCompressor method applyFilters.

/**
 * Update a list of RoutingPolicy statements by filtering traffic according to the input filter.
 *
 * @param statements The list of RoutingPolicy statements.
 * @param filter The filter used to restrict traffic
 * @return A new list of RoutingPolicy statements
 */
// PrefixTrie: capture the prefixes you are installing to allow traffic through. Restrict
// to those prefixes
// Boolean: are the prefixes for the default equivalence class?
private List<Statement> applyFilters(List<Statement> statements, @Nullable EquivalenceClassFilter filter) {
    If i = new If();
    List<Statement> newStatements = new ArrayList<>();
    List<Statement> falseStatements = new ArrayList<>();
    Statement reject = new StaticStatement(Statements.ExitReject);
    falseStatements.add(reject);
    if (filter == null) {
        StaticBooleanExpr sbe = new StaticBooleanExpr(BooleanExprs.False);
        i.setGuard(sbe);
    } else {
        AbstractionPrefixSet eps = new AbstractionPrefixSet(filter._prefixTrie);
        MatchPrefixSet match = new MatchPrefixSet(new DestinationNetwork(), eps);
        if (filter._isForDefaultSlice) {
            // Let traffic through if it passes the filter or was advertised from outside the network.
            Disjunction pfxOrExternal = new Disjunction();
            pfxOrExternal.setDisjuncts(ImmutableList.of(match, matchExternalTraffic()));
            i.setGuard(pfxOrExternal);
        } else {
            // Not default equivalence class, so just let traffic through if dest matches the filter
            i.setGuard(match);
        }
    }
    i.setFalseStatements(falseStatements);
    i.setTrueStatements(statements);
    newStatements.add(i);
    return newStatements;
}
Also used : Disjunction(org.batfish.datamodel.routing_policy.expr.Disjunction) DestinationNetwork(org.batfish.datamodel.routing_policy.expr.DestinationNetwork) AbstractionPrefixSet(org.batfish.datamodel.routing_policy.expr.AbstractionPrefixSet) StaticStatement(org.batfish.datamodel.routing_policy.statement.Statements.StaticStatement) Statement(org.batfish.datamodel.routing_policy.statement.Statement) StaticStatement(org.batfish.datamodel.routing_policy.statement.Statements.StaticStatement) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) ArrayList(java.util.ArrayList) If(org.batfish.datamodel.routing_policy.statement.If) StaticBooleanExpr(org.batfish.datamodel.routing_policy.expr.BooleanExprs.StaticBooleanExpr)

Example 12 with DestinationNetwork

use of org.batfish.datamodel.routing_policy.expr.DestinationNetwork in project batfish by batfish.

the class JuniperConfiguration method toRoutingPolicy.

private RoutingPolicy toRoutingPolicy(PolicyStatement ps) {
    String name = ps.getName();
    RoutingPolicy routingPolicy = new RoutingPolicy(name, _c);
    List<Statement> statements = routingPolicy.getStatements();
    boolean hasDefaultTerm = ps.getDefaultTerm().getFroms().size() > 0 || ps.getDefaultTerm().getThens().size() > 0;
    List<PsTerm> terms = new ArrayList<>();
    terms.addAll(ps.getTerms().values());
    if (hasDefaultTerm) {
        terms.add(ps.getDefaultTerm());
    }
    for (PsTerm term : terms) {
        List<Statement> thens = toStatements(term.getThens());
        if (!term.getFroms().isEmpty()) {
            If ifStatement = new If();
            ifStatement.setComment(term.getName());
            Conjunction conj = new Conjunction();
            List<BooleanExpr> subroutines = new ArrayList<>();
            for (PsFrom from : term.getFroms()) {
                if (from instanceof PsFromRouteFilter) {
                    int actionLineCounter = 0;
                    PsFromRouteFilter fromRouteFilter = (PsFromRouteFilter) from;
                    String routeFilterName = fromRouteFilter.getRouteFilterName();
                    RouteFilter rf = _routeFilters.get(routeFilterName);
                    for (RouteFilterLine line : rf.getLines()) {
                        if (line.getThens().size() > 0) {
                            String lineListName = name + "_ACTION_LINE_" + actionLineCounter;
                            RouteFilterList lineSpecificList = new RouteFilterList(lineListName);
                            line.applyTo(lineSpecificList);
                            actionLineCounter++;
                            _c.getRouteFilterLists().put(lineListName, lineSpecificList);
                            If lineSpecificIfStatement = new If();
                            String lineSpecificClauseName = routeFilterName + "_ACTION_LINE_" + actionLineCounter;
                            lineSpecificIfStatement.setComment(lineSpecificClauseName);
                            MatchPrefixSet mrf = new MatchPrefixSet(new DestinationNetwork(), new NamedPrefixSet(lineListName));
                            lineSpecificIfStatement.setGuard(mrf);
                            lineSpecificIfStatement.getTrueStatements().addAll(toStatements(line.getThens()));
                            statements.add(lineSpecificIfStatement);
                        }
                    }
                }
                BooleanExpr booleanExpr = from.toBooleanExpr(this, _c, _w);
                if (from instanceof PsFromPolicyStatement || from instanceof PsFromPolicyStatementConjunction) {
                    subroutines.add(booleanExpr);
                } else {
                    conj.getConjuncts().add(booleanExpr);
                }
            }
            if (!subroutines.isEmpty()) {
                ConjunctionChain chain = new ConjunctionChain(subroutines);
                conj.getConjuncts().add(chain);
            }
            BooleanExpr guard = conj.simplify();
            ifStatement.setGuard(guard);
            ifStatement.getTrueStatements().addAll(thens);
            statements.add(ifStatement);
        } else {
            statements.addAll(thens);
        }
    }
    If endOfPolicy = new If();
    endOfPolicy.setGuard(BooleanExprs.CallExprContext.toStaticBooleanExpr());
    endOfPolicy.setFalseStatements(Collections.singletonList(Statements.Return.toStaticStatement()));
    statements.add(endOfPolicy);
    return routingPolicy;
}
Also used : NamedPrefixSet(org.batfish.datamodel.routing_policy.expr.NamedPrefixSet) Statement(org.batfish.datamodel.routing_policy.statement.Statement) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) ArrayList(java.util.ArrayList) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) ConjunctionChain(org.batfish.datamodel.routing_policy.expr.ConjunctionChain) DestinationNetwork(org.batfish.datamodel.routing_policy.expr.DestinationNetwork) RouteFilterList(org.batfish.datamodel.RouteFilterList) Conjunction(org.batfish.datamodel.routing_policy.expr.Conjunction) If(org.batfish.datamodel.routing_policy.statement.If) BooleanExpr(org.batfish.datamodel.routing_policy.expr.BooleanExpr)

Example 13 with DestinationNetwork

use of org.batfish.datamodel.routing_policy.expr.DestinationNetwork in project batfish by batfish.

the class OspfTest method getExportPolicyStatements.

private static List<Statement> getExportPolicyStatements(InterfaceAddress address) {
    long externalOspfMetric = 20L;
    If exportIfMatchL2Prefix = new If();
    exportIfMatchL2Prefix.setGuard(new MatchPrefixSet(new DestinationNetwork(), new ExplicitPrefixSet(new PrefixSpace(ImmutableSet.of(PrefixRange.fromPrefix(address.getPrefix()))))));
    exportIfMatchL2Prefix.setTrueStatements(ImmutableList.of(new SetOspfMetricType(OspfMetricType.E1), new SetMetric(new LiteralLong(externalOspfMetric)), Statements.ExitAccept.toStaticStatement()));
    exportIfMatchL2Prefix.setFalseStatements(ImmutableList.of(Statements.ExitReject.toStaticStatement()));
    return ImmutableList.of(exportIfMatchL2Prefix);
}
Also used : DestinationNetwork(org.batfish.datamodel.routing_policy.expr.DestinationNetwork) ExplicitPrefixSet(org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet) SetMetric(org.batfish.datamodel.routing_policy.statement.SetMetric) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) PrefixSpace(org.batfish.datamodel.PrefixSpace) SetOspfMetricType(org.batfish.datamodel.routing_policy.statement.SetOspfMetricType) LiteralLong(org.batfish.datamodel.routing_policy.expr.LiteralLong) If(org.batfish.datamodel.routing_policy.statement.If)

Aggregations

DestinationNetwork (org.batfish.datamodel.routing_policy.expr.DestinationNetwork)13 MatchPrefixSet (org.batfish.datamodel.routing_policy.expr.MatchPrefixSet)13 RouteFilterList (org.batfish.datamodel.RouteFilterList)9 NamedPrefixSet (org.batfish.datamodel.routing_policy.expr.NamedPrefixSet)9 If (org.batfish.datamodel.routing_policy.statement.If)8 Prefix (org.batfish.datamodel.Prefix)7 SubRange (org.batfish.datamodel.SubRange)7 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)6 Statement (org.batfish.datamodel.routing_policy.statement.Statement)6 RouteFilterLine (org.batfish.datamodel.RouteFilterLine)5 BooleanExpr (org.batfish.datamodel.routing_policy.expr.BooleanExpr)5 Conjunction (org.batfish.datamodel.routing_policy.expr.Conjunction)5 Disjunction (org.batfish.datamodel.routing_policy.expr.Disjunction)5 ArrayList (java.util.ArrayList)4 MatchProtocol (org.batfish.datamodel.routing_policy.expr.MatchProtocol)4 GeneratedRoute (org.batfish.datamodel.GeneratedRoute)3 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)3 Ip (org.batfish.datamodel.Ip)3 PrefixSpace (org.batfish.datamodel.PrefixSpace)3 ExplicitPrefixSet (org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet)3