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 java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Strings;
14 import com.google.common.collect.Maps;
15 import java.util.Arrays;
16 import java.util.Iterator;
18 import java.util.Optional;
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,
38 final NormalizedNode<?, ?> rootNode, final YangInstanceIdentifier childPath) {
39 final Optional<YangInstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
40 return relativePath.isPresent() ? findNode(rootNode, relativePath.get()) : Optional.empty();
43 public static Optional<NormalizedNode<?, ?>> findNode(final Optional<NormalizedNode<?, ?>> parent,
44 final Iterable<PathArgument> relativePath) {
45 requireNonNull(parent, "Parent must not be null");
46 requireNonNull(relativePath, "Relative path must not be null");
48 Optional<NormalizedNode<?, ?>> currentNode = parent;
49 final Iterator<PathArgument> pathIterator = relativePath.iterator();
50 while (currentNode.isPresent() && pathIterator.hasNext()) {
51 currentNode = getDirectChild(currentNode.get(), pathIterator.next());
56 public static Optional<NormalizedNode<?, ?>> findNode(final Optional<NormalizedNode<?, ?>> parent,
57 final PathArgument... relativePath) {
58 return findNode(parent, Arrays.asList(relativePath));
61 public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> parent,
62 final Iterable<PathArgument> relativePath) {
63 return findNode(Optional.ofNullable(parent), relativePath);
66 public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> parent,
67 final PathArgument... relativePath) {
68 return findNode(parent, Arrays.asList(relativePath));
71 public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> tree,
72 final YangInstanceIdentifier path) {
73 requireNonNull(tree, "Tree must not be null");
74 requireNonNull(path, "Path must not be null");
76 return findNode(Optional.of(tree), path.getPathArguments());
79 @SuppressWarnings({ "unchecked", "rawtypes" })
80 public static Optional<NormalizedNode<?, ?>> getDirectChild(final NormalizedNode<?, ?> node,
81 final PathArgument pathArg) {
82 if (node instanceof LeafNode<?> || node instanceof LeafSetEntryNode<?>) {
83 return Optional.empty();
84 } else if (node instanceof DataContainerNode<?>) {
85 return (Optional) ((DataContainerNode<?>) node).getChild(pathArg);
86 } else if (node instanceof MapNode && pathArg instanceof NodeIdentifierWithPredicates) {
87 return (Optional) ((MapNode) node).getChild((NodeIdentifierWithPredicates) pathArg);
88 } else if (node instanceof LeafSetNode<?>) {
89 return (Optional) ((LeafSetNode<?>) node).getChild((NodeWithValue) pathArg);
91 return Optional.empty();
95 * Convert a data subtree under a node into a human-readable string format.
97 * @param node Data subtree root
98 * @return String containing a human-readable form of the subtree.
100 public static String toStringTree(final NormalizedNode<?, ?> node) {
101 final StringBuilder builder = new StringBuilder();
102 toStringTree(builder, node, 0);
103 return builder.toString();
106 private static void toStringTree(final StringBuilder builder, final NormalizedNode<?, ?> node, final int offset) {
107 final String prefix = Strings.repeat(" ", offset);
109 builder.append(prefix).append(toStringTree(node.getIdentifier()));
110 if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
111 final NormalizedNodeContainer<?, ?, ?> container = (NormalizedNodeContainer<?, ?, ?>) node;
113 builder.append(" {\n");
114 for (NormalizedNode<?, ?> child : container.getValue()) {
115 toStringTree(builder, child, offset + STRINGTREE_INDENT);
118 builder.append(prefix).append('}');
120 builder.append(' ').append(node.getValue());
122 builder.append('\n');
125 private static String toStringTree(final PathArgument identifier) {
126 if (identifier instanceof NodeIdentifierWithPredicates) {
127 return identifier.getNodeType().getLocalName()
128 + ((NodeIdentifierWithPredicates) identifier).getKeyValues().values();
129 } else if (identifier instanceof AugmentationIdentifier) {
130 return "augmentation";
132 return identifier.getNodeType().getLocalName();
137 * Find duplicate NormalizedNode instances within a subtree. Duplicates are those, which compare
138 * as equal, but do not refer to the same object.
140 * @param node A normalized node subtree, may not be null
141 * @return A Map of NormalizedNode/DuplicateEntry relationships.
143 public static Map<NormalizedNode<?, ?>, DuplicateEntry> findDuplicates(@Nonnull final NormalizedNode<?, ?> node) {
144 return Maps.filterValues(DuplicateFinder.findDuplicates(node), input -> !input.getDuplicates().isEmpty());