Make DataSchemaContextTree.NodeAndStack a record
[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     public record NodeAndStack(@NonNull DataSchemaContextNode<?> node, @NonNull SchemaInferenceStack stack) {
32         public NodeAndStack(final @NonNull DataSchemaContextNode<?> node, final @NonNull SchemaInferenceStack stack) {
33             this.node = requireNonNull(node);
34             this.stack = requireNonNull(stack);
35         }
36     }
37
38     private static final LoadingCache<EffectiveModelContext, @NonNull DataSchemaContextTree> TREES =
39         CacheBuilder.newBuilder().weakKeys().weakValues().build(new CacheLoader<>() {
40             @Override
41             public DataSchemaContextTree load(final EffectiveModelContext key) {
42                 return new DataSchemaContextTree(key);
43             }
44         });
45
46     private final @NonNull ContainerContextNode root;
47
48     private DataSchemaContextTree(final EffectiveModelContext ctx) {
49         super(ctx);
50         root = new ContainerContextNode(ctx);
51     }
52
53     public static @NonNull DataSchemaContextTree from(final @NonNull EffectiveModelContext ctx) {
54         return TREES.getUnchecked(ctx);
55     }
56
57     /**
58      * Find a child node as identified by an absolute {@link YangInstanceIdentifier}.
59      *
60      * @param path Path towards the child node
61      * @return Child node if present, or empty when corresponding child is not found.
62      * @throws NullPointerException if {@code path} is null
63      */
64     public @NonNull Optional<@NonNull DataSchemaContextNode<?>> findChild(final @NonNull YangInstanceIdentifier path) {
65         return root.findChild(path);
66     }
67
68     /**
69      * Find a child node as identified by an absolute {@link YangInstanceIdentifier} and return it along with a suitably
70      * initialized {@link SchemaInferenceStack}.
71      *
72      * @param path Path towards the child node
73      * @return A {@link NodeAndStack}, or empty when corresponding child is not found.
74      * @throws NullPointerException if {@code path} is null
75      */
76     public @NonNull CheckedValue<@NonNull NodeAndStack, @NonNull IllegalArgumentException> enterPath(
77             final YangInstanceIdentifier path) {
78         final var stack = SchemaInferenceStack.of((EffectiveModelContext) root.getDataSchemaNode());
79         DataSchemaContextNode<?> node = root;
80         for (var arg : path.getPathArguments()) {
81             final var child = node.enterChild(arg, stack);
82             if (child == null) {
83                 return CheckedValue.ofException(new IllegalArgumentException("Failed to find " + arg + " in " + node));
84             }
85             node = child;
86         }
87
88         return CheckedValue.ofValue(new NodeAndStack(node, stack));
89     }
90
91     public @NonNull DataSchemaContextNode<?> getRoot() {
92         return root;
93     }
94 }