X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fmdsal-netconf-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fmdsal%2Fconnector%2Fops%2Fget%2FFilterContentValidator.java;h=d104fddb96cdb86f18a76c3c80e901d768655a0b;hb=45cfee1861924b4a8086d38079ce8cbd320386d6;hp=459ae7c8a443e8d9daa4547658bbe646df291305;hpb=ec06edb7dd2288f618f3fa78b9ccce7754e03810;p=netconf.git diff --git a/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/ops/get/FilterContentValidator.java b/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/ops/get/FilterContentValidator.java index 459ae7c8a4..d104fddb96 100644 --- a/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/ops/get/FilterContentValidator.java +++ b/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/netconf/mdsal/connector/ops/get/FilterContentValidator.java @@ -7,7 +7,6 @@ */ package org.opendaylight.netconf.mdsal.connector.ops.get; -import static org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder; import static org.opendaylight.yangtools.yang.data.util.ParserStreamUtils.findSchemaNodeByNameAndNamespace; import com.google.common.base.Optional; @@ -21,18 +20,27 @@ import java.util.Deque; import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.xml.namespace.NamespaceContext; +import javax.xml.stream.XMLStreamWriter; import org.opendaylight.controller.config.util.xml.DocumentedException; import org.opendaylight.controller.config.util.xml.MissingNameSpaceException; import org.opendaylight.controller.config.util.xml.XmlElement; import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder; +import org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory; +import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec; +import org.opendaylight.yangtools.yang.data.util.codec.TypeAwareCodec; +import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; /** * Class validates filter content against schema context. @@ -42,9 +50,6 @@ public class FilterContentValidator { private static final Logger LOG = LoggerFactory.getLogger(FilterContentValidator.class); private final CurrentSchemaContext schemaContext; - /** - * @param schemaContext current schema context - */ public FilterContentValidator(final CurrentSchemaContext schemaContext) { this.schemaContext = schemaContext; } @@ -52,23 +57,24 @@ public class FilterContentValidator { /** * Validates filter content against this validator schema context. If the filter is valid, * method returns {@link YangInstanceIdentifier} of node which can be used as root for data selection. + * * @param filterContent filter content * @return YangInstanceIdentifier - * @throws DocumentedException if filter content is not valid + * @throws DocumentedException if filter content validation failed */ public YangInstanceIdentifier validate(final XmlElement filterContent) throws DocumentedException { try { final URI namespace = new URI(filterContent.getNamespace()); - final Module module = schemaContext.getCurrentContext().findModuleByNamespaceAndRevision(namespace, null); + final Module module = schemaContext.getCurrentContext().findModules(namespace).iterator().next(); final DataSchemaNode schema = getRootDataSchemaNode(module, namespace, filterContent.getName()); - final FilterTree filterTree = validateNode(filterContent, schema, new FilterTree(schema.getQName(), - Type.OTHER, schema)); + final FilterTree filterTree = validateNode( + filterContent, schema, new FilterTree(schema.getQName(), Type.OTHER, schema)); return getFilterDataRoot(filterTree, filterContent, YangInstanceIdentifier.builder()); - } catch (final DocumentedException e) { - throw e; - } catch (final Exception e) { + } catch (final URISyntaxException e) { + throw new RuntimeException("Wrong namespace in element + " + filterContent.toString(), e); + } catch (final ValidationException e) { LOG.debug("Filter content isn't valid", e); - throw new DocumentedException("Validation failed. Cause: " + e.getMessage(), + throw new DocumentedException("Validation failed. Cause: " + e.getMessage(), e, DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.UNKNOWN_NAMESPACE, DocumentedException.ErrorSeverity.ERROR); @@ -76,10 +82,11 @@ public class FilterContentValidator { } /** - * Returns module's child data node of given name space and name - * @param module module + * Returns module's child data node of given name space and name. + * + * @param module module * @param nameSpace name space - * @param name name + * @param name name * @return child data node schema * @throws DocumentedException if child with given name is not present */ @@ -92,8 +99,8 @@ public class FilterContentValidator { return childNode; } } - throw new DocumentedException("Unable to find node with namespace: " + nameSpace + "in schema context: " + - schemaContext.getCurrentContext().toString(), + throw new DocumentedException("Unable to find node with namespace: " + nameSpace + + "in schema context: " + schemaContext.getCurrentContext().toString(), DocumentedException.ErrorType.APPLICATION, DocumentedException.ErrorTag.UNKNOWN_NAMESPACE, DocumentedException.ErrorSeverity.ERROR); @@ -101,9 +108,10 @@ public class FilterContentValidator { /** * Recursively checks filter elements against the schema. Returns tree of nodes QNames as they appear in filter. - * @param element element to check + * + * @param element element to check * @param parentNodeSchema parent node schema - * @param tree parent node tree + * @param tree parent node tree * @return tree * @throws ValidationException if filter content is not valid */ @@ -124,7 +132,7 @@ public class FilterContentValidator { final DataSchemaNode childSchema = path.getLast(); validateNode(childElement, childSchema, subtree); } catch (URISyntaxException | MissingNameSpaceException e) { - throw new RuntimeException("Wrong namespace in element + " + childElement.toString()); + throw new RuntimeException("Wrong namespace in element + " + childElement.toString(), e); } } return tree; @@ -134,9 +142,10 @@ public class FilterContentValidator { * Searches for YangInstanceIdentifier of node, which can be used as root for data selection. * It goes as deep in tree as possible. Method stops traversing, when there are multiple child elements. If element * represents list and child elements are key values, then it builds YangInstanceIdentifier of list entry. - * @param tree QName tree + * + * @param tree QName tree * @param filterContent filter element - * @param builder builder @return YangInstanceIdentifier + * @param builder builder @return YangInstanceIdentifier */ private YangInstanceIdentifier getFilterDataRoot(FilterTree tree, final XmlElement filterContent, final InstanceIdentifierBuilder builder) { @@ -164,15 +173,15 @@ public class FilterContentValidator { final InstanceIdentifierBuilder builder) { Preconditions.checkArgument(tree.getSchemaNode() instanceof ListSchemaNode); final ListSchemaNode listSchemaNode = (ListSchemaNode) tree.getSchemaNode(); - final List keyDefinition = listSchemaNode.getKeyDefinition(); - final Map map = getKeyValues(pathToList, filterContent, keyDefinition); + + final Map map = getKeyValues(pathToList, filterContent, listSchemaNode); if (!map.isEmpty()) { builder.nodeWithKey(tree.getName(), map); } } private Map getKeyValues(final List path, final XmlElement filterContent, - final List keyDefinition) { + final ListSchemaNode listSchemaNode) { XmlElement current = filterContent; //find list element for (final String pathElement : path) { @@ -184,19 +193,44 @@ public class FilterContentValidator { current = childElements.get(0); } final Map keys = new HashMap<>(); - for (final QName qName : keyDefinition) { - final Optional childElements = current.getOnlyChildElementOptionally(qName.getLocalName()); + final List keyDefinition = listSchemaNode.getKeyDefinition(); + for (final QName qualifiedName : keyDefinition) { + final Optional childElements = + current.getOnlyChildElementOptionally(qualifiedName.getLocalName()); if (!childElements.isPresent()) { return Collections.emptyMap(); } final Optional keyValue = childElements.get().getOnlyTextContentOptionally(); if (keyValue.isPresent()) { - keys.put(qName, keyValue.get()); + final LeafSchemaNode listKey = (LeafSchemaNode) listSchemaNode.getDataChildByName(qualifiedName); + if (listKey instanceof IdentityrefTypeDefinition) { + keys.put(qualifiedName, keyValue.get()); + } else { + if (listKey.getType() instanceof IdentityrefTypeDefinition) { + final Document document = filterContent.getDomElement().getOwnerDocument(); + final NamespaceContext nsContext = new UniversalNamespaceContextImpl(document, false); + final XmlCodecFactory xmlCodecFactory = + XmlCodecFactory.create(schemaContext.getCurrentContext()); + final TypeAwareCodec identityrefTypeCodec = + xmlCodecFactory.codecFor(listKey); + final QName deserializedKey = + (QName) identityrefTypeCodec.parseValue(nsContext, keyValue.get()); + keys.put(qualifiedName, deserializedKey); + } else { + final Object deserializedKey = TypeDefinitionAwareCodec.from(listKey.getType()) + .deserialize(keyValue.get()); + keys.put(qualifiedName, deserializedKey); + } + } } } return keys; } + private enum Type { + LIST, CHOICE_CASE, OTHER + } + /** * Class represents tree of QNames as they are present in the filter. */ @@ -215,20 +249,20 @@ public class FilterContentValidator { } FilterTree addChild(final DataSchemaNode data) { - final Type type; - if (data instanceof ChoiceCaseNode) { - type = Type.CHOICE_CASE; + final Type childType; + if (data instanceof CaseSchemaNode) { + childType = Type.CHOICE_CASE; } else if (data instanceof ListSchemaNode) { - type = Type.LIST; + childType = Type.LIST; } else { - type = Type.OTHER; + childType = Type.OTHER; } - final QName name = data.getQName(); - FilterTree childTree = children.get(name); + final QName childName = data.getQName(); + FilterTree childTree = children.get(childName); if (childTree == null) { - childTree = new FilterTree(name, type, data); + childTree = new FilterTree(childName, childType, data); } - children.put(name, childTree); + children.put(childName, childTree); return childTree; } @@ -249,12 +283,10 @@ public class FilterContentValidator { } } - private enum Type { - LIST, CHOICE_CASE, OTHER - } - private static class ValidationException extends Exception { - public ValidationException(final XmlElement parent, final XmlElement child) { + private static final long serialVersionUID = 1L; + + ValidationException(final XmlElement parent, final XmlElement child) { super("Element " + child + " can't be child of " + parent); } }