Search in sources :

Example 6 with CallStatement

use of org.batfish.datamodel.routing_policy.statement.CallStatement in project batfish by batfish.

the class CiscoConfiguration method toRoutingPolicies.

private RoutingPolicy toRoutingPolicies(Configuration c, RouteMap map) {
    RoutingPolicy output = new RoutingPolicy(map.getName(), c);
    List<Statement> statements = output.getStatements();
    Map<Integer, RoutingPolicy> clauses = new HashMap<>();
    // descend map so continue targets are available
    RoutingPolicy followingClause = null;
    Integer followingClauseNumber = null;
    for (Entry<Integer, RouteMapClause> e : map.getClauses().descendingMap().entrySet()) {
        int clauseNumber = e.getKey();
        RouteMapClause rmClause = e.getValue();
        String clausePolicyName = getRouteMapClausePolicyName(map, clauseNumber);
        Conjunction conj = new Conjunction();
        // match ipv4s must be disjoined with match ipv6
        Disjunction matchIpOrPrefix = new Disjunction();
        for (RouteMapMatchLine rmMatch : rmClause.getMatchList()) {
            BooleanExpr matchExpr = rmMatch.toBooleanExpr(c, this, _w);
            if (rmMatch instanceof RouteMapMatchIpAccessListLine || rmMatch instanceof RouteMapMatchIpPrefixListLine || rmMatch instanceof RouteMapMatchIpv6AccessListLine || rmMatch instanceof RouteMapMatchIpv6PrefixListLine) {
                matchIpOrPrefix.getDisjuncts().add(matchExpr);
            } else {
                conj.getConjuncts().add(matchExpr);
            }
        }
        if (!matchIpOrPrefix.getDisjuncts().isEmpty()) {
            conj.getConjuncts().add(matchIpOrPrefix);
        }
        RoutingPolicy clausePolicy = new RoutingPolicy(clausePolicyName, c);
        c.getRoutingPolicies().put(clausePolicyName, clausePolicy);
        If ifStatement = new If();
        clausePolicy.getStatements().add(ifStatement);
        clauses.put(clauseNumber, clausePolicy);
        ifStatement.setComment(clausePolicyName);
        ifStatement.setGuard(conj);
        List<Statement> onMatchStatements = ifStatement.getTrueStatements();
        for (RouteMapSetLine rmSet : rmClause.getSetList()) {
            rmSet.applyTo(onMatchStatements, this, c, _w);
        }
        RouteMapContinue continueStatement = rmClause.getContinueLine();
        Integer continueTarget = null;
        RoutingPolicy continueTargetPolicy = null;
        if (continueStatement != null) {
            continueTarget = continueStatement.getTarget();
            int statementLine = continueStatement.getStatementLine();
            if (continueTarget == null) {
                continueTarget = followingClauseNumber;
            }
            if (continueTarget != null) {
                if (continueTarget <= clauseNumber) {
                    throw new BatfishException("Can only continue to later clause");
                }
                continueTargetPolicy = clauses.get(continueTarget);
                if (continueTargetPolicy == null) {
                    String name = "clause: '" + continueTarget + "' in route-map: '" + map.getName() + "'";
                    undefined(CiscoStructureType.ROUTE_MAP_CLAUSE, name, CiscoStructureUsage.ROUTE_MAP_CONTINUE, statementLine);
                    continueStatement = null;
                }
            } else {
                continueStatement = null;
            }
        }
        switch(rmClause.getAction()) {
            case ACCEPT:
                if (continueStatement == null) {
                    onMatchStatements.add(Statements.ReturnTrue.toStaticStatement());
                } else {
                    onMatchStatements.add(Statements.SetLocalDefaultActionAccept.toStaticStatement());
                    onMatchStatements.add(new CallStatement(continueTargetPolicy.getName()));
                }
                break;
            case REJECT:
                onMatchStatements.add(Statements.ReturnFalse.toStaticStatement());
                break;
            default:
                throw new BatfishException("Invalid action");
        }
        if (followingClause != null) {
            ifStatement.getFalseStatements().add(new CallStatement(followingClause.getName()));
        } else {
            ifStatement.getFalseStatements().add(Statements.ReturnLocalDefaultAction.toStaticStatement());
        }
        followingClause = clausePolicy;
        followingClauseNumber = clauseNumber;
    }
    statements.add(new CallStatement(followingClause.getName()));
    return output;
}
Also used : BatfishException(org.batfish.common.BatfishException) HashMap(java.util.HashMap) CallStatement(org.batfish.datamodel.routing_policy.statement.CallStatement) Statement(org.batfish.datamodel.routing_policy.statement.Statement) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) CallStatement(org.batfish.datamodel.routing_policy.statement.CallStatement) BigInteger(java.math.BigInteger) Disjunction(org.batfish.datamodel.routing_policy.expr.Disjunction) 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

CallStatement (org.batfish.datamodel.routing_policy.statement.CallStatement)6 Test (org.junit.Test)5 BufferedStatement (org.batfish.datamodel.routing_policy.statement.BufferedStatement)3 Statement (org.batfish.datamodel.routing_policy.statement.Statement)3 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)2 Conjunction (org.batfish.datamodel.routing_policy.expr.Conjunction)2 Disjunction (org.batfish.datamodel.routing_policy.expr.Disjunction)2 If (org.batfish.datamodel.routing_policy.statement.If)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 BigInteger (java.math.BigInteger)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BatfishException (org.batfish.common.BatfishException)1 Warnings (org.batfish.common.Warnings)1 CommonUtil (org.batfish.common.util.CommonUtil)1 BooleanExpr (org.batfish.datamodel.routing_policy.expr.BooleanExpr)1 ConjunctionChain (org.batfish.datamodel.routing_policy.expr.ConjunctionChain)1