@Override public AclLineMatchExpr visitOrMatchExpr(OrMatchExpr orMatchExpr) { return new AndMatchExpr(negate(orMatchExpr.getDisjuncts())); }
@Override public AclLineMatchExpr visitAndMatchExpr(AndMatchExpr andMatchExpr) { return new OrMatchExpr(negate(andMatchExpr.getConjuncts())); }
@Override public Void visitNotMatchExpr(NotMatchExpr notMatchExpr) { AclLineMatchExpr negatedOperand = Negate.negate(notMatchExpr.getOperand()); if (negatedOperand instanceof NotMatchExpr) { // negated leaf node. rather than recurse, just add to the conjuctsBuilders. addConstraint(notMatchExpr); } else { negatedOperand.accept(this); } return null; }
@Test public void visitTrueExpr() { assertThat(negate(TRUE), equalTo(FALSE)); } }
@Test public void visitFalseExpr() { assertThat(negate(FALSE), equalTo(TRUE)); }
@Test public void visitOriginatingFromDevice() { assertThat(negate(ORIGINATING_FROM_DEVICE), equalTo(not(ORIGINATING_FROM_DEVICE))); }
@Test public void visitPermittedByAcl() { PermittedByAcl permittedByFoo = permittedByAcl("foo"); assertThat(negate(permittedByFoo), equalTo(not(permittedByFoo))); }
@Test public void visitMatchSrcInterface() { MatchSrcInterface matchSrcInterface = AclLineMatchExprs.matchSrcInterface("foo"); assertThat(negate(matchSrcInterface), equalTo(not(matchSrcInterface))); }
@Test public void visitAndMatchExpr() { assertThat( negate(and(ORIGINATING_FROM_DEVICE, TRUE, FALSE)), equalTo(or(not(ORIGINATING_FROM_DEVICE), FALSE, TRUE))); }
@Test public void visitOrMatchExpr() { assertThat( negate(or(ORIGINATING_FROM_DEVICE, TRUE, FALSE)), equalTo(and(not(ORIGINATING_FROM_DEVICE), FALSE, TRUE))); }
@Test public void visitMatchHeaderSpace() { AclLineMatchExpr matchHeaderSpace = match(HeaderSpace.builder().build()); assertThat(negate(matchHeaderSpace), equalTo(not(matchHeaderSpace))); }
@Override public void add(AclLineMatchExpr expr) { /* * If expr is an AndMatchExpr (or can be simplified by one using deMorgan's law, then add each * conjunct separately (so we can detect and remove redundant conjuncts). This could create some * extra work though, so only do this if the conjunction won't be unsat after adding. */ if (_ipAccessListToBDD.visit(expr).and(getBdd()).isZero()) { /* * expr is inconsistent with the other conjuncts. Just add it now. */ addHelper(expr); return; } AclLineMatchExpr e = expr; if (e instanceof NotMatchExpr) { /* * Try to apply deMorgan. */ e = Negate.negate(((NotMatchExpr) e).getOperand()); } if (e instanceof AndMatchExpr) { ((AndMatchExpr) e).getConjuncts().forEach(this::add); } else { addHelper(e); } }
@Override public void add(AclLineMatchExpr expr) { /* * If expr is an OrMatchExpr (or can be simplified by one using deMorgan's law, then add each * disjunct separately (so we can detect and remove redundant disjuncts). This could create some * extra work though, so only do this if the conjunction won't be valid after adding. */ if (_ipAccessListToBDD.visit(expr).or(getBdd()).isOne()) { /* * expr is valid with the other conjuncts. Just add it now. */ addHelper(expr); return; } AclLineMatchExpr e = expr; if (e instanceof NotMatchExpr) { /* * Try to apply deMorgan. */ e = Negate.negate(((NotMatchExpr) e).getOperand()); } if (e instanceof OrMatchExpr) { ((OrMatchExpr) e).getDisjuncts().forEach(this::add); } else { addHelper(e); } } }