Teach YANG parser to retain DeclarationReference
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / EffectiveStmtCtx.java
index 6b0b7d544b14362c787c442001540ab0869c4da1..82ce9bcd4b8f4039d0f6e408d973af80721050ed 100644 (file)
@@ -11,11 +11,14 @@ import static com.google.common.base.Verify.verifyNotNull;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.VerifyException;
-import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaNodeDefaults;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
@@ -78,23 +81,13 @@ public interface EffectiveStmtCtx extends CommonStmtCtx, StmtContextCompat, Immu
             }
 
             /**
-             * Return this value as a legacy boolean for use with {@link DataSchemaNode#isConfiguration()}.
+             * Return this value as a {@link Boolean} for use with {@link DataSchemaNode#effectiveConfig()}.
              *
-             * @return A simple boolean
-             * @deprecated This method is only for transition and should be eliminated once DataSchemaNode is fixed.
+             * @return A boolean or null
              */
-            @Deprecated
-            public boolean asLegacy() {
-                return config == null || config;
-            }
-
             public @Nullable Boolean asNullable() {
                 return config;
             }
-
-            public Optional<Boolean> asOptional() {
-                return Optional.ofNullable(config);
-            }
         }
 
         /**
@@ -104,26 +97,57 @@ public interface EffectiveStmtCtx extends CommonStmtCtx, StmtContextCompat, Immu
          */
         @NonNull EffectiveConfig effectiveConfig();
 
+        // FIXME: 7.0.0: this is currently only used by AbstractTypeStatement
+        @NonNull QNameModule effectiveNamespace();
+
         /**
-         * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
-         * {@link Optional#empty()} is returned.
+         * Return the effective path of this statement. This method is intended for use with statements which naturally
+         * have a {@link QName} identifier and this identifier forms the ultimate step in their
+         * {@link SchemaNode#getPath()}.
+         *
+         * <p>
+         * Returned object conforms to {@link SchemaPathSupport}'s view of how these are to be handled. Users of this
+         * method are expected to consult {@link SchemaNodeDefaults#extractQName(Immutable)} and
+         * {@link SchemaNodeDefaults#extractPath(Object, Immutable)} to ensure correct implementation behaviour with
+         * respect to {@link SchemaNode#getQName()} and {@link SchemaNode#getPath()} respectively.
          *
-         * @return Optional SchemaPath
-         * @deprecated Use of SchemaPath in the context of effective statements is going away. Consider not providing
-         *             this information, if your users can exist without it.
+         * @return An {@link Immutable} effective path object
          */
-        // FIXME: 7.0.0: this needs to be a tri-state present/absent/disabled
-        @Deprecated
-        @NonNull Optional<SchemaPath> schemaPath();
+        // FIXME: Remove this when SchemaNode.getPath() is removed. QName users will store getArgument() instead.
+        default @NonNull Immutable effectivePath() {
+            return SchemaPathSupport.toEffectivePath(getSchemaPath());
+        }
 
-        @Deprecated
-        default @NonNull SchemaPath getSchemaPath() {
-            return schemaPath().orElseThrow();
+        /**
+         * Return an optional-to-provide path for {@link SchemaNode#getPath()}. The result of this method is expected
+         * to be consulted with {@link SchemaNodeDefaults#throwUnsupportedIfNull(Object, SchemaPath)} to get consistent
+         * API behaviour.
+         *
+         * @return Potentially-null {@link SchemaPath}.
+         */
+        // FIXME: Remove this when SchemaNode.getPath() is removed
+        default @Nullable SchemaPath optionalPath() {
+            return SchemaPathSupport.toOptionalPath(getSchemaPath());
         }
 
-        @Deprecated
-        default @Nullable SchemaPath wrapSchemaPath() {
-            return SchemaPathSupport.wrap(getSchemaPath());
+        /**
+         * Return the {@link SchemaNode#getPath()} of this statement. Not all statements have a SchemaPath, in which
+         * case null is returned.
+         *
+         * @return SchemaPath or null
+         */
+        // FIXME: Remove this when SchemaNode.getPath() is removed
+        @Nullable SchemaPath schemaPath();
+
+        /**
+         * Return the {@link SchemaNode#getPath()} of this statement, failing if it is not present.
+         *
+         * @return A SchemaPath.
+         * @throws VerifyException if {@link #schemaPath()} returns null
+         */
+        // FIXME: Remove this when SchemaNode.getPath() is removed
+        default @NonNull SchemaPath getSchemaPath() {
+            return verifyNotNull(schemaPath(), "Missing path for %s", this);
         }
     }
 
@@ -137,10 +161,17 @@ public interface EffectiveStmtCtx extends CommonStmtCtx, StmtContextCompat, Immu
     @Beta
     interface Current<A, D extends DeclaredStatement<A>> extends Parent, NamespaceStmtCtx, BoundStmtCtxCompat<A, D> {
 
-        @NonNull CommonStmtCtx root();
+        @NonNull QName moduleName();
 
         @Nullable EffectiveStatement<?, ?> original();
 
+        default <T> @Nullable T original(final @NonNull Class<T> type) {
+            return type.cast(original());
+        }
+
+        // FIXME: 7.0.0: this method should be moved to stmt.type in some shape or form
+        @NonNull QName argumentAsTypeQName();
+
         /**
          * Summon the <a href="https://en.wikipedia.org/wiki/Rabbit_of_Caerbannog">Rabbit of Caerbannog</a>.
          *
@@ -150,5 +181,20 @@ public interface EffectiveStmtCtx extends CommonStmtCtx, StmtContextCompat, Immu
         // FIXME: YANGTOOLS-1186: lob the Holy Hand Grenade of Antioch
         @Deprecated
         <E extends EffectiveStatement<A, D>> @NonNull StmtContext<A, D, E> caerbannog();
+
+        /**
+         * Compare another context for equality of {@code getEffectiveParent().getSchemaPath()}, just in a safer manner.
+         *
+         * @param other Other {@link Current}
+         * @return True if {@code other} has parent path equal to this context's parent path.
+         */
+        // FIXME: Remove this when SchemaNode.getPath() is removed
+        default boolean equalParentPath(final Current<A, D> other) {
+            final Parent ours = effectiveParent();
+            final Parent theirs = other.effectiveParent();
+            return ours == theirs
+                || ours != null && theirs != null && SchemaPathSupport.effectivelyEqual(
+                    ours.schemaPath(), theirs.schemaPath());
+        }
     }
 }