Merge "BUG-704 Remove pax from netconf identity-ref test."
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / 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.controller.md.sal.dom.store.impl.tree;
9
10 import java.util.AbstractMap.SimpleEntry;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Map;
15
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
18
19 import com.google.common.base.Optional;
20 import com.google.common.base.Preconditions;
21 import com.google.common.base.Predicate;
22 import com.google.common.base.Predicates;
23
24 public final class TreeNodeUtils {
25     private TreeNodeUtils() {
26         throw new UnsupportedOperationException("Utility class should not be instantiated");
27     }
28
29     /**
30      * Finds a node in tree
31      *
32      * @param tree Data Tree
33      * @param path Path to the node
34      * @return Optional with node if the node is present in tree, {@link Optional#absent()} otherwise.
35      *
36      */
37     public static <T extends StoreTreeNode<T>> Optional<T> findNode(final T tree, final InstanceIdentifier path) {
38         Optional<T> current = Optional.<T> of(tree);
39         Iterator<PathArgument> pathIter = path.getPath().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 InstanceIdentifier path) {
47         T current = tree;
48         List<PathArgument> nested = new ArrayList<>(path.getPath().size());
49         for(PathArgument pathArg : path.getPath()) {
50             Optional<T> potential = current.getChild(pathArg);
51             nested.add(pathArg);
52             Preconditions.checkArgument(potential.isPresent(),"Child %s is not present in tree.",nested);
53             current = potential.get();
54         }
55         return current;
56     }
57
58     /**
59      * Finds a node or closest parent in  the tree
60      *
61      * @param tree Data Tree
62      * @param path Path to the node
63      * @return Map.Entry Entry with key which is path to closest parent and value is parent node.
64      *
65      */
66     public static <T extends StoreTreeNode<T>> Map.Entry<InstanceIdentifier, T> findClosest(final T tree, final InstanceIdentifier path) {
67         return findClosestsOrFirstMatch(tree, path, Predicates.<T>alwaysFalse());
68     }
69
70     public static <T extends StoreTreeNode<T>> Map.Entry<InstanceIdentifier, T> findClosestsOrFirstMatch(final T tree, final InstanceIdentifier path, final Predicate<T> predicate) {
71         Optional<T> parent = Optional.<T>of(tree);
72         Optional<T> current = Optional.<T> of(tree);
73
74         int nesting = 0;
75         Iterator<PathArgument> pathIter = path.getPath().iterator();
76         while (current.isPresent() && pathIter.hasNext() && !predicate.apply(current.get())) {
77             parent = current;
78             current = current.get().getChild(pathIter.next());
79             nesting++;
80         }
81         if(current.isPresent()) {
82             final InstanceIdentifier currentPath = new InstanceIdentifier(path.getPath().subList(0, nesting));
83             return new SimpleEntry<InstanceIdentifier,T>(currentPath,current.get());
84         }
85
86         /*
87          * Subtracting 1 from nesting level at this point is safe, because we
88          * cannot reach here with nesting == 0: that would mean the above check
89          * for current.isPresent() failed, which it cannot, as current is always
90          * present. At any rate we check state just to be on the safe side.
91          */
92         Preconditions.checkState(nesting > 0);
93         final InstanceIdentifier parentPath = new InstanceIdentifier(path.getPath().subList(0, nesting - 1));
94
95         return new SimpleEntry<InstanceIdentifier,T>(parentPath,parent.get());
96     }
97
98     public static <T extends StoreTreeNode<T>> Optional<T> getChild(final Optional<T> parent,final PathArgument child) {
99         if(parent.isPresent()) {
100             return parent.get().getChild(child);
101         }
102         return Optional.absent();
103     }
104
105 }