@Override public Row build() { // Fill in missing columns with null entries _columns.keySet().stream().filter(c -> !_data.has(c)).forEach(c -> super.put(c, null)); return super.build(); } }
/** * Puts {@code object} into column {@code column} of the row, after checking if the object is * compatible with the Schema of the column */ @Override public TypedRowBuilder put(String column, @Nullable Object object) { checkArgument( _columns.containsKey(column), Row.missingColumnErrorMessage(column, _columns.keySet())); Schema expectedSchema = _columns.get(column).getSchema(); checkArgument( SchemaUtils.isValidObject(object, expectedSchema), String.format( "Cannot convert '%s' to Schema '%s' of column '%s'", object, expectedSchema, column)); super.put(column, object); return this; }
@VisibleForTesting Row rowOf(Object... objects) { checkArgument( objects.length % 2 == 0, "expecting an even number of parameters, not %s", objects.length); for (int i = 0; i + 1 < objects.length; i += 2) { checkArgument( objects[i] instanceof String, "argument %s must be a string, but is: %s", i, objects[i]); put((String) objects[i], objects[i + 1]); } return build(); }
@VisibleForTesting static Row getBgpEdgeRow( String node, @Nullable Ip ip, @Nullable Long asNumber, String remoteNode, @Nullable Ip remoteIp, @Nullable Long remoteAsNumber) { RowBuilder row = Row.builder(); row.put(COL_NODE, new Node(node)) .put(COL_IP, ip) .put(COL_AS_NUMBER, asNumber) .put(COL_REMOTE_NODE, new Node(remoteNode)) .put(COL_REMOTE_IP, remoteIp) .put(COL_REMOTE_AS_NUMBER, remoteAsNumber); return row.build(); }
@VisibleForTesting static Row getRow(String nodeName, Collection<Line> lines) { List<String> exposedLines = new ArrayList<>(); for (Line line : lines) { if (!line.requiresAuthentication()) { exposedLines.add(line.getName()); } } if (exposedLines.isEmpty()) { return null; } RowBuilder row = Row.builder(); row.put(COLUMN_NODE, new Node(nodeName)).put(COLUMN_LINE_NAMES, exposedLines); return row.build(); } }
private static void populateRouteAttributes( RowBuilder rowBuilder, @Nullable RouteRowAttribute routeRowAttribute, boolean base) { rowBuilder .put( (base ? COL_BASE_PREFIX : COL_DELTA_PREFIX) + COL_NEXT_HOP, routeRowAttribute != null ? routeRowAttribute.getNextHop() : null) .put( (base ? COL_BASE_PREFIX : COL_DELTA_PREFIX) + COL_METRIC, routeRowAttribute != null ? routeRowAttribute.getMetric() : null) .put( (base ? COL_BASE_PREFIX : COL_DELTA_PREFIX) + COL_ADMIN_DISTANCE, routeRowAttribute != null ? routeRowAttribute.getAdminDistance() : null) .put( (base ? COL_BASE_PREFIX : COL_DELTA_PREFIX) + COL_TAG, routeRowAttribute != null ? routeRowAttribute.getTag() : null); }
@VisibleForTesting static Row layer2EdgeToRow(Layer2Edge layer2Edge) { RowBuilder row = Row.builder(); row.put( COL_INTERFACE, new NodeInterfacePair( layer2Edge.getNode1().getHostname(), layer2Edge.getNode1().getInterfaceName())) .put(COL_VLAN, layer2Edge.getNode1().getSwitchportVlanId()) .put( COL_REMOTE_INTERFACE, new NodeInterfacePair( layer2Edge.getNode2().getHostname(), layer2Edge.getNode2().getInterfaceName())) .put(COL_REMOTE_VLAN, layer2Edge.getNode2().getSwitchportVlanId()); return row.build(); }
@VisibleForTesting static Row vxlanEdgeToRow(VxlanEdge edge) { RowBuilder row = Row.builder(); row.put(COL_VNI, edge.getVni()) .put(COL_NODE, new Node(edge.getTail().getHostname())) .put(COL_REMOTE_NODE, new Node(edge.getHead().getHostname())) .put(COL_VTEP_ADDRESS, edge.getTail().getSourceAddress()) .put(COL_REMOTE_VTEP_ADDRESS, edge.getHead().getSourceAddress()) .put(COL_VLAN, edge.getTail().getVlan()) .put(COL_REMOTE_VLAN, edge.getHead().getVlan()) .put(COL_UDP_PORT, edge.getUdpPort()) .put(COL_MULTICAST_GROUP, edge.getMulticastGroup()); return row.build(); }
@VisibleForTesting static Row layer1EdgeToRow(Layer1Edge layer1Edge) { RowBuilder row = Row.builder(); row.put( COL_INTERFACE, new NodeInterfacePair( layer1Edge.getNode1().getHostname(), layer1Edge.getNode1().getInterfaceName())) .put( COL_REMOTE_INTERFACE, new NodeInterfacePair( layer1Edge.getNode2().getHostname(), layer1Edge.getNode2().getInterfaceName())); return row.build(); }
@VisibleForTesting static Row eigrpEdgeToRow(EigrpEdge eigrpEdge) { RowBuilder row = Row.builder(); row.put( COL_INTERFACE, new NodeInterfacePair( eigrpEdge.getNode1().getHostname(), eigrpEdge.getNode1().getInterfaceName())) .put( COL_REMOTE_INTERFACE, new NodeInterfacePair( eigrpEdge.getNode2().getHostname(), eigrpEdge.getNode2().getInterfaceName())); return row.build(); }
@Test public void putAllCorrect() { Row row = Row.builder().put("col1", 20).put("col2", 21).put("col3", 24).build(); // all columns should be copied over when we don't limit assertThat( Row.builder().putAll(row).build(), equalTo(Row.builder().put("col1", 20).put("col2", 21).put("col3", 24).build())); // only specified columns should be copied assertThat( Row.builder().putAll(row, ImmutableSet.of("col1", "col3")).build(), equalTo(Row.builder().put("col1", 20).put("col3", 24).build())); }
@VisibleForTesting static Row getOspfEdgeRow(String node, String iface, String remoteNode, String remoteIface) { RowBuilder row = Row.builder(); row.put(COL_INTERFACE, new NodeInterfacePair(node, iface)) .put(COL_REMOTE_INTERFACE, new NodeInterfacePair(remoteNode, remoteIface)); return row.build(); }
/** * Mirrors the values of {@code columns} in {@code otherRow} * * @throws NoSuchElementException if one of the columns is not present in {@code otherRow}. */ public RowBuilder putAll(Row otherRow, Collection<String> columns) { columns.forEach(col -> put(col, otherRow.get(col))); return this; } }
@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 rowOfCorrect() { assertThat(Row.builder().rowOf(), equalTo(Row.builder().build())); assertThat(Row.builder().rowOf("a", 5), equalTo(Row.builder().put("a", 5).build())); assertThat( Row.builder().rowOf("a", 5, "b", 7), equalTo(Row.builder().put("a", 5).put("b", 7).build())); }
@Test public void testAddRowExtraColumn() { TableAnswerElement table = new TableAnswerElement(twoKeyTableMetadata); Row row = Row.builder().put("key1", null).put("key2", null).put("key3", null).build(); _thrown.expect(IllegalArgumentException.class); _thrown.expectMessage( String.format( "Row columns %s do not match metadata columns metadata %s", row.getColumnNames(), twoKeyTableMetadata.toColumnMap().keySet())); table.addRow(row); } }
@Test public void testAddValidRow() { TableAnswerElement table = new TableAnswerElement(twoKeyTableMetadata); Row row = Row.builder().put("key1", null).put("key2", null).build(); table.addRow(row); assertThat(table.getRows().getData(), contains(row)); }
static Row isisEdgeToRow(IsisEdge isisEdge) { RowBuilder row = Row.builder(); row.put( COL_INTERFACE, new NodeInterfacePair( isisEdge.getNode1().getNode(), isisEdge.getNode1().getInterfaceName())) .put( COL_REMOTE_INTERFACE, new NodeInterfacePair( isisEdge.getNode2().getNode(), isisEdge.getNode2().getInterfaceName())); return row.build(); }