@VisibleForTesting static SelfDescribingObject getRemoteAs(@Nonnull BgpPeerConfig peer) { if (peer instanceof BgpActivePeerConfig) { return new SelfDescribingObject(Schema.LONG, ((BgpActivePeerConfig) peer).getRemoteAs()); } if (peer instanceof BgpPassivePeerConfig) { return new SelfDescribingObject( Schema.list(Schema.LONG), ((BgpPassivePeerConfig) peer).getRemoteAs()); } throw new IllegalArgumentException( String.format("Peer is neither Active nor Passive: %s", peer)); }
private static TableMetadata createMetadata() { ImmutableList<ColumnMetadata> columnMetadata = ImmutableList.of( new ColumnMetadata(COL_FLOW, Schema.FLOW, "The flow", true, true), new ColumnMetadata( COL_BASE_TRACES, Schema.set(Schema.FLOW_TRACE), "The flow traces in the BASE environment", false, true), new ColumnMetadata( COL_DELTA_TRACES, Schema.set(Schema.FLOW_TRACE), "The flow traces in the DELTA environment", false, true)); return new TableMetadata(columnMetadata, "Flows with reduced reachability"); }
switch (schema.getType()) { case BASE: return convertType(jsonNode, schema.getBaseType()); case LIST: List<JsonNode> list = new TypeReference<List<JsonNode>>() {}); return list.stream() .map(in -> convertType(in, schema.getInnerSchema())) .collect(Collectors.toList()); case SET: new TypeReference<Set<JsonNode>>() {}); return set.stream() .map(in -> convertType(in, schema.getInnerSchema())) .collect(Collectors.toSet()); default: throw new IllegalArgumentException("Cannot handle Schema of type: " + schema.getType());
@SuppressWarnings({"rawtypes", "unchecked"}) private @Nonnull Comparator<?> schemaComparator(Schema schema) { if (schema.equals(Schema.ACL_TRACE)) { return COMPARATOR_ACL_TRACE; } else if (schema.equals(Schema.BOOLEAN)) { return naturalOrder(); } else if (schema.equals(Schema.DOUBLE)) { return naturalOrder(); } else if (schema.equals(Schema.FLOW)) { return naturalOrder(); } else if (schema.equals(Schema.FLOW_TRACE)) { return naturalOrder(); } else if (schema.equals(Schema.INTEGER)) { return naturalOrder(); } else if (schema.equals(Schema.INTERFACE)) { return naturalOrder(); } else if (schema.equals(Schema.IP)) { return naturalOrder(); } else if (schema.equals(Schema.ISSUE)) { return comparing(Issue::getSeverity); } else if (schema.getType() == Type.LIST) { Comparator schemaComparator = schemaComparator(schema.getInnerSchema()); return lexicographical(nullsFirst(schemaComparator)); } else if (schema.equals(Schema.LONG)) { return naturalOrder(); } else if (schema.equals(Schema.NODE)) { return COMPARATOR_NODE; } else if (schema.equals(Schema.PREFIX)) { return naturalOrder(); } else if (schema.getType() == Type.SET) {
if (targetSchema.equals(Schema.STRING) && outputPropertyValue != null && !(outputPropertyValue instanceof String)) { outputPropertyValue = outputPropertyValue.toString(); } else if ((targetSchema.equals(Schema.list(Schema.STRING)) || targetSchema.equals(Schema.set(Schema.STRING))) && outputPropertyValue != null && outputPropertyValue instanceof Collection<?>) { : e.toString()); outputPropertyValue = targetSchema.getType() == Type.LIST ? stream.collect(Collectors.toList()) : stream.collect(Collectors.toSet());
@Test public void isIntBased() { assertThat(Schema.INTEGER.isIntBased(), equalTo(true)); assertThat(Schema.list(Schema.INTEGER).isIntBased(), equalTo(true)); assertThat(Schema.set(Schema.INTEGER).isIntBased(), equalTo(true)); assertThat(Schema.set(Schema.list(Schema.INTEGER)).isIntBased(), equalTo(true)); assertThat(Schema.list(Schema.STRING).isIntBased(), equalTo(false)); }
@Test public void constructorTest() { new EqualsTester() .addEqualityGroup(new Schema("Set<List<Integer>>"), Schema.set(Schema.list(Schema.INTEGER))) .addEqualityGroup(new Schema("List<Set<Integer>>"), Schema.list(Schema.set(Schema.INTEGER))) .addEqualityGroup(new Schema("Set<Set<Integer>>"), Schema.set(Schema.set(Schema.INTEGER))) .addEqualityGroup(new Schema("List<Integer>"), Schema.list(Schema.INTEGER)) .addEqualityGroup(new Schema("Set<Integer>"), Schema.set(Schema.INTEGER)) .addEqualityGroup(new Schema("Integer"), Schema.INTEGER) .addEqualityGroup(new Schema("String"), Schema.STRING) .testEquals(); }
@Test public void isCollection() { assertThat(Schema.INTEGER.isCollection(), equalTo(false)); assertThat(Schema.list(Schema.INTEGER).isCollection(), equalTo(true)); assertThat(Schema.set(Schema.INTEGER).isCollection(), equalTo(true)); assertThat(Schema.set(Schema.list(Schema.INTEGER)).isCollection(), equalTo(true)); } }
case FUNCOFSUFFIX: if (!extraction.getSchemaAsObject().isIntBased()) { throw new BatfishException( "schema must be INT(LIST) with funcofsuffix-based extraction hint"); ? new TextNode(resultEntry.getValue().getPrefixPart(jpeHint.getIndex())) : resultEntry.getValue().getSuffix(); confirmValueType(value, extraction.getSchemaAsObject().getBaseType()); extractedList.add(value); if (extraction.getSchemaAsObject().getType() == Type.LIST || extraction.getSchemaAsObject().getType() == Type.SET) { ArrayNode arrayNode = BatfishObjectMapper.mapper().valueToTree(extractedList); _displayValues.get(entry.getKey()).put(displayVar, arrayNode);
@Test public void displayHintsParsingTest() throws IOException { String text = CommonUtil.readResource("org/batfish/datamodel/questions/displayHintsTest.json"); DisplayHints displayHints = BatfishObjectMapper.mapper().readValue(text, DisplayHints.class); // here, we only test for ExtractionHint level concepts // tests that sit with jsonpath question validate if prefix/suffix filters are parsed correctly assertThat( displayHints.getTextDesc().equals("${mynode} has nothing on ${myinterface}"), equalTo(true)); Map<String, Extraction> extractions = displayHints.getExtractions(); assertThat(extractions.size(), equalTo(3)); Extraction hint0 = extractions.get("node1"); assertThat(hint0.getSchemaAsObject(), equalTo(Schema.STRING)); assertThat( hint0.getSchemaAsObject().getBaseType().getCanonicalName(), equalTo("java.lang.String")); assertThat(hint0.getMethod().containsKey("use"), equalTo(true)); Extraction hint1 = extractions.get("interfaces1"); assertThat(hint1.getSchemaAsObject(), equalTo(Schema.list(Schema.STRING))); Extraction hint2 = extractions.get("nodes1"); assertThat(hint2.getSchemaAsObject(), equalTo(Schema.list(Schema.INTEGER))); } }
varName, composition.getDictionary().get(varName), compositionName)); if (extractions.get(varName).getSchemaAsObject().getType() == Type.LIST || extractions.get(varName).getSchemaAsObject().getType() == Type.SET) { if (!_displayValues.get(resultKey).containsKey(varName)) { throw new BatfishException( String varName = pEntry.getValue(); JsonNode varNode = _displayValues.get(resultKey).get(varName); if (extractions.get(varName).getSchemaAsObject().getType() == Type.LIST || extractions.get(varName).getSchemaAsObject().getType() == Type.SET) { object.set(propertyName, varNode.get(index)); } else { confirmValueType(object, composition.getSchemaAsObject().getBaseType()); arrayNode.add(object);
private void extractValuesFromPrefix( String displayVar, Extraction extraction, JsonPathExtractionHint jpeHint) { if (extraction.getSchemaAsObject().getType() == Type.LIST || extraction.getSchemaAsObject().getType() == Type.SET) { throw new BatfishException("Prefix-based hints are incompatible with list or set types"); } for (Entry<String, JsonPathResultEntry> entry : _result.entrySet()) { if (!_displayValues.containsKey(entry.getKey())) { _displayValues.put(entry.getKey(), new HashMap<>()); } String prefixPart = entry.getValue().getPrefixPart(jpeHint.getIndex()); _displayValues.get(entry.getKey()).put(displayVar, new TextNode(prefixPart)); } }
private void doCompositionSingleton( String resultKey, String compositionName, Composition composition) { ObjectMapper mapper = BatfishObjectMapper.mapper(); ObjectNode object = mapper.createObjectNode(); for (Entry<String, String> pEntry : composition.getDictionary().entrySet()) { String propertyName = pEntry.getKey(); String varName = pEntry.getValue(); if (!_displayValues.get(resultKey).containsKey(varName)) { throw new BatfishException( String.format( "varName '%s' for property '%s' of composition '%s' is not in display values", varName, propertyName, compositionName)); } object.set(propertyName, _displayValues.get(resultKey).get(varName)); } confirmValueType(object, composition.getSchemaAsObject().getBaseType()); _displayValues.get(resultKey).put(compositionName, object); }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof SelfDescribingObject)) { return false; } SelfDescribingObject rhs = (SelfDescribingObject) obj; return Objects.equals(_name, rhs._name) && _schema.equals(rhs._schema) && Objects.equals(_value, rhs._value); }
/** Generates a set Schema from the give base schema */ public static final Schema set(Schema baseSchema) { return new Schema("Set<" + baseSchema._schemaStr + ">"); }
case FUNCOFSUFFIX: if (!extraction.getSchema().isIntBased()) { throw new BatfishException( "schema must be INT(LIST) with funcofsuffix-based extraction hint"); ? new TextNode(result.getPrefixPart(extraction.getIndex())) : result.getSuffix(); confirmValueType(value, extraction.getSchema().getBaseType()); extractedList.add(value); if (extraction.getSchema().getType() == Type.LIST || extraction.getSchema().getType() == Type.SET) { answerValues.set(varName, BatfishObjectMapper.mapper().valueToTree(extractedList)); } else {
varName, composition.getDictionary().get(varName), compositionName)); if (extractions.get(varName).getSchema().getType() == Type.LIST || extractions.get(varName).getSchema().getType() == Type.SET) { if (answerValues.get(varName) == null) { throw new BatfishException( String varName = pEntry.getValue(); JsonNode varNode = answerValues.get(varName); if (extractions.get(varName).getSchema().getType() == Type.LIST || extractions.get(varName).getSchema().getType() == Type.SET) { object.set(propertyName, varNode.get(index)); } else { confirmValueType(object, composition.getSchema().getBaseType()); arrayNode.add(object);
private void doCompositions( Map<String, Composition> compositions, Map<String, Extraction> extractions) { for (String resultKey : _result.keySet()) { for (Entry<String, Composition> cEntry : compositions.entrySet()) { String compositionName = cEntry.getKey(); Composition composition = cEntry.getValue(); if (composition.getSchemaAsObject().getType() == Type.LIST || composition.getSchemaAsObject().getType() == Type.SET) { doCompositionList(resultKey, compositionName, composition, extractions); } else { doCompositionSingleton(resultKey, compositionName, composition); } } } }
private static void doCompositionSingleton( String compositionName, JsonPathToTableComposition composition, ObjectNode answerValues) { ObjectMapper mapper = BatfishObjectMapper.mapper(); ObjectNode object = mapper.createObjectNode(); for (Entry<String, String> pEntry : composition.getDictionary().entrySet()) { String propertyName = pEntry.getKey(); String varName = pEntry.getValue(); if (answerValues.get(varName) == null) { throw new BatfishException( String.format( "varName '%s' for property '%s' of composition '%s' is not in display values", varName, propertyName, compositionName)); } object.set(propertyName, answerValues.get(varName)); } confirmValueType(object, composition.getSchema().getBaseType()); answerValues.set(compositionName, object); }