Merge "BUG 2221 : Add metering to ShardTransaction actor"
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / NormalizedNodeInputStreamReader.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 org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.Node;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
29 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import java.io.DataInputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.math.BigDecimal;
41 import java.math.BigInteger;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.HashSet;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.Set;
48
49 /**
50  * NormalizedNodeInputStreamReader reads the byte stream and constructs the normalized node including its children nodes.
51  * This process goes in recursive manner, where each NodeTypes object signifies the start of the object, except END_NODE.
52  * If a node can have children, then that node's end is calculated based on appearance of END_NODE.
53  *
54  */
55
56 public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamReader {
57
58     private DataInputStream reader;
59
60     private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeInputStreamReader.class);
61
62     private Map<Integer, String> codedStringMap = new HashMap<>();
63     private static final String REVISION_ARG = "?revision=";
64
65     public NormalizedNodeInputStreamReader(InputStream stream) throws IOException {
66         Preconditions.checkNotNull(stream);
67         reader = new DataInputStream(stream);
68     }
69
70
71     public NormalizedNode<?, ?> readNormalizedNode() throws IOException {
72         NormalizedNode<?, ?> node = null;
73
74         // each node should start with a byte
75         byte nodeType = reader.readByte();
76
77         if(nodeType == NodeTypes.END_NODE) {
78             LOG.debug("End node reached. return");
79             return null;
80         }
81         else if(nodeType == NodeTypes.AUGMENTATION_NODE) {
82             LOG.debug("Reading augmentation node. will create augmentation identifier");
83
84             YangInstanceIdentifier.AugmentationIdentifier identifier =
85                 new YangInstanceIdentifier.AugmentationIdentifier(readQNameSet());
86             DataContainerNodeBuilder<YangInstanceIdentifier.AugmentationIdentifier, AugmentationNode> augmentationBuilder =
87                 Builders.augmentationBuilder().withNodeIdentifier(identifier);
88             augmentationBuilder = addDataContainerChildren(augmentationBuilder);
89             node = augmentationBuilder.build();
90
91         } else {
92             QName qName = readQName();
93
94             if(nodeType == NodeTypes.LEAF_SET_ENTRY_NODE) {
95                 LOG.debug("Reading leaf set entry node. Will create NodeWithValue instance identifier");
96
97                 // Read the object value
98                 Object value = readObject();
99
100                 YangInstanceIdentifier.NodeWithValue nodeWithValue = new YangInstanceIdentifier.NodeWithValue(qName, value);
101                 node =  Builders.leafSetEntryBuilder().withNodeIdentifier(nodeWithValue).withValue(value).build();
102
103             } else if(nodeType == NodeTypes.MAP_ENTRY_NODE) {
104                 LOG.debug("Reading map entry node. Will create node identifier with predicates.");
105
106                 YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier =
107                     new YangInstanceIdentifier.NodeIdentifierWithPredicates(qName, readKeyValueMap());
108                 DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder
109                     = Builders.mapEntryBuilder().withNodeIdentifier(nodeIdentifier);
110
111                 mapEntryBuilder = (DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates,
112                     MapEntryNode>)addDataContainerChildren(mapEntryBuilder);
113                 node = mapEntryBuilder.build();
114
115             } else {
116                 LOG.debug("Creating standard node identifier. ");
117                 YangInstanceIdentifier.NodeIdentifier identifier = new YangInstanceIdentifier.NodeIdentifier(qName);
118                 node = readNodeIdentifierDependentNode(nodeType, identifier);
119
120             }
121         }
122         return node;
123     }
124
125     private NormalizedNode<?, ?> readNodeIdentifierDependentNode(byte nodeType, YangInstanceIdentifier.NodeIdentifier identifier)
126         throws IOException {
127
128         switch(nodeType) {
129             case NodeTypes.LEAF_NODE :
130                 LOG.debug("Read leaf node");
131                 // Read the object value
132                 NormalizedNodeAttrBuilder leafBuilder = Builders.leafBuilder();
133                 return leafBuilder.withNodeIdentifier(identifier).withValue(readObject()).build();
134
135             case NodeTypes.ANY_XML_NODE :
136                 LOG.debug("Read xml node");
137                 Node value = (Node) readObject();
138                 return Builders.anyXmlBuilder().withValue(value).build();
139
140             case NodeTypes.MAP_NODE :
141                 LOG.debug("Read map node");
142                 CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = Builders.mapBuilder().withNodeIdentifier(identifier);
143                 mapBuilder = addMapNodeChildren(mapBuilder);
144                 return mapBuilder.build();
145
146             case NodeTypes.CHOICE_NODE :
147                 LOG.debug("Read choice node");
148                 DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ChoiceNode> choiceBuilder =
149                     Builders.choiceBuilder().withNodeIdentifier(identifier);
150                 choiceBuilder = addDataContainerChildren(choiceBuilder);
151                 return choiceBuilder.build();
152
153             case NodeTypes.ORDERED_MAP_NODE :
154                 LOG.debug("Reading ordered map node");
155                 CollectionNodeBuilder<MapEntryNode, OrderedMapNode> orderedMapBuilder =
156                     Builders.orderedMapBuilder().withNodeIdentifier(identifier);
157                 orderedMapBuilder = addMapNodeChildren(orderedMapBuilder);
158                 return orderedMapBuilder.build();
159
160             case NodeTypes.UNKEYED_LIST :
161                 LOG.debug("Read unkeyed list node");
162                 CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> unkeyedListBuilder =
163                     Builders.unkeyedListBuilder().withNodeIdentifier(identifier);
164                 unkeyedListBuilder = addUnkeyedListChildren(unkeyedListBuilder);
165                 return unkeyedListBuilder.build();
166
167             case NodeTypes.UNKEYED_LIST_ITEM :
168                 LOG.debug("Read unkeyed list item node");
169                 DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, UnkeyedListEntryNode> unkeyedListEntryBuilder
170                     = Builders.unkeyedListEntryBuilder().withNodeIdentifier(identifier);
171
172                 unkeyedListEntryBuilder = (DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, UnkeyedListEntryNode>)
173                     addDataContainerChildren(unkeyedListEntryBuilder);
174                 return unkeyedListEntryBuilder.build();
175
176             case NodeTypes.CONTAINER_NODE :
177                 LOG.debug("Read container node");
178                 DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> containerBuilder =
179                     Builders.containerBuilder().withNodeIdentifier(identifier);
180
181                 containerBuilder = (DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode>)
182                     addDataContainerChildren(containerBuilder);
183                 return containerBuilder.build();
184
185             case NodeTypes.LEAF_SET :
186                 LOG.debug("Read leaf set node");
187                 ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafSetBuilder =
188                     Builders.leafSetBuilder().withNodeIdentifier(identifier);
189                 leafSetBuilder = addLeafSetChildren(leafSetBuilder);
190                 return leafSetBuilder.build();
191
192             default :
193                 return null;
194         }
195     }
196
197     private QName readQName() throws IOException {
198         // Read in the same sequence of writing
199         String localName = readCodedString();
200         String namespace = readCodedString();
201         String revision = readCodedString();
202         String qName;
203         // Not using stringbuilder as compiler optimizes string concatenation of +
204         if(revision != null){
205             qName = "(" + namespace+ REVISION_ARG + revision + ")" +localName;
206         } else {
207             qName = "(" + namespace + ")" +localName;
208         }
209
210         return QNameFactory.create(qName);
211     }
212
213
214     private String readCodedString() throws IOException {
215         boolean readFromMap = reader.readBoolean();
216         if(readFromMap) {
217             return codedStringMap.get(reader.readInt());
218         } else {
219             String value = reader.readUTF();
220             if(value != null) {
221                 codedStringMap.put(Integer.valueOf(codedStringMap.size()), value);
222             }
223             return value;
224         }
225     }
226
227     private Set<QName> readQNameSet() throws IOException{
228         // Read the children count
229         int count = reader.readInt();
230         Set<QName> children = new HashSet<>(count);
231         for(int i = 0; i<count; i++) {
232             children.add(readQName());
233         }
234         return children;
235     }
236
237     private Map<QName, Object> readKeyValueMap() throws IOException {
238         int count = reader.readInt();
239         Map<QName, Object> keyValueMap = new HashMap<>(count);
240
241         for(int i = 0; i<count; i++) {
242             keyValueMap.put(readQName(), readObject());
243         }
244
245         return keyValueMap;
246     }
247
248     private Object readObject() throws IOException {
249         byte objectType = reader.readByte();
250         switch(objectType) {
251             case ValueTypes.BITS_TYPE:
252                 return readObjSet();
253
254             case ValueTypes.BOOL_TYPE :
255                 return reader.readBoolean();
256
257             case ValueTypes.BYTE_TYPE :
258                 return reader.readByte();
259
260             case ValueTypes.INT_TYPE :
261                 return reader.readInt();
262
263             case ValueTypes.LONG_TYPE :
264                 return reader.readLong();
265
266             case ValueTypes.QNAME_TYPE :
267                 return readQName();
268
269             case ValueTypes.SHORT_TYPE :
270                 return reader.readShort();
271
272             case ValueTypes.STRING_TYPE :
273                 return reader.readUTF();
274
275             case ValueTypes.BIG_DECIMAL_TYPE :
276                 return new BigDecimal(reader.readUTF());
277
278             case ValueTypes.BIG_INTEGER_TYPE :
279                 return new BigInteger(reader.readUTF());
280
281             case ValueTypes.YANG_IDENTIFIER_TYPE :
282                 int size = reader.readInt();
283
284                 List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>(size);
285
286                 for(int i=0; i<size; i++) {
287                     pathArguments.add(readPathArgument());
288                 }
289                 return YangInstanceIdentifier.create(pathArguments);
290
291             default :
292                 return null;
293         }
294     }
295
296     private Set<String> readObjSet() throws IOException {
297         int count = reader.readInt();
298         Set<String> children = new HashSet<>(count);
299         for(int i = 0; i<count; i++) {
300             children.add(readCodedString());
301         }
302         return children;
303     }
304
305     private YangInstanceIdentifier.PathArgument readPathArgument() throws IOException {
306         // read Type
307         int type = reader.readByte();
308
309         switch(type) {
310
311             case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
312                 return new YangInstanceIdentifier.AugmentationIdentifier(readQNameSet());
313
314             case PathArgumentTypes.NODE_IDENTIFIER :
315             return new YangInstanceIdentifier.NodeIdentifier(readQName());
316
317             case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES :
318             return new YangInstanceIdentifier.NodeIdentifierWithPredicates(readQName(), readKeyValueMap());
319
320             case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
321             return new YangInstanceIdentifier.NodeWithValue(readQName(), readObject());
322
323             default :
324                 return null;
325         }
326     }
327
328     private ListNodeBuilder<Object, LeafSetEntryNode<Object>> addLeafSetChildren(ListNodeBuilder<Object,
329         LeafSetEntryNode<Object>> builder)
330         throws IOException {
331
332         LOG.debug("Reading children of leaf set");
333         LeafSetEntryNode<Object> child = (LeafSetEntryNode<Object>)readNormalizedNode();
334
335         while(child != null) {
336             builder.withChild(child);
337             child = (LeafSetEntryNode<Object>)readNormalizedNode();
338         }
339         return builder;
340     }
341
342     private CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> addUnkeyedListChildren(
343         CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> builder)
344         throws IOException{
345
346         LOG.debug("Reading children of unkeyed list");
347         UnkeyedListEntryNode child = (UnkeyedListEntryNode)readNormalizedNode();
348
349         while(child != null) {
350             builder.withChild(child);
351             child = (UnkeyedListEntryNode)readNormalizedNode();
352         }
353         return builder;
354     }
355
356     private DataContainerNodeBuilder addDataContainerChildren(DataContainerNodeBuilder builder)
357         throws IOException {
358         LOG.debug("Reading data container (leaf nodes) nodes");
359
360         DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> child =
361             (DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>) readNormalizedNode();
362
363         while(child != null) {
364             builder.withChild(child);
365             child =
366                 (DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>) readNormalizedNode();
367         }
368         return builder;
369     }
370
371
372     private CollectionNodeBuilder addMapNodeChildren(CollectionNodeBuilder builder)
373         throws IOException {
374         LOG.debug("Reading map node children");
375         MapEntryNode child = (MapEntryNode)readNormalizedNode();
376
377         while(child != null){
378             builder.withChild(child);
379             child = (MapEntryNode)readNormalizedNode();
380         }
381
382         return builder;
383     }
384
385
386     @Override
387     public void close() throws IOException {
388         reader.close();
389     }
390
391 }