@Test public void autoCompleteRoleDimension() { NodeRolesData nodeRolesData = initNodeRoleData(); String queryDimension = "ROLE:dim10:"; List<AutocompleteSuggestion> suggestions = NodesSpecifier.autoComplete( queryDimension, ImmutableSet.of("role1", "role2"), nodeRolesData); Set<String> suggestionsText = suggestions.stream() .map(suggestion -> suggestion.getText()) .collect(ImmutableSet.toImmutableSet()); assertThat(suggestions, hasSize(3)); assertThat(suggestionsText, hasItem("ROLE:dim10:.*")); assertThat(suggestionsText, hasItem("ROLE:dim10:role1")); assertThat(suggestionsText, hasItem("ROLE:dim10:role2")); }
public static List<AutocompleteSuggestion> autoComplete( String query, Set<String> nodes, NodeRolesData nodeRoleData) { final String finalQuery = query == null ? "" : query; List<AutocompleteSuggestion> suggestions = Arrays.stream(Type.values()) .filter(type -> type.toString().startsWith(finalQuery.toUpperCase())) .map( type -> new AutocompleteSuggestion( type.toString() + ":", true, "Select nodes by " + type.toString())) .collect(Collectors.toList()); String[] parts = finalQuery.split(":"); if (!finalQuery.contains(":") || finalQuery.toUpperCase().startsWith("NAME:")) { String namePrefix = finalQuery.equalsIgnoreCase("NAME:") ? "" : (parts.length == 1 ? finalQuery : parts[1]); List<AutocompleteSuggestion> nameSuggestions = nodes.stream() .filter(n -> n.startsWith(namePrefix)) .map(n -> new AutocompleteSuggestion(n, false)) .collect(Collectors.toList()); if (!nameSuggestions.isEmpty()) { suggestions.add( new AutocompleteSuggestion( finalQuery + ".*", false, "All nodes whose names match " + namePrefix + ".*")); suggestions.addAll(nameSuggestions); } } if (finalQuery.toUpperCase().startsWith("ROLE:") && nodeRoleData != null) { if (parts.length == 3 || (parts.length == 2 && finalQuery.endsWith(":"))) { String roleDimension = parts[1]; Optional<NodeRoleDimension> optDimension = Optional.ofNullable(nodeRoleData.toNodeRoleDimensions().get(roleDimension)); if (optDimension.isPresent()) { NodeRoleDimension dimension = optDimension.get(); String roleDimensionPrefix = "ROLE:" + dimension.getName() + ":"; String rolePrefix = finalQuery.endsWith(":") ? "" : parts[2]; List<AutocompleteSuggestion> roleSuggestions = dimension.roleNamesFor(nodes).stream() .filter(role -> role.startsWith(rolePrefix)) .map( role -> new AutocompleteSuggestion( "ROLE:" + dimension.getName() + ":" + role, false, "All nodes that belong to this role")) .collect(Collectors.toList()); if (!roleSuggestions.isEmpty()) { suggestions.add( new AutocompleteSuggestion( roleDimensionPrefix + rolePrefix + ".*", false, "All nodes with matching roles")); suggestions.addAll(roleSuggestions); } } } else { String roleDimPrefix = finalQuery.equalsIgnoreCase("ROLE:") ? "" : parts[1]; suggestions.addAll( nodeRoleData.toNodeRoleDimensions().values().stream() .filter(dim -> dim.getName().startsWith(roleDimPrefix)) .map( dim -> new AutocompleteSuggestion( "ROLE:" + dim.getName() + ":", true, "Select nodes using this dimension")) .collect(ImmutableSet.toImmutableSet())); } } int rank = 0; for (AutocompleteSuggestion suggestion : suggestions) { AutocompleteSuggestion.builder(suggestion).setRank(rank).build(); rank++; } return suggestions; }
NodesSpecifier { public static List<AutocompleteSuggestion> autoComplete( String query, Set<String> nodes, NodeRolesData nodeRoleData) { final String finalQuery = query == null ? "" : query; List<AutocompleteSuggestion> suggestions = Arrays.stream(Type.values()) .filter(type -> type.toString().startsWith(finalQuery.toUpperCase())) .map( type -> new AutocompleteSuggestion( type.toString() + ":", true, "Select nodes by " + type.toString())) .collect(Collectors.toList()); String[] parts = finalQuery.split(":"); if (!finalQuery.contains(":") || finalQuery.toUpperCase().startsWith("NAME:")) { String namePrefix = finalQuery.equalsIgnoreCase("NAME:") ? "" : (parts.length == 1 ? finalQuery : parts[1]); List<AutocompleteSuggestion> nameSuggestions = nodes.stream() .filter(n -> n.startsWith(namePrefix)) .map(n -> new AutocompleteSuggestion(n, false)) .collect(Collectors.toList()); if (!nameSuggestions.isEmpty()) { suggestions.add( new AutocompleteSuggestion( finalQuery + ".*", false, "All nodes whose names match " + namePrefix + ".*")); suggestions.addAll(nameSuggestions); } } if (finalQuery.toUpperCase().startsWith("ROLE:") && nodeRoleData != null) { if (parts.length == 3 || (parts.length == 2 && finalQuery.endsWith(":"))) { String roleDimension = parts[1]; Optional<NodeRoleDimension> optDimension = Optional.ofNullable(nodeRoleData.toNodeRoleDimensions().get(roleDimension)); if (optDimension.isPresent()) { NodeRoleDimension dimension = optDimension.get(); String roleDimensionPrefix = "ROLE:" + dimension.getName() + ":"; String rolePrefix = finalQuery.endsWith(":") ? "" : parts[2]; List<AutocompleteSuggestion> roleSuggestions = dimension.roleNamesFor(nodes).stream() .filter(role -> role.startsWith(rolePrefix)) .map( role -> new AutocompleteSuggestion( "ROLE:" + dimension.getName() + ":" + role, false, "All nodes that belong to this role")) .collect(Collectors.toList()); if (!roleSuggestions.isEmpty()) { suggestions.add( new AutocompleteSuggestion( roleDimensionPrefix + rolePrefix + ".*", false, "All nodes with matching roles")); suggestions.addAll(roleSuggestions); } } } else { String roleDimPrefix = finalQuery.equalsIgnoreCase("ROLE:") ? "" : parts[1]; suggestions.addAll( nodeRoleData.toNodeRoleDimensions().values().stream() .filter(dim -> dim.getName().startsWith(roleDimPrefix)) .map( dim -> new AutocompleteSuggestion( "ROLE:" + dim.getName() + ":", true, "Select nodes using this dimension")) .collect(ImmutableSet.toImmutableSet())); } } int rank = 0; for (AutocompleteSuggestion suggestion : suggestions) { AutocompleteSuggestion.builder(suggestion).setRank(rank).build(); rank++; } return suggestions; } }
NodesSpecifier { public static List<AutocompleteSuggestion> autoComplete( String query, Set<String> nodes, NodeRolesData nodeRoleData) { final String finalQuery = query == null ? "" : query; List<AutocompleteSuggestion> suggestions = Arrays.stream(Type.values()) .filter(type -> type.toString().startsWith(finalQuery.toUpperCase())) .map( type -> new AutocompleteSuggestion( type.toString() + ":", true, "Select nodes by " + type.toString())) .collect(Collectors.toList()); String[] parts = finalQuery.split(":"); if (!finalQuery.contains(":") || finalQuery.toUpperCase().startsWith("NAME:")) { String namePrefix = finalQuery.equalsIgnoreCase("NAME:") ? "" : (parts.length == 1 ? finalQuery : parts[1]); List<AutocompleteSuggestion> nameSuggestions = nodes.stream() .filter(n -> n.startsWith(namePrefix)) .map(n -> new AutocompleteSuggestion(n, false)) .collect(Collectors.toList()); if (!nameSuggestions.isEmpty()) { suggestions.add( new AutocompleteSuggestion( finalQuery + ".*", false, "All nodes whose names match " + namePrefix + ".*")); suggestions.addAll(nameSuggestions); } } if (finalQuery.toUpperCase().startsWith("ROLE:") && nodeRoleData != null) { if (parts.length == 3 || (parts.length == 2 && finalQuery.endsWith(":"))) { String roleDimension = parts[1]; Optional<NodeRoleDimension> optDimension = Optional.ofNullable(nodeRoleData.toNodeRoleDimensions().get(roleDimension)); if (optDimension.isPresent()) { NodeRoleDimension dimension = optDimension.get(); String roleDimensionPrefix = "ROLE:" + dimension.getName() + ":"; String rolePrefix = finalQuery.endsWith(":") ? "" : parts[2]; List<AutocompleteSuggestion> roleSuggestions = dimension.roleNamesFor(nodes).stream() .filter(role -> role.startsWith(rolePrefix)) .map( role -> new AutocompleteSuggestion( "ROLE:" + dimension.getName() + ":" + role, false, "All nodes that belong to this role")) .collect(Collectors.toList()); if (!roleSuggestions.isEmpty()) { suggestions.add( new AutocompleteSuggestion( roleDimensionPrefix + rolePrefix + ".*", false, "All nodes with matching roles")); suggestions.addAll(roleSuggestions); } } } else { String roleDimPrefix = finalQuery.equalsIgnoreCase("ROLE:") ? "" : parts[1]; suggestions.addAll( nodeRoleData.toNodeRoleDimensions().values().stream() .filter(dim -> dim.getName().startsWith(roleDimPrefix)) .map( dim -> new AutocompleteSuggestion( "ROLE:" + dim.getName() + ":", true, "Select nodes using this dimension")) .collect(ImmutableSet.toImmutableSet())); } } int rank = 0; for (AutocompleteSuggestion suggestion : suggestions) { AutocompleteSuggestion.builder(suggestion).setRank(rank).build(); rank++; } return suggestions; } NodesSpecifier(String expression, boolean caseSensitive); @JsonCreator  NodesSpecifier(String expression); }
NodesSpecifier { public static List<AutocompleteSuggestion> autoComplete( String query, Set<String> nodes, NodeRolesData nodeRoleData) { final String finalQuery = query == null ? "" : query; List<AutocompleteSuggestion> suggestions = Arrays.stream(Type.values()) .filter(type -> type.toString().startsWith(finalQuery.toUpperCase())) .map( type -> new AutocompleteSuggestion( type.toString() + ":", true, "Select nodes by " + type.toString())) .collect(Collectors.toList()); String[] parts = finalQuery.split(":"); if (!finalQuery.contains(":") || finalQuery.toUpperCase().startsWith("NAME:")) { String namePrefix = finalQuery.equalsIgnoreCase("NAME:") ? "" : (parts.length == 1 ? finalQuery : parts[1]); List<AutocompleteSuggestion> nameSuggestions = nodes.stream() .filter(n -> n.startsWith(namePrefix)) .map(n -> new AutocompleteSuggestion(n, false)) .collect(Collectors.toList()); if (!nameSuggestions.isEmpty()) { suggestions.add( new AutocompleteSuggestion( finalQuery + ".*", false, "All nodes whose names match " + namePrefix + ".*")); suggestions.addAll(nameSuggestions); } } if (finalQuery.toUpperCase().startsWith("ROLE:") && nodeRoleData != null) { if (parts.length == 3 || (parts.length == 2 && finalQuery.endsWith(":"))) { String roleDimension = parts[1]; Optional<NodeRoleDimension> optDimension = Optional.ofNullable(nodeRoleData.toNodeRoleDimensions().get(roleDimension)); if (optDimension.isPresent()) { NodeRoleDimension dimension = optDimension.get(); String roleDimensionPrefix = "ROLE:" + dimension.getName() + ":"; String rolePrefix = finalQuery.endsWith(":") ? "" : parts[2]; List<AutocompleteSuggestion> roleSuggestions = dimension.roleNamesFor(nodes).stream() .filter(role -> role.startsWith(rolePrefix)) .map( role -> new AutocompleteSuggestion( "ROLE:" + dimension.getName() + ":" + role, false, "All nodes that belong to this role")) .collect(Collectors.toList()); if (!roleSuggestions.isEmpty()) { suggestions.add( new AutocompleteSuggestion( roleDimensionPrefix + rolePrefix + ".*", false, "All nodes with matching roles")); suggestions.addAll(roleSuggestions); } } } else { String roleDimPrefix = finalQuery.equalsIgnoreCase("ROLE:") ? "" : parts[1]; suggestions.addAll( nodeRoleData.toNodeRoleDimensions().values().stream() .filter(dim -> dim.getName().startsWith(roleDimPrefix)) .map( dim -> new AutocompleteSuggestion( "ROLE:" + dim.getName() + ":", true, "Select nodes using this dimension")) .collect(ImmutableSet.toImmutableSet())); } } int rank = 0; for (AutocompleteSuggestion suggestion : suggestions) { AutocompleteSuggestion.builder(suggestion).setRank(rank).build(); rank++; } return suggestions; } NodesSpecifier(String expression, boolean caseSensitive); @JsonCreator  NodesSpecifier(String expression); @Override boolean equals(Object obj); static List<AutocompleteSuggestion> autoComplete(
      String query, Set<String> nodes, NodeRolesData nodeRoleData); String getExpression(); Set<String> getMatchingNodes(IBatfish batfish, NetworkSnapshot snapshot); @JsonIgnore Set<String> getMatchingNodes(SpecifierContext ctxt); Set<String> getMatchingNodesByName(Set<String> nodes); Set<String> getMatchingNodesByRole(NodeRoleDimension roleDimension, Set<String> nodes); @JsonIgnore Pattern getRegex(); @JsonIgnore String getRoleDimension(); @JsonIgnore Type getType(); @JsonIgnore boolean getCaseSensitive(); @Override int hashCode(); @Override @JsonValue String toString(); }
