import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.Beta;
import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
import com.google.common.base.Strings;
+import com.google.common.collect.Maps;
+import java.util.Arrays;
import java.util.Iterator;
+import java.util.Map;
+import javax.annotation.Nonnull;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
@Beta
public final class NormalizedNodes {
private static final int STRINGTREE_INDENT = 4;
+ private static final Predicate<DuplicateEntry> DUPLICATES_ONLY = new Predicate<DuplicateEntry>() {
+ @Override
+ public boolean apply(final DuplicateEntry input) {
+ return !input.getDuplicates().isEmpty();
+ }
+ };
private NormalizedNodes() {
throw new UnsupportedOperationException("Utility class should not be instantiated");
}
}
- public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> tree, final YangInstanceIdentifier path) {
- checkNotNull(tree, "Tree must not be null");
- checkNotNull(path, "Path must not be null");
+ public static Optional<NormalizedNode<?, ?>> findNode(final Optional<NormalizedNode<?, ?>> parent, final Iterable<PathArgument> relativePath) {
+ checkNotNull(parent, "Parent must not be null");
+ checkNotNull(relativePath, "Relative path must not be null");
- Optional<NormalizedNode<?, ?>> currentNode = Optional.<NormalizedNode<?, ?>> of(tree);
- final Iterator<PathArgument> pathIterator = path.getPathArguments().iterator();
+ Optional<NormalizedNode<?, ?>> currentNode = parent;
+ final Iterator<PathArgument> pathIterator = relativePath.iterator();
while (currentNode.isPresent() && pathIterator.hasNext()) {
currentNode = getDirectChild(currentNode.get(), pathIterator.next());
}
return currentNode;
}
+ public static Optional<NormalizedNode<?, ?>> findNode(final Optional<NormalizedNode<?, ?>> parent, final PathArgument... relativePath) {
+ return findNode(parent, Arrays.asList(relativePath));
+ }
+
+ public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> parent, final Iterable<PathArgument> relativePath) {
+ return findNode(Optional.<NormalizedNode<?, ?>>fromNullable(parent), relativePath);
+ }
+
+ public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> parent, final PathArgument... relativePath) {
+ return findNode(parent, Arrays.asList(relativePath));
+ }
+
+ public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> tree, final YangInstanceIdentifier path) {
+ checkNotNull(tree, "Tree must not be null");
+ checkNotNull(path, "Path must not be null");
+
+ return findNode(Optional.<NormalizedNode<?, ?>>of(tree), path.getPathArguments());
+ }
+
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Optional<NormalizedNode<?, ?>> getDirectChild(final NormalizedNode<?, ?> node, final PathArgument pathArg) {
if (node instanceof LeafNode<?> || node instanceof LeafSetEntryNode<?>) {
return identifier.getNodeType().getLocalName();
}
}
+
+ /**
+ * Find duplicate NormalizedNode instances within a subtree. Duplicates are those, which compare
+ * as equal, but do not refer to the same object.
+ *
+ * @param node A normalized node subtree, may not be null
+ * @return A Map of NormalizedNode/DuplicateEntry relationships.
+ */
+ public static Map<NormalizedNode<?, ?>, DuplicateEntry> findDuplicates(@Nonnull final NormalizedNode<?, ?> node) {
+ return Maps.filterValues(DuplicateFinder.findDuplicates(node), DUPLICATES_ONLY);
+ }
}