/** * 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 }
/** * 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; }
InterfacePropertySpecifier.JAVA_MAP.get(property); try { PropertySpecifier.fillProperty(propertyDescriptor, iface, property, row); } catch (ClassCastException e) { throw new BatfishException(
BgpProcessPropertySpecifier.JAVA_MAP.get(property); try { PropertySpecifier.fillProperty( propertyDescriptor, bgpProcess, property, rowBuilder); } catch (ClassCastException e) {
OspfPropertySpecifier.JAVA_MAP.get(property); try { PropertySpecifier.fillProperty( propertyDescriptor, ospfProcess, property, rowBuilder); } catch (ClassCastException e) {
BgpPeerPropertySpecifier.JAVA_MAP.get(property); try { PropertySpecifier.fillProperty(propertyDescriptor, peer, property, rowBuilder); } catch (ClassCastException e) { throw new BatfishException(
@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())); }
VxlanVniPropertySpecifier.JAVA_MAP.get(property); try { PropertySpecifier.fillProperty( propertyDescriptor, vxlanVniProperties, property, row); } catch (ClassCastException e) {
@Test public void fillPropertyNull() { Configuration configuration = new Configuration("hostname", ConfigurationFormat.CISCO_IOS); String property = NodePropertySpecifier.NTP_SOURCE_INTERFACE; 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 null and the schemas shouldn't be assertThat(row.build(), equalTo(Row.builder().put(property, null).build())); } }
@Test public void fillPropertyListNonEmpty() { Configuration configuration = new Configuration("hostname", ConfigurationFormat.CISCO_IOS); configuration.setNtpServers(ImmutableSortedSet.of("sa", "sb")); 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 the right list and the schemas map should be List<String> assertThat( row.build(), equalTo(Row.builder().put(property, ImmutableList.of("sa", "sb")).build())); }
@Test public void fillPropertyMap() { Configuration configuration = new Configuration("hostname", ConfigurationFormat.CISCO_IOS); configuration.setInterfaces(ImmutableSortedMap.of("i1", new Interface("i1"))); String property = NodePropertySpecifier.INTERFACES; 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 the right list and the schemas map should be List<String> assertThat(row.build(), equalTo(Row.builder().put(property, ImmutableList.of("i1")).build())); }
@Test public void fillPropertyForcedString() { Configuration configuration = new Configuration("hostname", ConfigurationFormat.CISCO_IOS); configuration.setDefaultInboundAction(LineAction.PERMIT); String property = NodePropertySpecifier.DEFAULT_INBOUND_ACTION; 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 the String value assertThat( row.build(), equalTo(Row.builder().put(property, LineAction.PERMIT.toString()).build())); }
@Test public void fillPropertyFail() { PropertyDescriptor<Configuration> propDescriptor = new PropertyDescriptor<>(null, Schema.list(Schema.STRING)); _thrown.expect(ClassCastException.class); _thrown.expectMessage("Cannot recover object"); PropertySpecifier.fillProperty("col", "stringNotList", Row.builder(), propDescriptor); }