Add SchemaInferenceStack.ofSchemaPath()
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / SchemaOrderedNormalizedNodeWriter.java
1 /*
2  * Copyright (c) 2016 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.impl.schema;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
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;
33
34 /**
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.
37  */
38 public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
39     private final EffectiveModelContext schemaContext;
40     private final SchemaNode root;
41
42     private SchemaNode currentSchemaNode;
43
44     /**
45      * Create a new writer backed by a {@link NormalizedNodeStreamWriter}.
46      *
47      * @param writer Back-end writer
48      * @param schemaContext Associated {@link EffectiveModelContext}
49      */
50     public SchemaOrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer,
51             final EffectiveModelContext schemaContext) {
52         super(writer);
53         this.root = this.schemaContext = requireNonNull(schemaContext);
54     }
55
56     /**
57      * Create a new writer backed by a {@link NormalizedNodeStreamWriter}.
58      *
59      * @param writer Back-end writer
60      * @param schemaContext Associated {@link EffectiveModelContext}
61      * @param path root path
62      */
63     public SchemaOrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer,
64             final EffectiveModelContext schemaContext, final SchemaPath path) {
65         super(writer);
66         this.schemaContext = requireNonNull(schemaContext);
67
68         final SchemaInferenceStack stack = SchemaInferenceStack.ofSchemaPath(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;
74         } else {
75             root = schemaContext;
76         }
77     }
78
79     @Override
80     public SchemaOrderedNormalizedNodeWriter write(final NormalizedNode node) throws IOException {
81         if (schemaContext.equals(root)) {
82             currentSchemaNode = schemaContext.dataChildByName(node.getIdentifier().getNodeType());
83         } else {
84             currentSchemaNode = root;
85         }
86         return write(node, currentSchemaNode);
87     }
88
89     /**
90      * Iterate over the provided collection and emit write
91      * events to the encapsulated {@link NormalizedNodeStreamWriter}.
92      *
93      * @param nodes nodes
94      * @return NormalizedNodeWriter this
95      * @throws IOException when thrown from the backing writer.
96      */
97     public SchemaOrderedNormalizedNodeWriter write(final Collection<DataContainerChild> nodes) throws IOException {
98         currentSchemaNode = root;
99         if (writeChildren(nodes, currentSchemaNode, false)) {
100             return this;
101         }
102
103         throw new IllegalStateException("It wasn't possible to serialize nodes " + nodes);
104     }
105
106     private SchemaOrderedNormalizedNodeWriter write(final NormalizedNode node, final SchemaNode dataSchemaNode)
107             throws IOException {
108
109         //Set current schemaNode
110         try (SchemaNodeSetter sns = new SchemaNodeSetter(dataSchemaNode)) {
111             if (node == null) {
112                 return this;
113             }
114
115             if (wasProcessedAsCompositeNode(node)) {
116                 return this;
117             }
118
119             if (wasProcessAsSimpleNode(node)) {
120                 return this;
121             }
122         }
123
124         throw new IllegalStateException("It wasn't possible to serialize node " + node);
125     }
126
127     private void write(final Collection<NormalizedNode> nodes, final SchemaNode dataSchemaNode) throws IOException {
128         for (final NormalizedNode node : nodes) {
129             write(node, dataSchemaNode);
130         }
131     }
132
133     @Override
134     protected boolean writeChildren(final Iterable<? extends NormalizedNode> children) throws IOException {
135         return writeChildren(children, currentSchemaNode, true);
136     }
137
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);
144         }
145
146         if (parentSchemaNode instanceof DataNodeContainer) {
147             if (parentSchemaNode instanceof ListSchemaNode && qnameToNodes.containsKey(parentSchemaNode.getQName())) {
148                 write(qnameToNodes.get(parentSchemaNode.getQName()), parentSchemaNode);
149             } else {
150                 for (final DataSchemaNode schemaNode : ((DataNodeContainer) parentSchemaNode).getChildNodes()) {
151                     write(qnameToNodes.get(schemaNode.getQName()), schemaNode);
152                 }
153             }
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);
159                     }
160                 }
161             }
162         } else {
163             for (final NormalizedNode child : children) {
164                 writeLeaf(child);
165             }
166         }
167         if (endParent) {
168             getWriter().endNode();
169         }
170         return true;
171     }
172
173     private SchemaOrderedNormalizedNodeWriter writeLeaf(final NormalizedNode node) throws IOException {
174         if (wasProcessAsSimpleNode(node)) {
175             return this;
176         }
177
178         throw new IllegalStateException("It wasn't possible to serialize node " + node);
179     }
180
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);
185             }
186         } else {
187             qnameToNodes.put(child.getIdentifier().getNodeType(), child);
188         }
189     }
190
191     private final class SchemaNodeSetter implements AutoCloseable {
192
193         private final SchemaNode previousSchemaNode;
194
195         /**
196          * Sets current schema node new value and store old value for later restore.
197          */
198         SchemaNodeSetter(final SchemaNode schemaNode) {
199             previousSchemaNode = SchemaOrderedNormalizedNodeWriter.this.currentSchemaNode;
200             SchemaOrderedNormalizedNodeWriter.this.currentSchemaNode = schemaNode;
201         }
202
203         /**
204          * Restore previous schema node.
205          */
206         @Override
207         public void close() {
208             SchemaOrderedNormalizedNodeWriter.this.currentSchemaNode = previousSchemaNode;
209         }
210     }
211 }