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;
15 import java.util.AbstractMap.SimpleEntry;
16 import java.util.Iterator;
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;
23 * A set of utility methods for interacting with {@link org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNode} objects.
25 public final class TreeNodeUtils {
26 private TreeNodeUtils() {
27 throw new UnsupportedOperationException("Utility class should not be instantiated");
31 * Finds a node in tree
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.
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());
46 public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) {
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))));
56 current = potential.get();
63 * Finds a node or closest parent in the tree
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.
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());
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);
79 Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
80 while (current.isPresent() && pathIter.hasNext() && !predicate.apply(current.get())) {
82 current = current.get().getChild(pathIter.next());
85 if(current.isPresent()) {
86 final YangInstanceIdentifier currentPath = YangInstanceIdentifier.create(Iterables.limit(path.getPathArguments(), nesting));
87 return new SimpleEntry<YangInstanceIdentifier,T>(currentPath,current.get());
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.
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());
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();