2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.stream;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.collect.Iterables;
17 import java.io.IOException;
18 import java.util.List;
19 import javax.xml.transform.dom.DOMSource;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.util.ImmutableOffsetMap;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedAnydata;
29 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
33 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
35 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
40 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
43 * Utility for emitting a {@link YangInstanceIdentifier} into a {@link NormalizedNodeStreamWriter} as a set of
44 * {@code startXXXNode} events. An example of usage would be something along the lines of:
48 * YangInstanceIdentifier id;
49 * var result = new NormalizedNodeResult();
50 * try (var writer = ImmutableNormalizedNodeStreamWriter.from(result)) {
51 * try (var iidWriter = YangInstanceIdentifierWriter.open(writer, ctx, id)) {
52 * // Here the state of 'writer' reflects the nodes in 'id'
54 * // Here the writer is back to its initial state
57 * // NormalizedNode result, including the structure created from YangInstanceIdentifier
58 * var node = result.getResult();
62 public final class YangInstanceIdentifierWriter implements AutoCloseable {
63 private NormalizedNodeStreamWriter writer;
64 private final int endNodeCount;
66 private YangInstanceIdentifierWriter(final NormalizedNodeStreamWriter writer, final int endNodeCount) {
67 this.writer = requireNonNull(writer);
68 this.endNodeCount = endNodeCount;
72 * Open a writer, emitting events in target {@link NormalizedNodeStreamWriter}.
74 * @param writer Writer to enter
75 * @param root Root container
76 * @param path Path to enter
77 * @return A writer instance
78 * @throws IOException if the path cannot be entered
80 public static @NonNull YangInstanceIdentifierWriter open(final NormalizedNodeStreamWriter writer,
81 final DataNodeContainer root, final YangInstanceIdentifier path) throws IOException {
82 final var it = path.getPathArguments().iterator();
84 return new YangInstanceIdentifierWriter(writer, 0);
90 boolean reuse = false;
91 boolean terminal = false;
95 throw new IOException(parent + " is a terminal node, cannot resolve " + ImmutableList.copyOf(it));
98 final var arg = it.next();
99 if (arg instanceof AugmentationIdentifier augId) {
100 if (!(parent instanceof AugmentationTarget target)) {
101 throw new IOException(parent + " does not support augmentations, cannot resolve " + arg);
104 throw new IOException(parent + " is expecting a nested item, cannot resolve " + arg);
107 if (target instanceof DataNodeContainer container) {
108 parent = new EffectiveAugmentationSchema(enterAugmentation(target, augId), container);
109 } else if (target instanceof ChoiceSchemaNode) {
110 throw new IOException(parent + " should not use addressing through " + arg);
112 throw new IOException("Unhandled parent " + target + " while resolving " + arg);
114 writer.startAugmentationNode(augId);
115 } else if (arg instanceof NodeWithValue<?> nodeId) {
116 if (!(parent instanceof LeafListSchemaNode)) {
117 throw new IOException(parent + " does not support leaf-list entry " + arg);
120 throw new IOException(parent + " is already at its entry, cannot enter " + arg);
125 writer.startLeafSetEntryNode(nodeId);
126 } else if (arg instanceof NodeIdentifierWithPredicates nodeId) {
127 if (!(parent instanceof ListSchemaNode list)) {
128 throw new IOException(parent + " does not support map entry " + arg);
131 throw new IOException(parent + " is already at its entry, cannot enter " + arg);
133 if (!list.getQName().equals(nodeId.getNodeType())) {
134 throw new IOException(parent + " expects a matching map entry, cannot enter " + arg);
137 final var key = list.getKeyDefinition();
139 throw new IOException(parent + " does not expect map entry " + arg);
141 if (key.size() != nodeId.size()) {
142 throw new IOException(parent + " expects " + key.size() + " predicates, cannot use " + arg);
146 writer.startMapEntryNode(normalizePredicates(nodeId, key), 1);
147 } else if (arg instanceof NodeIdentifier nodeId) {
149 if (!(parent instanceof ListSchemaNode list)) {
150 throw new IOException(parent + " expects an identifiable entry, cannot enter " + arg);
153 if (!list.getKeyDefinition().isEmpty()) {
154 throw new IOException(parent + " expects a map entry, cannot enter " + arg);
156 if (!list.getQName().equals(nodeId.getNodeType())) {
157 throw new IOException(parent + " expects a matching entry, cannot enter " + arg);
161 writer.startUnkeyedListItem(nodeId, 1);
166 final DataSchemaNode child;
167 if (parent instanceof DataNodeContainer container) {
168 child = container.dataChildByName(nodeId.getNodeType());
169 } else if (parent instanceof ChoiceSchemaNode choice) {
170 child = choice.findDataSchemaChild(nodeId.getNodeType()).orElse(null);
172 throw new IOException("Unhandled parent " + parent + " when looking up " + arg);
176 throw new IOException("Failed to find child " + arg + " in parent " + parent);
179 // FIXME: check & repair augmentations (brr!)
181 if (child instanceof ContainerLike) {
183 writer.startContainerNode(nodeId, 1);
184 } else if (child instanceof ListSchemaNode list) {
187 if (list.getKeyDefinition().isEmpty()) {
188 writer.startUnkeyedList(nodeId, 1);
189 } else if (list.isUserOrdered()) {
190 writer.startOrderedMapNode(nodeId, 1);
192 writer.startMapNode(nodeId, 1);
194 } else if (child instanceof LeafSchemaNode) {
197 writer.startLeafNode(nodeId);
198 } else if (child instanceof ChoiceSchemaNode) {
200 writer.startChoiceNode(nodeId, 1);
201 } else if (child instanceof LeafListSchemaNode leafList) {
204 if (leafList.isUserOrdered()) {
205 writer.startOrderedLeafSet(nodeId, 1);
207 writer.startLeafSet(nodeId, 1);
209 } else if (child instanceof AnydataSchemaNode) {
212 writer.startAnydataNode(nodeId, NormalizedAnydata.class);
213 } else if (child instanceof AnyxmlSchemaNode) {
216 writer.startAnyxmlNode(nodeId, DOMSource.class);
218 throw new IOException("Unhandled child " + child);
221 throw new IOException("Unhandled argument " + arg);
225 } while (it.hasNext());
227 return new YangInstanceIdentifierWriter(writer, endNodes);
231 public void close() throws IOException {
232 if (writer != null) {
233 for (int i = 0; i < endNodeCount; ++i) {
240 private static NodeIdentifierWithPredicates normalizePredicates(final NodeIdentifierWithPredicates input,
241 final List<QName> key) throws IOException {
242 if (Iterables.elementsEqual(input.keySet(), key)) {
246 final var builder = ImmutableMap.<QName, Object>builderWithExpectedSize(key.size());
247 for (var qname : key) {
248 final var value = input.getValue(qname);
250 throw new IOException("Cannot normalize " + input + " to " + key + ", missing value for " + qname);
252 builder.put(qname, value);
255 return NodeIdentifierWithPredicates.of(input.getNodeType(), ImmutableOffsetMap.orderedCopyOf(builder.build()));
258 private static AugmentationSchemaNode enterAugmentation(final AugmentationTarget target,
259 final AugmentationIdentifier id) throws IOException {
260 final var augs = target.getAvailableAugmentations();
261 for (var augment : augs) {
262 if (id.equals(augmentationIdentifierFrom(augment))) {
266 throw new IOException("Cannot find augmentation " + id + " in " + target + ", available: "
267 + Collections2.transform(augs, YangInstanceIdentifierWriter::augmentationIdentifierFrom));
270 // FIXME: duplicate of data.util.DataSchemaContextNode.augmentationIdentifierFrom()
271 static @NonNull AugmentationIdentifier augmentationIdentifierFrom(final AugmentationSchemaNode schema) {
272 return new AugmentationIdentifier(
273 schema.getChildNodes().stream().map(DataSchemaNode::getQName).collect(ImmutableSet.toImmutableSet()));