Add DataSchemaContextTree.childByPath() 97/106097/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 21 May 2023 11:31:50 +0000 (13:31 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 21 May 2023 11:32:54 +0000 (13:32 +0200)
Going through Optional is nice, but the structure of users lends itself
to using @Nullable as well.

JIRA: YANGTOOLS-1413
Change-Id: I49e217a4c66a29093a94dc1eab45b788b834c79b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/InMemoryDataTree.java
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/di/InMemoryDataTreeFactory.java
data/yang-data-util/src/main/java/org/opendaylight/yangtools/yang/data/util/DataSchemaContextTree.java

index 7052d87df3de3726b400f4a9706bc2285e012481..ec48eb11ae50c85708a29d6a7e5154a456436db6 100644 (file)
@@ -107,13 +107,13 @@ public final class InMemoryDataTree extends AbstractDataTreeTip implements DataT
         LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
 
         final var contextTree = DataSchemaContextTree.from(newSchemaContext);
-        final var rootContextNode = contextTree.findChild(getRootPath());
-        if (!rootContextNode.isPresent()) {
+        final var rootContextNode = contextTree.childByPath(getRootPath());
+        if (rootContextNode == null) {
             LOG.warn("Could not find root {} in new schema context, not upgrading", getRootPath());
             return;
         }
 
-        final var rootSchemaNode = rootContextNode.orElseThrow().dataSchemaNode();
+        final var rootSchemaNode = rootContextNode.dataSchemaNode();
         if (!(rootSchemaNode instanceof DataNodeContainer)) {
             LOG.warn("Root {} resolves to non-container type {}, not upgrading", getRootPath(), rootSchemaNode);
             return;
index dc2a0653011421032448c1c6d5b1398bf72f9a63..7ea9f7d812f3a5c059c6fa3f6d3a36e09fb8d577 100644 (file)
@@ -154,10 +154,10 @@ public final class InMemoryDataTreeFactory implements DataTreeFactory {
     private static DataSchemaNode getRootSchemaNode(final EffectiveModelContext schemaContext,
             final YangInstanceIdentifier rootPath) {
         final var contextTree = DataSchemaContextTree.from(schemaContext);
-        final var rootContextNode = contextTree.findChild(rootPath);
-        checkArgument(rootContextNode.isPresent(), "Failed to find root %s in schema context", rootPath);
+        final var rootContextNode = contextTree.childByPath(rootPath);
+        checkArgument(rootContextNode != null, "Failed to find root %s in schema context", rootPath);
 
-        final var rootSchemaNode = rootContextNode.orElseThrow().dataSchemaNode();
+        final var rootSchemaNode = rootContextNode.dataSchemaNode();
         checkArgument(rootSchemaNode instanceof DataNodeContainer, "Root %s resolves to non-container type %s",
             rootPath, rootSchemaNode);
         return rootSchemaNode;
index 063ae6fbf6975b19f74e9654956cd2069de0b403..05a415cfc59f0763f611bc729e5bae501467ec0d 100644 (file)
@@ -14,6 +14,7 @@ import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
 import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.concepts.CheckedValue;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -54,12 +55,23 @@ public final class DataSchemaContextTree extends AbstractEffectiveModelContextPr
         return TREES.getUnchecked(ctx);
     }
 
+    /**
+     * Find a child node as identified by an absolute {@link YangInstanceIdentifier}.
+     *
+     * @param path Path towards the child node
+     * @return Child node if present, or {@code null} when corresponding child is not found.
+     * @throws NullPointerException if {@code path} is {@code null}
+     */
+    public @Nullable DataSchemaContext childByPath(final @NonNull YangInstanceIdentifier path) {
+        return root.childByPath(path);
+    }
+
     /**
      * Find a child node as identified by an absolute {@link YangInstanceIdentifier}.
      *
      * @param path Path towards the child node
      * @return Child node if present, or empty when corresponding child is not found.
-     * @throws NullPointerException if {@code path} is null
+     * @throws NullPointerException if {@code path} is {@code null}
      */
     public @NonNull Optional<@NonNull DataSchemaContext> findChild(final @NonNull YangInstanceIdentifier path) {
         // Optional.ofNullable() inline due to annotations