Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / StoreTreeNodes.java
1 /*
2  * Copyright (c) 2014 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.api.schema.tree;
9
10 import static com.google.common.base.Verify.verify;
11
12 import com.google.common.annotations.Beta;
13 import java.util.AbstractMap.SimpleImmutableEntry;
14 import java.util.Iterator;
15 import java.util.Map.Entry;
16 import java.util.Optional;
17 import java.util.function.Predicate;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20
21 /**
22  * A set of utility methods for interacting with {@link StoreTreeNode} objects.
23  */
24 @Beta
25 public final class StoreTreeNodes {
26     private StoreTreeNodes() {
27         throw new UnsupportedOperationException("Utility class should not be instantiated");
28     }
29
30     /**
31      * Finds a node in tree.
32      *
33      * @param <T>
34      *          Store tree node type.
35      * @param tree Data Tree
36      * @param path Path to the node
37      * @return Optional with node if the node is present in tree, {@link Optional#empty()} otherwise.
38      */
39     public static <T extends StoreTreeNode<T>> Optional<? extends T> findNode(final T tree,
40             final YangInstanceIdentifier path) {
41         Optional<? extends T> current = Optional.of(tree);
42         Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
43         while (current.isPresent() && pathIter.hasNext()) {
44             current = current.get().getChild(pathIter.next());
45         }
46         return current;
47     }
48
49     public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) {
50         T current = tree;
51
52         int depth = 1;
53         for (PathArgument pathArg : path.getPathArguments()) {
54             Optional<? extends T> potential = current.getChild(pathArg);
55             if (potential.isEmpty()) {
56                 throw new IllegalArgumentException(String.format("Child %s is not present in tree.",
57                         path.getAncestor(depth)));
58             }
59             current = potential.get();
60             ++depth;
61         }
62         return current;
63     }
64
65     /**
66      * Finds a node or closest parent in the tree.
67      *
68      * @param <T>
69      *          Store tree node type.
70      * @param tree Data Tree
71      * @param path Path to the node
72      * @return Map.Entry Entry with key which is path to closest parent and value is parent node.
73      */
74     public static <T extends StoreTreeNode<T>> Entry<YangInstanceIdentifier, T> findClosest(final T tree,
75             final YangInstanceIdentifier path) {
76         return findClosestsOrFirstMatch(tree, path, input -> false);
77     }
78
79     public static <T extends StoreTreeNode<T>> Entry<YangInstanceIdentifier, T> findClosestsOrFirstMatch(final T tree,
80             final YangInstanceIdentifier path, final Predicate<T> predicate) {
81         Optional<? extends T> parent = Optional.of(tree);
82         Optional<? extends T> current = Optional.of(tree);
83
84         int nesting = 0;
85         Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
86         while (current.isPresent() && pathIter.hasNext() && !predicate.test(current.get())) {
87             parent = current;
88             current = current.get().getChild(pathIter.next());
89             nesting++;
90         }
91         if (current.isPresent()) {
92             final YangInstanceIdentifier currentPath = path.getAncestor(nesting);
93             return new SimpleImmutableEntry<>(currentPath, current.get());
94         }
95
96         /*
97          * Subtracting 1 from nesting level at this point is safe, because we
98          * cannot reach here with nesting == 0: that would mean the above check
99          * for current.isPresent() failed, which it cannot, as current is always
100          * present. At any rate we verify state just to be on the safe side.
101          */
102         verify(nesting > 0);
103         return new SimpleImmutableEntry<>(path.getAncestor(nesting - 1), parent.get());
104     }
105
106     public static <T extends StoreTreeNode<T>> Optional<? extends T> getChild(final Optional<T> parent,
107             final PathArgument child) {
108         if (parent.isPresent()) {
109             return parent.get().getChild(child);
110         }
111         return Optional.empty();
112     }
113 }