X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-dom-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fdom%2Fstore%2Fimpl%2Ftree%2FTreeNodeUtils.java;h=fe98468b5cf370b4dddb003aeb04ee308382d58c;hb=48814d6a264b8f13e5db1422336d9ef25cb05fa9;hp=67cfde2e8c06e571b4a1ce248b42ca549d5eb53b;hpb=4f8166639477de8c1c9048baee2ba70003748756;p=controller.git diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/TreeNodeUtils.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/TreeNodeUtils.java index 67cfde2e8c..fe98468b5c 100644 --- a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/TreeNodeUtils.java +++ b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/tree/TreeNodeUtils.java @@ -8,15 +8,26 @@ package org.opendaylight.controller.md.sal.dom.store.impl.tree; import java.util.AbstractMap.SimpleEntry; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import java.util.Map; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument; import com.google.common.base.Optional; +import com.google.common.base.Preconditions; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; -public class TreeNodeUtils { +/** + * A set of utility methods for interacting with {@link org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNode} objects. + */ +public final class TreeNodeUtils { + private TreeNodeUtils() { + throw new UnsupportedOperationException("Utility class should not be instantiated"); + } /** * Finds a node in tree @@ -24,7 +35,6 @@ public class TreeNodeUtils { * @param tree Data Tree * @param path Path to the node * @return Optional with node if the node is present in tree, {@link Optional#absent()} otherwise. - * */ public static > Optional findNode(final T tree, final InstanceIdentifier path) { Optional current = Optional. of(tree); @@ -35,6 +45,18 @@ public class TreeNodeUtils { return current; } + public static > T findNodeChecked(final T tree, final InstanceIdentifier path) { + T current = tree; + List nested = new ArrayList<>(path.getPath().size()); + for(PathArgument pathArg : path.getPath()) { + Optional potential = current.getChild(pathArg); + nested.add(pathArg); + Preconditions.checkArgument(potential.isPresent(),"Child %s is not present in tree.",nested); + current = potential.get(); + } + return current; + } + /** * Finds a node or closest parent in the tree * @@ -44,12 +66,16 @@ public class TreeNodeUtils { * */ public static > Map.Entry findClosest(final T tree, final InstanceIdentifier path) { + return findClosestsOrFirstMatch(tree, path, Predicates.alwaysFalse()); + } + + public static > Map.Entry findClosestsOrFirstMatch(final T tree, final InstanceIdentifier path, final Predicate predicate) { Optional parent = Optional.of(tree); Optional current = Optional. of(tree); int nesting = 0; Iterator pathIter = path.getPath().iterator(); - while (current.isPresent() && pathIter.hasNext()) { + while (current.isPresent() && pathIter.hasNext() && !predicate.apply(current.get())) { parent = current; current = current.get().getChild(pathIter.next()); nesting++; @@ -58,10 +84,24 @@ public class TreeNodeUtils { final InstanceIdentifier currentPath = new InstanceIdentifier(path.getPath().subList(0, nesting)); return new SimpleEntry(currentPath,current.get()); } - // Nesting minus one is safe, since current is allways present when nesting = 0 - // so this prat of code is never triggered, in cases nesting == 0; + + /* + * Subtracting 1 from nesting level at this point is safe, because we + * cannot reach here with nesting == 0: that would mean the above check + * for current.isPresent() failed, which it cannot, as current is always + * present. At any rate we check state just to be on the safe side. + */ + Preconditions.checkState(nesting > 0); final InstanceIdentifier parentPath = new InstanceIdentifier(path.getPath().subList(0, nesting - 1)); + return new SimpleEntry(parentPath,parent.get()); } + public static > Optional getChild(final Optional parent,final PathArgument child) { + if(parent.isPresent()) { + return parent.get().getChild(child); + } + return Optional.absent(); + } + }