Merge "Bug 2480: Union objects are generated incorrectly when using bits type"
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / StoreTreeNodes.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.yangtools.yang.data.api.schema.tree;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Predicate;
14 import com.google.common.base.Predicates;
15 import com.google.common.collect.Iterables;
16 import java.util.AbstractMap.SimpleEntry;
17 import java.util.Iterator;
18 import java.util.Map;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21
22 /**
23  * A set of utility methods for interacting with {@link StoreTreeNode} objects.
24  */
25 @Beta
26 public final class StoreTreeNodes {
27     private StoreTreeNodes() {
28         throw new UnsupportedOperationException("Utility class should not be instantiated");
29     }
30
31     /**
32      * Finds a node in tree
33      *
34      * @param tree Data Tree
35      * @param path Path to the node
36      * @return Optional with node if the node is present in tree, {@link Optional#absent()} otherwise.
37      */
38     public static <T extends StoreTreeNode<T>> Optional<T> findNode(final T tree, final YangInstanceIdentifier path) {
39         Optional<T> current = Optional.<T> of(tree);
40         Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
41         while (current.isPresent() && pathIter.hasNext()) {
42             current = current.get().getChild(pathIter.next());
43         }
44         return current;
45     }
46
47     public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) {
48         T current = tree;
49
50         int i = 1;
51         for(PathArgument pathArg : path.getPathArguments()) {
52             Optional<T> potential = current.getChild(pathArg);
53             if (!potential.isPresent()) {
54                 throw new IllegalArgumentException(String.format("Child %s is not present in tree.",
55                         Iterables.toString(Iterables.limit(path.getPathArguments(), i))));
56             }
57             current = potential.get();
58             ++i;
59         }
60         return current;
61     }
62
63     /**
64      * Finds a node or closest parent in  the tree
65      *
66      * @param tree Data Tree
67      * @param path Path to the node
68      * @return Map.Entry Entry with key which is path to closest parent and value is parent node.
69      *
70      */
71     public static <T extends StoreTreeNode<T>> Map.Entry<YangInstanceIdentifier, T> findClosest(final T tree, final YangInstanceIdentifier path) {
72         return findClosestsOrFirstMatch(tree, path, Predicates.<T>alwaysFalse());
73     }
74
75     public static <T extends StoreTreeNode<T>> Map.Entry<YangInstanceIdentifier, T> findClosestsOrFirstMatch(final T tree, final YangInstanceIdentifier path, final Predicate<T> predicate) {
76         Optional<T> parent = Optional.<T>of(tree);
77         Optional<T> current = Optional.<T> of(tree);
78
79         int nesting = 0;
80         Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
81         while (current.isPresent() && pathIter.hasNext() && !predicate.apply(current.get())) {
82             parent = current;
83             current = current.get().getChild(pathIter.next());
84             nesting++;
85         }
86         if (current.isPresent()) {
87             final YangInstanceIdentifier currentPath = YangInstanceIdentifier.create(Iterables.limit(path.getPathArguments(), nesting));
88             return new SimpleEntry<YangInstanceIdentifier,T>(currentPath,current.get());
89         }
90
91         /*
92          * Subtracting 1 from nesting level at this point is safe, because we
93          * cannot reach here with nesting == 0: that would mean the above check
94          * for current.isPresent() failed, which it cannot, as current is always
95          * present. At any rate we check state just to be on the safe side.
96          */
97         Preconditions.checkState(nesting > 0);
98         final YangInstanceIdentifier parentPath = YangInstanceIdentifier.create(Iterables.limit(path.getPathArguments(), nesting - 1));
99         return new SimpleEntry<YangInstanceIdentifier,T>(parentPath,parent.get());
100     }
101
102     public static <T extends StoreTreeNode<T>> Optional<T> getChild(final Optional<T> parent, final PathArgument child) {
103         if (parent.isPresent()) {
104             return parent.get().getChild(child);
105         }
106         return Optional.absent();
107     }
108 }