Define the concept of unresolved QName
[yangtools.git] / yang / yang-xpath-api / src / main / java / org / opendaylight / yangtools / yang / xpath / api / YangXPathExpression.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.xpath.api;
9
10 import com.google.common.annotations.Beta;
11 import javax.xml.xpath.XPathExpressionException;
12 import org.opendaylight.yangtools.concepts.Immutable;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.common.QualifiedQName;
15 import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
16
17 /**
18  * An XPath expression. This interface defines a parsed representation of an XPath defined in a YANG context, as
19  * specified in <a href="https://tools.ietf.org/html/rfc7950#section-6.4">RFC7950, Section 6.4</a>.
20  *
21  * <p>
22  * The specification along with rules for {@code path} statement evaluation rules end up defining four incremental
23  * levels to which an XPath expression can be bound:
24  * <ul>
25  * <li>Unbound Expressions, which is a essentially a parse tree. No namespace binding has been performed, i.e. all
26  *     node identifiers are in {@link QualifiedQName} or {@link UnqualifiedQName} form. This level is typically not used
27  *     when dealing with YANG models directly, but can be useful for validating a String conforms to XPath syntax.
28  * </li>
29  * <li>Qualified-bound Expressions, where all {@link QualifiedQName}s are resolved and bound to {@link QName}s, but
30  *     {@link UnqualifiedQName}s are still present. This level corresponds to how far a YANG parser can interpret XPath
31  *     expressions defined in {@code typedef} statements and statements which are not fully instantiated, i.e. are
32  *     descendants of a {@code grouping} statement.
33  * </li>
34  * <li>Namespace-bound Expressions, where all node identifier references are resolved to {@link QName}s. This level
35  *     corresponds to how far a YANG parser can interpret XPath expressions at their place of instantiation, either in
36  *     the data tree or an {@code action}/@{code rpc}/{@code notification} or similar context.
37  * </li>
38  * <li>Context-bound Expressions, where the expression is bound to a {code context node}, i.e. {@code current()}
39  *     function result is know. This API does not handle this state, as it is inherently bound to a particular data
40  *     object model.
41  * </li>
42  * </ul>
43  *
44  * @author Robert Varga
45  */
46 @Beta
47 public interface YangXPathExpression extends Immutable {
48     /**
49      * A Qualified-bound expression. All {@link QualifiedQName}s are eliminated and replaced with {@link QName}s.
50      */
51     interface QualifiedBound extends YangXPathExpression {
52
53     }
54
55     interface UnqualifiedBound extends QualifiedBound {
56         @Override
57         YangQNameExpr.Resolved interpretAsQName(YangLiteralExpr expr) throws XPathExpressionException;
58     }
59
60     /**
61      * Return the root {@link YangExpr}.
62      *
63      * @return Root expression.
64      */
65     YangExpr getRootExpr();
66
67     /**
68      * Return the {@link YangXPathMathMode} used in this expression. All {@link YangNumberExpr} objects used by this
69      * expression are expected to be handled by this mode's {@link YangXPathMathSupport}.
70      *
71      * @return YangXPathMathMode
72      */
73     YangXPathMathMode getMathMode();
74
75     /**
76      * Attempt to interpret a {@link YangLiteralExpr} referenced by this expression as a {@link QName}. This method
77      * is required to perform late value binding of the expression when the literal needs to be interpreted as
78      * a reference to an {@code identity}.
79      *
80      * <p>
81      * The syntax of expr is required to conform to
82      * <a href="https://www.w3.org/TR/REC-xml-names/#NT-QName">XML QName format</a>, as further restricted by
83      * <a href="https://tools.ietf.org/html/rfc7950#section-9.10.3">YANG {@code identityref} Lexical Representation</a>.
84      *
85      * <p>
86      * Unfortunately we do not know when a literal will need to be interpreted in this way, as that can only be known
87      * at evaluation.
88      *
89      * @param expr Literal to be reinterpreted
90      * @return YangQNameExpr result of interpretation
91      * @throws XPathExpressionException when the literal cannot be interpreted as a QName
92      */
93     YangQNameExpr interpretAsQName(YangLiteralExpr expr) throws XPathExpressionException;
94
95     // FIXME: this really should be YangInstanceIdentifier without AugmentationIdentifier. Implementations are
96     //        strongly encouraged to validate it as such.
97     YangLocationPath interpretAsInstanceIdentifier(YangLiteralExpr expr) throws XPathExpressionException;
98 }