Propagate EffectiveModelContext to more places
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / DataSchemaContextTree.java
index 27224b49a00867044a7682a8ce8257420b85fd2f..07b71bb46b6974eed7eb0694a56ab5a0f2fcd785 100644 (file)
@@ -10,46 +10,50 @@ package org.opendaylight.yangtools.yang.data.util;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
-import java.util.Iterator;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-
-public class DataSchemaContextTree {
-
-    private static final LoadingCache<SchemaContext, DataSchemaContextTree> TREES = CacheBuilder.newBuilder()
-            .weakKeys()
-            .build(new CacheLoader<SchemaContext, DataSchemaContextTree>() {
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 
+/**
+ * Semantic tree binding a {@link EffectiveModelContext} to a {@link NormalizedNode} tree. Since the layout of the
+ * schema and data has differences, the mapping is not trivial -- which is where this class comes in.
+ *
+ * @author Robert Varga
+ */
+// FIXME: 6.0.0: @NonNullByDefault
+public final class DataSchemaContextTree {
+    private static final LoadingCache<EffectiveModelContext, DataSchemaContextTree> TREES = CacheBuilder.newBuilder()
+            .weakKeys().weakValues().build(new CacheLoader<EffectiveModelContext, DataSchemaContextTree>() {
                 @Override
-                public DataSchemaContextTree load(SchemaContext key) throws Exception {
+                public DataSchemaContextTree load(final EffectiveModelContext key) {
                     return new DataSchemaContextTree(key);
                 }
-
             });
 
     private final DataSchemaContextNode<?> root;
 
-    private DataSchemaContextTree(final SchemaContext ctx) {
+    private DataSchemaContextTree(final EffectiveModelContext ctx) {
         root = DataSchemaContextNode.from(ctx);
     }
 
-
-    public static DataSchemaContextTree from(SchemaContext ctx) {
+    public static @NonNull DataSchemaContextTree from(final @NonNull EffectiveModelContext ctx) {
         return TREES.getUnchecked(ctx);
     }
 
-    public DataSchemaContextNode<?> getChild(final YangInstanceIdentifier path) {
-        DataSchemaContextNode<?> currentOp = root;
-        Iterator<PathArgument> arguments = path.getPathArguments().iterator();
-        while (arguments.hasNext()) {
-            currentOp = currentOp.getChild(arguments.next());
-        }
-        return currentOp;
+    /**
+     * 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
+     */
+    public @NonNull Optional<@NonNull DataSchemaContextNode<?>> findChild(final @NonNull YangInstanceIdentifier path) {
+        return getRoot().findChild(path);
     }
 
     public DataSchemaContextNode<?> getRoot() {
         return root;
     }
-
 }