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.stream.NormalizedNodeStreamWriter;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
31 * A stream writer wrapper around this class will write node objects to stream in recursive manner.
32 * for example - If you have a ContainerNode which has a two LeafNode as children, then
33 * you will first call {@link #startContainerNode(YangInstanceIdentifier.NodeIdentifier, int)}, then will call
34 * {@link #leafNode(YangInstanceIdentifier.NodeIdentifier, Object)} twice and then, {@link #endNode()} to end
37 * Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
43 public class NormalizedNodeOutputStreamWriter implements NormalizedNodeStreamWriter {
45 private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeOutputStreamWriter.class);
47 static final byte IS_CODE_VALUE = 1;
48 static final byte IS_STRING_VALUE = 2;
49 static final byte IS_NULL_VALUE = 3;
51 private final DataOutput output;
53 private final Map<String, Integer> stringCodeMap = new HashMap<>();
55 public NormalizedNodeOutputStreamWriter(OutputStream stream) throws IOException {
56 Preconditions.checkNotNull(stream);
57 output = new DataOutputStream(stream);
60 public NormalizedNodeOutputStreamWriter(DataOutput output) throws IOException {
61 this.output = Preconditions.checkNotNull(output);
65 public void leafNode(YangInstanceIdentifier.NodeIdentifier name, Object value) throws IOException, IllegalArgumentException {
66 Preconditions.checkNotNull(name, "Node identifier should not be null");
67 LOG.debug("Writing a new leaf node");
68 startNode(name.getNodeType(), NodeTypes.LEAF_NODE);
74 public void startLeafSet(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
75 Preconditions.checkNotNull(name, "Node identifier should not be null");
76 LOG.debug("Starting a new leaf set");
78 startNode(name.getNodeType(), NodeTypes.LEAF_SET);
82 public void leafSetEntryNode(Object value) throws IOException, IllegalArgumentException {
83 LOG.debug("Writing a new leaf set entry node");
85 output.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
90 public void startContainerNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
91 Preconditions.checkNotNull(name, "Node identifier should not be null");
93 LOG.debug("Starting a new container node");
95 startNode(name.getNodeType(), NodeTypes.CONTAINER_NODE);
99 public void startUnkeyedList(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
100 Preconditions.checkNotNull(name, "Node identifier should not be null");
101 LOG.debug("Starting a new unkeyed list");
103 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST);
107 public void startUnkeyedListItem(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalStateException {
108 Preconditions.checkNotNull(name, "Node identifier should not be null");
109 LOG.debug("Starting a new unkeyed list item");
111 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST_ITEM);
115 public void startMapNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
116 Preconditions.checkNotNull(name, "Node identifier should not be null");
117 LOG.debug("Starting a new map node");
119 startNode(name.getNodeType(), NodeTypes.MAP_NODE);
123 public void startMapEntryNode(YangInstanceIdentifier.NodeIdentifierWithPredicates identifier, int childSizeHint) throws IOException, IllegalArgumentException {
124 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
125 LOG.debug("Starting a new map entry node");
126 startNode(identifier.getNodeType(), NodeTypes.MAP_ENTRY_NODE);
128 writeKeyValueMap(identifier.getKeyValues());
133 public void startOrderedMapNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
134 Preconditions.checkNotNull(name, "Node identifier should not be null");
135 LOG.debug("Starting a new ordered map node");
137 startNode(name.getNodeType(), NodeTypes.ORDERED_MAP_NODE);
141 public void startChoiceNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
142 Preconditions.checkNotNull(name, "Node identifier should not be null");
143 LOG.debug("Starting a new choice node");
145 startNode(name.getNodeType(), NodeTypes.CHOICE_NODE);
149 public void startAugmentationNode(YangInstanceIdentifier.AugmentationIdentifier identifier) throws IOException, IllegalArgumentException {
150 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
151 LOG.debug("Starting a new augmentation node");
153 output.writeByte(NodeTypes.AUGMENTATION_NODE);
154 writeQNameSet(identifier.getPossibleChildNames());
158 public void anyxmlNode(YangInstanceIdentifier.NodeIdentifier name, Object value) throws IOException, IllegalArgumentException {
159 Preconditions.checkNotNull(name, "Node identifier should not be null");
160 LOG.debug("Writing a new xml node");
162 startNode(name.getNodeType(), NodeTypes.ANY_XML_NODE);
168 public void endNode() throws IOException, IllegalStateException {
169 LOG.debug("Ending the node");
171 output.writeByte(NodeTypes.END_NODE);
175 public void close() throws IOException {
179 public void flush() throws IOException {
180 if (output instanceof OutputStream) {
181 ((OutputStream)output).flush();
185 private void startNode(final QName qName, byte nodeType) throws IOException {
187 Preconditions.checkNotNull(qName, "QName of node identifier should not be null.");
188 // First write the type of node
189 output.writeByte(nodeType);
194 private void writeQName(QName qName) throws IOException {
196 writeCodedString(qName.getLocalName());
197 writeCodedString(qName.getNamespace().toString());
198 writeCodedString(qName.getFormattedRevision());
201 private void writeCodedString(String key) throws IOException {
202 Integer value = stringCodeMap.get(key);
204 output.writeByte(IS_CODE_VALUE);
205 output.writeInt(value);
208 output.writeByte(IS_STRING_VALUE);
209 stringCodeMap.put(key, Integer.valueOf(stringCodeMap.size()));
210 output.writeUTF(key);
212 output.writeByte(IS_NULL_VALUE);
217 private void writeObjSet(Set<?> set) throws IOException {
219 output.writeInt(set.size());
221 if(o instanceof String){
222 writeCodedString(o.toString());
224 throw new IllegalArgumentException("Expected value type to be String but was : " +
233 public void writeYangInstanceIdentifier(YangInstanceIdentifier identifier) throws IOException {
234 Iterable<YangInstanceIdentifier.PathArgument> pathArguments = identifier.getPathArguments();
235 int size = Iterables.size(pathArguments);
236 output.writeInt(size);
238 for(YangInstanceIdentifier.PathArgument pathArgument : pathArguments) {
239 writePathArgument(pathArgument);
243 private void writePathArgument(YangInstanceIdentifier.PathArgument pathArgument) throws IOException {
245 byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
247 output.writeByte(type);
250 case PathArgumentTypes.NODE_IDENTIFIER :
252 YangInstanceIdentifier.NodeIdentifier nodeIdentifier =
253 (YangInstanceIdentifier.NodeIdentifier) pathArgument;
255 writeQName(nodeIdentifier.getNodeType());
258 case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
260 YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
261 (YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument;
262 writeQName(nodeIdentifierWithPredicates.getNodeType());
264 writeKeyValueMap(nodeIdentifierWithPredicates.getKeyValues());
267 case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
269 YangInstanceIdentifier.NodeWithValue nodeWithValue =
270 (YangInstanceIdentifier.NodeWithValue) pathArgument;
272 writeQName(nodeWithValue.getNodeType());
273 writeObject(nodeWithValue.getValue());
276 case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
278 YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
279 (YangInstanceIdentifier.AugmentationIdentifier) pathArgument;
281 // No Qname in augmentation identifier
282 writeQNameSet(augmentationIdentifier.getPossibleChildNames());
285 throw new IllegalStateException("Unknown node identifier type is found : " + pathArgument.getClass().toString() );
289 private void writeKeyValueMap(Map<QName, Object> keyValueMap) throws IOException {
290 if(keyValueMap != null && !keyValueMap.isEmpty()) {
291 output.writeInt(keyValueMap.size());
292 Set<QName> qNameSet = keyValueMap.keySet();
294 for(QName qName : qNameSet) {
296 writeObject(keyValueMap.get(qName));
303 private void writeQNameSet(Set<QName> children) throws IOException {
304 // Write each child's qname separately, if list is empty send count as 0
305 if(children != null && !children.isEmpty()) {
306 output.writeInt(children.size());
307 for(QName qName : children) {
311 LOG.debug("augmentation node does not have any child");
316 private void writeObject(Object value) throws IOException {
318 byte type = ValueTypes.getSerializableType(value);
319 // Write object type first
320 output.writeByte(type);
323 case ValueTypes.BOOL_TYPE:
324 output.writeBoolean((Boolean) value);
326 case ValueTypes.QNAME_TYPE:
327 writeQName((QName) value);
329 case ValueTypes.INT_TYPE:
330 output.writeInt((Integer) value);
332 case ValueTypes.BYTE_TYPE:
333 output.writeByte((Byte) value);
335 case ValueTypes.LONG_TYPE:
336 output.writeLong((Long) value);
338 case ValueTypes.SHORT_TYPE:
339 output.writeShort((Short) value);
341 case ValueTypes.BITS_TYPE:
342 writeObjSet((Set<?>) value);
344 case ValueTypes.BINARY_TYPE:
345 byte[] bytes = (byte[]) value;
346 output.writeInt(bytes.length);
349 case ValueTypes.YANG_IDENTIFIER_TYPE:
350 writeYangInstanceIdentifier((YangInstanceIdentifier) value);
352 case ValueTypes.NULL_TYPE :
355 output.writeUTF(value.toString());