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;
}
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;
}
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);
}
Aggregations