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;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.Iterables;
16 import java.io.Closeable;
17 import java.io.Flushable;
18 import java.io.IOException;
19 import java.util.Collection;
21 import javax.xml.stream.XMLStreamReader;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.YangModeledAnyXmlNode;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * This is an experimental iterator over a {@link NormalizedNode}. This is essentially
44 * the opposite of a {@link XMLStreamReader} -- unlike instantiating an iterator over
45 * the backing data, this encapsulates a {@link NormalizedNodeStreamWriter} and allows
46 * us to write multiple nodes.
49 public class NormalizedNodeWriter implements Closeable, Flushable {
50 private final NormalizedNodeStreamWriter writer;
52 protected NormalizedNodeWriter(final NormalizedNodeStreamWriter writer) {
53 this.writer = Preconditions.checkNotNull(writer);
56 protected final NormalizedNodeStreamWriter getWriter() {
61 * Create a new writer backed by a {@link NormalizedNodeStreamWriter}.
63 * @param writer Back-end writer
64 * @return A new instance.
66 public static NormalizedNodeWriter forStreamWriter(final NormalizedNodeStreamWriter writer) {
67 return forStreamWriter(writer, true);
71 * Create a new writer backed by a {@link NormalizedNodeStreamWriter}. Unlike the simple {@link #forStreamWriter(NormalizedNodeStreamWriter)}
72 * method, this allows the caller to switch off RFC6020 XML compliance, providing better
73 * throughput. The reason is that the XML mapping rules in RFC6020 require the encoding
74 * to emit leaf nodes which participate in a list's key first and in the order in which
75 * they are defined in the key. For JSON, this requirement is completely relaxed and leaves
76 * can be ordered in any way we see fit. The former requires a bit of work: first a lookup
77 * for each key and then for each emitted node we need to check whether it was already
80 * @param writer Back-end writer
81 * @param orderKeyLeaves whether the returned instance should be RFC6020 XML compliant.
82 * @return A new instance.
84 public static NormalizedNodeWriter forStreamWriter(final NormalizedNodeStreamWriter writer, final boolean orderKeyLeaves) {
85 return orderKeyLeaves ? new OrderedNormalizedNodeWriter(writer) : new NormalizedNodeWriter(writer);
89 * Iterate over the provided {@link NormalizedNode} and emit write
90 * events to the encapsulated {@link NormalizedNodeStreamWriter}.
93 * @return NormalizedNodeWriter this
94 * @throws IOException when thrown from the backing writer.
96 public NormalizedNodeWriter write(final NormalizedNode<?, ?> node) throws IOException {
97 if (wasProcessedAsCompositeNode(node)) {
101 if (wasProcessAsSimpleNode(node)) {
105 throw new IllegalStateException("It wasn't possible to serialize node " + node);
109 public void flush() throws IOException {
114 public void close() throws IOException {
120 * Emit a best guess of a hint for a particular set of children. It evaluates the
121 * iterable to see if the size can be easily gotten to. If it is, we hint at the
122 * real number of child nodes. Otherwise we emit UNKNOWN_SIZE.
124 * @param children Child nodes
125 * @return Best estimate of the collection size required to hold all the children.
127 protected static int childSizeHint(final Iterable<?> children) {
128 return (children instanceof Collection) ? ((Collection<?>) children).size() : UNKNOWN_SIZE;
131 protected boolean wasProcessAsSimpleNode(final NormalizedNode<?, ?> node) throws IOException {
132 if (node instanceof LeafSetEntryNode) {
133 final LeafSetEntryNode<?> nodeAsLeafList = (LeafSetEntryNode<?>)node;
134 final QName name = nodeAsLeafList.getIdentifier().getNodeType();
135 if (writer instanceof NormalizedNodeStreamAttributeWriter) {
136 ((NormalizedNodeStreamAttributeWriter) writer).leafSetEntryNode(name, nodeAsLeafList.getValue(),
137 nodeAsLeafList.getAttributes());
139 writer.leafSetEntryNode(name, nodeAsLeafList.getValue());
142 } else if (node instanceof LeafNode) {
143 final LeafNode<?> nodeAsLeaf = (LeafNode<?>)node;
144 if (writer instanceof NormalizedNodeStreamAttributeWriter) {
145 ((NormalizedNodeStreamAttributeWriter) writer).leafNode(nodeAsLeaf.getIdentifier(), nodeAsLeaf.getValue(), nodeAsLeaf.getAttributes());
147 writer.leafNode(nodeAsLeaf.getIdentifier(), nodeAsLeaf.getValue());
150 } else if (node instanceof AnyXmlNode) {
151 final AnyXmlNode anyXmlNode = (AnyXmlNode)node;
152 writer.anyxmlNode(anyXmlNode.getIdentifier(), anyXmlNode.getValue());
160 * Emit events for all children and then emit an endNode() event.
162 * @param children Child iterable
164 * @throws IOException when the writer reports it
166 protected boolean writeChildren(final Iterable<? extends NormalizedNode<?, ?>> children) throws IOException {
167 for (final NormalizedNode<?, ?> child : children) {
175 protected boolean writeMapEntryNode(final MapEntryNode node) throws IOException {
176 if (writer instanceof NormalizedNodeStreamAttributeWriter) {
177 ((NormalizedNodeStreamAttributeWriter) writer)
178 .startMapEntryNode(node.getIdentifier(), childSizeHint(node.getValue()), node.getAttributes());
180 writer.startMapEntryNode(node.getIdentifier(), childSizeHint(node.getValue()));
182 return writeChildren(node.getValue());
185 protected boolean wasProcessedAsCompositeNode(final NormalizedNode<?, ?> node) throws IOException {
186 if (node instanceof ContainerNode) {
187 final ContainerNode n = (ContainerNode) node;
188 if (writer instanceof NormalizedNodeStreamAttributeWriter) {
189 ((NormalizedNodeStreamAttributeWriter) writer).startContainerNode(n.getIdentifier(), childSizeHint(n.getValue()), n.getAttributes());
191 writer.startContainerNode(n.getIdentifier(), childSizeHint(n.getValue()));
193 return writeChildren(n.getValue());
195 if (node instanceof YangModeledAnyXmlNode) {
196 final YangModeledAnyXmlNode n = (YangModeledAnyXmlNode) node;
197 if (writer instanceof NormalizedNodeStreamAttributeWriter) {
198 ((NormalizedNodeStreamAttributeWriter) writer).startYangModeledAnyXmlNode(n.getIdentifier(), childSizeHint(n.getValue()), n.getAttributes());
200 writer.startYangModeledAnyXmlNode(n.getIdentifier(), childSizeHint(n.getValue()));
202 return writeChildren(n.getValue());
204 if (node instanceof MapEntryNode) {
205 return writeMapEntryNode((MapEntryNode) node);
207 if (node instanceof UnkeyedListEntryNode) {
208 final UnkeyedListEntryNode n = (UnkeyedListEntryNode) node;
209 writer.startUnkeyedListItem(n.getIdentifier(), childSizeHint(n.getValue()));
210 return writeChildren(n.getValue());
212 if (node instanceof ChoiceNode) {
213 final ChoiceNode n = (ChoiceNode) node;
214 writer.startChoiceNode(n.getIdentifier(), childSizeHint(n.getValue()));
215 return writeChildren(n.getValue());
217 if (node instanceof AugmentationNode) {
218 final AugmentationNode n = (AugmentationNode) node;
219 writer.startAugmentationNode(n.getIdentifier());
220 return writeChildren(n.getValue());
222 if (node instanceof UnkeyedListNode) {
223 final UnkeyedListNode n = (UnkeyedListNode) node;
224 writer.startUnkeyedList(n.getIdentifier(), childSizeHint(n.getValue()));
225 return writeChildren(n.getValue());
227 if (node instanceof OrderedMapNode) {
228 final OrderedMapNode n = (OrderedMapNode) node;
229 writer.startOrderedMapNode(n.getIdentifier(), childSizeHint(n.getValue()));
230 return writeChildren(n.getValue());
232 if (node instanceof MapNode) {
233 final MapNode n = (MapNode) node;
234 writer.startMapNode(n.getIdentifier(), childSizeHint(n.getValue()));
235 return writeChildren(n.getValue());
237 if (node instanceof OrderedLeafSetNode) {
238 final LeafSetNode<?> n = (LeafSetNode<?>) node;
239 writer.startOrderedLeafSet(n.getIdentifier(), childSizeHint(n.getValue()));
240 return writeChildren(n.getValue());
242 if (node instanceof LeafSetNode) {
243 final LeafSetNode<?> n = (LeafSetNode<?>) node;
244 writer.startLeafSet(n.getIdentifier(), childSizeHint(n.getValue()));
245 return writeChildren(n.getValue());
251 private static final class OrderedNormalizedNodeWriter extends NormalizedNodeWriter {
252 private static final Logger LOG = LoggerFactory.getLogger(OrderedNormalizedNodeWriter.class);
254 OrderedNormalizedNodeWriter(final NormalizedNodeStreamWriter writer) {
259 protected boolean writeMapEntryNode(final MapEntryNode node) throws IOException {
260 final NormalizedNodeStreamWriter nnWriter = getWriter();
261 if (nnWriter instanceof NormalizedNodeStreamAttributeWriter) {
262 ((NormalizedNodeStreamAttributeWriter) nnWriter).startMapEntryNode(node.getIdentifier(), childSizeHint(node.getValue()), node.getAttributes());
264 nnWriter.startMapEntryNode(node.getIdentifier(), childSizeHint(node.getValue()));
267 final Set<QName> qnames = node.getIdentifier().getKeyValues().keySet();
268 // Write out all the key children
269 for (final QName qname : qnames) {
270 final Optional<? extends NormalizedNode<?, ?>> child = node.getChild(new NodeIdentifier(qname));
271 if (child.isPresent()) {
274 LOG.info("No child for key element {} found", qname);
278 // Write all the rest
279 return writeChildren(Iterables.filter(node.getValue(), input -> {
280 if (input instanceof AugmentationNode) {
283 if (!qnames.contains(input.getNodeType())) {
287 LOG.debug("Skipping key child {}", input);