Merge branch 'master' of ../controller
[yangtools.git] / yang / 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 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;
20
21 /**
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.
24  *
25  * @author Robert Varga
26  */
27 // FIXME: 5.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>() {
31                 @Override
32                 public DataSchemaContextTree load(final SchemaContext key) throws Exception {
33                     return new DataSchemaContextTree(key);
34                 }
35             });
36
37     private final DataSchemaContextNode<?> root;
38
39     private DataSchemaContextTree(final SchemaContext ctx) {
40         root = DataSchemaContextNode.from(ctx);
41     }
42
43     public static @NonNull DataSchemaContextTree from(final @NonNull SchemaContext ctx) {
44         return TREES.getUnchecked(ctx);
45     }
46
47     /**
48      * Find a child node as identified by an absolute {@link YangInstanceIdentifier}.
49      *
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
53      */
54     public @NonNull Optional<@NonNull DataSchemaContextNode<?>> findChild(final @NonNull YangInstanceIdentifier path) {
55         return getRoot().findChild(path);
56     }
57
58     /**
59      * Get a child node as identified by an absolute {@link YangInstanceIdentifier}.
60      *
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
64      *
65      * @deprecated Use {@link #findChild(YangInstanceIdentifier)} instead.
66      */
67     @Deprecated(forRemoval = true)
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) {
73                 return null;
74             }
75         }
76         return currentOp;
77     }
78
79     public DataSchemaContextNode<?> getRoot() {
80         return root;
81     }
82 }