Search in sources :

Example 21 with Statement

use of org.batfish.datamodel.routing_policy.statement.Statement 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 22 with Statement

use of org.batfish.datamodel.routing_policy.statement.Statement 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)

Aggregations

Statement (org.batfish.datamodel.routing_policy.statement.Statement)22 If (org.batfish.datamodel.routing_policy.statement.If)16 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)13 Conjunction (org.batfish.datamodel.routing_policy.expr.Conjunction)9 MatchPrefixSet (org.batfish.datamodel.routing_policy.expr.MatchPrefixSet)8 CallStatement (org.batfish.datamodel.routing_policy.statement.CallStatement)8 Prefix (org.batfish.datamodel.Prefix)7 ArrayList (java.util.ArrayList)6 BatfishException (org.batfish.common.BatfishException)6 BooleanExpr (org.batfish.datamodel.routing_policy.expr.BooleanExpr)6 DestinationNetwork (org.batfish.datamodel.routing_policy.expr.DestinationNetwork)6 MatchProtocol (org.batfish.datamodel.routing_policy.expr.MatchProtocol)6 HashMap (java.util.HashMap)5 RouteFilterList (org.batfish.datamodel.RouteFilterList)5 SubRange (org.batfish.datamodel.SubRange)5 Disjunction (org.batfish.datamodel.routing_policy.expr.Disjunction)5 ExplicitPrefixSet (org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet)5 NamedPrefixSet (org.batfish.datamodel.routing_policy.expr.NamedPrefixSet)5 RouteFilterLine (org.batfish.datamodel.RouteFilterLine)4 CallExpr (org.batfish.datamodel.routing_policy.expr.CallExpr)4