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 javax.annotation.Nonnull;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 * Semantic tree binding a {@link SchemaContext} to a {@link NormalizedNode} tree. Since the layout of the schema
24 * and data has differences, the mapping is not trivial -- which is where this class comes in.
26 * @author Robert Varga
28 // FIXME: 3.0.0: @NonNullByDefault
29 public final class DataSchemaContextTree {
30 private static final LoadingCache<SchemaContext, DataSchemaContextTree> TREES = CacheBuilder.newBuilder()
31 .weakKeys().weakValues().build(new CacheLoader<SchemaContext, DataSchemaContextTree>() {
33 public DataSchemaContextTree load(@Nonnull final SchemaContext key) throws Exception {
34 return new DataSchemaContextTree(key);
38 private final DataSchemaContextNode<?> root;
40 private DataSchemaContextTree(final SchemaContext ctx) {
41 root = DataSchemaContextNode.from(ctx);
44 @Nonnull public static DataSchemaContextTree from(@Nonnull final SchemaContext ctx) {
45 return TREES.getUnchecked(ctx);
49 * Find a child node as identified by an absolute {@link YangInstanceIdentifier}.
51 * @param path Path towards the child node
52 * @return Child node if present, or empty when corresponding child is not found.
53 * @throws NullPointerException if {@code path} is null
55 public @NonNull Optional<@NonNull DataSchemaContextNode<?>> findChild(final @NonNull YangInstanceIdentifier path) {
56 return getRoot().findChild(path);
60 * Get a child node as identified by an absolute {@link YangInstanceIdentifier}.
62 * @param path Path towards the child node
63 * @return Child node if present, or null when corresponding child is not found.
64 * @throws NullPointerException if {@code path} is null
66 * @deprecated Use {@link #findChild(YangInstanceIdentifier)} instead.
69 public @Nullable DataSchemaContextNode<?> getChild(final YangInstanceIdentifier path) {
70 DataSchemaContextNode<?> currentOp = root;
71 for (PathArgument arg : path.getPathArguments()) {
72 currentOp = currentOp.getChild(arg);
73 if (currentOp == null) {
80 public DataSchemaContextNode<?> getRoot() {