Bug 499: Added support for old DOM Broker APIs.
[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 class TreeNodeUtils {
25
26     /**
27      * Finds a node in tree
28      *
29      * @param tree Data Tree
30      * @param path Path to the node
31      * @return Optional with node if the node is present in tree, {@link Optional#absent()} otherwise.
32      *
33      */
34     public static <T extends StoreTreeNode<T>> Optional<T> findNode(final T tree, final InstanceIdentifier path) {
35         Optional<T> current = Optional.<T> of(tree);
36         Iterator<PathArgument> pathIter = path.getPath().iterator();
37         while (current.isPresent() && pathIter.hasNext()) {
38             current = current.get().getChild(pathIter.next());
39         }
40         return current;
41     }
42
43
44     public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final InstanceIdentifier path) {
45         T current = tree;
46         List<PathArgument> nested = new ArrayList<>(path.getPath());
47         for(PathArgument pathArg : path.getPath()) {
48             Optional<T> potential = current.getChild(pathArg);
49             nested.add(pathArg);
50             Preconditions.checkArgument(potential.isPresent(),"Child %s is not present in tree.",nested);
51             current = potential.get();
52         }
53         return current;
54     }
55
56     /**
57      * Finds a node or closest parent in  the tree
58      *
59      * @param tree Data Tree
60      * @param path Path to the node
61      * @return Map.Entry Entry with key which is path to closest parent and value is parent node.
62      *
63      */
64     public static <T extends StoreTreeNode<T>> Map.Entry<InstanceIdentifier, T> findClosest(final T tree, final InstanceIdentifier path) {
65         return findClosestsOrFirstMatch(tree, path, Predicates.<T>alwaysFalse());
66     }
67
68     public static <T extends StoreTreeNode<T>> Map.Entry<InstanceIdentifier, T> findClosestsOrFirstMatch(final T tree, final InstanceIdentifier path, final Predicate<T> predicate) {
69         Optional<T> parent = Optional.<T>of(tree);
70         Optional<T> current = Optional.<T> of(tree);
71
72         int nesting = 0;
73         Iterator<PathArgument> pathIter = path.getPath().iterator();
74         while (current.isPresent() && pathIter.hasNext() && !predicate.apply(current.get())) {
75             parent = current;
76             current = current.get().getChild(pathIter.next());
77             nesting++;
78         }
79         if(current.isPresent()) {
80             final InstanceIdentifier currentPath = new InstanceIdentifier(path.getPath().subList(0, nesting));
81             return new SimpleEntry<InstanceIdentifier,T>(currentPath,current.get());
82         }
83         // Nesting minus one is safe, since current is allways present when nesting = 0
84         // so this prat of code is never triggered, in cases nesting == 0;
85         final InstanceIdentifier parentPath = new InstanceIdentifier(path.getPath().subList(0, nesting - 1));
86         return new SimpleEntry<InstanceIdentifier,T>(parentPath,parent.get());
87
88     }
89
90     public static <T extends StoreTreeNode<T>> Optional<T> getChild(final Optional<T> parent,final PathArgument child) {
91         if(parent.isPresent()) {
92             return parent.get().getChild(child);
93         }
94         return Optional.absent();
95     }
96
97 }