2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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 java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import org.eclipse.jdt.annotation.NonNullByDefault;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.yang.xpath.api.YangBinaryOperator;
18 import org.opendaylight.yangtools.yang.xpath.api.YangFunction;
19 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath;
20 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.QNameStep;
21 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.Relative;
22 import org.opendaylight.yangtools.yang.xpath.api.YangPathExpr;
23 import org.opendaylight.yangtools.yang.xpath.api.YangXPathAxis;
24 import org.opendaylight.yangtools.yang.xpath.api.YangXPathExpression;
27 * An expression as defined in <a href="https://www.rfc-editor.org/rfc/rfc7950#section-9.9.2">RFC7950 Section 9.9.2</a>,
28 * i.e. the argument of a {@code path} statement.
31 * Semantically a {@link PathExpression} is similar to a {@link YangXPathExpression} with guarantees around what
32 * subexpressions it can contain:
34 * <li>the root expression must be a {@link YangLocationPath}</li>
35 * <li>it can contain steps only along {@link YangXPathAxis#CHILD} and {@link YangXPathAxis#PARENT} axis</li>
36 * <li>all steps along {@link YangXPathAxis#CHILD} axis are {@link QNameStep}</li>
37 * <li>the only function invocation is {@link YangFunction#CURRENT}</li>
38 * <li>only {@link YangBinaryOperator#EQUALS} is allowed</li>
39 * <li>no literals nor numbers are allowed</li>
40 * <li>all qualified node identifiers must me resolved</li>
44 public interface PathExpression extends Immutable {
46 * Abstract base class for expressing steps of a PathExpression.
48 abstract sealed class Steps {
50 // Prevent external subclassing
54 public abstract int hashCode();
57 public abstract boolean equals(@Nullable Object obj);
60 public final String toString() {
61 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
64 abstract ToStringHelper addToStringAttributes(ToStringHelper helper);
68 * Steps of a PathExpression which is a LocationPath, corresponding to RFC7950 base specification.
70 final class LocationPathSteps extends Steps {
71 private final YangLocationPath locationPath;
73 public LocationPathSteps(final YangLocationPath locationPath) {
74 this.locationPath = requireNonNull(locationPath);
77 public YangLocationPath getLocationPath() {
82 public int hashCode() {
83 return locationPath.hashCode();
87 public boolean equals(final @Nullable Object obj) {
88 return this == obj || obj instanceof LocationPathSteps other && locationPath.equals(other.locationPath);
92 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
93 return helper.add("locationPath", locationPath);
98 * Steps of a PathExpression which is a combination of {@code deref()} function call and a relative path,
99 * corresponding to Errata 5617.
101 final class DerefSteps extends Steps {
102 private final Relative derefArgument;
103 private final Relative relativePath;
105 public DerefSteps(final Relative derefArgument, final Relative relativePath) {
106 this.derefArgument = requireNonNull(derefArgument);
107 this.relativePath = requireNonNull(relativePath);
110 public Relative getDerefArgument() {
111 return derefArgument;
114 public Relative getRelativePath() {
119 public int hashCode() {
120 return 31 * derefArgument.hashCode() + relativePath.hashCode();
124 public boolean equals(@Nullable final Object obj) {
125 return this == obj || obj instanceof DerefSteps other
126 && derefArgument.equals(other.derefArgument) && relativePath.equals(other.relativePath);
130 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
131 return helper.add("derefArgument", derefArgument).add("relativePath", relativePath);
136 * Returns the path expression formatted string as is defined in model. For example:
137 * {@code /prefix:container/prefix:container::cond[when()=foo]/prefix:leaf}
139 * @return the path expression formatted string as is defined in model.
141 String getOriginalString();
144 * Return the path of this expression, which can either be a {@link YangLocationPath} (compliant to RFC7950) or
145 * a {@link YangPathExpr} with filter being an invocation of {@link YangFunction#DEREF}.
147 * @return The path's steps
148 * @throws UnsupportedOperationException if the implementation has not parsed the string. Implementations are
149 * strongly encouraged to perform proper parsing.
154 * Returns <code>true</code> if the XPapth starts in root of YANG model, otherwise returns <code>false</code>.
156 * @return <code>true</code> if the XPapth starts in root of YANG model, otherwise returns <code>false</code>
158 default boolean isAbsolute() {
159 return getSteps() instanceof LocationPathSteps locationSteps && locationSteps.getLocationPath().isAbsolute();