Add DataSchemaContextNode/SchemaInferenceStack integration
[yangtools.git] / data / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / DataSchemaContextTree.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.util;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.concepts.CheckedValue;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.spi.AbstractEffectiveModelContextProvider;
22 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
23
24 /**
25  * Semantic tree binding a {@link EffectiveModelContext} to a {@link NormalizedNode} tree. Since the layout of the
26  * schema and data has differences, the mapping is not trivial -- which is where this class comes in.
27  *
28  * @author Robert Varga
29  */
30 public final class DataSchemaContextTree extends AbstractEffectiveModelContextProvider {
31     // FIXME: record once we have JDK17+
32     public static final class NodeAndStack {
33         private final @NonNull DataSchemaContextNode<?> node;
34         private final @NonNull SchemaInferenceStack stack;
35
36         NodeAndStack(final DataSchemaContextNode<?> node, final @NonNull SchemaInferenceStack stack) {
37             this.node = requireNonNull(node);
38             this.stack = requireNonNull(stack);
39         }
40
41         public @NonNull DataSchemaContextNode<?> node() {
42             return node;
43         }
44
45         public @NonNull SchemaInferenceStack stack() {
46             return stack;
47         }
48     }
49
50     private static final LoadingCache<EffectiveModelContext, @NonNull DataSchemaContextTree> TREES =
51         CacheBuilder.newBuilder().weakKeys().weakValues().build(new CacheLoader<>() {
52             @Override
53             public DataSchemaContextTree load(final EffectiveModelContext key) {
54                 return new DataSchemaContextTree(key);
55             }
56         });
57
58     private final @NonNull ContainerContextNode root;
59
60     private DataSchemaContextTree(final EffectiveModelContext ctx) {
61         super(ctx);
62         root = new ContainerContextNode(ctx);
63     }
64
65     public static @NonNull DataSchemaContextTree from(final @NonNull EffectiveModelContext ctx) {
66         return TREES.getUnchecked(ctx);
67     }
68
69     /**
70      * Find a child node as identified by an absolute {@link YangInstanceIdentifier}.
71      *
72      * @param path Path towards the child node
73      * @return Child node if present, or empty when corresponding child is not found.
74      * @throws NullPointerException if {@code path} is null
75      */
76     public @NonNull Optional<@NonNull DataSchemaContextNode<?>> findChild(final @NonNull YangInstanceIdentifier path) {
77         return root.findChild(path);
78     }
79
80     /**
81      * Find a child node as identified by an absolute {@link YangInstanceIdentifier} and return it along with a suitably
82      * initialized {@link SchemaInferenceStack}.
83      *
84      * @param path Path towards the child node
85      * @return A {@link NodeAndStack}, or empty when corresponding child is not found.
86      * @throws NullPointerException if {@code path} is null
87      */
88     public @NonNull CheckedValue<@NonNull NodeAndStack, @NonNull IllegalArgumentException> enterPath(
89             final YangInstanceIdentifier path) {
90         final var stack = SchemaInferenceStack.of((EffectiveModelContext) root.getDataSchemaNode());
91         DataSchemaContextNode<?> node = root;
92         for (var arg : path.getPathArguments()) {
93             final var child = node.enterChild(arg, stack);
94             if (child == null) {
95                 return CheckedValue.ofException(new IllegalArgumentException("Failed to find " + arg + " in " + node));
96             }
97             node = child;
98         }
99
100         return CheckedValue.ofValue(new NodeAndStack(node, stack));
101     }
102
103     public @NonNull DataSchemaContextNode<?> getRoot() {
104         return root;
105     }
106 }