2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.util;
10 import static java.util.Objects.requireNonNull;
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;
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.
28 * @author Robert Varga
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;
36 NodeAndStack(final DataSchemaContextNode<?> node, final @NonNull SchemaInferenceStack stack) {
37 this.node = requireNonNull(node);
38 this.stack = requireNonNull(stack);
41 public @NonNull DataSchemaContextNode<?> node() {
45 public @NonNull SchemaInferenceStack stack() {
50 private static final LoadingCache<EffectiveModelContext, @NonNull DataSchemaContextTree> TREES =
51 CacheBuilder.newBuilder().weakKeys().weakValues().build(new CacheLoader<>() {
53 public DataSchemaContextTree load(final EffectiveModelContext key) {
54 return new DataSchemaContextTree(key);
58 private final @NonNull ContainerContextNode root;
60 private DataSchemaContextTree(final EffectiveModelContext ctx) {
62 root = new ContainerContextNode(ctx);
65 public static @NonNull DataSchemaContextTree from(final @NonNull EffectiveModelContext ctx) {
66 return TREES.getUnchecked(ctx);
70 * Find a child node as identified by an absolute {@link YangInstanceIdentifier}.
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
76 public @NonNull Optional<@NonNull DataSchemaContextNode<?>> findChild(final @NonNull YangInstanceIdentifier path) {
77 return root.findChild(path);
81 * Find a child node as identified by an absolute {@link YangInstanceIdentifier} and return it along with a suitably
82 * initialized {@link SchemaInferenceStack}.
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
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);
95 return CheckedValue.ofException(new IllegalArgumentException("Failed to find " + arg + " in " + node));
100 return CheckedValue.ofValue(new NodeAndStack(node, stack));
103 public @NonNull DataSchemaContextNode<?> getRoot() {