Publish StoreUtils as NormalizedNodes
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / NormalizedNodes.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.api.schema;
9
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;
20
21 /**
22  * A set of utility methods for interacting with {@link NormalizedNode} objects.
23  */
24 @Beta
25 public final class NormalizedNodes {
26     private static final int STRINGTREE_INDENT = 4;
27
28     private NormalizedNodes() {
29         throw new UnsupportedOperationException("Utility class should not be instantiated");
30     }
31
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());
36         } else {
37             return Optional.absent();
38         }
39     }
40
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");
44
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());
49         }
50         return currentNode;
51     }
52
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);
63         }
64         return Optional.absent();
65     }
66
67     /**
68      * Convert a data subtree under a node into a human-readable string format.
69      *
70      * @param node Data subtree root
71      * @return String containing a human-readable form of the subtree.
72      */
73     public static String toStringTree(final NormalizedNode<?, ?> node) {
74         final StringBuilder builder = new StringBuilder();
75         toStringTree(builder, node, 0);
76         return builder.toString();
77     }
78
79     private static void toStringTree(final StringBuilder builder, final NormalizedNode<?, ?> node, final int offset) {
80         final String prefix = Strings.repeat(" ", offset);
81
82         builder.append(prefix).append(toStringTree(node.getIdentifier()));
83         if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
84             final NormalizedNodeContainer<?, ?, ?> container = (NormalizedNodeContainer<?, ?, ?>) node;
85
86             builder.append(" {\n");
87             for (NormalizedNode<?, ?> child : container.getValue()) {
88                 toStringTree(builder, child, offset + STRINGTREE_INDENT);
89             }
90
91             builder.append(prefix).append('}');
92         } else {
93             builder.append(' ').append(node.getValue());
94         }
95         builder.append('\n');
96     }
97
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";
106         } else {
107             return identifier.getNodeType().getLocalName();
108         }
109     }
110 }