Add SchemaInferenceStack.enterSchemaTree(SchemaNodeIdentifier) 50/95550/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 24 Mar 2021 08:51:29 +0000 (09:51 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 24 Mar 2021 08:53:41 +0000 (09:53 +0100)
SchemaNodeIdentifier is a well-known concept which we are integrating
with in multiple aspects. One key component is the ability to resolve
a SchemaNodeIdentifier in the context of an existing stack. While users
can do this easily themselves, it is more natural to provide this
ability out of the box.

JIRA: YANGTOOLS-1231
Change-Id: I9818f175d09fd4e6e4f610127929ee8553c3116e
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaInferenceStack.java

index b8f50c47528b814a8e4db0a468a77e1dd9696393..86e026ecffa4c9a7026617d2d9e7cf29779da961 100644 (file)
@@ -51,6 +51,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeAwareEffectiveStat
 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.GroupingEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
@@ -392,6 +393,28 @@ public final class SchemaInferenceStack implements Mutable, EffectiveModelContex
         return pushSchema(requireNonNull(nodeIdentifier));
     }
 
+    /**
+     * Lookup a {@code schema tree} node by its schema node identifier and push it to the stack.
+     *
+     * @param nodeIdentifier Schema node identifier of the schema tree node to enter
+     * @return Resolved schema tree node
+     * @throws NullPointerException if {@code nodeIdentifier} is null
+     * @throws IllegalArgumentException if the corresponding node cannot be found
+     */
+    public @NonNull SchemaTreeEffectiveStatement<?> enterSchemaTree(final SchemaNodeIdentifier nodeIdentifier) {
+        if (nodeIdentifier instanceof Absolute) {
+            clear();
+        }
+
+        final Iterator<QName> it = nodeIdentifier.getNodeIdentifiers().iterator();
+        SchemaTreeEffectiveStatement<?> ret;
+        do {
+            ret = enterSchemaTree(it.next());
+        } while (it.hasNext());
+
+        return ret;
+    }
+
     /**
      * Lookup a {@code schema tree} child by its node identifier and push it to the stack.
      *