3 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
11 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
13 import com.google.common.base.Preconditions;
14 import java.io.DataOutput;
15 import java.io.DataOutputStream;
16 import java.io.IOException;
17 import java.io.OutputStream;
18 import java.nio.charset.StandardCharsets;
19 import java.util.Collection;
20 import java.util.HashMap;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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;
32 * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
34 * A stream writer wrapper around this class will write node objects to stream in recursive manner.
35 * for example - If you have a ContainerNode which has a two LeafNode as children, then
36 * you will first call {@link #startContainerNode(YangInstanceIdentifier.NodeIdentifier, int)}, then will call
37 * {@link #leafNode(YangInstanceIdentifier.NodeIdentifier, Object)} twice and then, {@link #endNode()} to end
40 * Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
46 public class NormalizedNodeOutputStreamWriter implements NormalizedNodeStreamWriter {
48 private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeOutputStreamWriter.class);
50 static final byte SIGNATURE_MARKER = (byte) 0xab;
51 static final short CURRENT_VERSION = (short) 1;
53 static final byte IS_CODE_VALUE = 1;
54 static final byte IS_STRING_VALUE = 2;
55 static final byte IS_NULL_VALUE = 3;
57 private final DataOutput output;
59 private final Map<String, Integer> stringCodeMap = new HashMap<>();
61 private NormalizedNodeWriter normalizedNodeWriter;
63 private boolean wroteSignatureMarker;
65 public NormalizedNodeOutputStreamWriter(OutputStream stream) throws IOException {
66 Preconditions.checkNotNull(stream);
67 output = new DataOutputStream(stream);
70 public NormalizedNodeOutputStreamWriter(DataOutput output) {
71 this.output = Preconditions.checkNotNull(output);
74 private NormalizedNodeWriter normalizedNodeWriter() {
75 if(normalizedNodeWriter == null) {
76 normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(this);
79 return normalizedNodeWriter;
82 public void writeNormalizedNode(NormalizedNode<?, ?> node) throws IOException {
83 writeSignatureMarkerAndVersionIfNeeded();
84 normalizedNodeWriter().write(node);
87 private void writeSignatureMarkerAndVersionIfNeeded() throws IOException {
88 if(!wroteSignatureMarker) {
89 output.writeByte(SIGNATURE_MARKER);
90 output.writeShort(CURRENT_VERSION);
91 wroteSignatureMarker = true;
96 public void leafNode(YangInstanceIdentifier.NodeIdentifier name, Object value) throws IOException, IllegalArgumentException {
97 Preconditions.checkNotNull(name, "Node identifier should not be null");
98 LOG.debug("Writing a new leaf node");
99 startNode(name.getNodeType(), NodeTypes.LEAF_NODE);
105 public void startLeafSet(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
106 Preconditions.checkNotNull(name, "Node identifier should not be null");
107 LOG.debug("Starting a new leaf set");
109 startNode(name.getNodeType(), NodeTypes.LEAF_SET);
113 public void startOrderedLeafSet(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
114 Preconditions.checkNotNull(name, "Node identifier should not be null");
115 LOG.debug("Starting a new ordered leaf set");
117 startNode(name.getNodeType(), NodeTypes.ORDERED_LEAF_SET);
121 public void leafSetEntryNode(Object value) throws IOException, IllegalArgumentException {
122 LOG.debug("Writing a new leaf set entry node");
124 output.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
129 public void startContainerNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
130 Preconditions.checkNotNull(name, "Node identifier should not be null");
132 LOG.debug("Starting a new container node");
134 startNode(name.getNodeType(), NodeTypes.CONTAINER_NODE);
138 public void startUnkeyedList(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
139 Preconditions.checkNotNull(name, "Node identifier should not be null");
140 LOG.debug("Starting a new unkeyed list");
142 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST);
146 public void startUnkeyedListItem(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalStateException {
147 Preconditions.checkNotNull(name, "Node identifier should not be null");
148 LOG.debug("Starting a new unkeyed list item");
150 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST_ITEM);
154 public void startMapNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
155 Preconditions.checkNotNull(name, "Node identifier should not be null");
156 LOG.debug("Starting a new map node");
158 startNode(name.getNodeType(), NodeTypes.MAP_NODE);
162 public void startMapEntryNode(YangInstanceIdentifier.NodeIdentifierWithPredicates identifier, int childSizeHint) throws IOException, IllegalArgumentException {
163 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
164 LOG.debug("Starting a new map entry node");
165 startNode(identifier.getNodeType(), NodeTypes.MAP_ENTRY_NODE);
167 writeKeyValueMap(identifier.getKeyValues());
172 public void startOrderedMapNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
173 Preconditions.checkNotNull(name, "Node identifier should not be null");
174 LOG.debug("Starting a new ordered map node");
176 startNode(name.getNodeType(), NodeTypes.ORDERED_MAP_NODE);
180 public void startChoiceNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
181 Preconditions.checkNotNull(name, "Node identifier should not be null");
182 LOG.debug("Starting a new choice node");
184 startNode(name.getNodeType(), NodeTypes.CHOICE_NODE);
188 public void startAugmentationNode(YangInstanceIdentifier.AugmentationIdentifier identifier) throws IOException, IllegalArgumentException {
189 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
190 LOG.debug("Starting a new augmentation node");
192 output.writeByte(NodeTypes.AUGMENTATION_NODE);
193 writeQNameSet(identifier.getPossibleChildNames());
197 public void anyxmlNode(YangInstanceIdentifier.NodeIdentifier name, Object value) throws IOException, IllegalArgumentException {
198 Preconditions.checkNotNull(name, "Node identifier should not be null");
199 LOG.debug("Writing a new xml node");
201 startNode(name.getNodeType(), NodeTypes.ANY_XML_NODE);
207 public void endNode() throws IOException, IllegalStateException {
208 LOG.debug("Ending the node");
210 output.writeByte(NodeTypes.END_NODE);
214 public void close() throws IOException {
219 public void flush() throws IOException {
220 if (output instanceof OutputStream) {
221 ((OutputStream)output).flush();
225 private void startNode(final QName qName, byte nodeType) throws IOException {
227 Preconditions.checkNotNull(qName, "QName of node identifier should not be null.");
229 writeSignatureMarkerAndVersionIfNeeded();
231 // First write the type of node
232 output.writeByte(nodeType);
237 private void writeQName(QName qName) throws IOException {
239 writeCodedString(qName.getLocalName());
240 writeCodedString(qName.getNamespace().toString());
241 writeCodedString(qName.getFormattedRevision());
244 private void writeCodedString(String key) throws IOException {
245 Integer value = stringCodeMap.get(key);
247 output.writeByte(IS_CODE_VALUE);
248 output.writeInt(value);
251 output.writeByte(IS_STRING_VALUE);
252 stringCodeMap.put(key, Integer.valueOf(stringCodeMap.size()));
253 output.writeUTF(key);
255 output.writeByte(IS_NULL_VALUE);
260 private void writeObjSet(Set<?> set) throws IOException {
262 output.writeInt(set.size());
264 if(o instanceof String){
265 writeCodedString(o.toString());
267 throw new IllegalArgumentException("Expected value type to be String but was : " +
276 public void writeYangInstanceIdentifier(YangInstanceIdentifier identifier) throws IOException {
277 writeSignatureMarkerAndVersionIfNeeded();
278 writeYangInstanceIdentifierInternal(identifier);
281 private void writeYangInstanceIdentifierInternal(YangInstanceIdentifier identifier) throws IOException {
282 Collection<YangInstanceIdentifier.PathArgument> pathArguments = identifier.getPathArguments();
283 output.writeInt(pathArguments.size());
285 for(YangInstanceIdentifier.PathArgument pathArgument : pathArguments) {
286 writePathArgument(pathArgument);
290 public void writePathArgument(YangInstanceIdentifier.PathArgument pathArgument) throws IOException {
292 byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
294 output.writeByte(type);
297 case PathArgumentTypes.NODE_IDENTIFIER :
299 YangInstanceIdentifier.NodeIdentifier nodeIdentifier =
300 (YangInstanceIdentifier.NodeIdentifier) pathArgument;
302 writeQName(nodeIdentifier.getNodeType());
305 case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
307 YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
308 (YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument;
309 writeQName(nodeIdentifierWithPredicates.getNodeType());
311 writeKeyValueMap(nodeIdentifierWithPredicates.getKeyValues());
314 case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
316 YangInstanceIdentifier.NodeWithValue nodeWithValue =
317 (YangInstanceIdentifier.NodeWithValue) pathArgument;
319 writeQName(nodeWithValue.getNodeType());
320 writeObject(nodeWithValue.getValue());
323 case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
325 YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
326 (YangInstanceIdentifier.AugmentationIdentifier) pathArgument;
328 // No Qname in augmentation identifier
329 writeQNameSet(augmentationIdentifier.getPossibleChildNames());
332 throw new IllegalStateException("Unknown node identifier type is found : " + pathArgument.getClass().toString() );
336 private void writeKeyValueMap(Map<QName, Object> keyValueMap) throws IOException {
337 if(keyValueMap != null && !keyValueMap.isEmpty()) {
338 output.writeInt(keyValueMap.size());
339 Set<QName> qNameSet = keyValueMap.keySet();
341 for(QName qName : qNameSet) {
343 writeObject(keyValueMap.get(qName));
350 private void writeQNameSet(Set<QName> children) throws IOException {
351 // Write each child's qname separately, if list is empty send count as 0
352 if(children != null && !children.isEmpty()) {
353 output.writeInt(children.size());
354 for(QName qName : children) {
358 LOG.debug("augmentation node does not have any child");
363 private void writeObject(Object value) throws IOException {
365 byte type = ValueTypes.getSerializableType(value);
366 // Write object type first
367 output.writeByte(type);
370 case ValueTypes.BOOL_TYPE:
371 output.writeBoolean((Boolean) value);
373 case ValueTypes.QNAME_TYPE:
374 writeQName((QName) value);
376 case ValueTypes.INT_TYPE:
377 output.writeInt((Integer) value);
379 case ValueTypes.BYTE_TYPE:
380 output.writeByte((Byte) value);
382 case ValueTypes.LONG_TYPE:
383 output.writeLong((Long) value);
385 case ValueTypes.SHORT_TYPE:
386 output.writeShort((Short) value);
388 case ValueTypes.BITS_TYPE:
389 writeObjSet((Set<?>) value);
391 case ValueTypes.BINARY_TYPE:
392 byte[] bytes = (byte[]) value;
393 output.writeInt(bytes.length);
396 case ValueTypes.YANG_IDENTIFIER_TYPE:
397 writeYangInstanceIdentifierInternal((YangInstanceIdentifier) value);
399 case ValueTypes.NULL_TYPE :
401 case ValueTypes.STRING_BYTES_TYPE:
402 final byte[] valueBytes = value.toString().getBytes(StandardCharsets.UTF_8);
403 output.writeInt(valueBytes.length);
404 output.write(valueBytes);
407 output.writeUTF(value.toString());