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 com.google.common.collect.Iterables;
15 import java.io.DataOutput;
16 import java.io.DataOutputStream;
17 import java.io.IOException;
18 import java.io.OutputStream;
19 import java.util.HashMap;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
26 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
33 * A stream writer wrapper around this class will write node objects to stream in recursive manner.
34 * for example - If you have a ContainerNode which has a two LeafNode as children, then
35 * you will first call {@link #startContainerNode(YangInstanceIdentifier.NodeIdentifier, int)}, then will call
36 * {@link #leafNode(YangInstanceIdentifier.NodeIdentifier, Object)} twice and then, {@link #endNode()} to end
39 * Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
45 public class NormalizedNodeOutputStreamWriter implements NormalizedNodeStreamWriter {
47 private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeOutputStreamWriter.class);
49 static final byte SIGNATURE_MARKER = (byte) 0xab;
50 static final short CURRENT_VERSION = (short) 1;
52 static final byte IS_CODE_VALUE = 1;
53 static final byte IS_STRING_VALUE = 2;
54 static final byte IS_NULL_VALUE = 3;
56 private final DataOutput output;
58 private final Map<String, Integer> stringCodeMap = new HashMap<>();
60 private NormalizedNodeWriter normalizedNodeWriter;
62 private boolean wroteSignatureMarker;
64 public NormalizedNodeOutputStreamWriter(OutputStream stream) throws IOException {
65 Preconditions.checkNotNull(stream);
66 output = new DataOutputStream(stream);
69 public NormalizedNodeOutputStreamWriter(DataOutput output) throws IOException {
70 this.output = Preconditions.checkNotNull(output);
73 private NormalizedNodeWriter normalizedNodeWriter() {
74 if(normalizedNodeWriter == null) {
75 normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(this);
78 return normalizedNodeWriter;
81 public void writeNormalizedNode(NormalizedNode<?, ?> node) throws IOException {
82 writeSignatureMarkerAndVersionIfNeeded();
83 normalizedNodeWriter().write(node);
86 private void writeSignatureMarkerAndVersionIfNeeded() throws IOException {
87 if(!wroteSignatureMarker) {
88 output.writeByte(SIGNATURE_MARKER);
89 output.writeShort(CURRENT_VERSION);
90 wroteSignatureMarker = true;
95 public void leafNode(YangInstanceIdentifier.NodeIdentifier name, Object value) throws IOException, IllegalArgumentException {
96 Preconditions.checkNotNull(name, "Node identifier should not be null");
97 LOG.debug("Writing a new leaf node");
98 startNode(name.getNodeType(), NodeTypes.LEAF_NODE);
104 public void startLeafSet(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
105 Preconditions.checkNotNull(name, "Node identifier should not be null");
106 LOG.debug("Starting a new leaf set");
108 startNode(name.getNodeType(), NodeTypes.LEAF_SET);
112 public void leafSetEntryNode(Object value) throws IOException, IllegalArgumentException {
113 LOG.debug("Writing a new leaf set entry node");
115 output.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
120 public void startContainerNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
121 Preconditions.checkNotNull(name, "Node identifier should not be null");
123 LOG.debug("Starting a new container node");
125 startNode(name.getNodeType(), NodeTypes.CONTAINER_NODE);
129 public void startUnkeyedList(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
130 Preconditions.checkNotNull(name, "Node identifier should not be null");
131 LOG.debug("Starting a new unkeyed list");
133 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST);
137 public void startUnkeyedListItem(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalStateException {
138 Preconditions.checkNotNull(name, "Node identifier should not be null");
139 LOG.debug("Starting a new unkeyed list item");
141 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST_ITEM);
145 public void startMapNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
146 Preconditions.checkNotNull(name, "Node identifier should not be null");
147 LOG.debug("Starting a new map node");
149 startNode(name.getNodeType(), NodeTypes.MAP_NODE);
153 public void startMapEntryNode(YangInstanceIdentifier.NodeIdentifierWithPredicates identifier, int childSizeHint) throws IOException, IllegalArgumentException {
154 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
155 LOG.debug("Starting a new map entry node");
156 startNode(identifier.getNodeType(), NodeTypes.MAP_ENTRY_NODE);
158 writeKeyValueMap(identifier.getKeyValues());
163 public void startOrderedMapNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
164 Preconditions.checkNotNull(name, "Node identifier should not be null");
165 LOG.debug("Starting a new ordered map node");
167 startNode(name.getNodeType(), NodeTypes.ORDERED_MAP_NODE);
171 public void startChoiceNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
172 Preconditions.checkNotNull(name, "Node identifier should not be null");
173 LOG.debug("Starting a new choice node");
175 startNode(name.getNodeType(), NodeTypes.CHOICE_NODE);
179 public void startAugmentationNode(YangInstanceIdentifier.AugmentationIdentifier identifier) throws IOException, IllegalArgumentException {
180 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
181 LOG.debug("Starting a new augmentation node");
183 output.writeByte(NodeTypes.AUGMENTATION_NODE);
184 writeQNameSet(identifier.getPossibleChildNames());
188 public void anyxmlNode(YangInstanceIdentifier.NodeIdentifier name, Object value) throws IOException, IllegalArgumentException {
189 Preconditions.checkNotNull(name, "Node identifier should not be null");
190 LOG.debug("Writing a new xml node");
192 startNode(name.getNodeType(), NodeTypes.ANY_XML_NODE);
198 public void endNode() throws IOException, IllegalStateException {
199 LOG.debug("Ending the node");
201 output.writeByte(NodeTypes.END_NODE);
205 public void close() throws IOException {
209 public void flush() throws IOException {
210 if (output instanceof OutputStream) {
211 ((OutputStream)output).flush();
215 private void startNode(final QName qName, byte nodeType) throws IOException {
217 Preconditions.checkNotNull(qName, "QName of node identifier should not be null.");
219 writeSignatureMarkerAndVersionIfNeeded();
221 // First write the type of node
222 output.writeByte(nodeType);
227 private void writeQName(QName qName) throws IOException {
229 writeCodedString(qName.getLocalName());
230 writeCodedString(qName.getNamespace().toString());
231 writeCodedString(qName.getFormattedRevision());
234 private void writeCodedString(String key) throws IOException {
235 Integer value = stringCodeMap.get(key);
237 output.writeByte(IS_CODE_VALUE);
238 output.writeInt(value);
241 output.writeByte(IS_STRING_VALUE);
242 stringCodeMap.put(key, Integer.valueOf(stringCodeMap.size()));
243 output.writeUTF(key);
245 output.writeByte(IS_NULL_VALUE);
250 private void writeObjSet(Set<?> set) throws IOException {
252 output.writeInt(set.size());
254 if(o instanceof String){
255 writeCodedString(o.toString());
257 throw new IllegalArgumentException("Expected value type to be String but was : " +
266 public void writeYangInstanceIdentifier(YangInstanceIdentifier identifier) throws IOException {
267 writeSignatureMarkerAndVersionIfNeeded();
268 writeYangInstanceIdentifierInternal(identifier);
271 private void writeYangInstanceIdentifierInternal(YangInstanceIdentifier identifier) throws IOException {
272 Iterable<YangInstanceIdentifier.PathArgument> pathArguments = identifier.getPathArguments();
273 int size = Iterables.size(pathArguments);
274 output.writeInt(size);
276 for(YangInstanceIdentifier.PathArgument pathArgument : pathArguments) {
277 writePathArgument(pathArgument);
281 private void writePathArgument(YangInstanceIdentifier.PathArgument pathArgument) throws IOException {
283 byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
285 output.writeByte(type);
288 case PathArgumentTypes.NODE_IDENTIFIER :
290 YangInstanceIdentifier.NodeIdentifier nodeIdentifier =
291 (YangInstanceIdentifier.NodeIdentifier) pathArgument;
293 writeQName(nodeIdentifier.getNodeType());
296 case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
298 YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
299 (YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument;
300 writeQName(nodeIdentifierWithPredicates.getNodeType());
302 writeKeyValueMap(nodeIdentifierWithPredicates.getKeyValues());
305 case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
307 YangInstanceIdentifier.NodeWithValue nodeWithValue =
308 (YangInstanceIdentifier.NodeWithValue) pathArgument;
310 writeQName(nodeWithValue.getNodeType());
311 writeObject(nodeWithValue.getValue());
314 case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
316 YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
317 (YangInstanceIdentifier.AugmentationIdentifier) pathArgument;
319 // No Qname in augmentation identifier
320 writeQNameSet(augmentationIdentifier.getPossibleChildNames());
323 throw new IllegalStateException("Unknown node identifier type is found : " + pathArgument.getClass().toString() );
327 private void writeKeyValueMap(Map<QName, Object> keyValueMap) throws IOException {
328 if(keyValueMap != null && !keyValueMap.isEmpty()) {
329 output.writeInt(keyValueMap.size());
330 Set<QName> qNameSet = keyValueMap.keySet();
332 for(QName qName : qNameSet) {
334 writeObject(keyValueMap.get(qName));
341 private void writeQNameSet(Set<QName> children) throws IOException {
342 // Write each child's qname separately, if list is empty send count as 0
343 if(children != null && !children.isEmpty()) {
344 output.writeInt(children.size());
345 for(QName qName : children) {
349 LOG.debug("augmentation node does not have any child");
354 private void writeObject(Object value) throws IOException {
356 byte type = ValueTypes.getSerializableType(value);
357 // Write object type first
358 output.writeByte(type);
361 case ValueTypes.BOOL_TYPE:
362 output.writeBoolean((Boolean) value);
364 case ValueTypes.QNAME_TYPE:
365 writeQName((QName) value);
367 case ValueTypes.INT_TYPE:
368 output.writeInt((Integer) value);
370 case ValueTypes.BYTE_TYPE:
371 output.writeByte((Byte) value);
373 case ValueTypes.LONG_TYPE:
374 output.writeLong((Long) value);
376 case ValueTypes.SHORT_TYPE:
377 output.writeShort((Short) value);
379 case ValueTypes.BITS_TYPE:
380 writeObjSet((Set<?>) value);
382 case ValueTypes.BINARY_TYPE:
383 byte[] bytes = (byte[]) value;
384 output.writeInt(bytes.length);
387 case ValueTypes.YANG_IDENTIFIER_TYPE:
388 writeYangInstanceIdentifierInternal((YangInstanceIdentifier) value);
390 case ValueTypes.NULL_TYPE :
393 output.writeUTF(value.toString());