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