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.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOutput, NormalizedNodeStreamWriter {
27 private static final Logger LOG = LoggerFactory.getLogger(AbstractNormalizedNodeDataOutput.class);
29 private final DataOutput output;
31 private NormalizedNodeWriter normalizedNodeWriter;
32 private boolean headerWritten;
34 AbstractNormalizedNodeDataOutput(final DataOutput output) {
35 this.output = Preconditions.checkNotNull(output);
38 private void ensureHeaderWritten() throws IOException {
40 output.writeByte(TokenTypes.SIGNATURE_MARKER);
41 output.writeShort(streamVersion());
46 protected abstract short streamVersion();
47 protected abstract void writeQName(QName qname) throws IOException;
48 protected abstract void writeString(String string) throws IOException;
51 public final void write(final int b) throws IOException {
52 ensureHeaderWritten();
57 public final void write(final byte[] b) throws IOException {
58 ensureHeaderWritten();
63 public final void write(final byte[] b, final int off, final int len) throws IOException {
64 ensureHeaderWritten();
65 output.write(b, off, len);
69 public final void writeBoolean(final boolean v) throws IOException {
70 ensureHeaderWritten();
71 output.writeBoolean(v);
75 public final void writeByte(final int v) throws IOException {
76 ensureHeaderWritten();
81 public final void writeShort(final int v) throws IOException {
82 ensureHeaderWritten();
87 public final void writeChar(final int v) throws IOException {
88 ensureHeaderWritten();
93 public final void writeInt(final int v) throws IOException {
94 ensureHeaderWritten();
99 public final void writeLong(final long v) throws IOException {
100 ensureHeaderWritten();
105 public final void writeFloat(final float v) throws IOException {
106 ensureHeaderWritten();
107 output.writeFloat(v);
111 public final void writeDouble(final double v) throws IOException {
112 ensureHeaderWritten();
113 output.writeDouble(v);
117 public final void writeBytes(final String s) throws IOException {
118 ensureHeaderWritten();
119 output.writeBytes(s);
123 public final void writeChars(final String s) throws IOException {
124 ensureHeaderWritten();
125 output.writeChars(s);
129 public final void writeUTF(final String s) throws IOException {
130 ensureHeaderWritten();
134 private NormalizedNodeWriter normalizedNodeWriter() {
135 if(normalizedNodeWriter == null) {
136 normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(this);
139 return normalizedNodeWriter;
143 public void writeNormalizedNode(final NormalizedNode<?, ?> node) throws IOException {
144 ensureHeaderWritten();
145 normalizedNodeWriter().write(node);
149 public void leafNode(final YangInstanceIdentifier.NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
150 Preconditions.checkNotNull(name, "Node identifier should not be null");
151 LOG.debug("Writing a new leaf node");
152 startNode(name.getNodeType(), NodeTypes.LEAF_NODE);
158 public void startLeafSet(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
159 Preconditions.checkNotNull(name, "Node identifier should not be null");
160 LOG.debug("Starting a new leaf set");
162 startNode(name.getNodeType(), NodeTypes.LEAF_SET);
166 public void startOrderedLeafSet(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
167 Preconditions.checkNotNull(name, "Node identifier should not be null");
168 LOG.debug("Starting a new ordered leaf set");
170 startNode(name.getNodeType(), NodeTypes.ORDERED_LEAF_SET);
174 public void leafSetEntryNode(final Object value) throws IOException, IllegalArgumentException {
175 LOG.debug("Writing a new leaf set entry node");
177 output.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
182 public void startContainerNode(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
183 Preconditions.checkNotNull(name, "Node identifier should not be null");
185 LOG.debug("Starting a new container node");
187 startNode(name.getNodeType(), NodeTypes.CONTAINER_NODE);
191 public void startYangModeledAnyXmlNode(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
192 Preconditions.checkNotNull(name, "Node identifier should not be null");
194 LOG.debug("Starting a new yang modeled anyXml node");
196 startNode(name.getNodeType(), NodeTypes.YANG_MODELED_ANY_XML_NODE);
200 public void startUnkeyedList(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
201 Preconditions.checkNotNull(name, "Node identifier should not be null");
202 LOG.debug("Starting a new unkeyed list");
204 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST);
208 public void startUnkeyedListItem(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalStateException {
209 Preconditions.checkNotNull(name, "Node identifier should not be null");
210 LOG.debug("Starting a new unkeyed list item");
212 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST_ITEM);
216 public void startMapNode(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
217 Preconditions.checkNotNull(name, "Node identifier should not be null");
218 LOG.debug("Starting a new map node");
220 startNode(name.getNodeType(), NodeTypes.MAP_NODE);
224 public void startMapEntryNode(final YangInstanceIdentifier.NodeIdentifierWithPredicates identifier, final int childSizeHint) throws IOException, IllegalArgumentException {
225 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
226 LOG.debug("Starting a new map entry node");
227 startNode(identifier.getNodeType(), NodeTypes.MAP_ENTRY_NODE);
229 writeKeyValueMap(identifier.getKeyValues());
234 public void startOrderedMapNode(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
235 Preconditions.checkNotNull(name, "Node identifier should not be null");
236 LOG.debug("Starting a new ordered map node");
238 startNode(name.getNodeType(), NodeTypes.ORDERED_MAP_NODE);
242 public void startChoiceNode(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
243 Preconditions.checkNotNull(name, "Node identifier should not be null");
244 LOG.debug("Starting a new choice node");
246 startNode(name.getNodeType(), NodeTypes.CHOICE_NODE);
250 public void startAugmentationNode(final YangInstanceIdentifier.AugmentationIdentifier identifier) throws IOException, IllegalArgumentException {
251 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
252 LOG.debug("Starting a new augmentation node");
254 output.writeByte(NodeTypes.AUGMENTATION_NODE);
255 writeQNameSet(identifier.getPossibleChildNames());
259 public void anyxmlNode(final YangInstanceIdentifier.NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
260 Preconditions.checkNotNull(name, "Node identifier should not be null");
261 LOG.debug("Writing a new xml node");
263 startNode(name.getNodeType(), NodeTypes.ANY_XML_NODE);
269 public void endNode() throws IOException, IllegalStateException {
270 LOG.debug("Ending the node");
272 output.writeByte(NodeTypes.END_NODE);
276 public void close() throws IOException {
281 public void flush() throws IOException {
282 if (output instanceof OutputStream) {
283 ((OutputStream)output).flush();
287 private void startNode(final QName qName, final byte nodeType) throws IOException {
289 Preconditions.checkNotNull(qName, "QName of node identifier should not be null.");
291 ensureHeaderWritten();
293 // First write the type of node
294 output.writeByte(nodeType);
299 private void writeObjSet(final Set<?> set) throws IOException {
300 output.writeInt(set.size());
301 for (Object o : set) {
302 Preconditions.checkArgument(o instanceof String, "Expected value type to be String but was %s (%s)",
305 writeString((String) o);
310 public void writeYangInstanceIdentifier(final YangInstanceIdentifier identifier) throws IOException {
311 ensureHeaderWritten();
312 writeYangInstanceIdentifierInternal(identifier);
315 private void writeYangInstanceIdentifierInternal(final YangInstanceIdentifier identifier) throws IOException {
316 Collection<YangInstanceIdentifier.PathArgument> pathArguments = identifier.getPathArguments();
317 output.writeInt(pathArguments.size());
319 for(YangInstanceIdentifier.PathArgument pathArgument : pathArguments) {
320 writePathArgument(pathArgument);
325 public void writePathArgument(final YangInstanceIdentifier.PathArgument pathArgument) throws IOException {
327 byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
329 output.writeByte(type);
332 case PathArgumentTypes.NODE_IDENTIFIER:
334 YangInstanceIdentifier.NodeIdentifier nodeIdentifier =
335 (YangInstanceIdentifier.NodeIdentifier) pathArgument;
337 writeQName(nodeIdentifier.getNodeType());
340 case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
342 YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
343 (YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument;
344 writeQName(nodeIdentifierWithPredicates.getNodeType());
346 writeKeyValueMap(nodeIdentifierWithPredicates.getKeyValues());
349 case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
351 YangInstanceIdentifier.NodeWithValue nodeWithValue =
352 (YangInstanceIdentifier.NodeWithValue) pathArgument;
354 writeQName(nodeWithValue.getNodeType());
355 writeObject(nodeWithValue.getValue());
358 case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
360 YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
361 (YangInstanceIdentifier.AugmentationIdentifier) pathArgument;
363 // No Qname in augmentation identifier
364 writeQNameSet(augmentationIdentifier.getPossibleChildNames());
367 throw new IllegalStateException("Unknown node identifier type is found : " + pathArgument.getClass().toString() );
371 private void writeKeyValueMap(final Map<QName, Object> keyValueMap) throws IOException {
372 if (keyValueMap != null && !keyValueMap.isEmpty()) {
373 output.writeInt(keyValueMap.size());
375 for (QName qName : keyValueMap.keySet()) {
377 writeObject(keyValueMap.get(qName));
384 private void writeQNameSet(final Set<QName> children) throws IOException {
385 // Write each child's qname separately, if list is empty send count as 0
386 if (children != null && !children.isEmpty()) {
387 output.writeInt(children.size());
388 for (QName qName : children) {
392 LOG.debug("augmentation node does not have any child");
397 private void writeObject(final Object value) throws IOException {
399 byte type = ValueTypes.getSerializableType(value);
400 // Write object type first
401 output.writeByte(type);
404 case ValueTypes.BOOL_TYPE:
405 output.writeBoolean((Boolean) value);
407 case ValueTypes.QNAME_TYPE:
408 writeQName((QName) value);
410 case ValueTypes.INT_TYPE:
411 output.writeInt((Integer) value);
413 case ValueTypes.BYTE_TYPE:
414 output.writeByte((Byte) value);
416 case ValueTypes.LONG_TYPE:
417 output.writeLong((Long) value);
419 case ValueTypes.SHORT_TYPE:
420 output.writeShort((Short) value);
422 case ValueTypes.BITS_TYPE:
423 writeObjSet((Set<?>) value);
425 case ValueTypes.BINARY_TYPE:
426 byte[] bytes = (byte[]) value;
427 output.writeInt(bytes.length);
430 case ValueTypes.YANG_IDENTIFIER_TYPE:
431 writeYangInstanceIdentifierInternal((YangInstanceIdentifier) value);
433 case ValueTypes.NULL_TYPE :
435 case ValueTypes.STRING_BYTES_TYPE:
436 final byte[] valueBytes = value.toString().getBytes(StandardCharsets.UTF_8);
437 output.writeInt(valueBytes.length);
438 output.write(valueBytes);
441 output.writeUTF(value.toString());