NodesSpecifier { public static List<AutocompleteSuggestion> autoComplete( String query, Set<String> nodes, NodeRolesData nodeRoleData) { final String finalQuery = query == null ? "" : query; List<AutocompleteSuggestion> suggestions = Arrays.stream(Type.values()) .filter(type -> type.toString().startsWith(finalQuery.toUpperCase())) .map( type -> new AutocompleteSuggestion( type.toString() + ":", true, "Select nodes by " + type.toString())) .collect(Collectors.toList()); String[] parts = finalQuery.split(":"); if (!finalQuery.contains(":") || finalQuery.toUpperCase().startsWith("NAME:")) { String namePrefix = finalQuery.equalsIgnoreCase("NAME:") ? "" : (parts.length == 1 ? finalQuery : parts[1]); List<AutocompleteSuggestion> nameSuggestions = nodes.stream() .filter(n -> n.startsWith(namePrefix)) .map(n -> new AutocompleteSuggestion(n, false)) .collect(Collectors.toList()); if (!nameSuggestions.isEmpty()) { suggestions.add( new AutocompleteSuggestion( finalQuery + ".*", false, "All nodes whose names match " + namePrefix + ".*")); suggestions.addAll(nameSuggestions); } } if (finalQuery.toUpperCase().startsWith("ROLE:") && nodeRoleData != null) { if (parts.length == 3 || (parts.length == 2 && finalQuery.endsWith(":"))) { String roleDimension = parts[1]; Optional<NodeRoleDimension> optDimension = Optional.ofNullable(nodeRoleData.toNodeRoleDimensions().get(roleDimension)); if (optDimension.isPresent()) { NodeRoleDimension dimension = optDimension.get(); String roleDimensionPrefix = "ROLE:" + dimension.getName() + ":"; String rolePrefix = finalQuery.endsWith(":") ? "" : parts[2]; List<AutocompleteSuggestion> roleSuggestions = dimension.roleNamesFor(nodes).stream() .filter(role -> role.startsWith(rolePrefix)) .map( role -> new AutocompleteSuggestion( "ROLE:" + dimension.getName() + ":" + role, false, "All nodes that belong to this role")) .collect(Collectors.toList()); if (!roleSuggestions.isEmpty()) { suggestions.add( new AutocompleteSuggestion( roleDimensionPrefix + rolePrefix + ".*", false, "All nodes with matching roles")); suggestions.addAll(roleSuggestions); } } } else { String roleDimPrefix = finalQuery.equalsIgnoreCase("ROLE:") ? "" : parts[1]; suggestions.addAll( nodeRoleData.toNodeRoleDimensions().values().stream() .filter(dim -> dim.getName().startsWith(roleDimPrefix)) .map( dim -> new AutocompleteSuggestion( "ROLE:" + dim.getName() + ":", true, "Select nodes using this dimension")) .collect(ImmutableSet.toImmutableSet())); } } int rank = 0; for (AutocompleteSuggestion suggestion : suggestions) { AutocompleteSuggestion.builder(suggestion).setRank(rank).build(); rank++; } return suggestions; } NodesSpecifier(String expression, boolean caseSensitive); @JsonCreator  NodesSpecifier(String expression); @Override boolean equals(Object obj); static List<AutocompleteSuggestion> autoComplete(
      String query, Set<String> nodes, NodeRolesData nodeRoleData); String getExpression(); Set<String> getMatchingNodes(IBatfish batfish, NetworkSnapshot snapshot); @JsonIgnore Set<String> getMatchingNodes(SpecifierContext ctxt); Set<String> getMatchingNodesByName(Set<String> nodes); Set<String> getMatchingNodesByRole(NodeRoleDimension roleDimension, Set<String> nodes); @JsonIgnore Pattern getRegex(); @JsonIgnore String getRoleDimension(); @JsonIgnore Type getType(); @JsonIgnore boolean getCaseSensitive(); @Override int hashCode(); @Override @JsonValue String toString(); static final NodesSpecifier ALL; static final NodesSpecifier NONE; }