Remove NormalizedNodes.findNode(NormalizedNode, SchemaPath)
[yangtools.git] / data / 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 java.util.Objects.requireNonNull;
11
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;
17 import java.util.Map;
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;
28
29 /**
30  * A set of utility methods for interacting with {@link NormalizedNode} objects.
31  */
32 @Beta
33 public final class NormalizedNodes {
34     private static final int STRINGTREE_INDENT = 4;
35
36     private NormalizedNodes() {
37         // Hidden on purpose
38     }
39
40     /**
41      * Find duplicate NormalizedNode instances within a subtree. Duplicates are those, which compare
42      * as equal, but do not refer to the same object.
43      *
44      * @param node A normalized node subtree, may not be null
45      * @return A Map of NormalizedNode/DuplicateEntry relationships.
46      */
47     public static Map<NormalizedNode, DuplicateEntry> findDuplicates(final @NonNull NormalizedNode node) {
48         return Maps.filterValues(DuplicateFinder.findDuplicates(node), input -> !input.getDuplicates().isEmpty());
49     }
50
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();
55     }
56
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")
60                 .iterator();
61         Optional<NormalizedNode> currentNode = requireNonNull(parent, "Parent must not be null");
62         while (currentNode.isPresent() && pathIterator.hasNext()) {
63             currentNode = getDirectChild(currentNode.get(), pathIterator.next());
64         }
65         return currentNode;
66     }
67
68     public static Optional<NormalizedNode> findNode(final Optional<NormalizedNode> parent,
69             final PathArgument pathArg) {
70         return parent.flatMap(node -> getDirectChild(node, pathArg));
71     }
72
73     public static Optional<NormalizedNode> findNode(final Optional<NormalizedNode> parent,
74             final PathArgument... relativePath) {
75         return findNode(parent, Arrays.asList(relativePath));
76     }
77
78     public static Optional<NormalizedNode> findNode(final @Nullable NormalizedNode parent,
79             final PathArgument pathArg) {
80         return parent == null ? Optional.empty() : getDirectChild(parent, pathArg);
81     }
82
83     public static Optional<NormalizedNode> findNode(final NormalizedNode parent,
84             final Iterable<PathArgument> relativePath) {
85         return findNode(Optional.ofNullable(parent), relativePath);
86     }
87
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));
91     }
92
93     public static Optional<NormalizedNode> findNode(final NormalizedNode parent,
94             final PathArgument... relativePath) {
95         return findNode(parent, Arrays.asList(relativePath));
96     }
97
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());
102     }
103
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);
113         }
114         // Anything else, including ValueNode
115         return Optional.empty();
116     }
117
118     /**
119      * Convert a data subtree under a node into a human-readable string format.
120      *
121      * @param node Data subtree root
122      * @return String containing a human-readable form of the subtree.
123      */
124     public static String toStringTree(final NormalizedNode node) {
125         final StringBuilder sb = new StringBuilder();
126         toStringTree(sb, node, 0);
127         return sb.toString();
128     }
129
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) {
134             sb.append(" {\n");
135             for (NormalizedNode child : ((NormalizedNodeContainer<?>) node).body()) {
136                 toStringTree(sb, child, offset + STRINGTREE_INDENT);
137             }
138             sb.append(prefix).append('}');
139         } else {
140             sb.append(' ').append(node.body());
141         }
142         sb.append('\n');
143     }
144
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");
150         } else {
151             sb.append(arg.getNodeType().getLocalName());
152         }
153     }
154 }