public static AclLineMatchExprWithProvenance<AclLineMatchExpr> explainLiterals( Iterable<AclLineMatchExpr> conjuncts) { AclExplanation explanation = new AclExplanation(); conjuncts.forEach(explanation._visitor::visit); return explanation.build(); }
@Override public Void visitMatchHeaderSpace(MatchHeaderSpace matchHeaderSpace) { requireHeaderSpace(matchHeaderSpace); return null; }
@Override public Void visitOriginatingFromDevice(OriginatingFromDevice originatingFromDevice) { requireOriginatingFromDevice(originatingFromDevice); return null; }
@Override public Void visitMatchSrcInterface(MatchSrcInterface matchSrcInterface) { requireSourceInterfaces(matchSrcInterface); return null; }
/** A factor is either a literal or a conjunction of literals. */ static AclLineMatchExprWithProvenance<AclLineMatchExpr> explainFactor(AclLineMatchExpr factor) { if (factor instanceof AndMatchExpr) { return explainLiterals(((AndMatchExpr) factor).getConjuncts()); } return explainLiterals(ImmutableList.of(factor)); }
@Override public Void visitNotMatchExpr(NotMatchExpr notMatchExpr) { AclLineMatchExpr operand = notMatchExpr.getOperand(); if (operand instanceof MatchHeaderSpace) { forbidHeaderSpace((MatchHeaderSpace) operand); return null; } throw new IllegalArgumentException("Can only explain AclLineMatchExpr literals."); }
private static AclLineMatchExprWithProvenance<IpAccessListLineIndex> explainWithProvenance( IpAccessListToBDD ipAccessListToBDD, IpAccessList acl, Map<String, IpAccessList> namedAcls, IdentityHashMap<AclLineMatchExpr, IpAccessListLineIndex> literalsToLines) { // Convert acl to a single expression. AclLineMatchExpr aclExpr = AclToAclLineMatchExpr.toAclLineMatchExpr(ipAccessListToBDD, acl, namedAcls); // Reduce that expression to normal form. AclLineMatchExpr aclExprNf = AclLineMatchExprNormalizer.normalize(ipAccessListToBDD, aclExpr); // Simplify the normal form AclLineMatchExprWithProvenance<AclLineMatchExpr> aclExprNfExplained = AclExplanation.explainNormalForm(aclExprNf); // join the provenance information from the normal form with the literalsToLines mapping // above to obtain provenance back to the original acl lines IdentityHashMap<AclLineMatchExpr, Set<IpAccessListLineIndex>> conjunctsToLines = new IdentityHashMap<>(); for (Map.Entry<AclLineMatchExpr, Set<AclLineMatchExpr>> entry : aclExprNfExplained.getProvenance().entrySet()) { AclLineMatchExpr conjunct = entry.getKey(); conjunctsToLines.put( conjunct, entry.getValue().stream() .map(literalsToLines::get) .collect(ImmutableSet.toImmutableSet())); } return new AclLineMatchExprWithProvenance<>( aclExprNfExplained.getMatchExpr(), conjunctsToLines); }
public static AclLineMatchExprWithProvenance<AclLineMatchExpr> explainNormalForm( AclLineMatchExpr nf) { /* * A normal form is either a factor or a disjunction of factors. */ if (nf instanceof OrMatchExpr) { /* * Each disjunct is a factor. */ SortedSet<AclLineMatchExprWithProvenance<AclLineMatchExpr>> disjunctsWithProvenance = ((OrMatchExpr) nf) .getDisjuncts().stream() .map(AclExplanation::explainFactor) .collect(ImmutableSortedSet.toImmutableSortedSet(Ordering.natural())); List<AclLineMatchExpr> disjuncts = new LinkedList<>(); IdentityHashMap<AclLineMatchExpr, Set<AclLineMatchExpr>> provenance = new IdentityHashMap<>(); for (AclLineMatchExprWithProvenance<AclLineMatchExpr> matchExprWithProvenance : disjunctsWithProvenance) { disjuncts.add(matchExprWithProvenance.getMatchExpr()); provenance.putAll(matchExprWithProvenance.getProvenance()); } return new AclLineMatchExprWithProvenance<>(new OrMatchExpr(disjuncts), provenance); } return explainFactor(nf); } }