private Optional<IpAccessList> makeQueryAcl(IpAccessList originalAcl) { SearchFiltersQuestion question = (SearchFiltersQuestion) _question; switch (question.getType()) { case PERMIT: return Optional.of(originalAcl); case DENY: return Optional.of(toDenyAcl(originalAcl)); case MATCH_LINE: // for each ACL, construct a new ACL that accepts if and only if the specified line matches Integer lineNumber = question.getLineNumber(); checkState(lineNumber != null, "Cannot perform a match line query without a line number"); return originalAcl.getLines().size() > lineNumber ? Optional.of(toMatchLineAcl(lineNumber, originalAcl)) : Optional.empty(); default: throw new BatfishException("Unexpected query Type: " + question.getType()); } }
@Test public void testSetQuery() { SearchFiltersQuestion question = new SearchFiltersQuestion(); assertThat(question.getType(), is(Type.PERMIT)); assertThat(question.getLineNumber(), nullValue()); question = SearchFiltersQuestion.builder().setAction("deny").build(); assertThat(question.getType(), is(Type.DENY)); assertThat(question.getLineNumber(), nullValue()); question = SearchFiltersQuestion.builder().setAction("matchLine 5").build(); assertThat(question.getType(), is(Type.MATCH_LINE)); assertThat(question.getLineNumber(), is(5)); question = SearchFiltersQuestion.builder().setAction("permit").build(); assertThat(question.getType(), is(Type.PERMIT)); assertThat(question.getLineNumber(), nullValue()); exception.expect(BatfishException.class); exception.expectMessage("Unrecognized query: foo"); SearchFiltersQuestion.builder().setAction("foo").build(); }
@Test public void testDeserializationDefaultValues() throws IOException { String serialized = String.format("{\"class\":\"%s\"}", SearchFiltersQuestion.class.getCanonicalName()); SearchFiltersQuestion q = BatfishObjectMapper.mapper().readValue(serialized, SearchFiltersQuestion.class); assertThat(q.getFilterSpecifier(), notNullValue()); assertThat(q.getType(), is(Type.PERMIT)); assertThat(q.getNodesSpecifier(), notNullValue()); assertThat(q.getDataPlane(), equalTo(false)); assertThat(q.getNodes(), nullValue()); // src/dst IPs NOT stored in headerspace at this stage assertThat(q.getHeaderSpace().getDstIps(), nullValue()); assertThat(q.getHeaderSpace().getSrcIps(), nullValue()); // src/dst IPs are in specifiers at this stage SearchFiltersParameters parameters = q.toSearchFiltersParameters(); for (IpSpaceSpecifier s : Arrays.asList( parameters.getSourceIpSpaceSpecifier(), parameters.getDestinationIpSpaceSpecifier())) { assertThat( s.resolve(ImmutableSet.of(), MockSpecifierContext.builder().build()).getEntries().stream() .map(Entry::getIpSpace) .collect(ImmutableList.toImmutableList()), hasItem(UniverseIpSpace.INSTANCE)); assertThat(q.getLineNumber(), nullValue()); } }