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) {
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 {
210 public void flush() throws IOException {
211 if (output instanceof OutputStream) {
212 ((OutputStream)output).flush();
216 private void startNode(final QName qName, byte nodeType) throws IOException {
218 Preconditions.checkNotNull(qName, "QName of node identifier should not be null.");
220 writeSignatureMarkerAndVersionIfNeeded();
222 // First write the type of node
223 output.writeByte(nodeType);
228 private void writeQName(QName qName) throws IOException {
230 writeCodedString(qName.getLocalName());
231 writeCodedString(qName.getNamespace().toString());
232 writeCodedString(qName.getFormattedRevision());
235 private void writeCodedString(String key) throws IOException {
236 Integer value = stringCodeMap.get(key);
238 output.writeByte(IS_CODE_VALUE);
239 output.writeInt(value);
242 output.writeByte(IS_STRING_VALUE);
243 stringCodeMap.put(key, Integer.valueOf(stringCodeMap.size()));
244 output.writeUTF(key);
246 output.writeByte(IS_NULL_VALUE);
251 private void writeObjSet(Set<?> set) throws IOException {
253 output.writeInt(set.size());
255 if(o instanceof String){
256 writeCodedString(o.toString());
258 throw new IllegalArgumentException("Expected value type to be String but was : " +
267 public void writeYangInstanceIdentifier(YangInstanceIdentifier identifier) throws IOException {
268 writeSignatureMarkerAndVersionIfNeeded();
269 writeYangInstanceIdentifierInternal(identifier);
272 private void writeYangInstanceIdentifierInternal(YangInstanceIdentifier identifier) throws IOException {
273 Iterable<YangInstanceIdentifier.PathArgument> pathArguments = identifier.getPathArguments();
274 int size = Iterables.size(pathArguments);
275 output.writeInt(size);
277 for(YangInstanceIdentifier.PathArgument pathArgument : pathArguments) {
278 writePathArgument(pathArgument);
282 public void writePathArgument(YangInstanceIdentifier.PathArgument pathArgument) throws IOException {
284 byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
286 output.writeByte(type);
289 case PathArgumentTypes.NODE_IDENTIFIER :
291 YangInstanceIdentifier.NodeIdentifier nodeIdentifier =
292 (YangInstanceIdentifier.NodeIdentifier) pathArgument;
294 writeQName(nodeIdentifier.getNodeType());
297 case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
299 YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
300 (YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument;
301 writeQName(nodeIdentifierWithPredicates.getNodeType());
303 writeKeyValueMap(nodeIdentifierWithPredicates.getKeyValues());
306 case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
308 YangInstanceIdentifier.NodeWithValue nodeWithValue =
309 (YangInstanceIdentifier.NodeWithValue) pathArgument;
311 writeQName(nodeWithValue.getNodeType());
312 writeObject(nodeWithValue.getValue());
315 case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
317 YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
318 (YangInstanceIdentifier.AugmentationIdentifier) pathArgument;
320 // No Qname in augmentation identifier
321 writeQNameSet(augmentationIdentifier.getPossibleChildNames());
324 throw new IllegalStateException("Unknown node identifier type is found : " + pathArgument.getClass().toString() );
328 private void writeKeyValueMap(Map<QName, Object> keyValueMap) throws IOException {
329 if(keyValueMap != null && !keyValueMap.isEmpty()) {
330 output.writeInt(keyValueMap.size());
331 Set<QName> qNameSet = keyValueMap.keySet();
333 for(QName qName : qNameSet) {
335 writeObject(keyValueMap.get(qName));
342 private void writeQNameSet(Set<QName> children) throws IOException {
343 // Write each child's qname separately, if list is empty send count as 0
344 if(children != null && !children.isEmpty()) {
345 output.writeInt(children.size());
346 for(QName qName : children) {
350 LOG.debug("augmentation node does not have any child");
355 private void writeObject(Object value) throws IOException {
357 byte type = ValueTypes.getSerializableType(value);
358 // Write object type first
359 output.writeByte(type);
362 case ValueTypes.BOOL_TYPE:
363 output.writeBoolean((Boolean) value);
365 case ValueTypes.QNAME_TYPE:
366 writeQName((QName) value);
368 case ValueTypes.INT_TYPE:
369 output.writeInt((Integer) value);
371 case ValueTypes.BYTE_TYPE:
372 output.writeByte((Byte) value);
374 case ValueTypes.LONG_TYPE:
375 output.writeLong((Long) value);
377 case ValueTypes.SHORT_TYPE:
378 output.writeShort((Short) value);
380 case ValueTypes.BITS_TYPE:
381 writeObjSet((Set<?>) value);
383 case ValueTypes.BINARY_TYPE:
384 byte[] bytes = (byte[]) value;
385 output.writeInt(bytes.length);
388 case ValueTypes.YANG_IDENTIFIER_TYPE:
389 writeYangInstanceIdentifierInternal((YangInstanceIdentifier) value);
391 case ValueTypes.NULL_TYPE :
394 output.writeUTF(value.toString());