use of org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory in project netconf by opendaylight.
the class FilterContentValidator method getKeyValues.
private Map<QName, Object> getKeyValues(final List<String> path, final XmlElement filterContent, final DataSchemaNode parentSchemaNode, final ListSchemaNode listSchemaNode) {
XmlElement current = filterContent;
// find list element
for (final String pathElement : path) {
final List<XmlElement> childElements = current.getChildElements(pathElement);
// if there are multiple list entries present in the filter, we can't use any keys and must read whole list
if (childElements.size() != 1) {
return Map.of();
}
current = childElements.get(0);
}
final Map<QName, Object> keys = new HashMap<>();
final List<QName> keyDefinition = listSchemaNode.getKeyDefinition();
for (final QName qualifiedName : keyDefinition) {
final Optional<XmlElement> childElements = current.getOnlyChildElementOptionally(qualifiedName.getLocalName());
if (childElements.isEmpty()) {
return Map.of();
}
final Optional<String> keyValue = childElements.get().getOnlyTextContentOptionally();
if (keyValue.isPresent()) {
final LeafSchemaNode listKey = (LeafSchemaNode) listSchemaNode.getDataChildByName(qualifiedName);
if (listKey instanceof IdentityrefTypeDefinition) {
keys.put(qualifiedName, keyValue.get());
} else {
final TypeDefinition<? extends TypeDefinition<?>> keyType = listKey.getType();
if (keyType instanceof IdentityrefTypeDefinition || keyType instanceof LeafrefTypeDefinition) {
final Document document = filterContent.getDomElement().getOwnerDocument();
final NamespaceContext nsContext = new UniversalNamespaceContextImpl(document, false);
final EffectiveModelContext modelContext = schemaContext.getCurrentContext();
final XmlCodecFactory xmlCodecFactory = XmlCodecFactory.create(modelContext);
final SchemaInferenceStack resolver = SchemaInferenceStack.of(modelContext, Absolute.of(parentSchemaNode.getQName(), listSchemaNode.getQName(), listKey.getQName()));
final TypeAwareCodec<?, NamespaceContext, XMLStreamWriter> typeCodec = xmlCodecFactory.codecFor(listKey, resolver);
final Object deserializedKeyValue = typeCodec.parseValue(nsContext, keyValue.get());
keys.put(qualifiedName, deserializedKeyValue);
} else {
final Object deserializedKey = TypeDefinitionAwareCodec.from(keyType).deserialize(keyValue.get());
keys.put(qualifiedName, deserializedKey);
}
}
}
}
return keys;
}
Aggregations