/** Create column metadata. */ private static List<ColumnMetadata> getColumnMetadata() { return ImmutableList.of( new ColumnMetadata(COL_INITIATOR, Schema.NODE, "IPSec initiator"), new ColumnMetadata(COL_INIT_INTERFACE, Schema.INTERFACE, "Initiator Interface"), new ColumnMetadata(COL_INIT_IP, Schema.IP, "Initiator IP"), new ColumnMetadata(COL_RESPONDER, Schema.NODE, "IPSec responder"), new ColumnMetadata(COL_RESPONDER_INTERFACE, Schema.INTERFACE, "Responder Interface"), new ColumnMetadata(COL_RESPONDER_IP, Schema.IP, "Responder IP"), new ColumnMetadata( COL_TUNNEL_INTERFACES, Schema.STRING, "Tunnel interfaces pair used in peering session"), new ColumnMetadata(COL_STATUS, Schema.STRING, "IPSec session status")); } }
if (cm.getIsKey()) { diffColumnMetatadata.add( new ColumnMetadata(cm.getName(), cm.getSchema(), cm.getDescription(), true, false)); new ColumnMetadata(COL_KEY_PRESENCE, Schema.STRING, COL_KEY_PRESENCE_DESC, false, true)); if (cm.getIsKey()) { continue; new ColumnMetadata( baseColumnName(cm.getName()), cm.getSchema(), cm.getDescription(), false, false), new ColumnMetadata( deltaColumnName(cm.getName()), cm.getSchema(), cm.getDescription(), false, false));
public Operand(Type type, @Nullable Object value) { _type = type; _value = value; if (type == Type.COLUMN) { @SuppressWarnings("unchecked") List<String> columns = (List<String>) _value; for (String column : columns) { if (!ColumnMetadata.isLegalColumnName(column)) { throw new IllegalArgumentException("Illegal operand: Bad column name: " + column); } } } }
/** * Returns the list of values in all columns declared as key in the metadata. * * @param metadata Provides information on which columns are key and their {@link Schema} * @return The list */ public List<Object> getKey(List<ColumnMetadata> metadata) { List<Object> keyList = new LinkedList<>(); for (ColumnMetadata column : metadata) { if (column.getIsKey()) { keyList.add(get(column.getName(), column.getSchema())); } } return keyList; }
/** * Returns the list of values in all columns declared as value in the metadata. * * @param metadata Provides information on which columns are key and their {@link Schema} * @return The list */ public List<Object> getValue(List<ColumnMetadata> metadata) { List<Object> valueList = new LinkedList<>(); for (ColumnMetadata column : metadata) { if (column.getIsValue()) { valueList.add(get(column.getName(), column.getSchema())); } } return valueList; }
public TableMetadata(List<ColumnMetadata> columnMetadata, @Nullable String textDesc) { checkArgument(columnMetadata != null, "Column metadata cannot be null"); checkArgument(!columnMetadata.isEmpty(), "The number of columns should be greater than zero"); // check if there is a duplicate column name Set<String> duplicateCheckSet = new HashSet<>(); for (ColumnMetadata cm : columnMetadata) { if (!duplicateCheckSet.add(cm.getName())) { throw new IllegalArgumentException( "Cannot have two columns with the same name '" + cm.getName() + "'"); } } // if textDesc is null, make one up using key columns if there are any or all columns if not String desc = textDesc; if (desc == null) { boolean haveKeyColumns = columnMetadata.stream().anyMatch(ColumnMetadata::getIsKey); desc = columnMetadata.stream() .filter(cm -> !haveKeyColumns || cm.getIsKey()) .map(cm -> String.format("${%s}", cm.getName())) .collect(Collectors.joining(" | ")); } _columnMetadata = columnMetadata; _textDesc = desc; }
@Test public void testJacksonDeserializeWithDefaults() { ColumnMetadata c = ColumnMetadata.jsonCreator("name", Schema.STRING, "desc", null, null); assertThat(c.getName(), equalTo("name")); assertThat(c.getDescription(), equalTo("desc")); assertThat(c.getSchema(), equalTo(Schema.STRING)); assertThat(c.getIsKey(), equalTo(true)); assertThat(c.getIsValue(), equalTo(true)); }
@SuppressWarnings({"rawtypes", "unchecked"}) @VisibleForTesting @Nonnull Comparator<Row> columnComparator(ColumnMetadata columnMetadata) { Schema schema = columnMetadata.getSchema(); Comparator schemaComparator = schemaComparator(schema); Comparator comparator = comparing((Row r) -> r.get(columnMetadata.getName(), schema), nullsFirst(schemaComparator)); return comparator; }
/** * 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; }
/** * Does this table metadata contain a column by this name? * * @param columnName The name of the column to check * @return Whether a column by this name exists */ public boolean containsColumn(String columnName) { checkArgument(columnName != null, "Column name cannot be null"); return _columnMetadata.stream().anyMatch(cm -> columnName.equals(cm.getName())); }
.filter(cm -> (!cm.getIsKey() && cm.getIsValue())) .collect(Collectors.toList());
if (cm.getIsKey()) { continue; Object baseValue = baseRow == null ? null : baseRow.get(cm.getName(), cm.getSchema()); Object deltaValue = deltaRow == null ? null : deltaRow.get(cm.getName(), cm.getSchema()); rowBuilder .put(baseColumnName(cm.getName()), baseValue) .put(deltaColumnName(cm.getName()), deltaValue);
@VisibleForTesting static @Nonnull Map<String, MajorIssueConfig> computeMajorIssueConfigs(TableAnswerElement table) { Map<String, ImmutableList.Builder<MinorIssueConfig>> majorIssueConfigs = new HashMap<>(); // For every issue column of every row, extract the issue and use it to update the map table.getMetadata().getColumnMetadata().stream() .filter(c -> c.getSchema().equals(Schema.ISSUE)) .map(ColumnMetadata::getName) .flatMap( column -> table.getRowsList().stream() .filter(row -> row.hasNonNull(column)) .map(row -> row.getIssue(column))) .forEach( issue -> majorIssueConfigs .computeIfAbsent(issue.getType().getMajor(), m -> ImmutableList.builder()) .add( new MinorIssueConfig( issue.getType().getMinor(), issue.getSeverity(), issue.getUrl()))); return CommonUtil.toImmutableMap( majorIssueConfigs, Entry::getKey, // major issue type e -> new MajorIssueConfig(e.getKey(), e.getValue().build())); }
/** * Returns a {@link TableMetadata} object based on the (outer) question and the metadata for the * innerquestion, by selecting only columns mentioned in the question. * * @param question The outer question * @param innerMetadata The inner metadata * @return The final metadata */ static TableMetadata createMetadata(FilterTableQuestion question, TableMetadata innerMetadata) { if (question.getColumns() == null) { return innerMetadata; } else { List<ColumnMetadata> innerColumns = innerMetadata.getColumnMetadata(); // check if columns mentions names that are not in the innermetadata Sets.SetView<String> unknownColumns = Sets.difference( question.getColumns(), innerColumns.stream().map(ColumnMetadata::getName).collect(Collectors.toSet())); if (!unknownColumns.isEmpty()) { throw new IllegalArgumentException("Unknown columns: " + unknownColumns); } List<ColumnMetadata> columnMetadata = innerColumns.stream() .filter(cm -> question.getColumns().contains(cm.getName())) .collect(Collectors.toList()); return new TableMetadata(columnMetadata, innerMetadata.getTextDesc()); } }
/** Create column metadata. */ @VisibleForTesting static List<ColumnMetadata> getColumnMetadata() { return ImmutableList.of( new ColumnMetadata(COL_NODE, Schema.NODE, "Node hostname"), new ColumnMetadata(COL_VRFNAME, Schema.STRING, "VRF name"), new ColumnMetadata(COL_INTERFACE_NAME, Schema.STRING, "Interface name"), new ColumnMetadata(COL_IP, Schema.IP, "IP address"), new ColumnMetadata(COL_MASK, Schema.INTEGER, "IP address"), new ColumnMetadata( COL_ACTIVE, Schema.BOOLEAN, "Whether the interface with given IP is active")); } }
throw new IllegalArgumentException(message); Schema schema = columnMetadata.getSchema(); Function<Row, Integer> rowToInteger; if (schema.equals(Schema.INTEGER)) {
public ColumnMetadata( String name, Schema schema, String description, @Nullable Boolean isKey, @Nullable Boolean isValue) { if (!isLegalColumnName(name)) { throw new IllegalArgumentException( String.format( "Illegal column name '%s'. Column names should match '%s", name, COLUMN_NAME_PATTERN)); } _name = name; _schema = schema; _description = description; _isKey = firstNonNull(isKey, true); _isValue = firstNonNull(isValue, true); }
static TableMetadata getTableMetadata() { List<ColumnMetadata> columnMetadata = ImmutableList.of( new ColumnMetadata(COL_NODE, Schema.NODE, "The node where action takes place"), new ColumnMetadata(COL_VRF, Schema.STRING, "The VRF where action takes place"), new ColumnMetadata( COL_PEER, Schema.NODE, "The node's neighbor to which the action applies"), new ColumnMetadata(COL_ACTION, Schema.STRING, "The action that takes place"), new ColumnMetadata(COL_PREFIX, Schema.PREFIX, "The prefix in question")); return new TableMetadata(columnMetadata, TEXT_DESC); }
Set<String> issueColumns = tableMetadata.getColumnMetadata().stream() .filter(cm -> cm.getSchema().equals(Schema.ISSUE)) .map(ColumnMetadata::getName) .collect(ImmutableSet.toImmutableSet());
assertThat(ColumnMetadata.isLegalColumnName("~colNamewith~Tilde"), equalTo(true)); assertThat(ColumnMetadata.isLegalColumnName("_colNameWith_UnderScore"), equalTo(true)); assertThat(ColumnMetadata.isLegalColumnName("-colNameWith-hyphen"), equalTo(false)); assertThat(ColumnMetadata.isLegalColumnName("colNameWith-hyphen"), equalTo(true)); assertThat(ColumnMetadata.isLegalColumnName("column name"), equalTo(false)); assertThat(ColumnMetadata.isLegalColumnName(".test.colName"), equalTo(false)); assertThat(ColumnMetadata.isLegalColumnName("test.colName"), equalTo(true)); assertThat(ColumnMetadata.isLegalColumnName("@test@colName"), equalTo(false)); assertThat(ColumnMetadata.isLegalColumnName("test@colName"), equalTo(true)); assertThat(ColumnMetadata.isLegalColumnName("_colName_With~Under-Score0-9"), equalTo(true)); ColumnMetadata.isLegalColumnName("colNameWith!@#$%^&*()+Characters"), equalTo(false)); assertThat(ColumnMetadata.isLegalColumnName(""), equalTo(false)); assertThat(ColumnMetadata.isLegalColumnName("a"), equalTo(true)); ColumnMetadata.isLegalColumnName("~col_name_test:default:192.168.10.1/24~"), equalTo(true));