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 org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import java.io.DataOutputStream;
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.util.HashMap;
28 * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
30 * A stream writer wrapper around this class will write node objects to stream in recursive manner.
31 * for example - If you have a ContainerNode which has a two LeafNode as children, then
32 * you will first call {@link #startContainerNode(YangInstanceIdentifier.NodeIdentifier, int)}, then will call
33 * {@link #leafNode(YangInstanceIdentifier.NodeIdentifier, Object)} twice and then, {@link #endNode()} to end
36 * Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
42 public class NormalizedNodeOutputStreamWriter implements NormalizedNodeStreamWriter{
44 private DataOutputStream writer;
46 private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeOutputStreamWriter.class);
48 private Map<String, Integer> stringCodeMap = new HashMap<>();
50 public NormalizedNodeOutputStreamWriter(OutputStream stream) throws IOException {
51 Preconditions.checkNotNull(stream);
52 writer = new DataOutputStream(stream);
56 public void leafNode(YangInstanceIdentifier.NodeIdentifier name, Object value) throws IOException, IllegalArgumentException {
57 Preconditions.checkNotNull(name, "Node identifier should not be null");
58 LOG.debug("Writing a new leaf node");
59 startNode(name.getNodeType(), NodeTypes.LEAF_NODE);
65 public void startLeafSet(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
66 Preconditions.checkNotNull(name, "Node identifier should not be null");
67 LOG.debug("Starting a new leaf set");
69 startNode(name.getNodeType(), NodeTypes.LEAF_SET);
73 public void leafSetEntryNode(YangInstanceIdentifier.NodeWithValue name, Object value) throws IOException, IllegalArgumentException {
74 Preconditions.checkNotNull(name, "Node identifier should not be null");
76 LOG.debug("Writing a new leaf set entry node");
77 startNode(name.getNodeType(), NodeTypes.LEAF_SET_ENTRY_NODE);
83 public void startContainerNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
84 Preconditions.checkNotNull(name, "Node identifier should not be null");
86 LOG.debug("Starting a new container node");
88 startNode(name.getNodeType(), NodeTypes.CONTAINER_NODE);
92 public void startUnkeyedList(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
93 Preconditions.checkNotNull(name, "Node identifier should not be null");
94 LOG.debug("Starting a new unkeyed list");
96 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST);
100 public void startUnkeyedListItem(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalStateException {
101 Preconditions.checkNotNull(name, "Node identifier should not be null");
102 LOG.debug("Starting a new unkeyed list item");
104 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST_ITEM);
108 public void startMapNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
109 Preconditions.checkNotNull(name, "Node identifier should not be null");
110 LOG.debug("Starting a new map node");
112 startNode(name.getNodeType(), NodeTypes.MAP_NODE);
116 public void startMapEntryNode(YangInstanceIdentifier.NodeIdentifierWithPredicates identifier, int childSizeHint) throws IOException, IllegalArgumentException {
117 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
118 LOG.debug("Starting a new map entry node");
119 startNode(identifier.getNodeType(), NodeTypes.MAP_ENTRY_NODE);
121 writeKeyValueMap(identifier.getKeyValues());
126 public void startOrderedMapNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
127 Preconditions.checkNotNull(name, "Node identifier should not be null");
128 LOG.debug("Starting a new ordered map node");
130 startNode(name.getNodeType(), NodeTypes.ORDERED_MAP_NODE);
134 public void startChoiceNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
135 Preconditions.checkNotNull(name, "Node identifier should not be null");
136 LOG.debug("Starting a new choice node");
138 startNode(name.getNodeType(), NodeTypes.CHOICE_NODE);
142 public void startAugmentationNode(YangInstanceIdentifier.AugmentationIdentifier identifier) throws IOException, IllegalArgumentException {
143 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
144 LOG.debug("Starting a new augmentation node");
146 writer.writeByte(NodeTypes.AUGMENTATION_NODE);
147 writeQNameSet(identifier.getPossibleChildNames());
151 public void anyxmlNode(YangInstanceIdentifier.NodeIdentifier name, Object value) throws IOException, IllegalArgumentException {
152 Preconditions.checkNotNull(name, "Node identifier should not be null");
153 LOG.debug("Writing a new xml node");
155 startNode(name.getNodeType(), NodeTypes.ANY_XML_NODE);
161 public void endNode() throws IOException, IllegalStateException {
162 LOG.debug("Ending the node");
164 writer.writeByte(NodeTypes.END_NODE);
168 public void close() throws IOException {
173 public void flush() throws IOException {
177 private void startNode(final QName qName, byte nodeType) throws IOException {
179 Preconditions.checkNotNull(qName, "QName of node identifier should not be null.");
180 // First write the type of node
181 writer.writeByte(nodeType);
186 private void writeQName(QName qName) throws IOException {
188 writeCodedString(qName.getLocalName());
189 writeCodedString(qName.getNamespace().toString());
190 writeCodedString(qName.getFormattedRevision());
193 private void writeCodedString(String key) throws IOException {
194 Integer value = stringCodeMap.get(key);
197 writer.writeBoolean(true);
198 writer.writeInt(value);
201 stringCodeMap.put(key, Integer.valueOf(stringCodeMap.size()));
203 writer.writeBoolean(false);
204 writer.writeUTF(key);
208 private void writeObjSet(Set set) throws IOException {
210 writer.writeInt(set.size());
212 if(o instanceof String){
213 writeCodedString(o.toString());
215 throw new IllegalArgumentException("Expected value type to be String but was : " +
224 private void writeYangInstanceIdentifier(YangInstanceIdentifier identifier) throws IOException {
225 Iterable<YangInstanceIdentifier.PathArgument> pathArguments = identifier.getPathArguments();
226 int size = Iterables.size(pathArguments);
227 writer.writeInt(size);
229 for(YangInstanceIdentifier.PathArgument pathArgument : pathArguments) {
230 writePathArgument(pathArgument);
234 private void writePathArgument(YangInstanceIdentifier.PathArgument pathArgument) throws IOException {
236 byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
238 writer.writeByte(type);
241 case PathArgumentTypes.NODE_IDENTIFIER :
243 YangInstanceIdentifier.NodeIdentifier nodeIdentifier =
244 (YangInstanceIdentifier.NodeIdentifier) pathArgument;
246 writeQName(nodeIdentifier.getNodeType());
249 case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
251 YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
252 (YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument;
253 writeQName(nodeIdentifierWithPredicates.getNodeType());
255 writeKeyValueMap(nodeIdentifierWithPredicates.getKeyValues());
258 case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
260 YangInstanceIdentifier.NodeWithValue nodeWithValue =
261 (YangInstanceIdentifier.NodeWithValue) pathArgument;
263 writeQName(nodeWithValue.getNodeType());
264 writeObject(nodeWithValue.getValue());
267 case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
269 YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
270 (YangInstanceIdentifier.AugmentationIdentifier) pathArgument;
272 // No Qname in augmentation identifier
273 writeQNameSet(augmentationIdentifier.getPossibleChildNames());
276 throw new IllegalStateException("Unknown node identifier type is found : " + pathArgument.getClass().toString() );
280 private void writeKeyValueMap(Map<QName, Object> keyValueMap) throws IOException {
281 if(keyValueMap != null && !keyValueMap.isEmpty()) {
282 writer.writeInt(keyValueMap.size());
283 Set<QName> qNameSet = keyValueMap.keySet();
285 for(QName qName : qNameSet) {
287 writeObject(keyValueMap.get(qName));
294 private void writeQNameSet(Set<QName> children) throws IOException {
295 // Write each child's qname separately, if list is empty send count as 0
296 if(children != null && !children.isEmpty()) {
297 writer.writeInt(children.size());
298 for(QName qName : children) {
302 LOG.debug("augmentation node does not have any child");
307 private void writeObject(Object value) throws IOException {
309 byte type = ValueTypes.getSerializableType(value);
310 // Write object type first
311 writer.writeByte(type);
314 case ValueTypes.BOOL_TYPE:
315 writer.writeBoolean((Boolean) value);
317 case ValueTypes.QNAME_TYPE:
318 writeQName((QName) value);
320 case ValueTypes.INT_TYPE:
321 writer.writeInt((Integer) value);
323 case ValueTypes.BYTE_TYPE:
324 writer.writeByte((Byte) value);
326 case ValueTypes.LONG_TYPE:
327 writer.writeLong((Long) value);
329 case ValueTypes.SHORT_TYPE:
330 writer.writeShort((Short) value);
332 case ValueTypes.BITS_TYPE:
333 writeObjSet((Set) value);
335 case ValueTypes.YANG_IDENTIFIER_TYPE:
336 writeYangInstanceIdentifier((YangInstanceIdentifier) value);
339 writer.writeUTF(value.toString());