/** * Returns a list of suggestions based on the query, based on {@link * PropertySpecifier#baseAutoComplete}. */ public static List<AutocompleteSuggestion> autoComplete(String query) { return PropertySpecifier.baseAutoComplete(query, JAVA_MAP.keySet()); }
/** * Uses {@code propertyDescriptor} to extract the property value from {@code object} and insert * into {@code row} at {@code columnName}. * * @throws ClassCastException if the recovered property value is not compatible with the specified * {@link Schema} in the {@code propertyDescriptor}. */ public static <T> void fillProperty( PropertyDescriptor<T> propertyDescriptor, T object, String columnName, RowBuilder row) { checkArgument(propertyDescriptor != null, "'propertyDescriptor' cannot be null"); checkArgument(object != null, "'object' cannot be null"); checkArgument(columnName != null, "'columnName' cannot be null"); checkArgument(row != null, "'row' cannot be null"); Object propertyValue = propertyDescriptor.getGetter().apply(object); propertyValue = PropertySpecifier.convertTypeIfNeeded(propertyValue, propertyDescriptor.getSchema()); fillProperty(columnName, propertyValue, row, propertyDescriptor); // separate for testing }
/** * Returns a list of suggestions based on the query. The current implementation treats the query * as a substring of the property string. * * @param query The query that came to the concrete child class * @return The list of suggestions */ public static List<AutocompleteSuggestion> baseAutoComplete( @Nullable String query, Set<String> allProperties) { String finalQuery = firstNonNull(query, "").toLowerCase(); ImmutableList.Builder<AutocompleteSuggestion> suggestions = new ImmutableList.Builder<>(); String queryWithStars = ".*" + (finalQuery.isEmpty() ? "" : finalQuery + ".*"); Pattern queryPattern = safeGetPattern(queryWithStars); /* * if queryWithStars is not a valid Pattern, finalQuery must be a funky string that will not * match anything as string.contains or regex.matches; so we skip formalities altogether */ if (queryPattern != null) { suggestions.addAll( allProperties.stream() .filter(prop -> queryPattern.matcher(prop.toLowerCase()).matches()) .map(prop -> new AutocompleteSuggestion(prop, false)) .collect(Collectors.toList())); } return suggestions.build(); }
/** * Gets properties of nodes. * * @param propertySpecifier Specifies which properties to get * @param configurations configuration to use in extractions * @param nodes the set of nodes to focus on * @param columns a map from column name to {@link ColumnMetadata} * @return A multiset of {@link Row}s where each row corresponds to a node and columns correspond * to property values. */ public static Multiset<Row> getProperties( NodePropertySpecifier propertySpecifier, Map<String, Configuration> configurations, Set<String> nodes, Map<String, ColumnMetadata> columns) { Multiset<Row> rows = HashMultiset.create(); for (String nodeName : nodes) { RowBuilder row = Row.builder(columns).put(COL_NODE, new Node(nodeName)); for (String property : propertySpecifier.getMatchingProperties()) { PropertySpecifier.fillProperty( NodePropertySpecifier.JAVA_MAP.get(property), configurations.get(nodeName), property, row); } rows.add(row.build()); } return rows; }
/** Returns what is actually inserted into the answer */ static SelfDescribingObject insertedObject(Object obj, String structType) { Schema targetSchema = NamedStructureSpecifier.JAVA_MAP.get(structType).getSchema(); return new SelfDescribingObject( targetSchema, PropertySpecifier.convertTypeIfNeeded(obj, targetSchema)); } }
InterfacePropertySpecifier.JAVA_MAP.get(property); try { PropertySpecifier.fillProperty(propertyDescriptor, iface, property, row); } catch (ClassCastException e) { throw new BatfishException(
/** Null should remain null and not crash */ @Test public void convertTypeIfNeededNullInput() { Object propertyValueNull = PropertySpecifier.convertTypeIfNeeded(null, Schema.STRING); assertThat(propertyValueNull, equalTo(null)); }
BgpProcessPropertySpecifier.JAVA_MAP.get(property); try { PropertySpecifier.fillProperty( propertyDescriptor, bgpProcess, property, rowBuilder); } catch (ClassCastException e) {
/** * Returns a list of suggestions based on the query, based on {@link * PropertySpecifier#baseAutoComplete}. */ public static List<AutocompleteSuggestion> autoComplete(String query) { return PropertySpecifier.baseAutoComplete(query, JAVA_MAP.keySet()); }
/** Non-string objects should be mapped to their string forms when the schema is String */ @Test public void convertTypeIfNeededNonStringToString() { Object propertyValueObject = PropertySpecifier.convertTypeIfNeeded( new Configuration("cname", ConfigurationFormat.CISCO_IOS), Schema.STRING); assertThat(propertyValueObject, equalTo("cname")); }
OspfPropertySpecifier.JAVA_MAP.get(property); try { PropertySpecifier.fillProperty( propertyDescriptor, ospfProcess, property, rowBuilder); } catch (ClassCastException e) {
/** * Returns a list of suggestions based on the query, based on {@link * PropertySpecifier#baseAutoComplete}. */ public static List<AutocompleteSuggestion> autoComplete(String query) { return PropertySpecifier.baseAutoComplete(query, JAVA_MAP.keySet()); }
/** Collection of non-strings should be converted to collection of strings when desired */ @Test public void convertTypeIfNeededNonStringCollectionToStringCollection() { Object propertyValueCollection = PropertySpecifier.convertTypeIfNeeded( ImmutableList.of(new Configuration("cname", ConfigurationFormat.CISCO_IOS)), Schema.list(Schema.STRING)); assertThat(propertyValueCollection, equalTo(ImmutableList.of("cname"))); }
@Test public void fillPropertyListEmpty() { Configuration configuration = new Configuration("hostname", ConfigurationFormat.CISCO_IOS); String property = NodePropertySpecifier.NTP_SERVERS; PropertyDescriptor<Configuration> propertyDescriptor = NodePropertySpecifier.JAVA_MAP.get(property); RowBuilder row = Row.builder(); PropertySpecifier.fillProperty(propertyDescriptor, configuration, property, row); // the row should be filled out with an empty list assertThat(row.build(), equalTo(Row.builder().put(property, new LinkedList<String>()).build())); }
/** Returns a list of suggestions based on the query, based on {@link PropertySpecifier}. */ public static List<AutocompleteSuggestion> autoComplete(String query) { return PropertySpecifier.baseAutoComplete(query, JAVA_MAP.keySet()); }
/** Map should be converted to set of strings when the schema is string list */ @Test public void convertTypeIfNeededMapToStringSet() { Object propertyValueMap = PropertySpecifier.convertTypeIfNeeded( ImmutableSortedMap.of("k1", "v1", "k2", "v2"), Schema.list(Schema.STRING)); assertThat(propertyValueMap, equalTo(ImmutableSet.of("k1", "k2"))); }
BgpPeerPropertySpecifier.JAVA_MAP.get(property); try { PropertySpecifier.fillProperty(propertyDescriptor, peer, property, rowBuilder); } catch (ClassCastException e) { throw new BatfishException(
/** * Returns a list of suggestions based on the query. The current implementation treats the query * as a prefix of the property string. * * @param query The query to auto complete * @return The list of suggestions */ public static List<AutocompleteSuggestion> autoComplete(String query) { return PropertySpecifier.baseAutoComplete(query, JAVA_MAP.keySet()); }
/** Protect other objects for which conversion is not needed */ @Test public void convertTypeIfNeededNoConversion() { Object propertyValueOther = PropertySpecifier.convertTypeIfNeeded(new Node("node"), Schema.NODE); assertThat(propertyValueOther, equalTo(new Node("node"))); }
VxlanVniPropertySpecifier.JAVA_MAP.get(property); try { PropertySpecifier.fillProperty( propertyDescriptor, vxlanVniProperties, property, row); } catch (ClassCastException e) {