Search in sources :

Example 1 with XmlCodecFactory

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;
}
Also used : SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) IdentityrefTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition) HashMap(java.util.HashMap) QName(org.opendaylight.yangtools.yang.common.QName) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) Document(org.w3c.dom.Document) LeafrefTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition) NamespaceContext(javax.xml.namespace.NamespaceContext) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) XmlCodecFactory(org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Aggregations

HashMap (java.util.HashMap)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 XmlElement (org.opendaylight.netconf.api.xml.XmlElement)1 QName (org.opendaylight.yangtools.yang.common.QName)1 XmlCodecFactory (org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory)1 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)1 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)1 IdentityrefTypeDefinition (org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition)1 LeafrefTypeDefinition (org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition)1 SchemaInferenceStack (org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack)1 Document (org.w3c.dom.Document)1