Fix non-generic references to NormalizedNode
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / NormalizedNodeOutputStreamWriter.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
12
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;
20
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Set;
27
28 /**
29  * NormalizedNodeOutputStreamWriter will be used by distributed datastore to send normalized node in
30  * a stream.
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
35  * container node.
36  *
37  * Based on the each node, the node type is also written to the stream, that helps in reconstructing the object,
38  * while reading.
39  *
40  *
41  */
42
43 public class NormalizedNodeOutputStreamWriter implements NormalizedNodeStreamWriter{
44
45     private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeOutputStreamWriter.class);
46
47     private final DataOutputStream writer;
48
49     private final Map<String, Integer> stringCodeMap = new HashMap<>();
50
51     public NormalizedNodeOutputStreamWriter(OutputStream stream) throws IOException {
52         Preconditions.checkNotNull(stream);
53         writer = new DataOutputStream(stream);
54     }
55
56     @Override
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);
61
62         writeObject(value);
63     }
64
65     @Override
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");
69
70         startNode(name.getNodeType(), NodeTypes.LEAF_SET);
71     }
72
73     @Override
74     public void leafSetEntryNode(Object value) throws IOException, IllegalArgumentException {
75         LOG.debug("Writing a new leaf set entry node");
76
77         writer.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
78         writeObject(value);
79     }
80
81     @Override
82     public void startContainerNode(YangInstanceIdentifier.NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
83         Preconditions.checkNotNull(name, "Node identifier should not be null");
84
85         LOG.debug("Starting a new container node");
86
87         startNode(name.getNodeType(), NodeTypes.CONTAINER_NODE);
88     }
89
90     @Override
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");
94
95         startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST);
96     }
97
98     @Override
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");
102
103         startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST_ITEM);
104     }
105
106     @Override
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");
110
111         startNode(name.getNodeType(), NodeTypes.MAP_NODE);
112     }
113
114     @Override
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);
119
120         writeKeyValueMap(identifier.getKeyValues());
121
122     }
123
124     @Override
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");
128
129         startNode(name.getNodeType(), NodeTypes.ORDERED_MAP_NODE);
130     }
131
132     @Override
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");
136
137         startNode(name.getNodeType(), NodeTypes.CHOICE_NODE);
138     }
139
140     @Override
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");
144
145         writer.writeByte(NodeTypes.AUGMENTATION_NODE);
146         writeQNameSet(identifier.getPossibleChildNames());
147     }
148
149     @Override
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");
153
154         startNode(name.getNodeType(), NodeTypes.ANY_XML_NODE);
155
156         writeObject(value);
157     }
158
159     @Override
160     public void endNode() throws IOException, IllegalStateException {
161         LOG.debug("Ending the node");
162
163         writer.writeByte(NodeTypes.END_NODE);
164     }
165
166     @Override
167     public void close() throws IOException {
168         writer.close();
169     }
170
171     @Override
172     public void flush() throws IOException {
173         writer.flush();
174     }
175
176     private void startNode(final QName qName, byte nodeType) throws IOException {
177
178         Preconditions.checkNotNull(qName, "QName of node identifier should not be null.");
179         // First write the type of node
180         writer.writeByte(nodeType);
181         // Write Start Tag
182         writeQName(qName);
183     }
184
185     private void writeQName(QName qName) throws IOException {
186
187         writeCodedString(qName.getLocalName());
188         writeCodedString(qName.getNamespace().toString());
189         writeCodedString(qName.getFormattedRevision());
190     }
191
192     private void writeCodedString(String key) throws IOException {
193         Integer value = stringCodeMap.get(key);
194
195         if(value != null) {
196             writer.writeBoolean(true);
197             writer.writeInt(value);
198         } else {
199             if(key != null) {
200                 stringCodeMap.put(key, Integer.valueOf(stringCodeMap.size()));
201             }
202             writer.writeBoolean(false);
203             writer.writeUTF(key);
204         }
205     }
206
207     private void writeObjSet(Set set) throws IOException {
208         if(!set.isEmpty()){
209             writer.writeInt(set.size());
210             for(Object o : set){
211                 if(o instanceof String){
212                     writeCodedString(o.toString());
213                 } else {
214                     throw new IllegalArgumentException("Expected value type to be String but was : " +
215                         o.toString());
216                 }
217             }
218         } else {
219             writer.writeInt(0);
220         }
221     }
222
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);
227
228         for(YangInstanceIdentifier.PathArgument pathArgument : pathArguments) {
229             writePathArgument(pathArgument);
230         }
231     }
232
233     private void writePathArgument(YangInstanceIdentifier.PathArgument pathArgument) throws IOException {
234
235         byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
236
237         writer.writeByte(type);
238
239         switch(type) {
240             case PathArgumentTypes.NODE_IDENTIFIER :
241
242                 YangInstanceIdentifier.NodeIdentifier nodeIdentifier =
243                     (YangInstanceIdentifier.NodeIdentifier) pathArgument;
244
245                 writeQName(nodeIdentifier.getNodeType());
246                 break;
247
248             case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
249
250                 YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
251                     (YangInstanceIdentifier.NodeIdentifierWithPredicates) pathArgument;
252                 writeQName(nodeIdentifierWithPredicates.getNodeType());
253
254                 writeKeyValueMap(nodeIdentifierWithPredicates.getKeyValues());
255                 break;
256
257             case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
258
259                 YangInstanceIdentifier.NodeWithValue nodeWithValue =
260                     (YangInstanceIdentifier.NodeWithValue) pathArgument;
261
262                 writeQName(nodeWithValue.getNodeType());
263                 writeObject(nodeWithValue.getValue());
264                 break;
265
266             case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
267
268                 YangInstanceIdentifier.AugmentationIdentifier augmentationIdentifier =
269                     (YangInstanceIdentifier.AugmentationIdentifier) pathArgument;
270
271                 // No Qname in augmentation identifier
272                 writeQNameSet(augmentationIdentifier.getPossibleChildNames());
273                 break;
274             default :
275                 throw new IllegalStateException("Unknown node identifier type is found : " + pathArgument.getClass().toString() );
276         }
277     }
278
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();
283
284             for(QName qName : qNameSet) {
285                 writeQName(qName);
286                 writeObject(keyValueMap.get(qName));
287             }
288         } else {
289             writer.writeInt(0);
290         }
291     }
292
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) {
298                 writeQName(qName);
299             }
300         } else {
301             LOG.debug("augmentation node does not have any child");
302             writer.writeInt(0);
303         }
304     }
305
306     private void writeObject(Object value) throws IOException {
307
308         byte type = ValueTypes.getSerializableType(value);
309         // Write object type first
310         writer.writeByte(type);
311
312         switch(type) {
313             case ValueTypes.BOOL_TYPE:
314                 writer.writeBoolean((Boolean) value);
315                 break;
316             case ValueTypes.QNAME_TYPE:
317                 writeQName((QName) value);
318                 break;
319             case ValueTypes.INT_TYPE:
320                 writer.writeInt((Integer) value);
321                 break;
322             case ValueTypes.BYTE_TYPE:
323                 writer.writeByte((Byte) value);
324                 break;
325             case ValueTypes.LONG_TYPE:
326                 writer.writeLong((Long) value);
327                 break;
328             case ValueTypes.SHORT_TYPE:
329                 writer.writeShort((Short) value);
330                 break;
331             case ValueTypes.BITS_TYPE:
332                 writeObjSet((Set) value);
333                 break;
334             case ValueTypes.YANG_IDENTIFIER_TYPE:
335                 writeYangInstanceIdentifier((YangInstanceIdentifier) value);
336                 break;
337             default:
338                 writer.writeUTF(value.toString());
339                 break;
340         }
341     }
342 }