2 * Copyright (c) 2014, 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
9 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
11 import com.google.common.base.Preconditions;
12 import java.io.DataOutput;
13 import java.io.DataOutputStream;
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import java.nio.charset.StandardCharsets;
17 import java.util.Collection;
18 import java.util.HashMap;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
25 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
32 * A stream writer wrapper around this class will write node objects to stream in recursive manner.
33 * for example - If you have a ContainerNode which has a two LeafNode as children, then
34 * you will first call {@link #startContainerNode(YangInstanceIdentifier.NodeIdentifier, int)}, then will call
35 * {@link #leafNode(YangInstanceIdentifier.NodeIdentifier, Object)} twice and then, {@link #endNode()} to end
38 * Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
44 public class NormalizedNodeOutputStreamWriter implements NormalizedNodeStreamWriter {
46 private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeOutputStreamWriter.class);
48 static final byte SIGNATURE_MARKER = (byte) 0xab;
49 static final short CURRENT_VERSION = (short) 1;
51 static final byte IS_CODE_VALUE = 1;
52 static final byte IS_STRING_VALUE = 2;
53 static final byte IS_NULL_VALUE = 3;
55 private final DataOutput output;
57 private final Map<String, Integer> stringCodeMap = new HashMap<>();
59 private NormalizedNodeWriter normalizedNodeWriter;
61 private boolean wroteSignatureMarker;
64 * @deprecated Use {@link #NormalizedNodeOutputStreamWriter(DataOutput)} instead.
67 public NormalizedNodeOutputStreamWriter(final OutputStream stream) throws IOException {
68 this((DataOutput) new DataOutputStream(Preconditions.checkNotNull(stream)));
71 public NormalizedNodeOutputStreamWriter(DataOutput output) {
72 this.output = Preconditions.checkNotNull(output);
75 private NormalizedNodeWriter normalizedNodeWriter() {
76 if(normalizedNodeWriter == null) {
77 normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(this);
80 return normalizedNodeWriter;
83 public void writeNormalizedNode(NormalizedNode<?, ?> node) throws IOException {
84 writeSignatureMarkerAndVersionIfNeeded();
85 normalizedNodeWriter().write(node);
88 private void writeSignatureMarkerAndVersionIfNeeded() throws IOException {
89 if(!wroteSignatureMarker) {
90 output.writeByte(SIGNATURE_MARKER);
91 output.writeShort(CURRENT_VERSION);
92 wroteSignatureMarker = true;
97 public void leafNode(YangInstanceIdentifier.NodeIdentifier name, Object value) throws IOException, IllegalArgumentException {
98 Preconditions.checkNotNull(name, "Node identifier should not be null");
99 LOG.debug("Writing a new leaf node");
100 startNode(name.getNodeType(), NodeTypes.LEAF_NODE);
106 public void startLeafSet(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
107 Preconditions.checkNotNull(name, "Node identifier should not be null");
108 LOG.debug("Starting a new leaf set");
110 startNode(name.getNodeType(), NodeTypes.LEAF_SET);
114 public void leafSetEntryNode(Object value) throws IOException, IllegalArgumentException {
115 LOG.debug("Writing a new leaf set entry node");
117 output.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
122 public void startContainerNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
123 Preconditions.checkNotNull(name, "Node identifier should not be null");
125 LOG.debug("Starting a new container node");
127 startNode(name.getNodeType(), NodeTypes.CONTAINER_NODE);
131 public void startYangModeledAnyXmlNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
132 Preconditions.checkNotNull(name, "Node identifier should not be null");
134 LOG.debug("Starting a new yang modeled anyXml node");
136 startNode(name.getNodeType(), NodeTypes.YANG_MODELED_ANY_XML_NODE);
140 public void startUnkeyedList(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
141 Preconditions.checkNotNull(name, "Node identifier should not be null");
142 LOG.debug("Starting a new unkeyed list");
144 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST);
148 public void startUnkeyedListItem(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalStateException {
149 Preconditions.checkNotNull(name, "Node identifier should not be null");
150 LOG.debug("Starting a new unkeyed list item");
152 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST_ITEM);
156 public void startMapNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
157 Preconditions.checkNotNull(name, "Node identifier should not be null");
158 LOG.debug("Starting a new map node");
160 startNode(name.getNodeType(), NodeTypes.MAP_NODE);
164 public void startMapEntryNode(YangInstanceIdentifier.NodeIdentifierWithPredicates identifier, int childSizeHint) throws IOException, IllegalArgumentException {
165 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
166 LOG.debug("Starting a new map entry node");
167 startNode(identifier.getNodeType(), NodeTypes.MAP_ENTRY_NODE);
169 writeKeyValueMap(identifier.getKeyValues());
174 public void startOrderedMapNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
175 Preconditions.checkNotNull(name, "Node identifier should not be null");
176 LOG.debug("Starting a new ordered map node");
178 startNode(name.getNodeType(), NodeTypes.ORDERED_MAP_NODE);
182 public void startChoiceNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
183 Preconditions.checkNotNull(name, "Node identifier should not be null");
184 LOG.debug("Starting a new choice node");
186 startNode(name.getNodeType(), NodeTypes.CHOICE_NODE);
190 public void startAugmentationNode(YangInstanceIdentifier.AugmentationIdentifier identifier) throws IOException, IllegalArgumentException {
191 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
192 LOG.debug("Starting a new augmentation node");
194 output.writeByte(NodeTypes.AUGMENTATION_NODE);
195 writeQNameSet(identifier.getPossibleChildNames());
199 public void anyxmlNode(YangInstanceIdentifier.NodeIdentifier name, Object value) throws IOException, IllegalArgumentException {
200 Preconditions.checkNotNull(name, "Node identifier should not be null");
201 LOG.debug("Writing a new xml node");
203 startNode(name.getNodeType(), NodeTypes.ANY_XML_NODE);
209 public void endNode() throws IOException, IllegalStateException {
210 LOG.debug("Ending the node");
212 output.writeByte(NodeTypes.END_NODE);
216 public void close() throws IOException {
221 public void flush() throws IOException {
222 if (output instanceof OutputStream) {
223 ((OutputStream)output).flush();
227 private void startNode(final QName qName, byte nodeType) throws IOException {
229 Preconditions.checkNotNull(qName, "QName of node identifier should not be null.");
231 writeSignatureMarkerAndVersionIfNeeded();
233 // First write the type of node
234 output.writeByte(nodeType);
239 private void writeQName(QName qName) throws IOException {
241 writeCodedString(qName.getLocalName());
242 writeCodedString(qName.getNamespace().toString());
243 writeCodedString(qName.getFormattedRevision());
246 private void writeCodedString(String key) throws IOException {
247 Integer value = stringCodeMap.get(key);
249 output.writeByte(IS_CODE_VALUE);
250 output.writeInt(value);
253 output.writeByte(IS_STRING_VALUE);
254 stringCodeMap.put(key, Integer.valueOf(stringCodeMap.size()));
255 output.writeUTF(key);
257 output.writeByte(IS_NULL_VALUE);
262 private void writeObjSet(Set<?> set) throws IOException {
264 output.writeInt(set.size());
266 if(o instanceof String){
267 writeCodedString(o.toString());
269 throw new IllegalArgumentException("Expected value type to be String but was : " +
278 public void writeYangInstanceIdentifier(YangInstanceIdentifier identifier) throws IOException {
279 writeSignatureMarkerAndVersionIfNeeded();
280 writeYangInstanceIdentifierInternal(identifier);
283 private void writeYangInstanceIdentifierInternal(YangInstanceIdentifier identifier) throws IOException {
284 Collection<YangInstanceIdentifier.PathArgument> pathArguments = identifier.getPathArguments();
285 output.writeInt(pathArguments.size());
287 for(YangInstanceIdentifier.PathArgument pathArgument : pathArguments) {
288 writePathArgument(pathArgument);
292 public void writePathArgument(YangInstanceIdentifier.PathArgument pathArgument) throws IOException {
294 byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
296 output.writeByte(type);
299 case PathArgumentTypes.NODE_IDENTIFIER :
301 YangInstanceIdentifier.NodeIdentifier nodeIdentifier =
302 (YangInstanceIdentifier.NodeIdentifier) pathArgument;
304 writeQName(nodeIdentifier.getNodeType());
307 case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
309 YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
310 (YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument;
311 writeQName(nodeIdentifierWithPredicates.getNodeType());
313 writeKeyValueMap(nodeIdentifierWithPredicates.getKeyValues());
316 case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
318 YangInstanceIdentifier.NodeWithValue nodeWithValue =
319 (YangInstanceIdentifier.NodeWithValue) pathArgument;
321 writeQName(nodeWithValue.getNodeType());
322 writeObject(nodeWithValue.getValue());
325 case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
327 YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
328 (YangInstanceIdentifier.AugmentationIdentifier) pathArgument;
330 // No Qname in augmentation identifier
331 writeQNameSet(augmentationIdentifier.getPossibleChildNames());
334 throw new IllegalStateException("Unknown node identifier type is found : " + pathArgument.getClass().toString() );
338 private void writeKeyValueMap(Map<QName, Object> keyValueMap) throws IOException {
339 if(keyValueMap != null && !keyValueMap.isEmpty()) {
340 output.writeInt(keyValueMap.size());
341 Set<QName> qNameSet = keyValueMap.keySet();
343 for(QName qName : qNameSet) {
345 writeObject(keyValueMap.get(qName));
352 private void writeQNameSet(Set<QName> children) throws IOException {
353 // Write each child's qname separately, if list is empty send count as 0
354 if(children != null && !children.isEmpty()) {
355 output.writeInt(children.size());
356 for(QName qName : children) {
360 LOG.debug("augmentation node does not have any child");
365 private void writeObject(Object value) throws IOException {
367 byte type = ValueTypes.getSerializableType(value);
368 // Write object type first
369 output.writeByte(type);
372 case ValueTypes.BOOL_TYPE:
373 output.writeBoolean((Boolean) value);
375 case ValueTypes.QNAME_TYPE:
376 writeQName((QName) value);
378 case ValueTypes.INT_TYPE:
379 output.writeInt((Integer) value);
381 case ValueTypes.BYTE_TYPE:
382 output.writeByte((Byte) value);
384 case ValueTypes.LONG_TYPE:
385 output.writeLong((Long) value);
387 case ValueTypes.SHORT_TYPE:
388 output.writeShort((Short) value);
390 case ValueTypes.BITS_TYPE:
391 writeObjSet((Set<?>) value);
393 case ValueTypes.BINARY_TYPE:
394 byte[] bytes = (byte[]) value;
395 output.writeInt(bytes.length);
398 case ValueTypes.YANG_IDENTIFIER_TYPE:
399 writeYangInstanceIdentifierInternal((YangInstanceIdentifier) value);
401 case ValueTypes.NULL_TYPE :
403 case ValueTypes.STRING_BYTES_TYPE:
404 final byte[] valueBytes = value.toString().getBytes(StandardCharsets.UTF_8);
405 output.writeInt(valueBytes.length);
406 output.write(valueBytes);
409 output.writeUTF(value.toString());