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.cluster.datastore.node.utils.stream;
10 import com.google.common.base.Preconditions;
11 import java.io.DataOutput;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.io.StringWriter;
15 import java.nio.charset.StandardCharsets;
16 import java.util.Collection;
19 import javax.xml.transform.TransformerException;
20 import javax.xml.transform.TransformerFactory;
21 import javax.xml.transform.TransformerFactoryConfigurationError;
22 import javax.xml.transform.dom.DOMSource;
23 import javax.xml.transform.stream.StreamResult;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
33 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
34 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOutput, NormalizedNodeStreamWriter {
39 private static final Logger LOG = LoggerFactory.getLogger(AbstractNormalizedNodeDataOutput.class);
41 private final DataOutput output;
43 private NormalizedNodeWriter normalizedNodeWriter;
44 private boolean headerWritten;
45 private QName lastLeafSetQName;
47 AbstractNormalizedNodeDataOutput(final DataOutput output) {
48 this.output = Preconditions.checkNotNull(output);
51 private void ensureHeaderWritten() throws IOException {
53 output.writeByte(TokenTypes.SIGNATURE_MARKER);
54 output.writeShort(streamVersion());
59 protected abstract short streamVersion();
60 protected abstract void writeQName(QName qname) throws IOException;
61 protected abstract void writeString(String string) throws IOException;
64 public final void write(final int b) throws IOException {
65 ensureHeaderWritten();
70 public final void write(final byte[] b) throws IOException {
71 ensureHeaderWritten();
76 public final void write(final byte[] b, final int off, final int len) throws IOException {
77 ensureHeaderWritten();
78 output.write(b, off, len);
82 public final void writeBoolean(final boolean v) throws IOException {
83 ensureHeaderWritten();
84 output.writeBoolean(v);
88 public final void writeByte(final int v) throws IOException {
89 ensureHeaderWritten();
94 public final void writeShort(final int v) throws IOException {
95 ensureHeaderWritten();
100 public final void writeChar(final int v) throws IOException {
101 ensureHeaderWritten();
106 public final void writeInt(final int v) throws IOException {
107 ensureHeaderWritten();
112 public final void writeLong(final long v) throws IOException {
113 ensureHeaderWritten();
118 public final void writeFloat(final float v) throws IOException {
119 ensureHeaderWritten();
120 output.writeFloat(v);
124 public final void writeDouble(final double v) throws IOException {
125 ensureHeaderWritten();
126 output.writeDouble(v);
130 public final void writeBytes(final String s) throws IOException {
131 ensureHeaderWritten();
132 output.writeBytes(s);
136 public final void writeChars(final String s) throws IOException {
137 ensureHeaderWritten();
138 output.writeChars(s);
142 public final void writeUTF(final String s) throws IOException {
143 ensureHeaderWritten();
147 private NormalizedNodeWriter normalizedNodeWriter() {
148 if(normalizedNodeWriter == null) {
149 normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(this);
152 return normalizedNodeWriter;
156 public void writeNormalizedNode(final NormalizedNode<?, ?> node) throws IOException {
157 ensureHeaderWritten();
158 normalizedNodeWriter().write(node);
162 public void leafNode(final NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
163 Preconditions.checkNotNull(name, "Node identifier should not be null");
164 LOG.debug("Writing a new leaf node");
165 startNode(name.getNodeType(), NodeTypes.LEAF_NODE);
171 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
172 Preconditions.checkNotNull(name, "Node identifier should not be null");
173 LOG.debug("Starting a new leaf set");
175 lastLeafSetQName = name.getNodeType();
176 startNode(name.getNodeType(), NodeTypes.LEAF_SET);
180 public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
181 Preconditions.checkNotNull(name, "Node identifier should not be null");
182 LOG.debug("Starting a new ordered leaf set");
184 lastLeafSetQName = name.getNodeType();
185 startNode(name.getNodeType(), NodeTypes.ORDERED_LEAF_SET);
189 public void leafSetEntryNode(final QName name, final Object value) throws IOException, IllegalArgumentException {
190 LOG.debug("Writing a new leaf set entry node");
192 output.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
194 // lastLeafSetQName is set if the parent LeafSetNode was previously written. Otherwise this is a
195 // stand alone LeafSetEntryNode so write out it's name here.
196 if(lastLeafSetQName == null) {
204 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
205 Preconditions.checkNotNull(name, "Node identifier should not be null");
207 LOG.debug("Starting a new container node");
209 startNode(name.getNodeType(), NodeTypes.CONTAINER_NODE);
213 public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
214 Preconditions.checkNotNull(name, "Node identifier should not be null");
216 LOG.debug("Starting a new yang modeled anyXml node");
218 startNode(name.getNodeType(), NodeTypes.YANG_MODELED_ANY_XML_NODE);
222 public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
223 Preconditions.checkNotNull(name, "Node identifier should not be null");
224 LOG.debug("Starting a new unkeyed list");
226 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST);
230 public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalStateException {
231 Preconditions.checkNotNull(name, "Node identifier should not be null");
232 LOG.debug("Starting a new unkeyed list item");
234 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST_ITEM);
238 public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
239 Preconditions.checkNotNull(name, "Node identifier should not be null");
240 LOG.debug("Starting a new map node");
242 startNode(name.getNodeType(), NodeTypes.MAP_NODE);
246 public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint) throws IOException, IllegalArgumentException {
247 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
248 LOG.debug("Starting a new map entry node");
249 startNode(identifier.getNodeType(), NodeTypes.MAP_ENTRY_NODE);
251 writeKeyValueMap(identifier.getKeyValues());
256 public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
257 Preconditions.checkNotNull(name, "Node identifier should not be null");
258 LOG.debug("Starting a new ordered map node");
260 startNode(name.getNodeType(), NodeTypes.ORDERED_MAP_NODE);
264 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
265 Preconditions.checkNotNull(name, "Node identifier should not be null");
266 LOG.debug("Starting a new choice node");
268 startNode(name.getNodeType(), NodeTypes.CHOICE_NODE);
272 public void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException, IllegalArgumentException {
273 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
274 LOG.debug("Starting a new augmentation node");
276 output.writeByte(NodeTypes.AUGMENTATION_NODE);
277 writeQNameSet(identifier.getPossibleChildNames());
281 public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
282 Preconditions.checkNotNull(name, "Node identifier should not be null");
283 LOG.debug("Writing any xml node");
285 startNode(name.getNodeType(), NodeTypes.ANY_XML_NODE);
288 StreamResult xmlOutput = new StreamResult(new StringWriter());
289 TransformerFactory.newInstance().newTransformer().transform((DOMSource)value, xmlOutput);
290 writeObject(xmlOutput.getWriter().toString());
291 } catch (TransformerException | TransformerFactoryConfigurationError e) {
292 throw new IOException("Error writing anyXml", e);
297 public void endNode() throws IOException, IllegalStateException {
298 LOG.debug("Ending the node");
300 output.writeByte(NodeTypes.END_NODE);
304 public void close() throws IOException {
309 public void flush() throws IOException {
310 if (output instanceof OutputStream) {
311 ((OutputStream)output).flush();
315 private void startNode(final QName qName, final byte nodeType) throws IOException {
317 Preconditions.checkNotNull(qName, "QName of node identifier should not be null.");
319 ensureHeaderWritten();
321 // First write the type of node
322 output.writeByte(nodeType);
327 private void writeObjSet(final Set<?> set) throws IOException {
328 output.writeInt(set.size());
329 for (Object o : set) {
330 Preconditions.checkArgument(o instanceof String, "Expected value type to be String but was %s (%s)",
333 writeString((String) o);
338 public void writeSchemaPath(final SchemaPath path) throws IOException {
339 ensureHeaderWritten();
340 output.writeBoolean(path.isAbsolute());
342 final Collection<QName> qnames = path.getPath();
343 output.writeInt(qnames.size());
344 for (QName qname : qnames) {
350 public void writeYangInstanceIdentifier(final YangInstanceIdentifier identifier) throws IOException {
351 ensureHeaderWritten();
352 writeYangInstanceIdentifierInternal(identifier);
355 private void writeYangInstanceIdentifierInternal(final YangInstanceIdentifier identifier) throws IOException {
356 Collection<PathArgument> pathArguments = identifier.getPathArguments();
357 output.writeInt(pathArguments.size());
359 for(PathArgument pathArgument : pathArguments) {
360 writePathArgument(pathArgument);
365 public void writePathArgument(final PathArgument pathArgument) throws IOException {
367 byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
369 output.writeByte(type);
372 case PathArgumentTypes.NODE_IDENTIFIER:
374 NodeIdentifier nodeIdentifier = (NodeIdentifier) pathArgument;
376 writeQName(nodeIdentifier.getNodeType());
379 case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
381 NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
382 (NodeIdentifierWithPredicates) pathArgument;
383 writeQName(nodeIdentifierWithPredicates.getNodeType());
385 writeKeyValueMap(nodeIdentifierWithPredicates.getKeyValues());
388 case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
390 NodeWithValue<?> nodeWithValue = (NodeWithValue<?>) pathArgument;
392 writeQName(nodeWithValue.getNodeType());
393 writeObject(nodeWithValue.getValue());
396 case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
398 AugmentationIdentifier augmentationIdentifier = (AugmentationIdentifier) pathArgument;
400 // No Qname in augmentation identifier
401 writeQNameSet(augmentationIdentifier.getPossibleChildNames());
404 throw new IllegalStateException("Unknown node identifier type is found : " + pathArgument.getClass().toString() );
408 private void writeKeyValueMap(final Map<QName, Object> keyValueMap) throws IOException {
409 if (keyValueMap != null && !keyValueMap.isEmpty()) {
410 output.writeInt(keyValueMap.size());
412 for (QName qName : keyValueMap.keySet()) {
414 writeObject(keyValueMap.get(qName));
421 private void writeQNameSet(final Set<QName> children) throws IOException {
422 // Write each child's qname separately, if list is empty send count as 0
423 if (children != null && !children.isEmpty()) {
424 output.writeInt(children.size());
425 for (QName qName : children) {
429 LOG.debug("augmentation node does not have any child");
434 private void writeObject(final Object value) throws IOException {
436 byte type = ValueTypes.getSerializableType(value);
437 // Write object type first
438 output.writeByte(type);
441 case ValueTypes.BOOL_TYPE:
442 output.writeBoolean((Boolean) value);
444 case ValueTypes.QNAME_TYPE:
445 writeQName((QName) value);
447 case ValueTypes.INT_TYPE:
448 output.writeInt((Integer) value);
450 case ValueTypes.BYTE_TYPE:
451 output.writeByte((Byte) value);
453 case ValueTypes.LONG_TYPE:
454 output.writeLong((Long) value);
456 case ValueTypes.SHORT_TYPE:
457 output.writeShort((Short) value);
459 case ValueTypes.BITS_TYPE:
460 writeObjSet((Set<?>) value);
462 case ValueTypes.BINARY_TYPE:
463 byte[] bytes = (byte[]) value;
464 output.writeInt(bytes.length);
467 case ValueTypes.YANG_IDENTIFIER_TYPE:
468 writeYangInstanceIdentifierInternal((YangInstanceIdentifier) value);
470 case ValueTypes.NULL_TYPE :
472 case ValueTypes.STRING_BYTES_TYPE:
473 final byte[] valueBytes = value.toString().getBytes(StandardCharsets.UTF_8);
474 output.writeInt(valueBytes.length);
475 output.write(valueBytes);
478 output.writeUTF(value.toString());