@VisibleForTesting public static Set<String> getStructureNames(Map<String, Configuration> configurations) { ImmutableSet.Builder<String> structureNames = ImmutableSet.builder(); configurations .values() .forEach( configuration -> NamedStructureSpecifier.JAVA_MAP .values() .forEach( type -> { // fetch names of all defined structures of given type and configuration Object namedStructuresMap = type.getGetter().apply(configuration); // should be an instance of a Map if (namedStructuresMap instanceof Map<?, ?>) { ((Map<?, ?>) namedStructuresMap) .keySet() .forEach( key -> { if (key instanceof String) { structureNames.add((String) key); } }); } })); return structureNames.build(); }
new ColumnMetadata( getColumnName(prop), BgpPeerPropertySpecifier.JAVA_MAP.get(prop).getSchema(), "Property " + prop, false,
NamedStructureSpecifier.JAVA_MAP.get(structureType).getGetter();
/** * Creates {@link ColumnMetadata}s that the answer should have based on the {@code * propertySpecifier}. * * @param propertySpecifier The {@link VxlanVniPropertySpecifier} that describes the set of * properties * @return The {@link List} of {@link ColumnMetadata}s */ public static List<ColumnMetadata> createColumnMetadata( VxlanVniPropertySpecifier propertySpecifier) { return ImmutableList.<ColumnMetadata>builder() .add(new ColumnMetadata(COL_NODE, Schema.STRING, "Node", true, false)) .add(new ColumnMetadata(COL_VNI, Schema.INTEGER, "VXLAN Segment ID", true, false)) .addAll( propertySpecifier.getMatchingProperties().stream() .map( prop -> new ColumnMetadata( prop, VxlanVniPropertySpecifier.JAVA_MAP.get(prop).getSchema(), "Property " + prop, false, true)) .collect(Collectors.toList())) .build(); }
/** * Creates {@link ColumnMetadata}s that the answer should have based on the {@code * propertySpecifier}. * * @param propertySpecifier {@link BgpProcessPropertySpecifier} that describes the set of * properties * @return The {@link List} of {@link ColumnMetadata}s */ public static List<ColumnMetadata> createColumnMetadata( BgpProcessPropertySpecifier propertySpecifier) { return ImmutableList.<ColumnMetadata>builder() .add(new ColumnMetadata(COL_NODE, Schema.NODE, "Node", true, false)) .add(new ColumnMetadata(COL_VRF, Schema.STRING, "VRF", true, false)) .add(new ColumnMetadata(COL_ROUTER_ID, Schema.IP, "Router ID", true, false)) .addAll( propertySpecifier.getMatchingProperties().stream() .map( prop -> new ColumnMetadata( getColumnName(prop), BgpProcessPropertySpecifier.JAVA_MAP.get(prop).getSchema(), "Property " + prop, false, true)) .collect(Collectors.toList())) .build(); }
/** * Creates {@link ColumnMetadata}s that the answer should have based on the {@code * propertySpecifier}. * * @param propertySpecifier The {@link OspfPropertySpecifier} that describes the set of properties * @return The {@link List} of {@link ColumnMetadata}s */ public static List<ColumnMetadata> createColumnMetadata(OspfPropertySpecifier propertySpecifier) { return ImmutableList.<ColumnMetadata>builder() .add(new ColumnMetadata(COL_NODE, Schema.NODE, "Node", true, false)) .add(new ColumnMetadata(COL_VRF, Schema.STRING, "VRF", true, false)) .add(new ColumnMetadata(COL_PROCESS_ID, Schema.STRING, "Process ID", true, false)) .addAll( propertySpecifier.getMatchingProperties().stream() .map( prop -> new ColumnMetadata( getColumnName(prop), OspfPropertySpecifier.JAVA_MAP.get(prop).getSchema(), "Property " + prop, false, true)) .collect(Collectors.toList())) .build(); }
/** * Creates {@link ColumnMetadata}s that the answer should have based on the {@code * propertySpecifier}. * * @param propertySpecifier The {@link NodePropertySpecifier} that describes the set of properties * @return The {@link List} of {@link ColumnMetadata}s */ public static List<ColumnMetadata> createColumnMetadata(NodePropertySpecifier propertySpecifier) { return new ImmutableList.Builder<ColumnMetadata>() .add(new ColumnMetadata(COL_NODE, Schema.NODE, "Node", true, false)) .addAll( propertySpecifier.getMatchingProperties().stream() .map( prop -> new ColumnMetadata( getColumnName(prop), NodePropertySpecifier.JAVA_MAP.get(prop).getSchema(), "Property " + prop, false, true)) .collect(Collectors.toList())) .build(); }
/** * Creates {@link ColumnMetadata}s that the answer should have based on the {@code * propertySpecifier}. * * @param propertySpecifier The {@link InterfacePropertySpecifier} that describes the set of * properties * @return The {@link List} of {@link ColumnMetadata}s */ public static List<ColumnMetadata> createColumnMetadata( InterfacePropertySpecifier propertySpecifier) { return ImmutableList.<ColumnMetadata>builder() .add(new ColumnMetadata(COL_INTERFACE, Schema.INTERFACE, "Interface", true, false)) .addAll( propertySpecifier.getMatchingProperties().stream() .map( prop -> new ColumnMetadata( getColumnName(prop), InterfacePropertySpecifier.JAVA_MAP.get(prop).getSchema(), "Property " + prop, false, true)) .collect(Collectors.toList())) .build(); }
/** * 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 all structures of type {@code structureType} across {@code nodes} */ public static Set<String> getAllStructureNamesOfType( String structureType, Set<String> nodes, Map<String, Configuration> configurations) { Set<String> structNames = new HashSet<>(); Function<Configuration, Object> structMapGetter = NamedStructureSpecifier.JAVA_MAP.get(structureType).getGetter(); for (String node : nodes) { Object namedStructuresMap = structMapGetter.apply(configurations.get(node)); if (namedStructuresMap instanceof Map<?, ?>) { for (Map.Entry<?, ?> entry : ((Map<?, ?>) namedStructuresMap).entrySet()) { structNames.add((String) entry.getKey()); } } } return structNames; }
@VisibleForTesting static void fillProperty( String columnName, Object propertyValue, RowBuilder row, PropertyDescriptor<?> propertyDescriptor) { row.put(columnName, propertyValue); // if this barfs, the value cannot be converted to expected Schema row.build().get(columnName, propertyDescriptor.getSchema()); }
@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); }
/** 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)); } }