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.AugmentationNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
23 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
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 this.root = this.schemaContext = requireNonNull(schemaContext);
57 * Create a new writer backed by a {@link NormalizedNodeStreamWriter}.
59 * @param writer Back-end writer
60 * @param schemaContext Associated {@link EffectiveModelContext}
61 * @param path root path
63 public SchemaOrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer,
64 final EffectiveModelContext schemaContext, final SchemaPath path) {
66 this.schemaContext = requireNonNull(schemaContext);
68 final SchemaInferenceStack stack = SchemaInferenceStack.ofInstantiatedPath(schemaContext, path);
69 if (!stack.isEmpty()) {
70 final EffectiveStatement<?, ?> current = stack.currentStatement();
71 // FIXME: this should be one of NormalizedNodeContainer/NotificationDefinition/OperationDefinition
72 checkArgument(current instanceof SchemaNode, "Instantiating at %s is not supported", current);
73 root = (SchemaNode) current;
80 public SchemaOrderedNormalizedNodeWriter write(final NormalizedNode node) throws IOException {
81 if (schemaContext.equals(root)) {
82 currentSchemaNode = schemaContext.dataChildByName(node.getIdentifier().getNodeType());
84 currentSchemaNode = root;
86 return write(node, currentSchemaNode);
90 * Iterate over the provided collection and emit write
91 * events to the encapsulated {@link NormalizedNodeStreamWriter}.
94 * @return NormalizedNodeWriter this
95 * @throws IOException when thrown from the backing writer.
97 public SchemaOrderedNormalizedNodeWriter write(final Collection<DataContainerChild> nodes) throws IOException {
98 currentSchemaNode = root;
99 if (writeChildren(nodes, currentSchemaNode, false)) {
103 throw new IllegalStateException("It wasn't possible to serialize nodes " + nodes);
106 private SchemaOrderedNormalizedNodeWriter write(final NormalizedNode node, final SchemaNode dataSchemaNode)
109 //Set current schemaNode
110 try (SchemaNodeSetter sns = new SchemaNodeSetter(dataSchemaNode)) {
115 if (wasProcessedAsCompositeNode(node)) {
119 if (wasProcessAsSimpleNode(node)) {
124 throw new IllegalStateException("It wasn't possible to serialize node " + node);
127 private void write(final Collection<NormalizedNode> nodes, final SchemaNode dataSchemaNode) throws IOException {
128 for (final NormalizedNode node : nodes) {
129 write(node, dataSchemaNode);
134 protected boolean writeChildren(final Iterable<? extends NormalizedNode> children) throws IOException {
135 return writeChildren(children, currentSchemaNode, true);
138 private boolean writeChildren(final Iterable<? extends NormalizedNode> children, final SchemaNode parentSchemaNode,
139 final boolean endParent) throws IOException {
140 // Augmentations cannot be gotten with node.getChild so create our own structure with augmentations resolved
141 final Multimap<QName, NormalizedNode> qnameToNodes = ArrayListMultimap.create();
142 for (final NormalizedNode child : children) {
143 putChild(qnameToNodes, child);
146 if (parentSchemaNode instanceof DataNodeContainer) {
147 if (parentSchemaNode instanceof ListSchemaNode && qnameToNodes.containsKey(parentSchemaNode.getQName())) {
148 write(qnameToNodes.get(parentSchemaNode.getQName()), parentSchemaNode);
150 for (final DataSchemaNode schemaNode : ((DataNodeContainer) parentSchemaNode).getChildNodes()) {
151 write(qnameToNodes.get(schemaNode.getQName()), schemaNode);
154 } else if (parentSchemaNode instanceof ChoiceSchemaNode) {
155 for (final CaseSchemaNode ccNode : ((ChoiceSchemaNode) parentSchemaNode).getCases()) {
156 for (final DataSchemaNode dsn : ccNode.getChildNodes()) {
157 if (qnameToNodes.containsKey(dsn.getQName())) {
158 write(qnameToNodes.get(dsn.getQName()), dsn);
163 for (final NormalizedNode child : children) {
168 getWriter().endNode();
173 private SchemaOrderedNormalizedNodeWriter writeLeaf(final NormalizedNode node) throws IOException {
174 if (wasProcessAsSimpleNode(node)) {
178 throw new IllegalStateException("It wasn't possible to serialize node " + node);
181 private static void putChild(final Multimap<QName, NormalizedNode> qnameToNodes, final NormalizedNode child) {
182 if (child instanceof AugmentationNode) {
183 for (DataContainerChild grandChild : ((AugmentationNode) child).body()) {
184 putChild(qnameToNodes, grandChild);
187 qnameToNodes.put(child.getIdentifier().getNodeType(), child);
191 private final class SchemaNodeSetter implements AutoCloseable {
193 private final SchemaNode previousSchemaNode;
196 * Sets current schema node new value and store old value for later restore.
198 SchemaNodeSetter(final SchemaNode schemaNode) {
199 previousSchemaNode = SchemaOrderedNormalizedNodeWriter.this.currentSchemaNode;
200 SchemaOrderedNormalizedNodeWriter.this.currentSchemaNode = schemaNode;
204 * Restore previous schema node.
207 public void close() {
208 SchemaOrderedNormalizedNodeWriter.this.currentSchemaNode = previousSchemaNode;