Add SchemaTreeRoot interface 01/95201/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 17 Feb 2021 10:14:48 +0000 (11:14 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 17 Feb 2021 10:14:48 +0000 (11:14 +0100)
It is a common requirement to find a schema tree node based on its
SchemaNodeIdentifier.Absolute. Make this easy to do with both
EffectiveModuleContext and ModuleEffectiveStatement.

Change-Id: Ie1e47954d52333c8aa88f5d09beb97d437c389b8
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/EffectiveModelContext.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/stmt/ModuleEffectiveStatement.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/stmt/SchemaTreeRoot.java [new file with mode: 0644]

index 608f268a1b5af674405cf123180081dd0dfc8405..06dd97d43f5995216ecb64cf287a573f961d85f6 100644 (file)
@@ -18,24 +18,28 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeRoot;
 
 /**
  * {@link EffectiveStatement}-based result of YANG parser compilation. Unlike a SchemaContext, which it extends,
- * it gives access to individual {@link ModuleEffectiveStatement}s that comprise it.
+ * it gives access to individual {@link ModuleEffectiveStatement}s that comprise it. It also supports resolution of
+ * {@link Absolute} schema node identifiers via {@link #findSchemaTreeNode(Absolute)}.
  *
  * @author Robert Varga
  */
 @Beta
-// FIXME: 7.0.0: evaluate if we still need to extend SchemaContext here
-public interface EffectiveModelContext extends SchemaContext {
+// FIXME: 8.0.0: evaluate if we still need to extend SchemaContext here
+public interface EffectiveModelContext extends SchemaContext, SchemaTreeRoot {
 
-    Map<QNameModule, ModuleEffectiveStatement> getModuleStatements();
+    @NonNull Map<QNameModule, ModuleEffectiveStatement> getModuleStatements();
 
-    default Optional<ModuleEffectiveStatement> findModuleStatement(final QNameModule moduleName) {
+    default @NonNull Optional<ModuleEffectiveStatement> findModuleStatement(final QNameModule moduleName) {
         return Optional.ofNullable(getModuleStatements().get(requireNonNull(moduleName)));
     }
 
-    default Optional<ModuleEffectiveStatement> findModuleStatement(final QName moduleName) {
+    default @NonNull Optional<ModuleEffectiveStatement> findModuleStatement(final QName moduleName) {
         return findModuleStatement(moduleName.getModule());
     }
 
@@ -46,4 +50,17 @@ public interface EffectiveModelContext extends SchemaContext {
     default @NonNull ModuleEffectiveStatement getModuleStatement(final QName moduleName) {
         return getModuleStatement(moduleName.getModule());
     }
+
+    /**
+     * {@inheritDoc}
+     *
+     * @implSpec
+     *     Default implementation defers locates the module corresponding to the first element of path and then defers
+     *     to {@link ModuleEffectiveStatement#findSchemaTreeNode(Absolute)}.
+     */
+    @Override
+    default Optional<SchemaTreeEffectiveStatement<?>> findSchemaTreeNode(final Absolute path) {
+        return findModuleStatement(path.firstNodeIdentifier().getModule())
+            .flatMap(module -> module.findSchemaTreeNode(path));
+    }
 }
index 7f4784e6fe6ea64633d50ba6ce305efd1f5033e7..f006099427963ad0de233ba2bf56697b9e49dc59 100644 (file)
@@ -8,18 +8,21 @@
 package org.opendaylight.yangtools.yang.model.api.stmt;
 
 import com.google.common.annotations.Beta;
+import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 
 /**
  * Effective view of a {@link ModuleStatement}.
  */
 @Beta
-public interface ModuleEffectiveStatement extends DataTreeAwareEffectiveStatement<UnqualifiedQName, ModuleStatement> {
+public interface ModuleEffectiveStatement
+        extends DataTreeAwareEffectiveStatement<UnqualifiedQName, ModuleStatement>, SchemaTreeRoot {
     /**
      * Namespace mapping all known prefixes in a module to their modules. Note this namespace includes the module
      * in which it is instantiated.
@@ -62,4 +65,15 @@ public interface ModuleEffectiveStatement extends DataTreeAwareEffectiveStatemen
      * @return Local QNameModule
      */
     @NonNull QNameModule localQNameModule();
+
+    /**
+     * {@inheritDoc}
+     *
+     * @implSpec
+     *     Default implementation defers to {@link #findSchemaTreeNode(java.util.List)}.
+     */
+    @Override
+    default Optional<SchemaTreeEffectiveStatement<?>> findSchemaTreeNode(final Absolute path) {
+        return findSchemaTreeNode(path.getNodeIdentifiers());
+    }
 }
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/stmt/SchemaTreeRoot.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/stmt/SchemaTreeRoot.java
new file mode 100644 (file)
index 0000000..c778b98
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.model.api.stmt;
+
+import com.google.common.annotations.Beta;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
+
+/**
+ * Common interface implemented by entities which act as the root of the {@code schema tree} and are able to resolve an
+ * {@code Absolute absolute schema node identifier} to a {@link SchemaTreeEffectiveStatement}.
+ */
+@Beta
+public interface SchemaTreeRoot {
+    /**
+     * Find a {@code schema tree} node based on its absolute schema node identifier.
+     *
+     * @param path Absolute schema node identifier
+     * @return Found node, or empty
+     * @throws NullPointerException if {@code path} is null
+     */
+    @NonNull Optional<SchemaTreeEffectiveStatement<?>> findSchemaTreeNode(@NonNull Absolute path);
+}