2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.mdsal.connector.ops.get;
10 import static org.opendaylight.yangtools.yang.data.util.ParserStreamUtils.findSchemaNodeByNameAndNamespace;
13 import java.net.URISyntaxException;
14 import java.util.Collection;
15 import java.util.Deque;
16 import java.util.HashMap;
17 import java.util.List;
19 import org.opendaylight.controller.config.util.xml.DocumentedException;
20 import org.opendaylight.controller.config.util.xml.MissingNameSpaceException;
21 import org.opendaylight.controller.config.util.xml.XmlElement;
22 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
31 * Class validates filter content against schema context.
33 public class FilterContentValidator {
35 private final CurrentSchemaContext schemaContext;
38 * @param schemaContext current schema context
40 public FilterContentValidator(final CurrentSchemaContext schemaContext) {
41 this.schemaContext = schemaContext;
45 * Validates filter content against this validator schema context. If the filter is valid, method returns {@link YangInstanceIdentifier}
46 * of node which can be used as root for data selection.
47 * @param filterContent filter content
48 * @return YangInstanceIdentifier
49 * @throws DocumentedException if filter content is not valid
51 public YangInstanceIdentifier validate(final XmlElement filterContent) throws DocumentedException {
53 final URI namespace = new URI(filterContent.getNamespace());
54 final Module module = schemaContext.getCurrentContext().findModuleByNamespaceAndRevision(namespace, null);
55 final DataSchemaNode schema = getRootDataSchemaNode(module, namespace, filterContent.getName());
56 final FilterTree filterTree = validateNode(filterContent, schema, new FilterTree(schema.getQName(), Type.OTHER));
57 return getFilterDataRoot(filterTree, YangInstanceIdentifier.builder());
58 } catch (final DocumentedException e) {
60 } catch (final Exception e) {
61 throw new DocumentedException("Validation failed. Cause: " + e.getMessage(),
62 DocumentedException.ErrorType.APPLICATION,
63 DocumentedException.ErrorTag.UNKNOWN_NAMESPACE,
64 DocumentedException.ErrorSeverity.ERROR);
69 * Returns module's child data node of given name space and name
70 * @param module module
71 * @param nameSpace name space
73 * @return child data node schema
74 * @throws DocumentedException if child with given name is not present
76 private DataSchemaNode getRootDataSchemaNode(final Module module, final URI nameSpace, final String name) throws DocumentedException {
77 final Collection<DataSchemaNode> childNodes = module.getChildNodes();
78 for (final DataSchemaNode childNode : childNodes) {
79 final QName qName = childNode.getQName();
80 if (qName.getNamespace().equals(nameSpace) && qName.getLocalName().equals(name)) {
84 throw new DocumentedException("Unable to find node with namespace: " + nameSpace + "in schema context: " + schemaContext.getCurrentContext().toString(),
85 DocumentedException.ErrorType.APPLICATION,
86 DocumentedException.ErrorTag.UNKNOWN_NAMESPACE,
87 DocumentedException.ErrorSeverity.ERROR);
91 * Recursively checks filter elements against the schema. Returns tree of nodes QNames as they appear in filter.
92 * @param element element to check
93 * @param parentNodeSchema parent node schema
94 * @param tree parent node tree
96 * @throws ValidationException if filter content is not valid
98 private FilterTree validateNode(final XmlElement element, final DataSchemaNode parentNodeSchema, final FilterTree tree) throws ValidationException {
99 final List<XmlElement> childElements = element.getChildElements();
100 for (final XmlElement childElement : childElements) {
102 final Deque<DataSchemaNode> path = findSchemaNodeByNameAndNamespace(parentNodeSchema, childElement.getName(), new URI(childElement.getNamespace()));
103 if (path.isEmpty()) {
104 throw new ValidationException(element, childElement);
106 FilterTree subtree = tree;
107 for (final DataSchemaNode dataSchemaNode : path) {
108 subtree = subtree.addChild(dataSchemaNode);
110 final DataSchemaNode childSchema = path.getLast();
111 validateNode(childElement, childSchema, subtree);
112 } catch (URISyntaxException | MissingNameSpaceException e) {
113 throw new RuntimeException("Wrong namespace in element + " + childElement.toString());
120 * Searches for YangInstanceIdentifier of node, which can be used as root for data selection.
121 * It goes as deep in tree as possible. Method stops traversing, when there are multiple child elements
122 * or when it encounters list node.
123 * @param tree QName tree
124 * @param builder builder
125 * @return YangInstanceIdentifier
127 private YangInstanceIdentifier getFilterDataRoot(FilterTree tree, final YangInstanceIdentifier.InstanceIdentifierBuilder builder) {
128 builder.node(tree.getName());
129 while (tree.getChildren().size() == 1) {
130 final FilterTree child = tree.getChildren().iterator().next();
131 if (child.getType() == Type.CHOICE_CASE) {
135 builder.node(child.getName());
136 if (child.getType() == Type.LIST) {
137 return builder.build();
141 return builder.build();
145 * Class represents tree of QNames as they are present in the filter.
147 private static class FilterTree {
149 private final QName name;
150 private final Type type;
151 private final Map<QName, FilterTree> children;
153 FilterTree(final QName name, final Type type) {
156 this.children = new HashMap<>();
159 FilterTree addChild(final DataSchemaNode data) {
161 if (data instanceof ChoiceCaseNode) {
162 type = Type.CHOICE_CASE;
163 } else if (data instanceof ListSchemaNode) {
168 final QName name = data.getQName();
169 FilterTree childTree = children.get(name);
170 if (childTree == null) {
171 childTree = new FilterTree(name, type);
173 children.put(name, childTree);
177 Collection<FilterTree> getChildren() {
178 return children.values();
191 LIST, CHOICE_CASE, OTHER
194 private static class ValidationException extends Exception {
195 public ValidationException(final XmlElement parent, final XmlElement child) {
196 super("Element " + child + " can't be child of " + parent);