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.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.Iterables;
15 import com.google.common.collect.Maps;
16 import java.util.Arrays;
17 import java.util.Iterator;
19 import java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
31 * A set of utility methods for interacting with {@link NormalizedNode} objects.
34 public final class NormalizedNodes {
35 private static final int STRINGTREE_INDENT = 4;
37 private NormalizedNodes() {
42 * Find duplicate NormalizedNode instances within a subtree. Duplicates are those, which compare
43 * as equal, but do not refer to the same object.
45 * @param node A normalized node subtree, may not be null
46 * @return A Map of NormalizedNode/DuplicateEntry relationships.
48 public static Map<NormalizedNode<?, ?>, DuplicateEntry> findDuplicates(final @NonNull NormalizedNode<?, ?> node) {
49 return Maps.filterValues(DuplicateFinder.findDuplicates(node), input -> !input.getDuplicates().isEmpty());
52 public static Optional<NormalizedNode<?, ?>> findNode(final YangInstanceIdentifier rootPath,
53 final NormalizedNode<?, ?> rootNode, final YangInstanceIdentifier childPath) {
54 final Optional<YangInstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
55 return relativePath.isPresent() ? findNode(rootNode, relativePath.get()) : Optional.empty();
58 public static Optional<NormalizedNode<?, ?>> findNode(final Optional<NormalizedNode<?, ?>> parent,
59 final Iterable<PathArgument> relativePath) {
60 final Iterator<PathArgument> pathIterator = requireNonNull(relativePath, "Relative path must not be null")
62 Optional<NormalizedNode<?, ?>> currentNode = requireNonNull(parent, "Parent must not be null");
63 while (currentNode.isPresent() && pathIterator.hasNext()) {
64 currentNode = getDirectChild(currentNode.get(), pathIterator.next());
69 public static Optional<NormalizedNode<?, ?>> findNode(final Optional<NormalizedNode<?, ?>> parent,
70 final PathArgument... relativePath) {
71 return findNode(parent, Arrays.asList(relativePath));
74 public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> parent,
75 final Iterable<PathArgument> relativePath) {
76 return findNode(Optional.ofNullable(parent), relativePath);
80 * Lookup a node based on relative SchemaPath.
82 * @deprecated Use {@link #findNode(NormalizedNode, Descendant)} instead.
84 @Deprecated(forRemoval = true)
85 public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> parent,
86 final SchemaPath relativePath) {
87 checkArgument(!relativePath.isAbsolute(), "%s is not a relative path", relativePath);
88 return findNode(Optional.ofNullable(parent), Iterables.transform(relativePath.getPathFromRoot(),
89 NodeIdentifier::new));
92 public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> parent, final Descendant path) {
93 return findNode(Optional.ofNullable(parent),
94 Iterables.transform(path.getNodeIdentifiers(), NodeIdentifier::new));
97 public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> parent,
98 final PathArgument... relativePath) {
99 return findNode(parent, Arrays.asList(relativePath));
102 public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> tree,
103 final YangInstanceIdentifier path) {
104 return findNode(Optional.of(requireNonNull(tree, "Tree must not be null")),
105 requireNonNull(path, "Path must not be null").getPathArguments());
108 @SuppressWarnings({ "unchecked", "rawtypes" })
109 public static Optional<NormalizedNode<?, ?>> getDirectChild(final NormalizedNode<?, ?> node,
110 final PathArgument pathArg) {
111 if (node instanceof ValueNode) {
112 return Optional.empty();
113 } else if (node instanceof DataContainerNode) {
114 return (Optional) ((DataContainerNode<?>) node).getChild(pathArg);
115 } else if (node instanceof MapNode && pathArg instanceof NodeIdentifierWithPredicates) {
116 return (Optional) ((MapNode) node).getChild((NodeIdentifierWithPredicates) pathArg);
117 } else if (node instanceof LeafSetNode && pathArg instanceof NodeWithValue) {
118 return (Optional) ((LeafSetNode<?>) node).getChild((NodeWithValue) pathArg);
120 return Optional.empty();
124 * Convert a data subtree under a node into a human-readable string format.
126 * @param node Data subtree root
127 * @return String containing a human-readable form of the subtree.
129 public static String toStringTree(final NormalizedNode<?, ?> node) {
130 final StringBuilder builder = new StringBuilder();
131 toStringTree(builder, node, 0);
132 return builder.toString();
135 private static void toStringTree(final StringBuilder builder, final NormalizedNode<?, ?> node, final int offset) {
136 final String prefix = " ".repeat(offset);
138 builder.append(prefix).append(toStringTree(node.getIdentifier()));
139 if (node instanceof NormalizedNodeContainer) {
140 final NormalizedNodeContainer<?, ?, ?> container = (NormalizedNodeContainer<?, ?, ?>) node;
142 builder.append(" {\n");
143 for (NormalizedNode<?, ?> child : container.getValue()) {
144 toStringTree(builder, child, offset + STRINGTREE_INDENT);
147 builder.append(prefix).append('}');
149 builder.append(' ').append(node.getValue());
151 builder.append('\n');
154 private static String toStringTree(final PathArgument identifier) {
155 if (identifier instanceof NodeIdentifierWithPredicates) {
156 return identifier.getNodeType().getLocalName() + ((NodeIdentifierWithPredicates) identifier).values();
157 } else if (identifier instanceof AugmentationIdentifier) {
158 return "augmentation";
160 return identifier.getNodeType().getLocalName();