2 * Copyright (c) 2013 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.yangtools.yang.model.api;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.NoSuchElementException;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.yangtools.yang.common.QName;
23 * Node which can contains other nodes.
25 public interface DataNodeContainer {
27 * Returns set of all newly defined types within this DataNodeContainer.
29 * @return typedef statements in lexicographical order
31 Collection<? extends TypeDefinition<?>> getTypeDefinitions();
34 * Returns set of all child nodes defined within this DataNodeContainer. Although the return type is a collection,
35 * each node is guaranteed to be present at most once.
38 * Note that the nodes returned are <strong>NOT</strong> {@code data nodes}, but rather {@link DataSchemaNode}s,
39 * hence {@link ChoiceSchemaNode} and {@link CaseSchemaNode} are present instead of their children. This
40 * is consistent with {@code schema tree}.
42 * @return child nodes in lexicographical order
44 Collection<? extends DataSchemaNode> getChildNodes();
47 * Returns set of all groupings defined within this DataNodeContainer.
49 * @return grouping statements in lexicographical order
51 Collection<? extends GroupingDefinition> getGroupings();
54 * Returns the child node corresponding to the specified name.
57 * Note that the nodes searched are <strong>NOT</strong> {@code data nodes}, but rather {@link DataSchemaNode}s,
58 * hence {@link ChoiceSchemaNode} and {@link CaseSchemaNode} are returned instead of their matching children. This
59 * is consistent with {@code schema tree}.
61 * @param name QName of child
62 * @return child node of this DataNodeContainer if child with given name is present, null otherwise
63 * @deprecated Use {@link #findDataChildByName(QName)} instead.
64 * @throws NullPointerException if {@code name} is null
67 default @Nullable DataSchemaNode getDataChildByName(final QName name) {
68 return findDataChildByName(name).orElse(null);
72 * Returns the child node corresponding to the specified name.
75 * Note that the nodes searched are <strong>NOT</strong> {@code data nodes}, but rather {@link DataSchemaNode}s,
76 * hence {@link ChoiceSchemaNode} and {@link CaseSchemaNode} are returned instead of their matching children.
78 * @param name QName of child
79 * @return child node of this DataNodeContainer if child with given name is present, empty otherwise
80 * @throws NullPointerException if {@code name} is null
82 Optional<DataSchemaNode> findDataChildByName(QName name);
85 * Returns the child node corresponding to the specified name.
88 * Note that the nodes searched are <strong>NOT</strong> {@code data nodes}, but rather {@link DataSchemaNode}s,
89 * hence {@link ChoiceSchemaNode} and {@link CaseSchemaNode} are returned instead of their matching children.
91 * @param first QName of first child
92 * @param others QNames of subsequent children
93 * @return child node of this DataNodeContainer if child with given name is present, empty otherwise
94 * @throws NullPointerException if any argument is null
96 default Optional<DataSchemaNode> findDataChildByName(final QName first, final QName... others) {
97 Optional<DataSchemaNode> optCurrent = findDataChildByName(first);
98 for (QName qname : others) {
99 if (optCurrent.isPresent()) {
100 final DataSchemaNode current = optCurrent.get();
101 if (current instanceof DataNodeContainer) {
102 optCurrent = ((DataNodeContainer) current).findDataChildByName(qname);
107 return Optional.empty();
113 * Returns grouping nodes used ny this container.
115 * @return Set of all uses nodes defined within this DataNodeContainer
117 Collection<? extends UsesNode> getUses();
120 * Returns a {@code data node} identified by a QName. This method is distinct from
121 * {@link #findDataChildByName(QName)} in that it skips over {@link ChoiceSchemaNode}s and {@link CaseSchemaNode}s,
122 * hence mirroring layout of the {@code data tree}, not {@code schema tree}.
124 * @param name QName identifier of the data node
125 * @return Direct or indirect child of this DataNodeContainer which is a {@code data node}, empty otherwise
126 * @throws NullPointerException if {@code name} is null
129 default Optional<DataSchemaNode> findDataTreeChild(final QName name) {
130 // First we try to find a direct child and check if it is a data node (as per RFC7950)
131 final Optional<DataSchemaNode> optDataChild = findDataChildByName(name);
132 if (HelperMethods.isDataNode(optDataChild)) {
136 // There either is no such node present, or there are Choice/CaseSchemaNodes with the same name involved,
137 // hence we have to resort to a full search.
138 for (DataSchemaNode child : getChildNodes()) {
139 if (child instanceof ChoiceSchemaNode) {
140 for (CaseSchemaNode choiceCase : ((ChoiceSchemaNode) child).getCases()) {
141 final Optional<DataSchemaNode> caseChild = choiceCase.findDataTreeChild(name);
142 if (caseChild.isPresent()) {
149 return Optional.empty();
153 * Returns a {@code data node} identified by a series of QNames. This is equivalent to incrementally calling
154 * {@link #findDataTreeChild(QName)}.
156 * @param path Series of QNames towards identifying the requested data node
157 * @return Direct or indirect child of this DataNodeContainer which is a {@code data node}, empty otherwise
158 * @throws IllegalArgumentException if {@code path} is determined to go beyond a not-container-nor-list node.
159 * @throws NoSuchElementException if {@code path} is empty
160 * @throws NullPointerException if {@code path} is null or contains a null
163 default Optional<DataSchemaNode> findDataTreeChild(final QName... path) {
164 return findDataTreeChild(Arrays.asList(path));
168 * Returns a {@code data node} identified by a series of QNames. This is equivalent to incrementally calling
169 * {@link #findDataTreeChild(QName)}.
171 * @param path Series of QNames towards identifying the requested data node
172 * @return Direct or indirect child of this DataNodeContainer which is a {@code data node}, empty otherwise
173 * @throws IllegalArgumentException if {@code path} is determined to go beyond a not-container-nor-list node.
174 * @throws NoSuchElementException if {@code path} is empty
175 * @throws NullPointerException if {@code path} is null or contains a null
178 default Optional<DataSchemaNode> findDataTreeChild(final Iterable<QName> path) {
179 final Iterator<QName> it = path.iterator();
180 DataNodeContainer parent = this;
182 final Optional<DataSchemaNode> optChild = parent.findDataTreeChild(requireNonNull(it.next()));
183 if (optChild.isEmpty() || !it.hasNext()) {
187 final DataSchemaNode child = optChild.get();
188 checkArgument(child instanceof DataNodeContainer, "Path %s extends beyond terminal child %s", path, child);
189 parent = (DataNodeContainer) child;