return new ShorthandNodeSpecifier(new NodesSpecifier(trimmedInput));
/** * Prepares for calling the full auto complete function: {@link * NodesSpecifier#autoComplete(String, Set, NodeRolesData)} by extracting the set of nodes and * node roles from batfish pointer. * * @param query The query to auto complete * @param batfish The pointer to batfish * @return A list of {@link AutocompleteSuggestion} objects */ public static List<AutocompleteSuggestion> autoComplete(String query, IBatfish batfish) { return autoComplete(query, batfish.loadConfigurations().keySet(), batfish.getNodeRolesData()); }
@Override public Set<String> resolve(SpecifierContext ctxt) { return _shorthandSpecifier.getMatchingNodes(ctxt); } }
public static LocationSpecifier from(@Nonnull NodesSpecifier ingressNodes) { switch (ingressNodes.getType()) { case NAME: return new NodeNameRegexInterfaceLocationSpecifier(ingressNodes.getRegex()); case ROLE: return new NodeRoleRegexInterfaceLocationSpecifier( ingressNodes.getRoleDimension(), ingressNodes.getRegex()); default: throw new BatfishException("Unexpected NodesSpecifier type: " + ingressNodes.getType()); } } }
@JsonIgnore /** Return the set of nodes that match this specifier */ public Set<String> getMatchingNodes(SpecifierContext ctxt) { switch (_type) { case NAME: return getMatchingNodesByName(ctxt.getConfigs().keySet()); case ROLE: Optional<NodeRoleDimension> roleDimension = ctxt.getNodeRoleDimension(_roleDimension); if (roleDimension.isPresent()) { return getMatchingNodesByRole(roleDimension.get(), ctxt.getConfigs().keySet()); } else { return Collections.emptySet(); } default: throw new BatfishException("Unhandled NodesSpecifier type: " + _type); } }
@Test public void constructorExplicitName() { NodesSpecifier specifier = new NodesSpecifier("name:lhr-.*"); assertThat(specifier.getType(), equalTo(Type.NAME)); assertThat( specifier.getRegex().pattern(), equalTo(Pattern.compile("lhr-.*", Pattern.CASE_INSENSITIVE).pattern())); }
@Test public void constructorRole() { NodesSpecifier specifier = new NodesSpecifier("role:dim:svr.*"); assertThat(specifier.getType(), equalTo(Type.ROLE)); assertThat(specifier.getRoleDimension(), equalTo("dim")); assertThat( specifier.getRegex().pattern(), equalTo(Pattern.compile("svr.*", Pattern.CASE_INSENSITIVE).pattern())); }
Set<String> includeNodes = question.getNodeRegex().getMatchingNodes(_batfish); Set<String> innerIncludeNodes = innerNRQuestion.getNodeRegex().getMatchingNodes(_batfish); for (Map.Entry<String, SortedSet<String>> entry : roleNodeMap.entrySet()) { String role = entry.getKey(); Set<String> roleNodes = entry.getValue(); String regex = namesToRegex(Sets.intersection(innerIncludeNodes, roleNodes)); innerNRQuestion.setNodeRegex(new NodesSpecifier(regex)); Answerer innerAnswerer = _batfish.createAnswerer(innerQuestion); AnswerElement innerAnswer = innerAnswerer.answer();
@Test public void getMatchingNodesByName() { NodesSpecifier specifier = new NodesSpecifier("name:lhr-.*"); String matchingRouter = "lhr-border1"; String nonMatchingRouter = "svr-border1"; Set<String> nodes = new ImmutableSet.Builder<String>().add(matchingRouter).add(nonMatchingRouter).build(); Set<String> matchingNodes = specifier.getMatchingNodesByName(nodes); assertThat(matchingNodes, hasItem(matchingRouter)); assertThat(matchingNodes, not(hasItem(nonMatchingRouter))); }
@Test public void getMatchingNodesByRole() { NodesSpecifier specifier = new NodesSpecifier("role:dim:match.*"); NodeRoleDimension.builder().setName("dim").setRoles(roles).build(); Set<String> matchingNodes = specifier.getMatchingNodesByRole(roleDimension, nodes);
.filter(nodesSpecifier.getRegex().asPredicate()) .forEach( node -> {
@Test public void constructorImplicitName() { NodesSpecifier specifier = new NodesSpecifier("lhr-.*"); assertThat(specifier.getType(), equalTo(Type.NAME)); assertThat( specifier.getRegex().pattern(), equalTo(Pattern.compile("lhr-.*", Pattern.CASE_INSENSITIVE).pattern())); }
public static NodeSpecifier from(@Nullable NodesSpecifier nodesSpecifier) { if (nodesSpecifier == null) { return null; } switch (nodesSpecifier.getType()) { case NAME: return new NameRegexNodeSpecifier(nodesSpecifier.getRegex()); case ROLE: return new RoleRegexNodeSpecifier( nodesSpecifier.getRegex(), nodesSpecifier.getRoleDimension()); default: throw new BatfishException("Unexpected NodesSpecifier type: " + nodesSpecifier.getType()); } } }
@Test public void testDifference() { assertThat( new FlexibleNodeSpecifierFactory().buildNodeSpecifier("foo - bar"), equalTo( new DifferenceNodeSpecifier( new ShorthandNodeSpecifier(new NodesSpecifier("foo")), new ShorthandNodeSpecifier(new NodesSpecifier("bar"))))); } }
/** Return the set of nodes that match this specifier */ public Set<String> getMatchingNodes(IBatfish batfish) { return getMatchingNodes(batfish.specifierContext()); }
@Test public void autoCompleteEmptyString() { Set<String> nodes = new HashSet<>(); nodes.add("node1"); String query = ""; List<AutocompleteSuggestion> suggestions = NodesSpecifier.autoComplete(query, nodes, null); // autocomplete should yield four entries: NAME:, ROLE:, .*, node1 assertThat(suggestions, hasSize(4)); }
@Test public void testLimitRemoteNodes() { BgpSessionCompatibilityQuestion q = new BgpSessionCompatibilityQuestion(null, new NodesSpecifier("node1"), null, null); Multiset<Row> rows = getQuestionResults(q); assertThat(rows, equalTo(ImmutableMultiset.of(ROW_2))); }
@Override public NodesAnswerElement answer() { NodesQuestion question = (NodesQuestion) _question; Map<String, Configuration> configurations = _batfish.loadConfigurations(); // initRemoteBgpNeighbors(_batfish, configurations); SortedMap<String, Configuration> answerNodes = new TreeMap<>(); Set<String> nodes = question.getNodes().getMatchingNodes(_batfish); for (String node : configurations.keySet()) { if (nodes.contains(node)) { answerNodes.put(node, configurations.get(node)); } } return new NodesAnswerElement(answerNodes, question.getSummary()); }
@Test public void autoCompleteNothing() { Set<String> nodes = new HashSet<>(); nodes.add("node1"); String query = "matchNothing"; List<AutocompleteSuggestion> suggestions = NodesSpecifier.autoComplete(query, nodes, null); // autocomplete should yield nothing assertThat(suggestions, hasSize(0)); }
@Test public void testLimitNodes() { BgpSessionCompatibilityQuestion q = new BgpSessionCompatibilityQuestion(new NodesSpecifier("node1"), null, null, null); Multiset<Row> rows = getQuestionResults(q); assertThat(rows, equalTo(ImmutableMultiset.of(ROW_1))); }