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;
16 import java.util.Iterator;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
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.stmt.SchemaNodeIdentifier.Descendant;
30 * A set of utility methods for interacting with {@link NormalizedNode} objects.
33 public final class NormalizedNodes {
34 private static final int STRINGTREE_INDENT = 4;
36 private NormalizedNodes() {
41 * Find duplicate NormalizedNode instances within a subtree. Duplicates are those, which compare
42 * as equal, but do not refer to the same object.
44 * @param node A normalized node subtree, may not be null
45 * @return A Map of NormalizedNode/DuplicateEntry relationships.
47 public static Map<NormalizedNode, DuplicateEntry> findDuplicates(final @NonNull NormalizedNode node) {
48 return Maps.filterValues(DuplicateFinder.findDuplicates(node), input -> !input.getDuplicates().isEmpty());
51 public static Optional<NormalizedNode> findNode(final YangInstanceIdentifier rootPath,
52 final NormalizedNode rootNode, final YangInstanceIdentifier childPath) {
53 final Optional<YangInstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
54 return relativePath.isPresent() ? findNode(rootNode, relativePath.get()) : Optional.empty();
57 public static Optional<NormalizedNode> findNode(final Optional<NormalizedNode> parent,
58 final Iterable<PathArgument> relativePath) {
59 final Iterator<PathArgument> pathIterator = requireNonNull(relativePath, "Relative path must not be null")
61 Optional<NormalizedNode> currentNode = requireNonNull(parent, "Parent must not be null");
62 while (currentNode.isPresent() && pathIterator.hasNext()) {
63 currentNode = getDirectChild(currentNode.get(), pathIterator.next());
68 public static Optional<NormalizedNode> findNode(final Optional<NormalizedNode> parent,
69 final PathArgument pathArg) {
70 return parent.flatMap(node -> getDirectChild(node, pathArg));
73 public static Optional<NormalizedNode> findNode(final Optional<NormalizedNode> parent,
74 final PathArgument... relativePath) {
75 return findNode(parent, Arrays.asList(relativePath));
78 public static Optional<NormalizedNode> findNode(final @Nullable NormalizedNode parent,
79 final PathArgument pathArg) {
80 return parent == null ? Optional.empty() : getDirectChild(parent, pathArg);
83 public static Optional<NormalizedNode> findNode(final NormalizedNode parent,
84 final Iterable<PathArgument> relativePath) {
85 return findNode(Optional.ofNullable(parent), relativePath);
88 public static Optional<NormalizedNode> findNode(final NormalizedNode parent, final Descendant path) {
89 return findNode(Optional.ofNullable(parent),
90 Lists.transform(path.getNodeIdentifiers(), NodeIdentifier::new));
93 public static Optional<NormalizedNode> findNode(final NormalizedNode parent,
94 final PathArgument... relativePath) {
95 return findNode(parent, Arrays.asList(relativePath));
98 public static Optional<NormalizedNode> findNode(final NormalizedNode tree,
99 final YangInstanceIdentifier path) {
100 return findNode(Optional.of(requireNonNull(tree, "Tree must not be null")),
101 requireNonNull(path, "Path must not be null").getPathArguments());
104 @SuppressWarnings({ "unchecked", "rawtypes" })
105 public static Optional<NormalizedNode> getDirectChild(final NormalizedNode node,
106 final PathArgument pathArg) {
107 if (node instanceof DataContainerNode) {
108 return (Optional) ((DataContainerNode) node).findChildByArg(pathArg);
109 } else if (node instanceof MapNode && pathArg instanceof NodeIdentifierWithPredicates) {
110 return (Optional) ((MapNode) node).findChildByArg((NodeIdentifierWithPredicates) pathArg);
111 } else if (node instanceof LeafSetNode && pathArg instanceof NodeWithValue) {
112 return (Optional) ((LeafSetNode<?>) node).findChildByArg((NodeWithValue<?>) pathArg);
114 // Anything else, including ValueNode
115 return Optional.empty();
119 * Convert a data subtree under a node into a human-readable string format.
121 * @param node Data subtree root
122 * @return String containing a human-readable form of the subtree.
124 public static String toStringTree(final NormalizedNode node) {
125 final StringBuilder sb = new StringBuilder();
126 toStringTree(sb, node, 0);
127 return sb.toString();
130 private static void toStringTree(final StringBuilder sb, final NormalizedNode node, final int offset) {
131 final String prefix = " ".repeat(offset);
132 appendPathArgument(sb.append(prefix), node.getIdentifier());
133 if (node instanceof NormalizedNodeContainer) {
135 for (NormalizedNode child : ((NormalizedNodeContainer<?>) node).body()) {
136 toStringTree(sb, child, offset + STRINGTREE_INDENT);
138 sb.append(prefix).append('}');
140 sb.append(' ').append(node.body());
145 private static void appendPathArgument(final StringBuilder sb, final PathArgument arg) {
146 if (arg instanceof NodeIdentifierWithPredicates) {
147 sb.append(arg.getNodeType().getLocalName()).append(((NodeIdentifierWithPredicates) arg).values());
148 } else if (arg instanceof AugmentationIdentifier) {
149 sb.append("augmentation");
151 sb.append(arg.getNodeType().getLocalName());