use of org.batfish.datamodel.AclIpSpaceLine in project batfish by batfish.
the class IpSpaceSimplifier method visitAclIpSpace.
@Override
public IpSpace visitAclIpSpace(AclIpSpace aclIpSpace) {
/*
* To simplify an AclIpSpace: 1) Simplify the IpSpace of each line. 2) Remove EmptyIpSpace
* lines. 3) Remove all lines after the first UniverseIpSpace line - More generally, we could
* remove all lines whose spaces are covered by a previous line, but this is not implemented.
* It's also probably too expensive to implement a complete IpSpace subset operation, so we'll
* stick to the easy and most important case.
*/
List<AclIpSpaceLine> simplifiedLines = new ArrayList<>();
for (AclIpSpaceLine line : aclIpSpace.getLines()) {
IpSpace simplifiedLineIpSpace = line.getIpSpace().accept(this);
if (simplifiedLineIpSpace == EmptyIpSpace.INSTANCE) {
continue;
}
AclIpSpaceLine simplifiedLine = line.rebuild().setIpSpace(simplifiedLineIpSpace).build();
simplifiedLines.add(simplifiedLine);
if (simplifiedLineIpSpace == UniverseIpSpace.INSTANCE) {
break;
}
}
/*
* If there is only one line, and it accepts, then simplify to the space of that line.
*/
if (simplifiedLines.size() == 1 && simplifiedLines.get(0).getAction() == LineAction.ACCEPT) {
return simplifiedLines.get(0).getIpSpace();
}
/*
* If all lines reject (or there are no lines), simplify to EmptyIpSpace.
*/
if (simplifiedLines.stream().allMatch(line -> line.getAction() == LineAction.REJECT)) {
return EmptyIpSpace.INSTANCE;
}
/*
* If all lines are accepts, and the last accepts UniverseIpSpace, then this can be simplified
* to UniverseIpSpace.
*/
if (simplifiedLines.get(simplifiedLines.size() - 1).getIpSpace() == UniverseIpSpace.INSTANCE && simplifiedLines.stream().allMatch(line -> line.getAction() == LineAction.ACCEPT)) {
return UniverseIpSpace.INSTANCE;
}
return AclIpSpace.builder().setLines(simplifiedLines).build();
}
use of org.batfish.datamodel.AclIpSpaceLine in project batfish by batfish.
the class IpSpaceBooleanExprTransformer method visitAclIpSpace.
@Override
public BooleanExpr visitAclIpSpace(AclIpSpace aclIpSpace) {
// right fold
BooleanExpr expr = FalseExpr.INSTANCE;
for (int i = aclIpSpace.getLines().size() - 1; i >= 0; i--) {
AclIpSpaceLine line = aclIpSpace.getLines().get(i);
expr = new IfThenElse(line.getIpSpace().accept(this), line.getAction() == LineAction.ACCEPT ? TrueExpr.INSTANCE : FalseExpr.INSTANCE, expr);
}
return expr;
}
Aggregations