2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.store.impl.tree;
10 import java.util.AbstractMap.SimpleEntry;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
16 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNode;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
20 import com.google.common.base.Optional;
21 import com.google.common.base.Preconditions;
22 import com.google.common.base.Predicate;
23 import com.google.common.base.Predicates;
26 * A set of utility methods for interacting with {@link TreeNode} objects.
28 public final class TreeNodeUtils {
29 private TreeNodeUtils() {
30 throw new UnsupportedOperationException("Utility class should not be instantiated");
34 * Finds a node in tree
36 * @param tree Data Tree
37 * @param path Path to the node
38 * @return Optional with node if the node is present in tree, {@link Optional#absent()} otherwise.
40 public static <T extends StoreTreeNode<T>> Optional<T> findNode(final T tree, final InstanceIdentifier path) {
41 Optional<T> current = Optional.<T> of(tree);
42 Iterator<PathArgument> pathIter = path.getPath().iterator();
43 while (current.isPresent() && pathIter.hasNext()) {
44 current = current.get().getChild(pathIter.next());
49 public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final InstanceIdentifier path) {
51 List<PathArgument> nested = new ArrayList<>(path.getPath().size());
52 for(PathArgument pathArg : path.getPath()) {
53 Optional<T> potential = current.getChild(pathArg);
55 Preconditions.checkArgument(potential.isPresent(),"Child %s is not present in tree.",nested);
56 current = potential.get();
62 * Finds a node or closest parent in the tree
64 * @param tree Data Tree
65 * @param path Path to the node
66 * @return Map.Entry Entry with key which is path to closest parent and value is parent node.
69 public static <T extends StoreTreeNode<T>> Map.Entry<InstanceIdentifier, T> findClosest(final T tree, final InstanceIdentifier path) {
70 return findClosestsOrFirstMatch(tree, path, Predicates.<T>alwaysFalse());
73 public static <T extends StoreTreeNode<T>> Map.Entry<InstanceIdentifier, T> findClosestsOrFirstMatch(final T tree, final InstanceIdentifier path, final Predicate<T> predicate) {
74 Optional<T> parent = Optional.<T>of(tree);
75 Optional<T> current = Optional.<T> of(tree);
78 Iterator<PathArgument> pathIter = path.getPath().iterator();
79 while (current.isPresent() && pathIter.hasNext() && !predicate.apply(current.get())) {
81 current = current.get().getChild(pathIter.next());
84 if(current.isPresent()) {
85 final InstanceIdentifier currentPath = new InstanceIdentifier(path.getPath().subList(0, nesting));
86 return new SimpleEntry<InstanceIdentifier,T>(currentPath,current.get());
90 * Subtracting 1 from nesting level at this point is safe, because we
91 * cannot reach here with nesting == 0: that would mean the above check
92 * for current.isPresent() failed, which it cannot, as current is always
93 * present. At any rate we check state just to be on the safe side.
95 Preconditions.checkState(nesting > 0);
96 final InstanceIdentifier parentPath = new InstanceIdentifier(path.getPath().subList(0, nesting - 1));
98 return new SimpleEntry<InstanceIdentifier,T>(parentPath,parent.get());
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);
105 return Optional.absent();