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 com.google.common.cache.CacheBuilder;
11 import com.google.common.cache.CacheLoader;
12 import com.google.common.cache.LoadingCache;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 * Semantic tree binding a {@link SchemaContext} to a {@link NormalizedNode} tree. Since the layout of the schema
23 * and data has differences, the mapping is not trivial -- which is where this class comes in.
25 * @author Robert Varga
27 // FIXME: 3.0.0: @NonNullByDefault
28 public final class DataSchemaContextTree {
29 private static final LoadingCache<SchemaContext, DataSchemaContextTree> TREES = CacheBuilder.newBuilder()
30 .weakKeys().weakValues().build(new CacheLoader<SchemaContext, DataSchemaContextTree>() {
32 public DataSchemaContextTree load(final SchemaContext key) throws Exception {
33 return new DataSchemaContextTree(key);
37 private final DataSchemaContextNode<?> root;
39 private DataSchemaContextTree(final SchemaContext ctx) {
40 root = DataSchemaContextNode.from(ctx);
43 public static @NonNull DataSchemaContextTree from(final @NonNull SchemaContext ctx) {
44 return TREES.getUnchecked(ctx);
48 * Find a child node as identified by an absolute {@link YangInstanceIdentifier}.
50 * @param path Path towards the child node
51 * @return Child node if present, or empty when corresponding child is not found.
52 * @throws NullPointerException if {@code path} is null
54 public @NonNull Optional<@NonNull DataSchemaContextNode<?>> findChild(final @NonNull YangInstanceIdentifier path) {
55 return getRoot().findChild(path);
59 * Get a child node as identified by an absolute {@link YangInstanceIdentifier}.
61 * @param path Path towards the child node
62 * @return Child node if present, or null when corresponding child is not found.
63 * @throws NullPointerException if {@code path} is null
65 * @deprecated Use {@link #findChild(YangInstanceIdentifier)} instead.
68 public @Nullable DataSchemaContextNode<?> getChild(final YangInstanceIdentifier path) {
69 DataSchemaContextNode<?> currentOp = root;
70 for (PathArgument arg : path.getPathArguments()) {
71 currentOp = currentOp.getChild(arg);
72 if (currentOp == null) {
79 public DataSchemaContextNode<?> getRoot() {