Clean up DataSchemaContext{Node,Tree}
[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 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;
21
22 /**
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.
25  *
26  * @author Robert Varga
27  */
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>() {
32                 @Override
33                 public DataSchemaContextTree load(@Nonnull final SchemaContext key) throws Exception {
34                     return new DataSchemaContextTree(key);
35                 }
36             });
37
38     private final DataSchemaContextNode<?> root;
39
40     private DataSchemaContextTree(final SchemaContext ctx) {
41         root = DataSchemaContextNode.from(ctx);
42     }
43
44     @Nonnull public static DataSchemaContextTree from(@Nonnull final SchemaContext ctx) {
45         return TREES.getUnchecked(ctx);
46     }
47
48     /**
49      * Find a child node as identified by an absolute {@link YangInstanceIdentifier}.
50      *
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
54      */
55     public @NonNull Optional<@NonNull DataSchemaContextNode<?>> findChild(final @NonNull YangInstanceIdentifier path) {
56         return getRoot().findChild(path);
57     }
58
59     /**
60      * Get a child node as identified by an absolute {@link YangInstanceIdentifier}.
61      *
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
65      *
66      * @deprecated Use {@link #findChild(YangInstanceIdentifier)} instead.
67      */
68     @Deprecated
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) {
74                 return null;
75             }
76         }
77         return currentOp;
78     }
79
80     public DataSchemaContextNode<?> getRoot() {
81         return root;
82     }
83 }