2 * Copyright (c) 2015 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.controller.sal.rest.impl;
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.base.Predicate;
15 import com.google.common.collect.Iterables;
16 import java.io.IOException;
17 import java.util.Collection;
20 import org.opendaylight.controller.sal.rest.api.RestconfNormalizedNodeWriter;
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.OrderedMapNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamAttributeWriter;
37 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
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 DepthAwareNormalizedNodeWriter implements RestconfNormalizedNodeWriter {
49 private final NormalizedNodeStreamWriter writer;
50 protected int currentDepth = 0;
51 protected final int maxDepth;
53 private DepthAwareNormalizedNodeWriter(final NormalizedNodeStreamWriter writer, final int maxDepth) {
54 this.writer = Preconditions.checkNotNull(writer);
55 this.maxDepth = maxDepth;
58 protected final NormalizedNodeStreamWriter getWriter() {
63 * Create a new writer backed by a {@link NormalizedNodeStreamWriter}.
65 * @param writer Back-end writer
66 * @return A new instance.
68 public static DepthAwareNormalizedNodeWriter forStreamWriter(final NormalizedNodeStreamWriter writer, final int maxDepth) {
69 return forStreamWriter(writer, true, maxDepth);
73 * Create a new writer backed by a {@link NormalizedNodeStreamWriter}. Unlike the simple {@link #forStreamWriter(NormalizedNodeStreamWriter)}
74 * method, this allows the caller to switch off RFC6020 XML compliance, providing better
75 * throughput. The reason is that the XML mapping rules in RFC6020 require the encoding
76 * to emit leaf nodes which participate in a list's key first and in the order in which
77 * they are defined in the key. For JSON, this requirement is completely relaxed and leaves
78 * can be ordered in any way we see fit. The former requires a bit of work: first a lookup
79 * for each key and then for each emitted node we need to check whether it was already
82 * @param writer Back-end writer
83 * @param orderKeyLeaves whether the returned instance should be RFC6020 XML compliant.
84 * @return A new instance.
86 public static DepthAwareNormalizedNodeWriter forStreamWriter(final NormalizedNodeStreamWriter writer, final boolean
87 orderKeyLeaves, final int maxDepth) {
89 return new OrderedDepthAwareNormalizedNodeWriter(writer, maxDepth);
91 return new DepthAwareNormalizedNodeWriter(writer, maxDepth);
96 * Iterate over the provided {@link NormalizedNode} and emit write
97 * events to the encapsulated {@link NormalizedNodeStreamWriter}.
101 * @throws IOException when thrown from the backing writer.
103 public final DepthAwareNormalizedNodeWriter write(final NormalizedNode<?, ?> node) throws IOException {
104 if (wasProcessedAsCompositeNode(node)) {
108 if (wasProcessAsSimpleNode(node)) {
112 throw new IllegalStateException("It wasn't possible to serialize node " + node);
116 public void flush() throws IOException {
121 public void close() throws IOException {
127 * Emit a best guess of a hint for a particular set of children. It evaluates the
128 * iterable to see if the size can be easily gotten to. If it is, we hint at the
129 * real number of child nodes. Otherwise we emit UNKNOWN_SIZE.
131 * @param children Child nodes
132 * @return Best estimate of the collection size required to hold all the children.
134 static final int childSizeHint(final Iterable<?> children) {
135 return (children instanceof Collection) ? ((Collection<?>) children).size() : UNKNOWN_SIZE;
138 private boolean wasProcessAsSimpleNode(final NormalizedNode<?, ?> node) throws IOException {
139 if (node instanceof LeafSetEntryNode) {
140 if (currentDepth < maxDepth) {
141 final LeafSetEntryNode<?> nodeAsLeafList = (LeafSetEntryNode<?>) node;
142 if (writer instanceof NormalizedNodeStreamAttributeWriter) {
143 ((NormalizedNodeStreamAttributeWriter) writer).leafSetEntryNode(nodeAsLeafList.getValue(), nodeAsLeafList.getAttributes());
145 writer.leafSetEntryNode(nodeAsLeafList.getValue());
149 } else if (node instanceof LeafNode) {
150 final LeafNode<?> nodeAsLeaf = (LeafNode<?>)node;
151 if(writer instanceof NormalizedNodeStreamAttributeWriter) {
152 ((NormalizedNodeStreamAttributeWriter) writer).leafNode(nodeAsLeaf.getIdentifier(), nodeAsLeaf.getValue(), nodeAsLeaf.getAttributes());
154 writer.leafNode(nodeAsLeaf.getIdentifier(), nodeAsLeaf.getValue());
157 } else if (node instanceof AnyXmlNode) {
158 final AnyXmlNode anyXmlNode = (AnyXmlNode)node;
159 writer.anyxmlNode(anyXmlNode.getIdentifier(), anyXmlNode.getValue());
167 * Emit events for all children and then emit an endNode() event.
169 * @param children Child iterable
171 * @throws IOException when the writer reports it
173 protected final boolean writeChildren(final Iterable<? extends NormalizedNode<?, ?>> children) throws IOException {
174 if (currentDepth < maxDepth) {
175 for (NormalizedNode<?, ?> child : children) {
183 protected boolean writeMapEntryChildren(final MapEntryNode mapEntryNode) throws IOException {
184 if (currentDepth < maxDepth) {
185 writeChildren(mapEntryNode.getValue());
186 } else if (currentDepth == maxDepth) {
187 writeOnlyKeys(mapEntryNode.getIdentifier().getKeyValues());
192 private void writeOnlyKeys(Map<QName, Object> keyValues) throws IllegalArgumentException, IOException {
193 for (Map.Entry<QName, Object> entry : keyValues.entrySet()) {
194 writer.leafNode(new NodeIdentifier(entry.getKey()), entry.getValue());
200 protected boolean writeMapEntryNode(final MapEntryNode node) throws IOException {
201 if(writer instanceof NormalizedNodeStreamAttributeWriter) {
202 ((NormalizedNodeStreamAttributeWriter) writer)
203 .startMapEntryNode(node.getIdentifier(), childSizeHint(node.getValue()), node.getAttributes());
205 writer.startMapEntryNode(node.getIdentifier(), childSizeHint(node.getValue()));
208 writeMapEntryChildren(node);
213 private boolean wasProcessedAsCompositeNode(final NormalizedNode<?, ?> node) throws IOException {
214 boolean processedAsCompositeNode = false;
215 if (node instanceof ContainerNode) {
216 final ContainerNode n = (ContainerNode) node;
217 if(writer instanceof NormalizedNodeStreamAttributeWriter) {
218 ((NormalizedNodeStreamAttributeWriter) writer).startContainerNode(n.getIdentifier(), childSizeHint(n.getValue()), n.getAttributes());
220 writer.startContainerNode(n.getIdentifier(), childSizeHint(n.getValue()));
223 processedAsCompositeNode = writeChildren(n.getValue());
226 else if (node instanceof MapEntryNode) {
227 processedAsCompositeNode = writeMapEntryNode((MapEntryNode) node);
229 else if (node instanceof UnkeyedListEntryNode) {
230 final UnkeyedListEntryNode n = (UnkeyedListEntryNode) node;
231 writer.startUnkeyedListItem(n.getIdentifier(), childSizeHint(n.getValue()));
233 processedAsCompositeNode = writeChildren(n.getValue());
236 else if (node instanceof ChoiceNode) {
237 final ChoiceNode n = (ChoiceNode) node;
238 writer.startChoiceNode(n.getIdentifier(), childSizeHint(n.getValue()));
239 processedAsCompositeNode = writeChildren(n.getValue());
241 else if (node instanceof AugmentationNode) {
242 final AugmentationNode n = (AugmentationNode) node;
243 writer.startAugmentationNode(n.getIdentifier());
244 processedAsCompositeNode = writeChildren(n.getValue());
246 else if (node instanceof UnkeyedListNode) {
247 final UnkeyedListNode n = (UnkeyedListNode) node;
248 writer.startUnkeyedList(n.getIdentifier(), childSizeHint(n.getValue()));
249 processedAsCompositeNode = writeChildren(n.getValue());
251 else if (node instanceof OrderedMapNode) {
252 final OrderedMapNode n = (OrderedMapNode) node;
253 writer.startOrderedMapNode(n.getIdentifier(), childSizeHint(n.getValue()));
254 processedAsCompositeNode = writeChildren(n.getValue());
256 else if (node instanceof MapNode) {
257 final MapNode n = (MapNode) node;
258 writer.startMapNode(n.getIdentifier(), childSizeHint(n.getValue()));
259 processedAsCompositeNode = writeChildren(n.getValue());
261 else if (node instanceof LeafSetNode) {
262 //covers also OrderedLeafSetNode for which doesn't exist start* method
263 final LeafSetNode<?> n = (LeafSetNode<?>) node;
264 writer.startLeafSet(n.getIdentifier(), childSizeHint(n.getValue()));
266 processedAsCompositeNode = writeChildren(n.getValue());
270 return processedAsCompositeNode;
273 private static final class OrderedDepthAwareNormalizedNodeWriter extends DepthAwareNormalizedNodeWriter {
274 private static final Logger LOG = LoggerFactory.getLogger(OrderedDepthAwareNormalizedNodeWriter.class);
276 OrderedDepthAwareNormalizedNodeWriter(final NormalizedNodeStreamWriter writer, final int maxDepth) {
277 super(writer, maxDepth);
281 protected boolean writeMapEntryNode(final MapEntryNode node) throws IOException {
282 final NormalizedNodeStreamWriter writer = getWriter();
283 if(writer instanceof NormalizedNodeStreamAttributeWriter) {
284 ((NormalizedNodeStreamAttributeWriter) writer).startMapEntryNode(node.getIdentifier(), childSizeHint(node.getValue()), node.getAttributes());
286 writer.startMapEntryNode(node.getIdentifier(), childSizeHint(node.getValue()));
289 final Set<QName> qnames = node.getIdentifier().getKeyValues().keySet();
290 // Write out all the key children
291 for (QName qname : qnames) {
292 final Optional<? extends NormalizedNode<?, ?>> child = node.getChild(new NodeIdentifier(qname));
293 if (child.isPresent()) {
296 LOG.info("No child for key element {} found", qname);
300 // Write all the rest
302 boolean result = writeChildren(Iterables.filter(node.getValue(), new Predicate<NormalizedNode<?, ?>>() {
304 public boolean apply(final NormalizedNode<?, ?> input) {
305 if (input instanceof AugmentationNode) {
308 if (!qnames.contains(input.getNodeType())) {
312 LOG.debug("Skipping key child {}", input);