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.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.util.HashMap;
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 private final DataOutputStream writer;
49 private final Map<String, Integer> stringCodeMap = new HashMap<>();
51 public NormalizedNodeOutputStreamWriter(OutputStream stream) throws IOException {
52 Preconditions.checkNotNull(stream);
53 writer = new DataOutputStream(stream);
57 public void leafNode(YangInstanceIdentifier.NodeIdentifier name, Object value) throws IOException, IllegalArgumentException {
58 Preconditions.checkNotNull(name, "Node identifier should not be null");
59 LOG.debug("Writing a new leaf node");
60 startNode(name.getNodeType(), NodeTypes.LEAF_NODE);
66 public void startLeafSet(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
67 Preconditions.checkNotNull(name, "Node identifier should not be null");
68 LOG.debug("Starting a new leaf set");
70 startNode(name.getNodeType(), NodeTypes.LEAF_SET);
74 public void leafSetEntryNode(Object value) throws IOException, IllegalArgumentException {
75 LOG.debug("Writing a new leaf set entry node");
77 writer.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
82 public void startContainerNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
83 Preconditions.checkNotNull(name, "Node identifier should not be null");
85 LOG.debug("Starting a new container node");
87 startNode(name.getNodeType(), NodeTypes.CONTAINER_NODE);
91 public void startUnkeyedList(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
92 Preconditions.checkNotNull(name, "Node identifier should not be null");
93 LOG.debug("Starting a new unkeyed list");
95 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST);
99 public void startUnkeyedListItem(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalStateException {
100 Preconditions.checkNotNull(name, "Node identifier should not be null");
101 LOG.debug("Starting a new unkeyed list item");
103 startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST_ITEM);
107 public void startMapNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
108 Preconditions.checkNotNull(name, "Node identifier should not be null");
109 LOG.debug("Starting a new map node");
111 startNode(name.getNodeType(), NodeTypes.MAP_NODE);
115 public void startMapEntryNode(YangInstanceIdentifier.NodeIdentifierWithPredicates identifier, int childSizeHint) throws IOException, IllegalArgumentException {
116 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
117 LOG.debug("Starting a new map entry node");
118 startNode(identifier.getNodeType(), NodeTypes.MAP_ENTRY_NODE);
120 writeKeyValueMap(identifier.getKeyValues());
125 public void startOrderedMapNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
126 Preconditions.checkNotNull(name, "Node identifier should not be null");
127 LOG.debug("Starting a new ordered map node");
129 startNode(name.getNodeType(), NodeTypes.ORDERED_MAP_NODE);
133 public void startChoiceNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
134 Preconditions.checkNotNull(name, "Node identifier should not be null");
135 LOG.debug("Starting a new choice node");
137 startNode(name.getNodeType(), NodeTypes.CHOICE_NODE);
141 public void startAugmentationNode(YangInstanceIdentifier.AugmentationIdentifier identifier) throws IOException, IllegalArgumentException {
142 Preconditions.checkNotNull(identifier, "Node identifier should not be null");
143 LOG.debug("Starting a new augmentation node");
145 writer.writeByte(NodeTypes.AUGMENTATION_NODE);
146 writeQNameSet(identifier.getPossibleChildNames());
150 public void anyxmlNode(YangInstanceIdentifier.NodeIdentifier name, Object value) throws IOException, IllegalArgumentException {
151 Preconditions.checkNotNull(name, "Node identifier should not be null");
152 LOG.debug("Writing a new xml node");
154 startNode(name.getNodeType(), NodeTypes.ANY_XML_NODE);
160 public void endNode() throws IOException, IllegalStateException {
161 LOG.debug("Ending the node");
163 writer.writeByte(NodeTypes.END_NODE);
167 public void close() throws IOException {
172 public void flush() throws IOException {
176 private void startNode(final QName qName, byte nodeType) throws IOException {
178 Preconditions.checkNotNull(qName, "QName of node identifier should not be null.");
179 // First write the type of node
180 writer.writeByte(nodeType);
185 private void writeQName(QName qName) throws IOException {
187 writeCodedString(qName.getLocalName());
188 writeCodedString(qName.getNamespace().toString());
189 writeCodedString(qName.getFormattedRevision());
192 private void writeCodedString(String key) throws IOException {
193 Integer value = stringCodeMap.get(key);
196 writer.writeBoolean(true);
197 writer.writeInt(value);
200 stringCodeMap.put(key, Integer.valueOf(stringCodeMap.size()));
202 writer.writeBoolean(false);
203 writer.writeUTF(key);
207 private void writeObjSet(Set<?> set) throws IOException {
209 writer.writeInt(set.size());
211 if(o instanceof String){
212 writeCodedString(o.toString());
214 throw new IllegalArgumentException("Expected value type to be String but was : " +
223 private void writeYangInstanceIdentifier(YangInstanceIdentifier identifier) throws IOException {
224 Iterable<YangInstanceIdentifier.PathArgument> pathArguments = identifier.getPathArguments();
225 int size = Iterables.size(pathArguments);
226 writer.writeInt(size);
228 for(YangInstanceIdentifier.PathArgument pathArgument : pathArguments) {
229 writePathArgument(pathArgument);
233 private void writePathArgument(YangInstanceIdentifier.PathArgument pathArgument) throws IOException {
235 byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
237 writer.writeByte(type);
240 case PathArgumentTypes.NODE_IDENTIFIER :
242 YangInstanceIdentifier.NodeIdentifier nodeIdentifier =
243 (YangInstanceIdentifier.NodeIdentifier) pathArgument;
245 writeQName(nodeIdentifier.getNodeType());
248 case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
250 YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
251 (YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument;
252 writeQName(nodeIdentifierWithPredicates.getNodeType());
254 writeKeyValueMap(nodeIdentifierWithPredicates.getKeyValues());
257 case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
259 YangInstanceIdentifier.NodeWithValue nodeWithValue =
260 (YangInstanceIdentifier.NodeWithValue) pathArgument;
262 writeQName(nodeWithValue.getNodeType());
263 writeObject(nodeWithValue.getValue());
266 case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
268 YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
269 (YangInstanceIdentifier.AugmentationIdentifier) pathArgument;
271 // No Qname in augmentation identifier
272 writeQNameSet(augmentationIdentifier.getPossibleChildNames());
275 throw new IllegalStateException("Unknown node identifier type is found : " + pathArgument.getClass().toString() );
279 private void writeKeyValueMap(Map<QName, Object> keyValueMap) throws IOException {
280 if(keyValueMap != null && !keyValueMap.isEmpty()) {
281 writer.writeInt(keyValueMap.size());
282 Set<QName> qNameSet = keyValueMap.keySet();
284 for(QName qName : qNameSet) {
286 writeObject(keyValueMap.get(qName));
293 private void writeQNameSet(Set<QName> children) throws IOException {
294 // Write each child's qname separately, if list is empty send count as 0
295 if(children != null && !children.isEmpty()) {
296 writer.writeInt(children.size());
297 for(QName qName : children) {
301 LOG.debug("augmentation node does not have any child");
306 private void writeObject(Object value) throws IOException {
308 byte type = ValueTypes.getSerializableType(value);
309 // Write object type first
310 writer.writeByte(type);
313 case ValueTypes.BOOL_TYPE:
314 writer.writeBoolean((Boolean) value);
316 case ValueTypes.QNAME_TYPE:
317 writeQName((QName) value);
319 case ValueTypes.INT_TYPE:
320 writer.writeInt((Integer) value);
322 case ValueTypes.BYTE_TYPE:
323 writer.writeByte((Byte) value);
325 case ValueTypes.LONG_TYPE:
326 writer.writeLong((Long) value);
328 case ValueTypes.SHORT_TYPE:
329 writer.writeShort((Short) value);
331 case ValueTypes.BITS_TYPE:
332 writeObjSet((Set<?>) value);
334 case ValueTypes.YANG_IDENTIFIER_TYPE:
335 writeYangInstanceIdentifier((YangInstanceIdentifier) value);
338 writer.writeUTF(value.toString());