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.yangtools.yang.data.impl.schema.tree;
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;
16 import java.util.AbstractMap.SimpleEntry;
17 import java.util.Iterator;
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;
25 * A set of utility methods for interacting with {@link org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNode} objects.
27 public final class TreeNodeUtils {
28 private TreeNodeUtils() {
29 throw new UnsupportedOperationException("Utility class should not be instantiated");
33 * Finds a node in tree
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.
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());
48 public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) {
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))));
58 current = potential.get();
65 * Finds a node or closest parent in the tree
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.
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());
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);
81 Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
82 while (current.isPresent() && pathIter.hasNext() && !predicate.apply(current.get())) {
84 current = current.get().getChild(pathIter.next());
87 if(current.isPresent()) {
88 final YangInstanceIdentifier currentPath = YangInstanceIdentifier.create(path.getPath().subList(0, nesting));
89 return new SimpleEntry<YangInstanceIdentifier,T>(currentPath,current.get());
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.
98 Preconditions.checkState(nesting > 0);
99 final YangInstanceIdentifier parentPath = YangInstanceIdentifier.create(path.getPath().subList(0, nesting - 1));
101 return new SimpleEntry<YangInstanceIdentifier,T>(parentPath,parent.get());
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);
108 return Optional.absent();