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 leafSetEntryNode(final Object value) throws IOException, IllegalArgumentException {
167 LOG.debug("Writing a new leaf set entry node");
169 output.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
174 public void startContainerNode(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
175 Preconditions.checkNotNull(name, "Node identifier should not be null");
177 LOG.debug("Starting a new container node");
179 startNode(name.getNodeType(), NodeTypes.CONTAINER_NODE);
183 public void startYangModeledAnyXmlNode(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
184 Preconditions.checkNotNull(name, "Node identifier should not be null");
186 LOG.debug("Starting a new yang modeled anyXml node");
188 startNode(name.getNodeType(), NodeTypes.YANG_MODELED_ANY_XML_NODE);
192 public void startUnkeyedList(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
193 Preconditions.checkNotNull(name, "Node identifier should not be null");
194 LOG.debug("Starting a new unkeyed list");
196 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST);
200 public void startUnkeyedListItem(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalStateException {
201 Preconditions.checkNotNull(name, "Node identifier should not be null");
202 LOG.debug("Starting a new unkeyed list item");
204 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST_ITEM);
208 public void startMapNode(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
209 Preconditions.checkNotNull(name, "Node identifier should not be null");
210 LOG.debug("Starting a new map node");
212 startNode(name.getNodeType(), NodeTypes.MAP_NODE);
216 public void startMapEntryNode(final YangInstanceIdentifier.NodeIdentifierWithPredicates identifier, final int childSizeHint) throws IOException, IllegalArgumentException {
217 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
218 LOG.debug("Starting a new map entry node");
219 startNode(identifier.getNodeType(), NodeTypes.MAP_ENTRY_NODE);
221 writeKeyValueMap(identifier.getKeyValues());
226 public void startOrderedMapNode(final YangInstanceIdentifier.NodeIdentifier name, final int childSizeHint) throws IOException, IllegalArgumentException {
227 Preconditions.checkNotNull(name, "Node identifier should not be null");
228 LOG.debug("Starting a new ordered map node");
230 startNode(name.getNodeType(), NodeTypes.ORDERED_MAP_NODE);
234 public void startChoiceNode(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 choice node");
238 startNode(name.getNodeType(), NodeTypes.CHOICE_NODE);
242 public void startAugmentationNode(final YangInstanceIdentifier.AugmentationIdentifier identifier) throws IOException, IllegalArgumentException {
243 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
244 LOG.debug("Starting a new augmentation node");
246 output.writeByte(NodeTypes.AUGMENTATION_NODE);
247 writeQNameSet(identifier.getPossibleChildNames());
251 public void anyxmlNode(final YangInstanceIdentifier.NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
252 Preconditions.checkNotNull(name, "Node identifier should not be null");
253 LOG.debug("Writing a new xml node");
255 startNode(name.getNodeType(), NodeTypes.ANY_XML_NODE);
261 public void endNode() throws IOException, IllegalStateException {
262 LOG.debug("Ending the node");
264 output.writeByte(NodeTypes.END_NODE);
268 public void close() throws IOException {
273 public void flush() throws IOException {
274 if (output instanceof OutputStream) {
275 ((OutputStream)output).flush();
279 private void startNode(final QName qName, final byte nodeType) throws IOException {
281 Preconditions.checkNotNull(qName, "QName of node identifier should not be null.");
283 ensureHeaderWritten();
285 // First write the type of node
286 output.writeByte(nodeType);
291 private void writeObjSet(final Set<?> set) throws IOException {
292 output.writeInt(set.size());
293 for (Object o : set) {
294 Preconditions.checkArgument(o instanceof String, "Expected value type to be String but was %s (%s)",
297 writeString((String) o);
302 public void writeYangInstanceIdentifier(final YangInstanceIdentifier identifier) throws IOException {
303 ensureHeaderWritten();
304 writeYangInstanceIdentifierInternal(identifier);
307 private void writeYangInstanceIdentifierInternal(final YangInstanceIdentifier identifier) throws IOException {
308 Collection<YangInstanceIdentifier.PathArgument> pathArguments = identifier.getPathArguments();
309 output.writeInt(pathArguments.size());
311 for(YangInstanceIdentifier.PathArgument pathArgument : pathArguments) {
312 writePathArgument(pathArgument);
317 public void writePathArgument(final YangInstanceIdentifier.PathArgument pathArgument) throws IOException {
319 byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
321 output.writeByte(type);
324 case PathArgumentTypes.NODE_IDENTIFIER:
326 YangInstanceIdentifier.NodeIdentifier nodeIdentifier =
327 (YangInstanceIdentifier.NodeIdentifier) pathArgument;
329 writeQName(nodeIdentifier.getNodeType());
332 case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
334 YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
335 (YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument;
336 writeQName(nodeIdentifierWithPredicates.getNodeType());
338 writeKeyValueMap(nodeIdentifierWithPredicates.getKeyValues());
341 case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
343 YangInstanceIdentifier.NodeWithValue nodeWithValue =
344 (YangInstanceIdentifier.NodeWithValue) pathArgument;
346 writeQName(nodeWithValue.getNodeType());
347 writeObject(nodeWithValue.getValue());
350 case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
352 YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
353 (YangInstanceIdentifier.AugmentationIdentifier) pathArgument;
355 // No Qname in augmentation identifier
356 writeQNameSet(augmentationIdentifier.getPossibleChildNames());
359 throw new IllegalStateException("Unknown node identifier type is found : " + pathArgument.getClass().toString() );
363 private void writeKeyValueMap(final Map<QName, Object> keyValueMap) throws IOException {
364 if (keyValueMap != null && !keyValueMap.isEmpty()) {
365 output.writeInt(keyValueMap.size());
367 for (QName qName : keyValueMap.keySet()) {
369 writeObject(keyValueMap.get(qName));
376 private void writeQNameSet(final Set<QName> children) throws IOException {
377 // Write each child's qname separately, if list is empty send count as 0
378 if (children != null && !children.isEmpty()) {
379 output.writeInt(children.size());
380 for (QName qName : children) {
384 LOG.debug("augmentation node does not have any child");
389 private void writeObject(final Object value) throws IOException {
391 byte type = ValueTypes.getSerializableType(value);
392 // Write object type first
393 output.writeByte(type);
396 case ValueTypes.BOOL_TYPE:
397 output.writeBoolean((Boolean) value);
399 case ValueTypes.QNAME_TYPE:
400 writeQName((QName) value);
402 case ValueTypes.INT_TYPE:
403 output.writeInt((Integer) value);
405 case ValueTypes.BYTE_TYPE:
406 output.writeByte((Byte) value);
408 case ValueTypes.LONG_TYPE:
409 output.writeLong((Long) value);
411 case ValueTypes.SHORT_TYPE:
412 output.writeShort((Short) value);
414 case ValueTypes.BITS_TYPE:
415 writeObjSet((Set<?>) value);
417 case ValueTypes.BINARY_TYPE:
418 byte[] bytes = (byte[]) value;
419 output.writeInt(bytes.length);
422 case ValueTypes.YANG_IDENTIFIER_TYPE:
423 writeYangInstanceIdentifierInternal((YangInstanceIdentifier) value);
425 case ValueTypes.NULL_TYPE :
427 case ValueTypes.STRING_BYTES_TYPE:
428 final byte[] valueBytes = value.toString().getBytes(StandardCharsets.UTF_8);
429 output.writeInt(valueBytes.length);
430 output.write(valueBytes);
433 output.writeUTF(value.toString());