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