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 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);
38 private static final LoadingCache<EffectiveModelContext, @NonNull DataSchemaContextTree> TREES =
39 CacheBuilder.newBuilder().weakKeys().weakValues().build(new CacheLoader<>() {
41 public DataSchemaContextTree load(final EffectiveModelContext key) {
42 return new DataSchemaContextTree(key);
46 private final @NonNull ContainerContextNode root;
48 private DataSchemaContextTree(final EffectiveModelContext ctx) {
50 root = new ContainerContextNode(ctx);
53 public static @NonNull DataSchemaContextTree from(final @NonNull EffectiveModelContext ctx) {
54 return TREES.getUnchecked(ctx);
58 * Find a child node as identified by an absolute {@link YangInstanceIdentifier}.
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
64 public @NonNull Optional<@NonNull DataSchemaContextNode<?>> findChild(final @NonNull YangInstanceIdentifier path) {
65 return root.findChild(path);
69 * Find a child node as identified by an absolute {@link YangInstanceIdentifier} and return it along with a suitably
70 * initialized {@link SchemaInferenceStack}.
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
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);
83 return CheckedValue.ofException(new IllegalArgumentException("Failed to find " + arg + " in " + node));
88 return CheckedValue.ofValue(new NodeAndStack(node, stack));
91 public @NonNull DataSchemaContextNode<?> getRoot() {