2 * Copyright (c) 2016 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.impl.schema;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.collect.ArrayListMultimap;
14 import com.google.common.collect.Multimap;
15 import java.io.IOException;
16 import java.util.Collection;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
22 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaTreeInference;
30 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
32 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
35 * This is an iterator over a {@link NormalizedNode}. Unlike {@link NormalizedNodeWriter}, this iterates over elements
36 * in the order as they are defined in YANG file.
38 public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
39 private final EffectiveModelContext schemaContext;
40 private final SchemaNode root;
42 private SchemaNode currentSchemaNode;
45 * Create a new writer backed by a {@link NormalizedNodeStreamWriter}.
47 * @param writer Back-end writer
48 * @param schemaContext Associated {@link EffectiveModelContext}
50 public SchemaOrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer,
51 final EffectiveModelContext schemaContext) {
53 root = this.schemaContext = requireNonNull(schemaContext);
56 private SchemaOrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer,
57 final SchemaInferenceStack stack) {
59 schemaContext = stack.getEffectiveModelContext();
61 if (!stack.isEmpty()) {
62 final EffectiveStatement<?, ?> current = stack.currentStatement();
63 // FIXME: this should be one of NormalizedNodeContainer/NotificationDefinition/OperationDefinition
64 checkArgument(current instanceof SchemaNode, "Instantiating at %s is not supported", current);
65 root = (SchemaNode) current;
72 * Create a new writer backed by a {@link NormalizedNodeStreamWriter}.
74 * @param writer Back-end writer
75 * @param schemaContext Associated {@link EffectiveModelContext}
76 * @param path root path
78 public SchemaOrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer,
79 final EffectiveModelContext schemaContext, final Absolute path) {
80 this(writer, SchemaInferenceStack.of(schemaContext, path));
84 * Create a new writer backed by a {@link NormalizedNodeStreamWriter}.
86 * @param writer Back-end writer
87 * @param rootInference A SchemaTreeInference pointing to the root element
89 public SchemaOrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer,
90 final SchemaTreeInference rootInference) {
91 this(writer, SchemaInferenceStack.ofInference(rootInference));
95 public SchemaOrderedNormalizedNodeWriter write(final NormalizedNode node) throws IOException {
96 if (schemaContext.equals(root)) {
97 currentSchemaNode = schemaContext.dataChildByName(node.getIdentifier().getNodeType());
99 currentSchemaNode = root;
101 return write(node, currentSchemaNode);
105 * Iterate over the provided collection and emit write
106 * events to the encapsulated {@link NormalizedNodeStreamWriter}.
109 * @return NormalizedNodeWriter this
110 * @throws IOException when thrown from the backing writer.
112 public SchemaOrderedNormalizedNodeWriter write(final Collection<DataContainerChild> nodes) throws IOException {
113 currentSchemaNode = root;
114 if (writeChildren(nodes, currentSchemaNode, false)) {
118 throw new IllegalStateException("It wasn't possible to serialize nodes " + nodes);
121 private SchemaOrderedNormalizedNodeWriter write(final NormalizedNode node, final SchemaNode dataSchemaNode)
124 //Set current schemaNode
125 try (SchemaNodeSetter sns = new SchemaNodeSetter(dataSchemaNode)) {
130 if (wasProcessedAsCompositeNode(node)) {
134 if (wasProcessAsSimpleNode(node)) {
139 throw new IllegalStateException("It wasn't possible to serialize node " + node);
142 private void write(final Collection<NormalizedNode> nodes, final SchemaNode dataSchemaNode) throws IOException {
143 for (final NormalizedNode node : nodes) {
144 write(node, dataSchemaNode);
149 protected boolean writeChildren(final Iterable<? extends NormalizedNode> children) throws IOException {
150 return writeChildren(children, currentSchemaNode, true);
153 private boolean writeChildren(final Iterable<? extends NormalizedNode> children, final SchemaNode parentSchemaNode,
154 final boolean endParent) throws IOException {
155 // Augmentations cannot be gotten with node.getChild so create our own structure with augmentations resolved
156 final var qnameToNodes = ArrayListMultimap.<QName, NormalizedNode>create();
157 for (final NormalizedNode child : children) {
158 putChild(qnameToNodes, child);
161 if (parentSchemaNode instanceof DataNodeContainer parentContainer) {
162 if (parentContainer instanceof ListSchemaNode && qnameToNodes.containsKey(parentSchemaNode.getQName())) {
163 write(qnameToNodes.get(parentSchemaNode.getQName()), parentSchemaNode);
165 for (final DataSchemaNode schemaNode : parentContainer.getChildNodes()) {
166 write(qnameToNodes.get(schemaNode.getQName()), schemaNode);
169 } else if (parentSchemaNode instanceof ChoiceSchemaNode parentChoice) {
170 for (final CaseSchemaNode ccNode : parentChoice.getCases()) {
171 for (final DataSchemaNode dsn : ccNode.getChildNodes()) {
172 if (qnameToNodes.containsKey(dsn.getQName())) {
173 write(qnameToNodes.get(dsn.getQName()), dsn);
178 for (final NormalizedNode child : children) {
183 getWriter().endNode();
188 private SchemaOrderedNormalizedNodeWriter writeLeaf(final NormalizedNode node) throws IOException {
189 if (wasProcessAsSimpleNode(node)) {
193 throw new IllegalStateException("It wasn't possible to serialize node " + node);
196 private static void putChild(final Multimap<QName, NormalizedNode> qnameToNodes, final NormalizedNode child) {
197 qnameToNodes.put(child.getIdentifier().getNodeType(), child);
200 private final class SchemaNodeSetter implements AutoCloseable {
201 private final SchemaNode previousSchemaNode;
204 * Sets current schema node new value and store old value for later restore.
206 SchemaNodeSetter(final SchemaNode schemaNode) {
207 previousSchemaNode = currentSchemaNode;
208 currentSchemaNode = schemaNode;
212 * Restore previous schema node.
215 public void close() {
216 currentSchemaNode = previousSchemaNode;