use of org.batfish.datamodel.AclIpSpace 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.AclIpSpace in project batfish by batfish.
the class IpSpaceBooleanExprTransformerTest method testVisitAclIpSpace.
@Test
public void testVisitAclIpSpace() {
AclIpSpace ipSpace = AclIpSpace.builder().thenRejecting(UniverseIpSpace.INSTANCE).thenPermitting(EmptyIpSpace.INSTANCE).build();
BooleanExpr expr = ipSpace.accept(SRC_IP_SPACE_BOOLEAN_EXPR_TRANSFORMER);
assertThat(expr, equalTo(new IfThenElse(// Matches UniverseIpSpace
TrueExpr.INSTANCE, // Reject
FalseExpr.INSTANCE, new IfThenElse(// Matches EmptyIpSpace
FalseExpr.INSTANCE, // Accept
TrueExpr.INSTANCE, // Matches nothing so reject
FalseExpr.INSTANCE))));
}
use of org.batfish.datamodel.AclIpSpace in project batfish by batfish.
the class IpSpaceSpecializerTest method testSpecializeAclIpSpace.
@Test
public void testSpecializeAclIpSpace() {
AclIpSpace ipSpace = AclIpSpace.builder().thenPermitting(Prefix.parse("0.0.1.0/24")).thenRejecting(Prefix.parse("0.0.1.4/30")).thenPermitting(Prefix.parse("0.0.1.6/31")).build();
assertThat(trivialSpecializer.visitAclIpSpace(ipSpace), equalTo(ipSpace));
assertThat(whitelistAnySpecializer.visitAclIpSpace(ipSpace), equalTo(ipSpace));
assertThat(blacklistAnySpecializer.visitAclIpSpace(ipSpace), equalTo(AclIpSpace.builder().thenPermitting(EmptyIpSpace.INSTANCE).thenRejecting(EmptyIpSpace.INSTANCE).thenPermitting(EmptyIpSpace.INSTANCE).build()));
// headerspace is contained in all lines
IpSpaceSpecializer specializer = new IpSpaceSpecializer(ImmutableSortedSet.of(new IpWildcard("0.0.1.6/32")), ImmutableSortedSet.of());
assertThat(specializer.visitAclIpSpace(ipSpace), equalTo(AclIpSpace.builder().thenPermitting(UniverseIpSpace.INSTANCE).thenRejecting(UniverseIpSpace.INSTANCE).thenPermitting(UniverseIpSpace.INSTANCE).build()));
// headerspace is outside of all lines
specializer = new IpSpaceSpecializer(ImmutableSortedSet.of(new IpWildcard("1.1.1.1/32")), ImmutableSortedSet.of());
assertThat(specializer.visitAclIpSpace(ipSpace), equalTo(AclIpSpace.builder().thenPermitting(EmptyIpSpace.INSTANCE).thenRejecting(EmptyIpSpace.INSTANCE).thenPermitting(EmptyIpSpace.INSTANCE).build()));
// not contained in any line, and intersects the first only
specializer = new IpSpaceSpecializer(ImmutableSortedSet.of(new IpWildcard(new Ip(0x00000100L), new Ip(0xFF0FFF00L))), ImmutableSortedSet.of());
assertThat(specializer.visitAclIpSpace(ipSpace), equalTo(AclIpSpace.builder().thenPermitting(Prefix.parse("0.0.1.0/24")).thenRejecting(EmptyIpSpace.INSTANCE).thenPermitting(EmptyIpSpace.INSTANCE).build()));
}
Aggregations