use of org.batfish.datamodel.routing_policy.expr.Disjunction in project batfish by batfish.
the class TransferSSA method compute.
/*
* Convert a Batfish AST boolean expression to a symbolic Z3 boolean expression
* by performing inlining of stateful side effects.
*/
private TransferResult<BoolExpr, BoolExpr> compute(BooleanExpr expr, TransferParam<SymbolicRoute> p) {
// TODO: right now everything is IPV4
if (expr instanceof MatchIpv4) {
p.debug("MatchIpv4");
return fromExpr(_enc.mkTrue());
}
if (expr instanceof MatchIpv6) {
p.debug("MatchIpv6");
return fromExpr(_enc.mkFalse());
}
if (expr instanceof Conjunction) {
p.debug("Conjunction");
Conjunction c = (Conjunction) expr;
BoolExpr acc = _enc.mkTrue();
TransferResult<BoolExpr, BoolExpr> result = new TransferResult<>();
for (BooleanExpr be : c.getConjuncts()) {
TransferResult<BoolExpr, BoolExpr> r = compute(be, p.indent());
result = result.addChangedVariables(r);
acc = _enc.mkAnd(acc, r.getReturnValue());
}
p.debug("has changed variable");
return result.setReturnValue(acc);
}
if (expr instanceof Disjunction) {
p.debug("Disjunction");
Disjunction d = (Disjunction) expr;
BoolExpr acc = _enc.mkFalse();
TransferResult<BoolExpr, BoolExpr> result = new TransferResult<>();
for (BooleanExpr be : d.getDisjuncts()) {
TransferResult<BoolExpr, BoolExpr> r = compute(be, p.indent());
result = result.addChangedVariables(r);
acc = _enc.mkOr(acc, r.getReturnValue());
}
p.debug("has changed variable");
return result.setReturnValue(acc);
}
if (expr instanceof ConjunctionChain) {
p.debug("ConjunctionChain");
ConjunctionChain d = (ConjunctionChain) expr;
List<BooleanExpr> conjuncts = new ArrayList<>(d.getSubroutines());
if (p.getDefaultPolicy() != null) {
BooleanExpr be = new CallExpr(p.getDefaultPolicy().getDefaultPolicy());
conjuncts.add(be);
}
if (conjuncts.size() == 0) {
return fromExpr(_enc.mkTrue());
} else {
TransferResult<BoolExpr, BoolExpr> result = new TransferResult<>();
BoolExpr acc = _enc.mkFalse();
for (int i = conjuncts.size() - 1; i >= 0; i--) {
BooleanExpr conjunct = conjuncts.get(i);
TransferParam<SymbolicRoute> param = p.setDefaultPolicy(null).setChainContext(TransferParam.ChainContext.CONJUNCTION);
TransferResult<BoolExpr, BoolExpr> r = compute(conjunct, param);
result = result.addChangedVariables(r);
acc = _enc.mkIf(r.getFallthroughValue(), acc, r.getReturnValue());
}
p.debug("ConjunctionChain Result: " + acc);
return result.setReturnValue(acc);
}
}
if (expr instanceof DisjunctionChain) {
p.debug("DisjunctionChain");
DisjunctionChain d = (DisjunctionChain) expr;
List<BooleanExpr> disjuncts = new ArrayList<>(d.getSubroutines());
if (p.getDefaultPolicy() != null) {
BooleanExpr be = new CallExpr(p.getDefaultPolicy().getDefaultPolicy());
disjuncts.add(be);
}
if (disjuncts.size() == 0) {
return fromExpr(_enc.mkTrue());
} else {
TransferResult<BoolExpr, BoolExpr> result = new TransferResult<>();
BoolExpr acc = _enc.mkFalse();
for (int i = disjuncts.size() - 1; i >= 0; i--) {
BooleanExpr disjunct = disjuncts.get(i);
TransferParam<SymbolicRoute> param = p.setDefaultPolicy(null).setChainContext(TransferParam.ChainContext.CONJUNCTION);
TransferResult<BoolExpr, BoolExpr> r = compute(disjunct, param);
result.addChangedVariables(r);
acc = _enc.mkIf(r.getFallthroughValue(), acc, r.getReturnValue());
}
p.debug("DisjunctionChain Result: " + acc);
return result.setReturnValue(acc);
}
}
if (expr instanceof Not) {
p.debug("mkNot");
Not n = (Not) expr;
TransferResult<BoolExpr, BoolExpr> result = compute(n.getExpr(), p);
return result.setReturnValue(_enc.mkNot(result.getReturnValue()));
}
if (expr instanceof MatchProtocol) {
MatchProtocol mp = (MatchProtocol) expr;
Protocol proto = Protocol.fromRoutingProtocol(mp.getProtocol());
if (proto == null) {
p.debug("MatchProtocol(" + mp.getProtocol().protocolName() + "): false");
return fromExpr(_enc.mkFalse());
}
if (_other.getProtocolHistory() == null) {
BoolExpr protoMatch = _enc.mkBool(proto.equals(_proto));
p.debug("MatchProtocol(" + mp.getProtocol().protocolName() + "): " + protoMatch);
return fromExpr(protoMatch);
}
BoolExpr protoMatch = _other.getProtocolHistory().checkIfValue(proto);
p.debug("MatchProtocol(" + mp.getProtocol().protocolName() + "): " + protoMatch);
return fromExpr(protoMatch);
}
if (expr instanceof MatchPrefixSet) {
p.debug("MatchPrefixSet");
MatchPrefixSet m = (MatchPrefixSet) expr;
// For BGP, may change prefix length
TransferResult<BoolExpr, BoolExpr> result = matchPrefixSet(_conf, m.getPrefixSet(), p.getData());
return result.setReturnAssignedValue(_enc.mkTrue());
// TODO: implement me
} else if (expr instanceof MatchPrefix6Set) {
p.debug("MatchPrefix6Set");
return fromExpr(_enc.mkFalse());
} else if (expr instanceof CallExpr) {
p.debug("CallExpr");
// TODO: the call can modify certain fields, need to keep track of these variables
CallExpr c = (CallExpr) expr;
String name = c.getCalledPolicyName();
RoutingPolicy pol = _conf.getRoutingPolicies().get(name);
p = p.setCallContext(TransferParam.CallContext.EXPR_CALL);
TransferResult<BoolExpr, BoolExpr> r = compute(pol.getStatements(), p.indent().enterScope(name), initialResult());
p.debug("CallExpr (return): " + r.getReturnValue());
p.debug("CallExpr (fallthrough): " + r.getFallthroughValue());
return r;
} else if (expr instanceof WithEnvironmentExpr) {
p.debug("WithEnvironmentExpr");
// TODO: this is not correct
WithEnvironmentExpr we = (WithEnvironmentExpr) expr;
// TODO: postStatements() and preStatements()
return compute(we.getExpr(), p);
} else if (expr instanceof MatchCommunitySet) {
p.debug("MatchCommunitySet");
MatchCommunitySet mcs = (MatchCommunitySet) expr;
return fromExpr(matchCommunitySet(_conf, mcs.getExpr(), p.getData()));
} else if (expr instanceof BooleanExprs.StaticBooleanExpr) {
BooleanExprs.StaticBooleanExpr b = (BooleanExprs.StaticBooleanExpr) expr;
switch(b.getType()) {
case CallExprContext:
p.debug("CallExprContext");
return fromExpr(_enc.mkBool(p.getCallContext() == TransferParam.CallContext.EXPR_CALL));
case CallStatementContext:
p.debug("CallStmtContext");
return fromExpr(_enc.mkBool(p.getCallContext() == TransferParam.CallContext.STMT_CALL));
case True:
p.debug("True");
return fromExpr(_enc.mkTrue());
case False:
p.debug("False");
return fromExpr(_enc.mkFalse());
default:
throw new BatfishException("Unhandled " + BooleanExprs.class.getCanonicalName() + ": " + b.getType());
}
} else if (expr instanceof MatchAsPath) {
p.debug("MatchAsPath");
System.out.println("Warning: use of unimplemented feature MatchAsPath");
return fromExpr(_enc.mkFalse());
}
String s = (_isExport ? "export" : "import");
String msg = String.format("Unimplemented feature %s for %s transfer function on interface %s", expr.toString(), s, _graphEdge.toString());
throw new BatfishException(msg);
}
use of org.batfish.datamodel.routing_policy.expr.Disjunction in project batfish by batfish.
the class AstVisitor method visit.
/*
* Walk starting from an AST boolean expression
*/
public void visit(Configuration conf, BooleanExpr e, Consumer<Statement> fs, Consumer<BooleanExpr> fe) {
fe.accept(e);
if (e instanceof Conjunction) {
Conjunction c = (Conjunction) e;
for (BooleanExpr be : c.getConjuncts()) {
visit(conf, be, fs, fe);
}
} else if (e instanceof Disjunction) {
Disjunction d = (Disjunction) e;
for (BooleanExpr be : d.getDisjuncts()) {
visit(conf, be, fs, fe);
}
} else if (e instanceof ConjunctionChain) {
ConjunctionChain c = (ConjunctionChain) e;
for (BooleanExpr be : c.getSubroutines()) {
visit(conf, be, fs, fe);
}
} else if (e instanceof DisjunctionChain) {
DisjunctionChain d = (DisjunctionChain) e;
for (BooleanExpr be : d.getSubroutines()) {
visit(conf, be, fs, fe);
}
} else if (e instanceof Not) {
Not n = (Not) e;
visit(conf, n.getExpr(), fs, fe);
} else if (e instanceof CallExpr) {
CallExpr c = (CallExpr) e;
RoutingPolicy rp = conf.getRoutingPolicies().get(c.getCalledPolicyName());
visit(conf, rp.getStatements(), fs, fe);
}
}
use of org.batfish.datamodel.routing_policy.expr.Disjunction 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.Disjunction in project batfish by batfish.
the class RouteReflectionTest method setup.
/**
* Initialize builders with values common to all tests
*/
@Before
public void setup() {
_ab = new BgpAdvertisement.Builder().setClusterList(ImmutableSortedSet.of()).setCommunities(ImmutableSortedSet.of()).setDstVrf(Configuration.DEFAULT_VRF_NAME).setOriginType(OriginType.INCOMPLETE).setSrcProtocol(RoutingProtocol.AGGREGATE).setSrcVrf(Configuration.DEFAULT_VRF_NAME).setType(BgpAdvertisementType.EBGP_SENT);
_nf = new NetworkFactory();
_cb = _nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS);
_ib = _nf.interfaceBuilder().setActive(true);
_nb = _nf.bgpNeighborBuilder().setLocalAs(2);
_pb = _nf.bgpProcessBuilder();
_vb = _nf.vrfBuilder().setName(Configuration.DEFAULT_VRF_NAME);
If acceptIffBgp = new If();
Disjunction guard = new Disjunction();
guard.setDisjuncts(ImmutableList.of(new MatchProtocol(RoutingProtocol.BGP), new MatchProtocol(RoutingProtocol.IBGP)));
acceptIffBgp.setGuard(guard);
acceptIffBgp.setTrueStatements(ImmutableList.of(Statements.ExitAccept.toStaticStatement()));
acceptIffBgp.setFalseStatements(ImmutableList.of(Statements.ExitReject.toStaticStatement()));
/* Builder that creates default BGP export policy */
_defaultExportPolicyBuilder = _nf.routingPolicyBuilder().setStatements(ImmutableList.of(acceptIffBgp));
/* Builder that creates BGP export policy that rejects everything */
_nullExportPolicyBuilder = _nf.routingPolicyBuilder().setStatements(ImmutableList.of(Statements.ExitReject.toStaticStatement()));
}
Aggregations