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.api.schema;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Strings;
15 import com.google.common.collect.Maps;
16 import java.util.Arrays;
17 import java.util.Iterator;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
27 * A set of utility methods for interacting with {@link NormalizedNode} objects.
30 public final class NormalizedNodes {
31 private static final int STRINGTREE_INDENT = 4;
33 private NormalizedNodes() {
34 throw new UnsupportedOperationException("Utility class should not be instantiated");
37 public static Optional<NormalizedNode<?, ?>> findNode(final YangInstanceIdentifier rootPath, final NormalizedNode<?, ?> rootNode, final YangInstanceIdentifier childPath) {
38 final Optional<YangInstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
39 return relativePath.isPresent() ? findNode(rootNode, relativePath.get()) : Optional.absent();
42 public static Optional<NormalizedNode<?, ?>> findNode(final Optional<NormalizedNode<?, ?>> parent, final Iterable<PathArgument> relativePath) {
43 checkNotNull(parent, "Parent must not be null");
44 checkNotNull(relativePath, "Relative path must not be null");
46 Optional<NormalizedNode<?, ?>> currentNode = parent;
47 final Iterator<PathArgument> pathIterator = relativePath.iterator();
48 while (currentNode.isPresent() && pathIterator.hasNext()) {
49 currentNode = getDirectChild(currentNode.get(), pathIterator.next());
54 public static Optional<NormalizedNode<?, ?>> findNode(final Optional<NormalizedNode<?, ?>> parent, final PathArgument... relativePath) {
55 return findNode(parent, Arrays.asList(relativePath));
58 public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> parent, final Iterable<PathArgument> relativePath) {
59 return findNode(Optional.fromNullable(parent), relativePath);
62 public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> parent, final PathArgument... relativePath) {
63 return findNode(parent, Arrays.asList(relativePath));
66 public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> tree, final YangInstanceIdentifier path) {
67 checkNotNull(tree, "Tree must not be null");
68 checkNotNull(path, "Path must not be null");
70 return findNode(Optional.of(tree), path.getPathArguments());
73 @SuppressWarnings({ "unchecked", "rawtypes" })
74 public static Optional<NormalizedNode<?, ?>> getDirectChild(final NormalizedNode<?, ?> node, final PathArgument pathArg) {
75 if (node instanceof LeafNode<?> || node instanceof LeafSetEntryNode<?>) {
76 return Optional.absent();
77 } else if (node instanceof DataContainerNode<?>) {
78 return (Optional) ((DataContainerNode<?>) node).getChild(pathArg);
79 } else if (node instanceof MapNode && pathArg instanceof NodeIdentifierWithPredicates) {
80 return (Optional) ((MapNode) node).getChild((NodeIdentifierWithPredicates) pathArg);
81 } else if (node instanceof LeafSetNode<?>) {
82 return (Optional) ((LeafSetNode<?>) node).getChild((NodeWithValue) pathArg);
84 return Optional.absent();
88 * Convert a data subtree under a node into a human-readable string format.
90 * @param node Data subtree root
91 * @return String containing a human-readable form of the subtree.
93 public static String toStringTree(final NormalizedNode<?, ?> node) {
94 final StringBuilder builder = new StringBuilder();
95 toStringTree(builder, node, 0);
96 return builder.toString();
99 private static void toStringTree(final StringBuilder builder, final NormalizedNode<?, ?> node, final int offset) {
100 final String prefix = Strings.repeat(" ", offset);
102 builder.append(prefix).append(toStringTree(node.getIdentifier()));
103 if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
104 final NormalizedNodeContainer<?, ?, ?> container = (NormalizedNodeContainer<?, ?, ?>) node;
106 builder.append(" {\n");
107 for (NormalizedNode<?, ?> child : container.getValue()) {
108 toStringTree(builder, child, offset + STRINGTREE_INDENT);
111 builder.append(prefix).append('}');
113 builder.append(' ').append(node.getValue());
115 builder.append('\n');
118 private static String toStringTree(final PathArgument identifier) {
119 if (identifier instanceof NodeIdentifierWithPredicates) {
120 return identifier.getNodeType().getLocalName() +
121 ((NodeIdentifierWithPredicates) identifier).getKeyValues().values();
122 } else if (identifier instanceof AugmentationIdentifier) {
123 return "augmentation";
125 return identifier.getNodeType().getLocalName();
130 * Find duplicate NormalizedNode instances within a subtree. Duplicates are those, which compare
131 * as equal, but do not refer to the same object.
133 * @param node A normalized node subtree, may not be null
134 * @return A Map of NormalizedNode/DuplicateEntry relationships.
136 public static Map<NormalizedNode<?, ?>, DuplicateEntry> findDuplicates(@Nonnull final NormalizedNode<?, ?> node) {
137 return Maps.filterValues(DuplicateFinder.findDuplicates(node), input -> !input.getDuplicates().isEmpty());