use of org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet in project batfish by batfish.
the class TransferSSA method matchPrefixSet.
/*
* Converts a prefix set to a boolean expression.
*/
private TransferResult<BoolExpr, BoolExpr> matchPrefixSet(Configuration conf, PrefixSetExpr e, SymbolicRoute other) {
ArithExpr otherLen = other.getPrefixLength();
TransferResult<BoolExpr, BoolExpr> result = new TransferResult<>();
if (e instanceof ExplicitPrefixSet) {
ExplicitPrefixSet x = (ExplicitPrefixSet) e;
Set<PrefixRange> ranges = x.getPrefixSpace().getPrefixRanges();
if (ranges.isEmpty()) {
return result.setReturnValue(_enc.mkTrue());
}
// by checking for static/connected/OSPF routes specifically.
if (ranges.size() == 1) {
for (PrefixRange r : ranges) {
int start = r.getLengthRange().getStart();
int end = r.getLengthRange().getEnd();
Prefix pfx = r.getPrefix();
if (start == end && start == pfx.getPrefixLength()) {
String router = _conf.getName();
Set<Prefix> origin = _enc.getOriginatedNetworks().get(router, Protocol.BGP);
if (origin != null && origin.contains(pfx)) {
// Compute static and connected routes
Set<Prefix> ostatic = _enc.getOriginatedNetworks().get(router, Protocol.STATIC);
Set<Prefix> oconn = _enc.getOriginatedNetworks().get(router, Protocol.CONNECTED);
boolean hasStatic = ostatic != null && ostatic.contains(pfx);
boolean hasConnected = oconn != null && oconn.contains(pfx);
ArithExpr originLength = _enc.mkInt(pfx.getPrefixLength());
if (hasStatic || hasConnected) {
BoolExpr directRoute = _enc.isRelevantFor(originLength, r);
ArithExpr newLength = _enc.mkIf(directRoute, originLength, otherLen);
result = result.addChangedVariable("PREFIX-LEN", newLength);
return result.setReturnValue(directRoute);
} else {
// Also use network statement if OSPF has a route with the correct length
SymbolicRoute rec = _enc.getBestNeighborPerProtocol(router, Protocol.OSPF);
if (rec != null) {
BoolExpr ospfRelevant = _enc.isRelevantFor(rec.getPrefixLength(), r);
ArithExpr newLength = _enc.mkIf(ospfRelevant, originLength, otherLen);
result = result.addChangedVariable("PREFIX-LEN", newLength);
return result.setReturnValue(ospfRelevant);
}
}
}
}
}
}
// Compute if the other best route is relevant for this match statement
BoolExpr acc = _enc.mkFalse();
for (PrefixRange range : ranges) {
acc = _enc.mkOr(acc, _enc.isRelevantFor(otherLen, range));
}
return result.setReturnValue(acc);
} else if (e instanceof NamedPrefixSet) {
NamedPrefixSet x = (NamedPrefixSet) e;
String name = x.getName();
RouteFilterList fl = conf.getRouteFilterLists().get(name);
return result.setReturnValue(matchFilterList(fl, other));
} else {
throw new BatfishException("TODO: match prefix set: " + e);
}
}
use of org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet 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