use of org.batfish.datamodel.routing_policy.expr.NamedPrefixSet 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;
}
Aggregations