Search in sources :

Example 1 with Policy_expressionContext

use of org.batfish.grammar.flatjuniper.FlatJuniperParser.Policy_expressionContext in project batfish by batfish.

the class ConfigurationBuilder method toComplexPolicyStatement.

private String toComplexPolicyStatement(Policy_expressionContext expr) {
    if (expr.pe_nested() != null) {
        return toComplexPolicyStatement(expr.pe_nested().policy_expression());
    } else if (expr.variable() != null) {
        String name = expr.variable().getText();
        return name;
    } else if (expr.pe_conjunction() != null) {
        Set<String> conjuncts = new LinkedHashSet<>();
        for (Policy_expressionContext conjunctCtx : expr.pe_conjunction().policy_expression()) {
            String conjunctName = toComplexPolicyStatement(conjunctCtx);
            conjuncts.add(conjunctName);
        }
        String conjunctionPolicyName = "~CONJUNCTION_POLICY_" + _conjunctionPolicyIndex + "~";
        _conjunctionPolicyIndex++;
        PolicyStatement conjunctionPolicy = new PolicyStatement(conjunctionPolicyName, -1);
        PsTerm conjunctionPolicyTerm = conjunctionPolicy.getDefaultTerm();
        PsFromPolicyStatementConjunction from = new PsFromPolicyStatementConjunction(conjuncts);
        conjunctionPolicyTerm.getFroms().add(from);
        conjunctionPolicyTerm.getThens().add(PsThenAccept.INSTANCE);
        _configuration.getPolicyStatements().put(conjunctionPolicyName, conjunctionPolicy);
        return conjunctionPolicyName;
    } else if (expr.pe_disjunction() != null) {
        Set<String> disjuncts = new LinkedHashSet<>();
        for (Policy_expressionContext disjunctCtx : expr.pe_disjunction().policy_expression()) {
            String disjunctName = toComplexPolicyStatement(disjunctCtx);
            disjuncts.add(disjunctName);
        }
        String disjunctionPolicyName = "~DISJUNCTION_POLICY_" + _disjunctionPolicyIndex + "~";
        _disjunctionPolicyIndex++;
        PolicyStatement disjunctionPolicy = new PolicyStatement(disjunctionPolicyName, -1);
        PsTerm disjunctionPolicyTerm = disjunctionPolicy.getDefaultTerm();
        for (String disjunct : disjuncts) {
            PsFromPolicyStatement from = new PsFromPolicyStatement(disjunct);
            disjunctionPolicyTerm.getFroms().add(from);
        }
        disjunctionPolicyTerm.getThens().add(PsThenAccept.INSTANCE);
        _configuration.getPolicyStatements().put(disjunctionPolicyName, disjunctionPolicy);
        return disjunctionPolicyName;
    } else {
        throw new BatfishException("Invalid policy expression");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PsTerm(org.batfish.representation.juniper.PsTerm) PsFromPolicyStatement(org.batfish.representation.juniper.PsFromPolicyStatement) BatfishException(org.batfish.common.BatfishException) PsFromPolicyStatementConjunction(org.batfish.representation.juniper.PsFromPolicyStatementConjunction) PsThenCommunitySet(org.batfish.representation.juniper.PsThenCommunitySet) SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) Set(java.util.Set) PolicyStatement(org.batfish.representation.juniper.PolicyStatement) PsFromPolicyStatement(org.batfish.representation.juniper.PsFromPolicyStatement) Policy_expressionContext(org.batfish.grammar.flatjuniper.FlatJuniperParser.Policy_expressionContext)

Example 2 with Policy_expressionContext

use of org.batfish.grammar.flatjuniper.FlatJuniperParser.Policy_expressionContext in project batfish by batfish.

the class ConfigurationBuilder method exitB_export.

@Override
public void exitB_export(B_exportContext ctx) {
    Policy_expressionContext expr = ctx.expr;
    String name;
    int line;
    if (expr.variable() != null) {
        name = expr.variable().getText();
        line = expr.variable().getStart().getLine();
    } else {
        name = toComplexPolicyStatement(expr);
        line = expr.getStart().getLine();
    }
    _currentBgpGroup.getExportPolicies().put(name, line);
}
Also used : Policy_expressionContext(org.batfish.grammar.flatjuniper.FlatJuniperParser.Policy_expressionContext)

Example 3 with Policy_expressionContext

use of org.batfish.grammar.flatjuniper.FlatJuniperParser.Policy_expressionContext in project batfish by batfish.

the class ConfigurationBuilder method exitB_import.

@Override
public void exitB_import(B_importContext ctx) {
    Policy_expressionContext expr = ctx.expr;
    String name;
    int line;
    if (expr.variable() != null) {
        name = expr.variable().getText();
        line = expr.variable().getStart().getLine();
    } else {
        name = toComplexPolicyStatement(expr);
        line = expr.getStart().getLine();
    }
    _currentBgpGroup.getImportPolicies().put(name, line);
}
Also used : Policy_expressionContext(org.batfish.grammar.flatjuniper.FlatJuniperParser.Policy_expressionContext)

Aggregations

Policy_expressionContext (org.batfish.grammar.flatjuniper.FlatJuniperParser.Policy_expressionContext)3 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 SortedSet (java.util.SortedSet)1 TreeSet (java.util.TreeSet)1 BatfishException (org.batfish.common.BatfishException)1 PolicyStatement (org.batfish.representation.juniper.PolicyStatement)1 PsFromPolicyStatement (org.batfish.representation.juniper.PsFromPolicyStatement)1 PsFromPolicyStatementConjunction (org.batfish.representation.juniper.PsFromPolicyStatementConjunction)1 PsTerm (org.batfish.representation.juniper.PsTerm)1 PsThenCommunitySet (org.batfish.representation.juniper.PsThenCommunitySet)1