Search in sources :

Example 1 with AclIpSpaceLine

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();
}
Also used : AclIpSpaceLine(org.batfish.datamodel.AclIpSpaceLine) AclIpSpaceLine(org.batfish.datamodel.AclIpSpaceLine) Set(java.util.Set) IpSpace(org.batfish.datamodel.IpSpace) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) IpWildcardSetIpSpace(org.batfish.datamodel.IpWildcardSetIpSpace) List(java.util.List) AclIpSpace(org.batfish.datamodel.AclIpSpace) EmptyIpSpace(org.batfish.datamodel.EmptyIpSpace) GenericIpSpaceVisitor(org.batfish.datamodel.visitors.GenericIpSpaceVisitor) LineAction(org.batfish.datamodel.LineAction) UniverseIpSpace(org.batfish.datamodel.UniverseIpSpace) IpWildcard(org.batfish.datamodel.IpWildcard) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) IpSpace(org.batfish.datamodel.IpSpace) IpWildcardSetIpSpace(org.batfish.datamodel.IpWildcardSetIpSpace) AclIpSpace(org.batfish.datamodel.AclIpSpace) EmptyIpSpace(org.batfish.datamodel.EmptyIpSpace) UniverseIpSpace(org.batfish.datamodel.UniverseIpSpace) ArrayList(java.util.ArrayList)

Example 2 with AclIpSpaceLine

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;
}
Also used : AclIpSpaceLine(org.batfish.datamodel.AclIpSpaceLine) IfThenElse(org.batfish.z3.expr.IfThenElse) BooleanExpr(org.batfish.z3.expr.BooleanExpr)

Aggregations

AclIpSpaceLine (org.batfish.datamodel.AclIpSpaceLine)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 AclIpSpace (org.batfish.datamodel.AclIpSpace)1 EmptyIpSpace (org.batfish.datamodel.EmptyIpSpace)1 Ip (org.batfish.datamodel.Ip)1 IpSpace (org.batfish.datamodel.IpSpace)1 IpWildcard (org.batfish.datamodel.IpWildcard)1 IpWildcardSetIpSpace (org.batfish.datamodel.IpWildcardSetIpSpace)1 LineAction (org.batfish.datamodel.LineAction)1 Prefix (org.batfish.datamodel.Prefix)1 UniverseIpSpace (org.batfish.datamodel.UniverseIpSpace)1 GenericIpSpaceVisitor (org.batfish.datamodel.visitors.GenericIpSpaceVisitor)1 BooleanExpr (org.batfish.z3.expr.BooleanExpr)1 IfThenElse (org.batfish.z3.expr.IfThenElse)1