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();
122 public static @NonNull SchemaPath of(final SchemaNodeIdentifier path) {
123 if (path instanceof Absolute) {
124 return of((Absolute) path);
125 } else if (path instanceof Descendant) {
126 return of((Descendant) path);
128 throw new IllegalStateException("Unexpected path " + requireNonNull(path));
132 public static @NonNull SchemaPath of(final Absolute path) {
133 return SchemaPath.ROOT.createChild(path.getNodeIdentifiers());
136 public static @NonNull SchemaPath of(final Descendant path) {
137 return SchemaPath.SAME.createChild(path.getNodeIdentifiers());
141 * Constructs new instance of this class with the concrete path.
144 * list of QName instances which specifies exact path to the
147 * boolean value which specifies if the path is absolute or
150 * @return A SchemaPath instance.
152 public static @NonNull SchemaPath create(final Iterable<QName> path, final boolean absolute) {
153 return (absolute ? ROOT : SAME).createChild(path);
157 * Constructs new instance of this class with the concrete path.
160 * boolean value which specifies if the path is absolute or
163 * a single QName which specifies exact path to the
166 * @return A SchemaPath instance.
168 public static @NonNull SchemaPath create(final boolean absolute, final QName element) {
169 return (absolute ? ROOT : SAME).createChild(element);
173 * Constructs new instance of this class with the concrete path.
176 * boolean value which specifies if the path is absolute or
179 * one or more QName instances which specifies exact path to the
182 * @return A SchemaPath instance.
184 public static @NonNull SchemaPath create(final boolean absolute, final QName... path) {
185 return create(Arrays.asList(path), absolute);
189 * Create a child path based on concatenation of this path and a relative path.
191 * @param relative Relative path
192 * @return A new child path
194 public @NonNull SchemaPath createChild(final Iterable<QName> relative) {
195 if (Iterables.isEmpty(relative)) {
199 SchemaPath parentPath = this;
200 for (QName item : relative) {
201 parentPath = parentPath.createChild(item);
208 * Create a child path based on concatenation of this path and a relative path.
210 * @param relative Relative SchemaPath
211 * @return A new child path
213 public @NonNull SchemaPath createChild(final SchemaPath relative) {
214 checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
215 return createChild(relative.getPathFromRoot());
219 * Create a child path based on concatenation of this path and an additional path element.
221 * @param element Relative SchemaPath elements
222 * @return A new child path
224 public abstract @NonNull SchemaPath createChild(QName element);
227 * Create a child path based on concatenation of this path and additional
230 * @param elements Relative SchemaPath elements
231 * @return A new child path
233 public @NonNull SchemaPath createChild(final QName... elements) {
234 return createChild(Arrays.asList(elements));
238 * Returns the list of nodes which need to be traversed to get from the
239 * starting point (root for absolute SchemaPaths) to the node represented
242 * @return list of <code>qname</code> instances which represents
243 * path from the root to the schema node.
245 public List<QName> getPathFromRoot() {
247 return ImmutableList.of();
249 return parent == null ? ImmutableList.of(qname) : new PathFromRoot(this);
253 * Returns the list of nodes which need to be traversed to get from this
254 * node to the starting point (root for absolute SchemaPaths).
256 * @return list of <code>qname</code> instances which represents
257 * path from the schema node towards the root.
259 public Iterable<QName> getPathTowardsRoot() {
260 return () -> new UnmodifiableIterator<>() {
261 private SchemaPath current = SchemaPath.this;
264 public boolean hasNext() {
265 return current.parent != null;
269 public QName next() {
270 if (current.parent != null) {
271 final QName ret = current.qname;
272 current = current.parent;
276 throw new NoSuchElementException("No more elements available");
282 * Returns the immediate parent SchemaPath.
284 * @return Parent path, null if this SchemaPath is already toplevel.
286 public SchemaPath getParent() {
291 * Get the last component of this path.
293 * @return The last component of this path.
295 public final QName getLastComponent() {
300 * Describes whether schema path is|isn't absolute.
302 * @return boolean value which is <code>true</code> if schema path is
305 public abstract boolean isAbsolute();
308 * Return this path as a {@link SchemaNodeIdentifier}.
310 * @return A SchemaNodeIdentifier.
311 * @throws IllegalStateException if this path is empty
313 public final SchemaNodeIdentifier asSchemaNodeIdentifier() {
314 checkState(qname != null, "Cannot convert empty %s", this);
315 final List<QName> path = getPathFromRoot();
316 return isAbsolute() ? Absolute.of(path) : Descendant.of(path);
320 * Return this path as an {@link Absolute} SchemaNodeIdentifier.
322 * @return An SchemaNodeIdentifier.
323 * @throws IllegalStateException if this path is empty or is not absolute.
325 public final Absolute asAbsolute() {
326 final SchemaNodeIdentifier ret = asSchemaNodeIdentifier();
327 if (ret instanceof Absolute) {
328 return (Absolute) ret;
330 throw new IllegalStateException("Path " + this + " is relative");
334 * Return this path as an {@link Descendant} SchemaNodeIdentifier.
336 * @return An SchemaNodeIdentifier.
337 * @throws IllegalStateException if this path is empty or is not relative.
339 public final Descendant asDescendant() {
340 final SchemaNodeIdentifier ret = asSchemaNodeIdentifier();
341 if (ret instanceof Descendant) {
342 return (Descendant) ret;
344 throw new IllegalStateException("Path " + this + " is absolute");
348 public final int hashCode() {
353 public boolean equals(final Object obj) {
360 if (getClass() != obj.getClass()) {
363 final SchemaPath other = (SchemaPath) obj;
364 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
368 public final String toString() {
369 return MoreObjects.toStringHelper(this).add("path", getPathFromRoot()).toString();