2 * Copyright (c) 2014 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.api.schema.stream;
10 import static org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter.UNKNOWN_SIZE;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Iterables;
15 import java.io.Closeable;
16 import java.io.Flushable;
17 import java.io.IOException;
18 import java.util.Collection;
20 import javax.xml.stream.XMLStreamReader;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.YangModeledAnyXmlNode;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * This is an experimental iterator over a {@link NormalizedNode}. This is essentially
43 * the opposite of a {@link XMLStreamReader} -- unlike instantiating an iterator over
44 * the backing data, this encapsulates a {@link NormalizedNodeStreamWriter} and allows
45 * us to write multiple nodes.
48 public class NormalizedNodeWriter implements Closeable, Flushable {
49 private final NormalizedNodeStreamWriter writer;
51 protected NormalizedNodeWriter(final NormalizedNodeStreamWriter writer) {
52 this.writer = Preconditions.checkNotNull(writer);
55 protected final NormalizedNodeStreamWriter getWriter() {
60 * Create a new writer backed by a {@link NormalizedNodeStreamWriter}.
62 * @param writer Back-end writer
63 * @return A new instance.
65 public static NormalizedNodeWriter forStreamWriter(final NormalizedNodeStreamWriter writer) {
66 return forStreamWriter(writer, true);
70 * Create a new writer backed by a {@link NormalizedNodeStreamWriter}. Unlike the simple {@link #forStreamWriter(NormalizedNodeStreamWriter)}
71 * method, this allows the caller to switch off RFC6020 XML compliance, providing better
72 * throughput. The reason is that the XML mapping rules in RFC6020 require the encoding
73 * to emit leaf nodes which participate in a list's key first and in the order in which
74 * they are defined in the key. For JSON, this requirement is completely relaxed and leaves
75 * can be ordered in any way we see fit. The former requires a bit of work: first a lookup
76 * for each key and then for each emitted node we need to check whether it was already
79 * @param writer Back-end writer
80 * @param orderKeyLeaves whether the returned instance should be RFC6020 XML compliant.
81 * @return A new instance.
83 public static NormalizedNodeWriter forStreamWriter(final NormalizedNodeStreamWriter writer, final boolean orderKeyLeaves) {
85 return new OrderedNormalizedNodeWriter(writer);
87 return new NormalizedNodeWriter(writer);
92 * Iterate over the provided {@link NormalizedNode} and emit write
93 * events to the encapsulated {@link NormalizedNodeStreamWriter}.
96 * @return NormalizedNodeWriter this
97 * @throws IOException when thrown from the backing writer.
99 public NormalizedNodeWriter write(final NormalizedNode<?, ?> node) throws IOException {
100 if (wasProcessedAsCompositeNode(node)) {
104 if (wasProcessAsSimpleNode(node)) {
108 throw new IllegalStateException("It wasn't possible to serialize node " + node);
112 public void flush() throws IOException {
117 public void close() throws IOException {
123 * Emit a best guess of a hint for a particular set of children. It evaluates the
124 * iterable to see if the size can be easily gotten to. If it is, we hint at the
125 * real number of child nodes. Otherwise we emit UNKNOWN_SIZE.
127 * @param children Child nodes
128 * @return Best estimate of the collection size required to hold all the children.
130 protected static int childSizeHint(final Iterable<?> children) {
131 return (children instanceof Collection) ? ((Collection<?>) children).size() : UNKNOWN_SIZE;
134 protected boolean wasProcessAsSimpleNode(final NormalizedNode<?, ?> node) throws IOException {
135 if (node instanceof LeafSetEntryNode) {
136 final LeafSetEntryNode<?> nodeAsLeafList = (LeafSetEntryNode<?>)node;
137 final QName name = nodeAsLeafList.getIdentifier().getNodeType();
138 if (writer instanceof NormalizedNodeStreamAttributeWriter) {
139 ((NormalizedNodeStreamAttributeWriter) writer).leafSetEntryNode(name, nodeAsLeafList.getValue(),
140 nodeAsLeafList.getAttributes());
142 writer.leafSetEntryNode(name, nodeAsLeafList.getValue());
145 } else if (node instanceof LeafNode) {
146 final LeafNode<?> nodeAsLeaf = (LeafNode<?>)node;
147 if (writer instanceof NormalizedNodeStreamAttributeWriter) {
148 ((NormalizedNodeStreamAttributeWriter) writer).leafNode(nodeAsLeaf.getIdentifier(), nodeAsLeaf.getValue(), nodeAsLeaf.getAttributes());
150 writer.leafNode(nodeAsLeaf.getIdentifier(), nodeAsLeaf.getValue());
153 } else if (node instanceof AnyXmlNode) {
154 final AnyXmlNode anyXmlNode = (AnyXmlNode)node;
155 writer.anyxmlNode(anyXmlNode.getIdentifier(), anyXmlNode.getValue());
163 * Emit events for all children and then emit an endNode() event.
165 * @param children Child iterable
167 * @throws IOException when the writer reports it
169 protected boolean writeChildren(final Iterable<? extends NormalizedNode<?, ?>> children) throws IOException {
170 for (final NormalizedNode<?, ?> child : children) {
178 protected boolean writeMapEntryNode(final MapEntryNode node) throws IOException {
179 if (writer instanceof NormalizedNodeStreamAttributeWriter) {
180 ((NormalizedNodeStreamAttributeWriter) writer)
181 .startMapEntryNode(node.getIdentifier(), childSizeHint(node.getValue()), node.getAttributes());
183 writer.startMapEntryNode(node.getIdentifier(), childSizeHint(node.getValue()));
185 return writeChildren(node.getValue());
188 protected boolean wasProcessedAsCompositeNode(final NormalizedNode<?, ?> node) throws IOException {
189 if (node instanceof ContainerNode) {
190 final ContainerNode n = (ContainerNode) node;
191 if (writer instanceof NormalizedNodeStreamAttributeWriter) {
192 ((NormalizedNodeStreamAttributeWriter) writer).startContainerNode(n.getIdentifier(), childSizeHint(n.getValue()), n.getAttributes());
194 writer.startContainerNode(n.getIdentifier(), childSizeHint(n.getValue()));
196 return writeChildren(n.getValue());
198 if (node instanceof YangModeledAnyXmlNode) {
199 final YangModeledAnyXmlNode n = (YangModeledAnyXmlNode) node;
200 if (writer instanceof NormalizedNodeStreamAttributeWriter) {
201 ((NormalizedNodeStreamAttributeWriter) writer).startYangModeledAnyXmlNode(n.getIdentifier(), childSizeHint(n.getValue()), n.getAttributes());
203 writer.startYangModeledAnyXmlNode(n.getIdentifier(), childSizeHint(n.getValue()));
205 return writeChildren(n.getValue());
207 if (node instanceof MapEntryNode) {
208 return writeMapEntryNode((MapEntryNode) node);
210 if (node instanceof UnkeyedListEntryNode) {
211 final UnkeyedListEntryNode n = (UnkeyedListEntryNode) node;
212 writer.startUnkeyedListItem(n.getIdentifier(), childSizeHint(n.getValue()));
213 return writeChildren(n.getValue());
215 if (node instanceof ChoiceNode) {
216 final ChoiceNode n = (ChoiceNode) node;
217 writer.startChoiceNode(n.getIdentifier(), childSizeHint(n.getValue()));
218 return writeChildren(n.getValue());
220 if (node instanceof AugmentationNode) {
221 final AugmentationNode n = (AugmentationNode) node;
222 writer.startAugmentationNode(n.getIdentifier());
223 return writeChildren(n.getValue());
225 if (node instanceof UnkeyedListNode) {
226 final UnkeyedListNode n = (UnkeyedListNode) node;
227 writer.startUnkeyedList(n.getIdentifier(), childSizeHint(n.getValue()));
228 return writeChildren(n.getValue());
230 if (node instanceof OrderedMapNode) {
231 final OrderedMapNode n = (OrderedMapNode) node;
232 writer.startOrderedMapNode(n.getIdentifier(), childSizeHint(n.getValue()));
233 return writeChildren(n.getValue());
235 if (node instanceof MapNode) {
236 final MapNode n = (MapNode) node;
237 writer.startMapNode(n.getIdentifier(), childSizeHint(n.getValue()));
238 return writeChildren(n.getValue());
240 if (node instanceof OrderedLeafSetNode) {
241 final LeafSetNode<?> n = (LeafSetNode<?>) node;
242 writer.startOrderedLeafSet(n.getIdentifier(), childSizeHint(n.getValue()));
243 return writeChildren(n.getValue());
245 if (node instanceof LeafSetNode) {
246 final LeafSetNode<?> n = (LeafSetNode<?>) node;
247 writer.startLeafSet(n.getIdentifier(), childSizeHint(n.getValue()));
248 return writeChildren(n.getValue());
254 private static final class OrderedNormalizedNodeWriter extends NormalizedNodeWriter {
255 private static final Logger LOG = LoggerFactory.getLogger(OrderedNormalizedNodeWriter.class);
257 OrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer) {
262 protected boolean writeMapEntryNode(final MapEntryNode node) throws IOException {
263 final NormalizedNodeStreamWriter nnWriter = getWriter();
264 if (nnWriter instanceof NormalizedNodeStreamAttributeWriter) {
265 ((NormalizedNodeStreamAttributeWriter) nnWriter).startMapEntryNode(node.getIdentifier(), childSizeHint(node.getValue()), node.getAttributes());
267 nnWriter.startMapEntryNode(node.getIdentifier(), childSizeHint(node.getValue()));
270 final Set<QName> qnames = node.getIdentifier().getKeyValues().keySet();
271 // Write out all the key children
272 for (final QName qname : qnames) {
273 final Optional<? extends NormalizedNode<?, ?>> child = node.getChild(new NodeIdentifier(qname));
274 if (child.isPresent()) {
277 LOG.info("No child for key element {} found", qname);
281 // Write all the rest
282 return writeChildren(Iterables.filter(node.getValue(), input -> {
283 if (input instanceof AugmentationNode) {
286 if (!qnames.contains(input.getNodeType())) {
290 LOG.debug("Skipping key child {}", input);