BUG-1092: rename data.api.InstanceIdentifier to YangInstanceIdentifier
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / TreeNodeUtils.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.impl.schema.tree;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Predicate;
13 import com.google.common.base.Predicates;
14 import com.google.common.collect.Iterables;
15
16 import java.util.AbstractMap.SimpleEntry;
17 import java.util.Iterator;
18 import java.util.Map;
19
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNode;
23
24 /**
25  * A set of utility methods for interacting with {@link org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNode} objects.
26  */
27 public final class TreeNodeUtils {
28     private TreeNodeUtils() {
29         throw new UnsupportedOperationException("Utility class should not be instantiated");
30     }
31
32     /**
33      * Finds a node in tree
34      *
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#absent()} otherwise.
38      */
39     public static <T extends StoreTreeNode<T>> Optional<T> findNode(final T tree, final YangInstanceIdentifier path) {
40         Optional<T> current = Optional.<T> of(tree);
41         Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
42         while (current.isPresent() && pathIter.hasNext()) {
43             current = current.get().getChild(pathIter.next());
44         }
45         return current;
46     }
47
48     public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) {
49         T current = tree;
50
51         int i = 1;
52         for(PathArgument pathArg : path.getPathArguments()) {
53             Optional<T> potential = current.getChild(pathArg);
54             if (!potential.isPresent()) {
55                 throw new IllegalArgumentException(String.format("Child %s is not present in tree.",
56                         Iterables.toString(Iterables.limit(path.getPathArguments(), i))));
57             }
58             current = potential.get();
59             ++i;
60         }
61         return current;
62     }
63
64     /**
65      * Finds a node or closest parent in  the tree
66      *
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>> Map.Entry<YangInstanceIdentifier, T> findClosest(final T tree, final YangInstanceIdentifier path) {
73         return findClosestsOrFirstMatch(tree, path, Predicates.<T>alwaysFalse());
74     }
75
76     public static <T extends StoreTreeNode<T>> Map.Entry<YangInstanceIdentifier, T> findClosestsOrFirstMatch(final T tree, final YangInstanceIdentifier path, final Predicate<T> predicate) {
77         Optional<T> parent = Optional.<T>of(tree);
78         Optional<T> current = Optional.<T> of(tree);
79
80         int nesting = 0;
81         Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
82         while (current.isPresent() && pathIter.hasNext() && !predicate.apply(current.get())) {
83             parent = current;
84             current = current.get().getChild(pathIter.next());
85             nesting++;
86         }
87         if(current.isPresent()) {
88             final YangInstanceIdentifier currentPath = YangInstanceIdentifier.create(path.getPath().subList(0, nesting));
89             return new SimpleEntry<YangInstanceIdentifier,T>(currentPath,current.get());
90         }
91
92         /*
93          * Subtracting 1 from nesting level at this point is safe, because we
94          * cannot reach here with nesting == 0: that would mean the above check
95          * for current.isPresent() failed, which it cannot, as current is always
96          * present. At any rate we check state just to be on the safe side.
97          */
98         Preconditions.checkState(nesting > 0);
99         final YangInstanceIdentifier parentPath = YangInstanceIdentifier.create(path.getPath().subList(0, nesting - 1));
100
101         return new SimpleEntry<YangInstanceIdentifier,T>(parentPath,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
111 }