0fb94c54ac33f01171d485c7666dda5e0a8b41c9
[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 com.google.common.annotations.Beta;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Predicate;
13 import com.google.common.base.Predicates;
14 import com.google.common.base.Verify;
15 import java.util.AbstractMap.SimpleImmutableEntry;
16 import java.util.Iterator;
17 import java.util.Map.Entry;
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      * @param <T>
33      *          Store tree node type.
34      * @param tree Data Tree
35      * @param path Path to the node
36      * @return Optional with node if the node is present in tree, {@link Optional#absent()} otherwise.
37      */
38     public static <T extends StoreTreeNode<T>> Optional<T> findNode(final T tree, final YangInstanceIdentifier path) {
39         Optional<T> current = Optional.of(tree);
40         Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
41         while (current.isPresent() && pathIter.hasNext()) {
42             current = current.get().getChild(pathIter.next());
43         }
44         return current;
45     }
46
47     public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) {
48         T current = tree;
49
50         int i = 1;
51         for(PathArgument pathArg : path.getPathArguments()) {
52             Optional<T> potential = current.getChild(pathArg);
53             if (!potential.isPresent()) {
54                 throw new IllegalArgumentException(String.format("Child %s is not present in tree.",
55                         path.getAncestor(i)));
56             }
57             current = potential.get();
58             ++i;
59         }
60         return current;
61     }
62
63     /**
64      * Finds a node or closest parent in  the tree
65      * @param <T>
66      *          Store tree node type.
67      * @param tree Data Tree
68      * @param path Path to the node
69      * @return Map.Entry Entry with key which is path to closest parent and value is parent node.
70      *
71      */
72     public static <T extends StoreTreeNode<T>> Entry<YangInstanceIdentifier, T> findClosest(final T tree,
73             final YangInstanceIdentifier path) {
74         return findClosestsOrFirstMatch(tree, path, Predicates.alwaysFalse());
75     }
76
77     public static <T extends StoreTreeNode<T>> Entry<YangInstanceIdentifier, T> findClosestsOrFirstMatch(final T tree,
78             final YangInstanceIdentifier path, final Predicate<T> predicate) {
79         Optional<T> parent = Optional.of(tree);
80         Optional<T> current = Optional.of(tree);
81
82         int nesting = 0;
83         Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
84         while (current.isPresent() && pathIter.hasNext() && !predicate.apply(current.get())) {
85             parent = current;
86             current = current.get().getChild(pathIter.next());
87             nesting++;
88         }
89         if (current.isPresent()) {
90             final YangInstanceIdentifier currentPath = path.getAncestor(nesting);
91             return new SimpleImmutableEntry<>(currentPath, current.get());
92         }
93
94         /*
95          * Subtracting 1 from nesting level at this point is safe, because we
96          * cannot reach here with nesting == 0: that would mean the above check
97          * for current.isPresent() failed, which it cannot, as current is always
98          * present. At any rate we verify state just to be on the safe side.
99          */
100         Verify.verify(nesting > 0);
101         return new SimpleImmutableEntry<>(path.getAncestor(nesting - 1), parent.get());
102     }
103
104     public static <T extends StoreTreeNode<T>> Optional<T> getChild(final Optional<T> parent, final PathArgument child) {
105         if (parent.isPresent()) {
106             return parent.get().getChild(child);
107         }
108         return Optional.absent();
109     }
110 }