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