@Test public void testGetActionsToReport() { Ip ip1 = Ip.parse("1.1.1.1"); Prefix prefix1 = Prefix.create(ip1, 24); Set<IpProtocol> protocols1 = ImmutableSet.of(IpProtocol.TCP); HeaderSpace headerSpace1 = HeaderSpace.builder().setDstIps(prefix1.toIpSpace()).setIpProtocols(protocols1).build(); Ip ip2 = Ip.parse("2.2.2.2"); Prefix prefix2 = Prefix.create(ip2, 24); Set<IpProtocol> protocols2 = ImmutableSet.of(IpProtocol.UDP); HeaderSpace headerSpace2 = HeaderSpace.builder().setDstIps(prefix2.toIpSpace()).setIpProtocols(protocols2).build(); List<AclLine> aclLines = ImmutableList.of( acceptingHeaderSpace(headerSpace1), ExprAclLine.rejectingHeaderSpace(headerSpace2)); BDDPacket pkt = new BDDPacket(); IpAccessListToBdd bddConverter = new IpAccessListToBddImpl( pkt, BDDSourceManager.empty(pkt), ImmutableMap.of(), ImmutableMap.of()); HeaderSpaceToBDD hsConverter = new HeaderSpaceToBDD(pkt, ImmutableMap.of()); { HeaderSpace headerSpace = HeaderSpace.builder() .setDstIps(Prefix.create(ip1, prefix1.getPrefixLength() - 8).toIpSpace()) .build(); Map<Integer, LineBehavior> reportedActions = getBehaviorToReport(aclLines, hsConverter.toBDD(headerSpace), bddConverter, null); assertThat(reportedActions, equalTo(ImmutableMap.of(0, LineBehavior.PERMIT))); } { HeaderSpace headerSpace = HeaderSpace.builder() .setDstIps(Prefix.create(ip1, prefix1.getPrefixLength() + 4).toIpSpace()) .build(); Map<Integer, LineBehavior> reportedActions = getBehaviorToReport(aclLines, hsConverter.toBDD(headerSpace), bddConverter, null); assertThat(reportedActions, equalTo(ImmutableMap.of(0, LineBehavior.PERMIT))); } { HeaderSpace headerSpace = HeaderSpace.builder().setIpProtocols(ImmutableSet.of(IpProtocol.TCP)).build(); Map<Integer, LineBehavior> reportedActions = getBehaviorToReport(aclLines, hsConverter.toBDD(headerSpace), bddConverter, null); assertThat(reportedActions, equalTo(ImmutableMap.of(0, LineBehavior.PERMIT))); } { HeaderSpace headerSpace = HeaderSpace.builder().build(); Map<Integer, LineBehavior> reportedActions = getBehaviorToReport(aclLines, hsConverter.toBDD(headerSpace), bddConverter, Action.DENY); assertThat(reportedActions, equalTo(ImmutableMap.of(1, LineBehavior.DENY))); } { HeaderSpace headerSpace = HeaderSpace.builder().setDstIps(Prefix.parse("3.3.3.0/24").toIpSpace()).build(); Map<Integer, LineBehavior> reportedActions = getBehaviorToReport(aclLines, hsConverter.toBDD(headerSpace), bddConverter, null); assertThat(reportedActions, anEmptyMap()); } { HeaderSpace headerSpace = HeaderSpace.builder().setSrcIps(Prefix.parse("3.3.3.0/24").toIpSpace()).build(); Map<Integer, LineBehavior> reportedActions = getBehaviorToReport(aclLines, hsConverter.toBDD(headerSpace), bddConverter, null); assertThat( reportedActions, equalTo(ImmutableMap.of(0, LineBehavior.PERMIT, 1, LineBehavior.DENY))); } }
@VisibleForTesting static SortedMap<Integer, LineBehavior> getBehaviorToReport( List<AclLine> aclLines, BDD headerSpaceBdd, IpAccessListToBdd bddConverter, @Nullable Action action) { LineBehaviorFinder behaviorFinder = new LineBehaviorFinder(bddConverter, action, headerSpaceBdd); ImmutableSortedMap.Builder<Integer, LineBehavior> actionsToReport = ImmutableSortedMap.naturalOrder(); for (int i = 0; i < aclLines.size(); i++) { LineBehavior lineBehavior = behaviorFinder.visit(aclLines.get(i)); if (lineBehavior != null) { actionsToReport.put(i, lineBehavior); } } return actionsToReport.build(); }
FindMatchingFilterLinesAnswerer extends Answerer { @VisibleForTesting static SortedMap<Integer, LineBehavior> getBehaviorToReport( List<AclLine> aclLines, BDD headerSpaceBdd, IpAccessListToBdd bddConverter, @Nullable Action action) { LineBehaviorFinder behaviorFinder = new LineBehaviorFinder(bddConverter, action, headerSpaceBdd); ImmutableSortedMap.Builder<Integer, LineBehavior> actionsToReport = ImmutableSortedMap.naturalOrder(); for (int i = 0; i < aclLines.size(); i++) { LineBehavior lineBehavior = behaviorFinder.visit(aclLines.get(i)); if (lineBehavior != null) { actionsToReport.put(i, lineBehavior); } } return actionsToReport.build(); } }
FindMatchingFilterLinesAnswerer extends Answerer { @VisibleForTesting static SortedMap<Integer, LineBehavior> getBehaviorToReport( List<AclLine> aclLines, BDD headerSpaceBdd, IpAccessListToBdd bddConverter, @Nullable Action action) { LineBehaviorFinder behaviorFinder = new LineBehaviorFinder(bddConverter, action, headerSpaceBdd); ImmutableSortedMap.Builder<Integer, LineBehavior> actionsToReport = ImmutableSortedMap.naturalOrder(); for (int i = 0; i < aclLines.size(); i++) { LineBehavior lineBehavior = behaviorFinder.visit(aclLines.get(i)); if (lineBehavior != null) { actionsToReport.put(i, lineBehavior); } } return actionsToReport.build(); } FindMatchingFilterLinesAnswerer(Question question, IBatfish batfish); }
FindMatchingFilterLinesAnswerer extends Answerer { @VisibleForTesting static SortedMap<Integer, LineBehavior> getBehaviorToReport( List<AclLine> aclLines, BDD headerSpaceBdd, IpAccessListToBdd bddConverter, @Nullable Action action) { LineBehaviorFinder behaviorFinder = new LineBehaviorFinder(bddConverter, action, headerSpaceBdd); ImmutableSortedMap.Builder<Integer, LineBehavior> actionsToReport = ImmutableSortedMap.naturalOrder(); for (int i = 0; i < aclLines.size(); i++) { LineBehavior lineBehavior = behaviorFinder.visit(aclLines.get(i)); if (lineBehavior != null) { actionsToReport.put(i, lineBehavior); } } return actionsToReport.build(); } FindMatchingFilterLinesAnswerer(Question question, IBatfish batfish); @Override TableAnswerElement answer(NetworkSnapshot snapshot); }
FindMatchingFilterLinesAnswerer extends Answerer { @VisibleForTesting static SortedMap<Integer, LineBehavior> getBehaviorToReport( List<AclLine> aclLines, BDD headerSpaceBdd, IpAccessListToBdd bddConverter, @Nullable Action action) { LineBehaviorFinder behaviorFinder = new LineBehaviorFinder(bddConverter, action, headerSpaceBdd); ImmutableSortedMap.Builder<Integer, LineBehavior> actionsToReport = ImmutableSortedMap.naturalOrder(); for (int i = 0; i < aclLines.size(); i++) { LineBehavior lineBehavior = behaviorFinder.visit(aclLines.get(i)); if (lineBehavior != null) { actionsToReport.put(i, lineBehavior); } } return actionsToReport.build(); } FindMatchingFilterLinesAnswerer(Question question, IBatfish batfish); @Override TableAnswerElement answer(NetworkSnapshot snapshot); static final String COL_NODE; static final String COL_FILTER; static final String COL_LINE; static final String COL_LINE_INDEX; static final String COL_ACTION; static final List<ColumnMetadata> COLUMN_METADATA; }