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 {
@JsonCreator public DisplayHints( @JsonProperty(PROP_COMPOSITIONS) Map<String, Composition> compositions, @JsonProperty(PROP_EXTRACTIONS) Map<String, Extraction> extractions, @JsonProperty(PROP_TEXT_DESC) String textDesc) { _compositions = firstNonNull(compositions, new HashMap<>()); _extractions = firstNonNull(extractions, new HashMap<>()); _textDesc = firstNonNull(textDesc, ""); Set<String> varsInEntities = new HashSet<>(); for (Entry<String, Composition> entry : _compositions.entrySet()) { entry.getValue().validate(entry.getKey()); varsInEntities.addAll(entry.getValue().getVars()); } for (Entry<String, Extraction> entry : _extractions.entrySet()) { entry.getValue().validate(entry.getKey()); } // all extraction vars mentioned in entity configuration should have extraction hints Set<String> varsInExtractionHints = _extractions.keySet(); SetView<String> missingExtractionVars = Sets.difference(varsInEntities, varsInExtractionHints); if (!missingExtractionVars.isEmpty()) { throw new BatfishException( "entities refer to variables that are not in extraction hints: " + missingExtractionVars); } // the names of entities and extraction vars should have no overlap Set<String> commonNames = Sets.intersection(varsInExtractionHints, _compositions.keySet()); if (!commonNames.isEmpty()) { throw new BatfishException( "entities and extraction vars should not have common names: " + commonNames); } }
outliers.setUse(UseType.SUFFIXOFSUFFIX); outliers.setFilter("$.outliers[*]"); Extraction outliersEx = new Extraction(); try { outliersEx.setMethod( BatfishObjectMapper.clone(outliers, new TypeReference<Map<String, JsonNode>>() {})); } catch (IOException e) { throw new BatfishException("Error creating extraction", e); outliersEx.setSchema(Schema.list(Schema.STRING)); extractions.put("outlierNames", outliersEx); role.setUse(UseType.SUFFIXOFSUFFIX); role.setFilter("$.role"); Extraction roleEx = new Extraction(); try { roleEx.setMethod( BatfishObjectMapper.clone(role, new TypeReference<Map<String, JsonNode>>() {})); } catch (IOException e) { throw new BatfishException("Error creating extraction", e); roleEx.setSchema(Schema.STRING); extractions.put("role", roleEx); name.setUse(UseType.SUFFIXOFSUFFIX); name.setFilter("$.name"); Extraction nameEx = new Extraction(); try { nameEx.setMethod(
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);
public static JsonPathExtractionHint fromExtractionHint(Extraction extraction) throws IOException { JsonPathExtractionHint jpExtractionHint = BatfishObjectMapper.clone(extraction.getMethod(), JsonPathExtractionHint.class);
@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))); } }
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)); } }