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.collect.Lists;
14 import com.google.common.collect.Maps;
15 import java.util.Arrays;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
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;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
28 * A set of utility methods for interacting with {@link NormalizedNode} objects.
31 public final class NormalizedNodes {
32 private static final int STRINGTREE_INDENT = 4;
34 private NormalizedNodes() {
39 * Find duplicate NormalizedNode instances within a subtree. Duplicates are those, which compare
40 * as equal, but do not refer to the same object.
42 * @param node A normalized node subtree, may not be null
43 * @return A Map of NormalizedNode/DuplicateEntry relationships.
45 public static Map<NormalizedNode, DuplicateEntry> findDuplicates(final @NonNull NormalizedNode node) {
46 return Maps.filterValues(DuplicateFinder.findDuplicates(node), input -> !input.getDuplicates().isEmpty());
49 public static Optional<NormalizedNode> findNode(final YangInstanceIdentifier rootPath,
50 final NormalizedNode rootNode, final YangInstanceIdentifier childPath) {
51 final var relativePath = childPath.relativeTo(rootPath);
52 return relativePath.isPresent() ? findNode(rootNode, relativePath.orElseThrow()) : Optional.empty();
55 public static Optional<NormalizedNode> findNode(final Optional<NormalizedNode> parent,
56 final Iterable<PathArgument> relativePath) {
57 final var pathIterator = requireNonNull(relativePath, "Relative path must not be null").iterator();
58 var currentNode = requireNonNull(parent, "Parent must not be null");
59 while (currentNode.isPresent() && pathIterator.hasNext()) {
60 currentNode = getDirectChild(currentNode.orElseThrow(), pathIterator.next());
65 public static Optional<NormalizedNode> findNode(final Optional<NormalizedNode> parent,
66 final PathArgument pathArg) {
67 return parent.flatMap(node -> getDirectChild(node, pathArg));
70 public static Optional<NormalizedNode> findNode(final Optional<NormalizedNode> parent,
71 final PathArgument... relativePath) {
72 return findNode(parent, Arrays.asList(relativePath));
75 public static Optional<NormalizedNode> findNode(final @Nullable NormalizedNode parent,
76 final PathArgument pathArg) {
77 return parent == null ? Optional.empty() : getDirectChild(parent, pathArg);
80 public static Optional<NormalizedNode> findNode(final NormalizedNode parent,
81 final Iterable<PathArgument> relativePath) {
82 return findNode(Optional.ofNullable(parent), relativePath);
85 public static Optional<NormalizedNode> findNode(final NormalizedNode parent, final Descendant path) {
86 return findNode(Optional.ofNullable(parent),
87 Lists.transform(path.getNodeIdentifiers(), NodeIdentifier::new));
90 public static Optional<NormalizedNode> findNode(final NormalizedNode parent,
91 final PathArgument... relativePath) {
92 return findNode(parent, Arrays.asList(relativePath));
95 public static Optional<NormalizedNode> findNode(final NormalizedNode tree,
96 final YangInstanceIdentifier path) {
97 return findNode(Optional.of(requireNonNull(tree, "Tree must not be null")),
98 requireNonNull(path, "Path must not be null").getPathArguments());
101 @SuppressWarnings({ "unchecked", "rawtypes" })
102 public static Optional<NormalizedNode> getDirectChild(final NormalizedNode node,
103 final PathArgument pathArg) {
104 if (node instanceof DataContainerNode dataContainer) {
105 return (Optional) dataContainer.findChildByArg(pathArg);
106 } else if (node instanceof MapNode map && pathArg instanceof NodeIdentifierWithPredicates nip) {
107 return (Optional) map.findChildByArg(nip);
108 } else if (node instanceof LeafSetNode<?> leafSet && pathArg instanceof NodeWithValue<?> nwv) {
109 return (Optional) leafSet.findChildByArg(nwv);
111 // Anything else, including ValueNode
112 return Optional.empty();
116 * Convert a data subtree under a node into a human-readable string format.
118 * @param node Data subtree root
119 * @return String containing a human-readable form of the subtree.
121 public static String toStringTree(final NormalizedNode node) {
122 final StringBuilder sb = new StringBuilder();
123 toStringTree(sb, node, 0);
124 return sb.toString();
127 private static void toStringTree(final StringBuilder sb, final NormalizedNode node, final int offset) {
128 final String prefix = " ".repeat(offset);
129 appendPathArgument(sb.append(prefix), node.getIdentifier());
130 if (node instanceof NormalizedNodeContainer<?> container) {
132 for (var child : container.body()) {
133 toStringTree(sb, child, offset + STRINGTREE_INDENT);
135 sb.append(prefix).append('}');
137 sb.append(' ').append(node.body());
142 private static void appendPathArgument(final StringBuilder sb, final PathArgument arg) {
143 sb.append(arg.getNodeType().getLocalName());
144 if (arg instanceof NodeIdentifierWithPredicates nip) {
145 sb.append(nip.values());