import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
return new InstanceIdentifier(ImmutableList.<PathArgument>builder().addAll(path).add(arg).build());
}
+ /**
+ * Get the relative path from an ancestor. This method attempts to perform the reverse
+ * of concatenating a base (ancestor) and a path.
+ *
+ * @param ancestor Ancestor against which the relative path should be calculated
+ * @return This object's relative path from parent, or Optional.absent() if the
+ * specified parent is not in fact an ancestor of this object.
+ */
+ public Optional<InstanceIdentifier> relativeTo(final InstanceIdentifier ancestor) {
+ if (ancestor.contains(this)) {
+ final int common = ancestor.path.size();
+ return Optional.of(new InstanceIdentifier(path.subList(common, path.size())));
+ } else {
+ return Optional.absent();
+ }
+ }
+
// Static factories & helpers
public static InstanceIdentifier of(final QName name) {
}
public static Optional<NormalizedNode<?, ?>> findNode(final InstanceIdentifier rootPath, final NormalizedNode<?, ?> rootNode, final InstanceIdentifier childPath) {
- if(rootPath.contains(childPath)) {
- int common = rootPath.getPath().size();
- InstanceIdentifier relativePath = new InstanceIdentifier(childPath.getPath().subList(common, childPath.getPath().size()));
- return findNode(rootNode, relativePath);
+ final Optional<InstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
+ if (relativePath.isPresent()) {
+ return findNode(rootNode, relativePath.get());
+ } else {
+ return Optional.absent();
}
- return Optional.absent();
}
public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> tree, final InstanceIdentifier path) {
checkNotNull(path, "Path must not be null");
Optional<NormalizedNode<?, ?>> currentNode = Optional.<NormalizedNode<?, ?>> of(tree);
- Iterator<PathArgument> pathIterator = path.getPath().iterator();
+ final Iterator<PathArgument> pathIterator = path.getPath().iterator();
while (currentNode.isPresent() && pathIterator.hasNext()) {
currentNode = getDirectChild(currentNode.get(), pathIterator.next());
}