@Override public AnswerElement answer() { SpecifiersQuestion question = (SpecifiersQuestion) _question; SpecifierContext context = _batfish.specifierContext(); switch (question.getQueryType()) { case FILTER: return resolveFilter(question, context); case INTERFACE: return resolveInterface(question, context); case IP_SPACE: return resolveIpSpace(question, context); case IP_SPACE_OF_LOCATION: return resolveIpSpaceOfLocation(question, context); case LOCATION: return resolveLocation(question, context); case NODE: return resolveNode(question, context); default: throw new IllegalArgumentException("Unhandled query type: " + question.getQueryType()); } }
@Override protected Answerer createAnswerer(Question question, IBatfish batfish) { return new SpecifiersAnswerer(question, batfish); }
@Test public void resolveIpSpaceOfLocationTest() { SpecifiersQuestion questionWithLocation = new SpecifiersQuestion(QueryType.IP_SPACE_OF_LOCATION); questionWithLocation.setLocationSpecifierInput(_c1.getHostname()); // only c1:iface1 should be present assertThat( resolveIpSpaceOfLocation(questionWithLocation, _context).getRows().getData(), equalTo( ImmutableMultiset.of( Row.of( COL_LOCATIONS, ImmutableSet.of(new InterfaceLocation(_c1.getHostname(), _iface1.getName())) .toString(), COL_IP_SPACE, _iface1.getAddress().getIp().toIpSpace().toString())))); }
@Test public void resolveFilterTest() { SpecifiersQuestion question = new SpecifiersQuestion(QueryType.FILTER); question.setFilterSpecifierInput(_filter1.getName()); // only filter1 should be present (with a node specifier all nodes are included) assertThat( resolveFilter(question, _context).getRows().getData(), equalTo( ImmutableMultiset.of( Row.of( COL_NODE, new Node(_c1.getHostname()), COL_FILTER_NAME, _filter1.getName())))); // nothing should match since the node specifier does not match anything question.setNodeSpecifierInput("foofoo"); assertThat( resolveFilter(question, _context).getRows().getData(), equalTo(ImmutableMultiset.of())); }
@Test public void resolveInterfaceTest() { SpecifiersQuestion question = new SpecifiersQuestion(QueryType.INTERFACE); question.setInterfaceSpecifierInput(_iface1.getName()); // only iface1 should be present (with a node specifier all nodes are included) assertThat( resolveInterface(question, _context).getRows().getData(), equalTo( ImmutableMultiset.of( Row.of( COL_INTERFACE, new NodeInterfacePair(_c1.getHostname(), _iface1.getName()))))); // nothing should match since the node specifier does not match anything question.setNodeSpecifierInput("foofoo"); assertThat( resolveInterface(question, _context).getRows().getData(), equalTo(ImmutableMultiset.of())); }
@Test public void resolveNodeTest() { SpecifiersQuestion question = new SpecifiersQuestion(QueryType.NODE); question.setNodeSpecifierInput(_c1.getHostname()); // only c1 should be present assertThat( resolveNode(question, _context).getRows().getData(), equalTo(ImmutableMultiset.of(Row.of(COL_NODE, new Node(_c1.getHostname()))))); } }
@Test public void resolveLocationTest() { SpecifiersQuestion question = new SpecifiersQuestion(QueryType.LOCATION); question.setLocationSpecifierInput(_c1.getHostname()); // only c1:iface1 should be present assertThat( resolveLocation(question, _context).getRows().getData(), equalTo( ImmutableMultiset.of( Row.of( COL_LOCATION, new InterfaceLocation(_c1.getHostname(), _iface1.getName()).toString())))); }
@Test public void resolveIpSpaceTest() { String prefix = "3.3.3.3/24"; SpecifiersQuestion questionWithIp = new SpecifiersQuestion(QueryType.LOCATION); questionWithIp.setIpSpaceSpecifierInput(prefix); // both interfacelocations should be mapped to 3.3.3.3/24 assertThat( resolveIpSpace(questionWithIp, _context).getRows().getData(), equalTo( ImmutableMultiset.of( Row.of( COL_IP_SPACE, IpWildcardSetIpSpace.builder() .including(new IpWildcard(prefix)) .build() .toString())))); }
@Test public void resolveIpSpaceOfLocationTestDefault() { SpecifiersQuestion question = new SpecifiersQuestion(QueryType.IP_SPACE); assertThat( resolveIpSpaceOfLocation(question, _context).getRows().getData(), equalTo( ImmutableMultiset.of( Row.of( COL_LOCATIONS, ImmutableSet.of(new InterfaceLocation(_c1.getHostname(), _iface1.getName())) .toString(), COL_IP_SPACE, _iface1.getAddress().getIp().toIpSpace().toString()), Row.of( COL_LOCATIONS, ImmutableSet.of(new InterfaceLocation(_c2.getHostname(), _iface2.getName())) .toString(), COL_IP_SPACE, _iface2.getAddress().getIp().toIpSpace().toString())))); }