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;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Strings;
14 import java.util.Iterator;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 * A set of utility methods for interacting with {@link NormalizedNode} objects.
25 public final class NormalizedNodes {
26 private static final int STRINGTREE_INDENT = 4;
28 private NormalizedNodes() {
29 throw new UnsupportedOperationException("Utility class should not be instantiated");
32 public static Optional<NormalizedNode<?, ?>> findNode(final YangInstanceIdentifier rootPath, final NormalizedNode<?, ?> rootNode, final YangInstanceIdentifier childPath) {
33 final Optional<YangInstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
34 if (relativePath.isPresent()) {
35 return findNode(rootNode, relativePath.get());
37 return Optional.absent();
41 public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> tree, final YangInstanceIdentifier path) {
42 checkNotNull(tree, "Tree must not be null");
43 checkNotNull(path, "Path must not be null");
45 Optional<NormalizedNode<?, ?>> currentNode = Optional.<NormalizedNode<?, ?>> of(tree);
46 final Iterator<PathArgument> pathIterator = path.getPathArguments().iterator();
47 while (currentNode.isPresent() && pathIterator.hasNext()) {
48 currentNode = getDirectChild(currentNode.get(), pathIterator.next());
53 @SuppressWarnings({ "unchecked", "rawtypes" })
54 public static Optional<NormalizedNode<?, ?>> getDirectChild(final NormalizedNode<?, ?> node, final PathArgument pathArg) {
55 if (node instanceof LeafNode<?> || node instanceof LeafSetEntryNode<?>) {
56 return Optional.absent();
57 } else if (node instanceof DataContainerNode<?>) {
58 return (Optional) ((DataContainerNode<?>) node).getChild(pathArg);
59 } else if (node instanceof MapNode && pathArg instanceof NodeIdentifierWithPredicates) {
60 return (Optional) ((MapNode) node).getChild((NodeIdentifierWithPredicates) pathArg);
61 } else if (node instanceof LeafSetNode<?>) {
62 return (Optional) ((LeafSetNode<?>) node).getChild((NodeWithValue) pathArg);
64 return Optional.absent();
68 * Convert a data subtree under a node into a human-readable string format.
70 * @param node Data subtree root
71 * @return String containing a human-readable form of the subtree.
73 public static String toStringTree(final NormalizedNode<?, ?> node) {
74 final StringBuilder builder = new StringBuilder();
75 toStringTree(builder, node, 0);
76 return builder.toString();
79 private static void toStringTree(final StringBuilder builder, final NormalizedNode<?, ?> node, final int offset) {
80 final String prefix = Strings.repeat(" ", offset);
82 builder.append(prefix).append(toStringTree(node.getIdentifier()));
83 if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
84 final NormalizedNodeContainer<?, ?, ?> container = (NormalizedNodeContainer<?, ?, ?>) node;
86 builder.append(" {\n");
87 for (NormalizedNode<?, ?> child : container.getValue()) {
88 toStringTree(builder, child, offset + STRINGTREE_INDENT);
91 builder.append(prefix).append('}');
93 builder.append(' ').append(node.getValue());
98 private static String toStringTree(final PathArgument identifier) {
99 if (identifier instanceof NodeIdentifierWithPredicates) {
100 StringBuilder builder = new StringBuilder();
101 builder.append(identifier.getNodeType().getLocalName());
102 builder.append(((NodeIdentifierWithPredicates) identifier).getKeyValues().values());
103 return builder.toString();
104 } else if (identifier instanceof AugmentationIdentifier) {
105 return "augmentation";
107 return identifier.getNodeType().getLocalName();