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.nio.charset.StandardCharsets;
15 import java.util.Collection;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
27 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOutput, NormalizedNodeStreamWriter {
32 private static final Logger LOG = LoggerFactory.getLogger(AbstractNormalizedNodeDataOutput.class);
34 private final DataOutput output;
36 private NormalizedNodeWriter normalizedNodeWriter;
37 private boolean headerWritten;
38 private QName lastLeafSetQName;
40 AbstractNormalizedNodeDataOutput(final DataOutput output) {
41 this.output = Preconditions.checkNotNull(output);
44 private void ensureHeaderWritten() throws IOException {
46 output.writeByte(TokenTypes.SIGNATURE_MARKER);
47 output.writeShort(streamVersion());
52 protected abstract short streamVersion();
53 protected abstract void writeQName(QName qname) throws IOException;
54 protected abstract void writeString(String string) throws IOException;
57 public final void write(final int b) throws IOException {
58 ensureHeaderWritten();
63 public final void write(final byte[] b) throws IOException {
64 ensureHeaderWritten();
69 public final void write(final byte[] b, final int off, final int len) throws IOException {
70 ensureHeaderWritten();
71 output.write(b, off, len);
75 public final void writeBoolean(final boolean v) throws IOException {
76 ensureHeaderWritten();
77 output.writeBoolean(v);
81 public final void writeByte(final int v) throws IOException {
82 ensureHeaderWritten();
87 public final void writeShort(final int v) throws IOException {
88 ensureHeaderWritten();
93 public final void writeChar(final int v) throws IOException {
94 ensureHeaderWritten();
99 public final void writeInt(final int v) throws IOException {
100 ensureHeaderWritten();
105 public final void writeLong(final long v) throws IOException {
106 ensureHeaderWritten();
111 public final void writeFloat(final float v) throws IOException {
112 ensureHeaderWritten();
113 output.writeFloat(v);
117 public final void writeDouble(final double v) throws IOException {
118 ensureHeaderWritten();
119 output.writeDouble(v);
123 public final void writeBytes(final String s) throws IOException {
124 ensureHeaderWritten();
125 output.writeBytes(s);
129 public final void writeChars(final String s) throws IOException {
130 ensureHeaderWritten();
131 output.writeChars(s);
135 public final void writeUTF(final String s) throws IOException {
136 ensureHeaderWritten();
140 private NormalizedNodeWriter normalizedNodeWriter() {
141 if(normalizedNodeWriter == null) {
142 normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(this);
145 return normalizedNodeWriter;
149 public void writeNormalizedNode(final NormalizedNode<?, ?> node) throws IOException {
150 ensureHeaderWritten();
151 normalizedNodeWriter().write(node);
155 public void leafNode(final NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
156 Preconditions.checkNotNull(name, "Node identifier should not be null");
157 LOG.debug("Writing a new leaf node");
158 startNode(name.getNodeType(), NodeTypes.LEAF_NODE);
164 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
165 Preconditions.checkNotNull(name, "Node identifier should not be null");
166 LOG.debug("Starting a new leaf set");
168 lastLeafSetQName = name.getNodeType();
169 startNode(name.getNodeType(), NodeTypes.LEAF_SET);
173 public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
174 Preconditions.checkNotNull(name, "Node identifier should not be null");
175 LOG.debug("Starting a new ordered leaf set");
177 lastLeafSetQName = name.getNodeType();
178 startNode(name.getNodeType(), NodeTypes.ORDERED_LEAF_SET);
182 public void leafSetEntryNode(final QName name, final Object value) throws IOException, IllegalArgumentException {
183 LOG.debug("Writing a new leaf set entry node");
185 output.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
187 // lastLeafSetQName is set if the parent LeafSetNode was previously written. Otherwise this is a
188 // stand alone LeafSetEntryNode so write out it's name here.
189 if(lastLeafSetQName == null) {
197 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
198 Preconditions.checkNotNull(name, "Node identifier should not be null");
200 LOG.debug("Starting a new container node");
202 startNode(name.getNodeType(), NodeTypes.CONTAINER_NODE);
206 public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
207 Preconditions.checkNotNull(name, "Node identifier should not be null");
209 LOG.debug("Starting a new yang modeled anyXml node");
211 startNode(name.getNodeType(), NodeTypes.YANG_MODELED_ANY_XML_NODE);
215 public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
216 Preconditions.checkNotNull(name, "Node identifier should not be null");
217 LOG.debug("Starting a new unkeyed list");
219 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST);
223 public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalStateException {
224 Preconditions.checkNotNull(name, "Node identifier should not be null");
225 LOG.debug("Starting a new unkeyed list item");
227 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST_ITEM);
231 public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
232 Preconditions.checkNotNull(name, "Node identifier should not be null");
233 LOG.debug("Starting a new map node");
235 startNode(name.getNodeType(), NodeTypes.MAP_NODE);
239 public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint) throws IOException, IllegalArgumentException {
240 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
241 LOG.debug("Starting a new map entry node");
242 startNode(identifier.getNodeType(), NodeTypes.MAP_ENTRY_NODE);
244 writeKeyValueMap(identifier.getKeyValues());
249 public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
250 Preconditions.checkNotNull(name, "Node identifier should not be null");
251 LOG.debug("Starting a new ordered map node");
253 startNode(name.getNodeType(), NodeTypes.ORDERED_MAP_NODE);
257 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
258 Preconditions.checkNotNull(name, "Node identifier should not be null");
259 LOG.debug("Starting a new choice node");
261 startNode(name.getNodeType(), NodeTypes.CHOICE_NODE);
265 public void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException, IllegalArgumentException {
266 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
267 LOG.debug("Starting a new augmentation node");
269 output.writeByte(NodeTypes.AUGMENTATION_NODE);
270 writeQNameSet(identifier.getPossibleChildNames());
274 public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
275 Preconditions.checkNotNull(name, "Node identifier should not be null");
276 LOG.debug("Writing a new xml node");
278 startNode(name.getNodeType(), NodeTypes.ANY_XML_NODE);
284 public void endNode() throws IOException, IllegalStateException {
285 LOG.debug("Ending the node");
287 output.writeByte(NodeTypes.END_NODE);
291 public void close() throws IOException {
296 public void flush() throws IOException {
297 if (output instanceof OutputStream) {
298 ((OutputStream)output).flush();
302 private void startNode(final QName qName, final byte nodeType) throws IOException {
304 Preconditions.checkNotNull(qName, "QName of node identifier should not be null.");
306 ensureHeaderWritten();
308 // First write the type of node
309 output.writeByte(nodeType);
314 private void writeObjSet(final Set<?> set) throws IOException {
315 output.writeInt(set.size());
316 for (Object o : set) {
317 Preconditions.checkArgument(o instanceof String, "Expected value type to be String but was %s (%s)",
320 writeString((String) o);
325 public void writeYangInstanceIdentifier(final YangInstanceIdentifier identifier) throws IOException {
326 ensureHeaderWritten();
327 writeYangInstanceIdentifierInternal(identifier);
330 private void writeYangInstanceIdentifierInternal(final YangInstanceIdentifier identifier) throws IOException {
331 Collection<PathArgument> pathArguments = identifier.getPathArguments();
332 output.writeInt(pathArguments.size());
334 for(PathArgument pathArgument : pathArguments) {
335 writePathArgument(pathArgument);
340 public void writePathArgument(final PathArgument pathArgument) throws IOException {
342 byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
344 output.writeByte(type);
347 case PathArgumentTypes.NODE_IDENTIFIER:
349 NodeIdentifier nodeIdentifier = (NodeIdentifier) pathArgument;
351 writeQName(nodeIdentifier.getNodeType());
354 case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
356 NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
357 (NodeIdentifierWithPredicates) pathArgument;
358 writeQName(nodeIdentifierWithPredicates.getNodeType());
360 writeKeyValueMap(nodeIdentifierWithPredicates.getKeyValues());
363 case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
365 NodeWithValue<?> nodeWithValue = (NodeWithValue<?>) pathArgument;
367 writeQName(nodeWithValue.getNodeType());
368 writeObject(nodeWithValue.getValue());
371 case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
373 AugmentationIdentifier augmentationIdentifier = (AugmentationIdentifier) pathArgument;
375 // No Qname in augmentation identifier
376 writeQNameSet(augmentationIdentifier.getPossibleChildNames());
379 throw new IllegalStateException("Unknown node identifier type is found : " + pathArgument.getClass().toString() );
383 private void writeKeyValueMap(final Map<QName, Object> keyValueMap) throws IOException {
384 if (keyValueMap != null && !keyValueMap.isEmpty()) {
385 output.writeInt(keyValueMap.size());
387 for (QName qName : keyValueMap.keySet()) {
389 writeObject(keyValueMap.get(qName));
396 private void writeQNameSet(final Set<QName> children) throws IOException {
397 // Write each child's qname separately, if list is empty send count as 0
398 if (children != null && !children.isEmpty()) {
399 output.writeInt(children.size());
400 for (QName qName : children) {
404 LOG.debug("augmentation node does not have any child");
409 private void writeObject(final Object value) throws IOException {
411 byte type = ValueTypes.getSerializableType(value);
412 // Write object type first
413 output.writeByte(type);
416 case ValueTypes.BOOL_TYPE:
417 output.writeBoolean((Boolean) value);
419 case ValueTypes.QNAME_TYPE:
420 writeQName((QName) value);
422 case ValueTypes.INT_TYPE:
423 output.writeInt((Integer) value);
425 case ValueTypes.BYTE_TYPE:
426 output.writeByte((Byte) value);
428 case ValueTypes.LONG_TYPE:
429 output.writeLong((Long) value);
431 case ValueTypes.SHORT_TYPE:
432 output.writeShort((Short) value);
434 case ValueTypes.BITS_TYPE:
435 writeObjSet((Set<?>) value);
437 case ValueTypes.BINARY_TYPE:
438 byte[] bytes = (byte[]) value;
439 output.writeInt(bytes.length);
442 case ValueTypes.YANG_IDENTIFIER_TYPE:
443 writeYangInstanceIdentifierInternal((YangInstanceIdentifier) value);
445 case ValueTypes.NULL_TYPE :
447 case ValueTypes.STRING_BYTES_TYPE:
448 final byte[] valueBytes = value.toString().getBytes(StandardCharsets.UTF_8);
449 output.writeInt(valueBytes.length);
450 output.write(valueBytes);
453 output.writeUTF(value.toString());