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 com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.base.MoreObjects;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterables;
17 import com.google.common.collect.UnmodifiableIterator;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.NoSuchElementException;
21 import java.util.Objects;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.yangtools.concepts.Immutable;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
27 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
30 * Represents unique path to the every node inside the module.
32 * @deprecated This path is not really unique, as it does not handle YANG namespace overlap correctly. There are two
33 * different replacements for this class:
35 * <li>{@link SchemaNodeIdentifier} for use in
36 * <a href="https://datatracker.ietf.org/doc/html/rfc7950#section-6.5">YANG schema addressing</a>
38 * <li>{@link EffectiveStatementInference} for use in contexts where the intent is to exchange pointer
39 * to a specific statement. Unlike SchemaPath, though, it does not require additional lookup in most
42 * This class is scheduled for removal in the next major release.
44 @Deprecated(since = "7.0.8", forRemoval = true)
45 public abstract class SchemaPath implements Immutable {
48 * An absolute SchemaPath.
50 private static final class AbsoluteSchemaPath extends SchemaPath {
51 private AbsoluteSchemaPath(final SchemaPath parent, final QName qname) {
56 public boolean isAbsolute() {
61 public AbsoluteSchemaPath createChild(final QName element) {
62 return new AbsoluteSchemaPath(this, requireNonNull(element));
67 * A relative SchemaPath.
69 private static final class RelativeSchemaPath extends SchemaPath {
70 private RelativeSchemaPath(final SchemaPath parent, final QName qname) {
75 public boolean isAbsolute() {
80 public RelativeSchemaPath createChild(final QName element) {
81 return new RelativeSchemaPath(this, requireNonNull(element));
86 * Shared instance of the conceptual root schema node.
88 public static final @NonNull SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
91 * Shared instance of the "same" relative schema node.
93 public static final @NonNull SchemaPath SAME = new RelativeSchemaPath(null, null);
98 private final SchemaPath parent;
103 private final QName qname;
106 * Cached hash code. We can use this since we are immutable.
108 private final int hash;
110 SchemaPath(final SchemaPath parent, final QName qname) {
111 this.parent = parent;
114 int tmp = Objects.hashCode(parent);
116 tmp = tmp * 31 + qname.hashCode();
123 * Constructs new instance of this class with the concrete path.
126 * list of QName instances which specifies exact path to the
129 * boolean value which specifies if the path is absolute or
132 * @return A SchemaPath instance.
134 public static @NonNull SchemaPath create(final Iterable<QName> path, final boolean absolute) {
135 return (absolute ? ROOT : SAME).createChild(path);
139 * Constructs new instance of this class with the concrete path.
142 * boolean value which specifies if the path is absolute or
145 * a single QName which specifies exact path to the
148 * @return A SchemaPath instance.
150 public static @NonNull SchemaPath create(final boolean absolute, final QName element) {
151 return (absolute ? ROOT : SAME).createChild(element);
155 * Constructs new instance of this class with the concrete path.
158 * boolean value which specifies if the path is absolute or
161 * one or more QName instances which specifies exact path to the
164 * @return A SchemaPath instance.
166 public static @NonNull SchemaPath create(final boolean absolute, final QName... path) {
167 return create(Arrays.asList(path), absolute);
171 * Create a child path based on concatenation of this path and a relative path.
173 * @param relative Relative path
174 * @return A new child path
176 public @NonNull SchemaPath createChild(final Iterable<QName> relative) {
177 if (Iterables.isEmpty(relative)) {
181 SchemaPath parentPath = this;
182 for (QName item : relative) {
183 parentPath = parentPath.createChild(item);
190 * Create a child path based on concatenation of this path and a relative path.
192 * @param relative Relative SchemaPath
193 * @return A new child path
195 public @NonNull SchemaPath createChild(final SchemaPath relative) {
196 checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
197 return createChild(relative.getPathFromRoot());
201 * Create a child path based on concatenation of this path and an additional path element.
203 * @param element Relative SchemaPath elements
204 * @return A new child path
206 public abstract @NonNull SchemaPath createChild(QName element);
209 * Create a child path based on concatenation of this path and additional
212 * @param elements Relative SchemaPath elements
213 * @return A new child path
215 public @NonNull SchemaPath createChild(final QName... elements) {
216 return createChild(Arrays.asList(elements));
220 * Returns the list of nodes which need to be traversed to get from the
221 * starting point (root for absolute SchemaPaths) to the node represented
224 * @return list of <code>qname</code> instances which represents
225 * path from the root to the schema node.
227 public List<QName> getPathFromRoot() {
229 return ImmutableList.of();
231 return parent == null ? ImmutableList.of(qname) : new PathFromRoot(this);
235 * Returns the list of nodes which need to be traversed to get from this
236 * node to the starting point (root for absolute SchemaPaths).
238 * @return list of <code>qname</code> instances which represents
239 * path from the schema node towards the root.
241 public Iterable<QName> getPathTowardsRoot() {
242 return () -> new UnmodifiableIterator<>() {
243 private SchemaPath current = SchemaPath.this;
246 public boolean hasNext() {
247 return current.parent != null;
251 public QName next() {
252 if (current.parent != null) {
253 final QName ret = current.qname;
254 current = current.parent;
258 throw new NoSuchElementException("No more elements available");
264 * Returns the immediate parent SchemaPath.
266 * @return Parent path, null if this SchemaPath is already toplevel.
268 public SchemaPath getParent() {
273 * Get the last component of this path.
275 * @return The last component of this path.
277 public final QName getLastComponent() {
282 * Describes whether schema path is|isn't absolute.
284 * @return boolean value which is <code>true</code> if schema path is
287 public abstract boolean isAbsolute();
290 * Return this path as a {@link SchemaNodeIdentifier}.
292 * @return A SchemaNodeIdentifier.
293 * @throws IllegalStateException if this path is empty
295 public final SchemaNodeIdentifier asSchemaNodeIdentifier() {
296 checkState(qname != null, "Cannot convert empty %s", this);
297 final List<QName> path = getPathFromRoot();
298 return isAbsolute() ? Absolute.of(path) : Descendant.of(path);
302 * Return this path as an {@link Absolute} SchemaNodeIdentifier.
304 * @return An SchemaNodeIdentifier.
305 * @throws IllegalStateException if this path is empty or is not absolute.
307 public final Absolute asAbsolute() {
308 final SchemaNodeIdentifier ret = asSchemaNodeIdentifier();
309 if (ret instanceof Absolute) {
310 return (Absolute) ret;
312 throw new IllegalStateException("Path " + this + " is relative");
316 * Return this path as an {@link Descendant} SchemaNodeIdentifier.
318 * @return An SchemaNodeIdentifier.
319 * @throws IllegalStateException if this path is empty or is not relative.
321 public final Descendant asDescendant() {
322 final SchemaNodeIdentifier ret = asSchemaNodeIdentifier();
323 if (ret instanceof Descendant) {
324 return (Descendant) ret;
326 throw new IllegalStateException("Path " + this + " is absolute");
330 public final int hashCode() {
335 public boolean equals(final Object obj) {
342 if (getClass() != obj.getClass()) {
345 final SchemaPath other = (SchemaPath) obj;
346 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
350 public final String toString() {
351 return MoreObjects.toStringHelper(this).add("path", getPathFromRoot()).toString();