Lower debugging in sal-clustering-commons
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / NormalizedNodeInputStreamReader.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Strings;
13 import java.io.DataInput;
14 import java.io.IOException;
15 import java.io.StringReader;
16 import java.math.BigDecimal;
17 import java.math.BigInteger;
18 import java.nio.charset.StandardCharsets;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import javax.xml.parsers.DocumentBuilderFactory;
27 import javax.xml.parsers.ParserConfigurationException;
28 import javax.xml.transform.dom.DOMSource;
29 import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
40 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
43 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.w3c.dom.Element;
47 import org.xml.sax.InputSource;
48 import org.xml.sax.SAXException;
49
50 /**
51  * NormalizedNodeInputStreamReader reads the byte stream and constructs the normalized node including its children
52  * nodes. This process goes in recursive manner, where each NodeTypes object signifies the start of the object, except
53  * END_NODE. If a node can have children, then that node's end is calculated based on appearance of END_NODE.
54  */
55 public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput {
56
57     private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeInputStreamReader.class);
58
59     private static final String REVISION_ARG = "?revision=";
60
61     private final DataInput input;
62
63     private final Map<Integer, String> codedStringMap = new HashMap<>();
64
65     private QName lastLeafSetQName;
66
67     private NormalizedNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier,
68                                       Object, LeafNode<Object>> leafBuilder;
69
70     @SuppressWarnings("rawtypes")
71     private NormalizedNodeAttrBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafSetEntryBuilder;
72
73     private final StringBuilder reusableStringBuilder = new StringBuilder(50);
74
75     private boolean readSignatureMarker = true;
76
77     /**
78      * Constructs an instance.
79      *
80      * @deprecated Use {@link NormalizedNodeInputOutput#newDataInput(DataInput)} instead.
81      */
82     @Deprecated
83     public NormalizedNodeInputStreamReader(final DataInput input) {
84         this(input, false);
85     }
86
87     NormalizedNodeInputStreamReader(final DataInput input, final boolean versionChecked) {
88         this.input = Preconditions.checkNotNull(input);
89         readSignatureMarker = !versionChecked;
90     }
91
92     @Override
93     public NormalizedNode<?, ?> readNormalizedNode() throws IOException {
94         readSignatureMarkerAndVersionIfNeeded();
95         return readNormalizedNodeInternal();
96     }
97
98     private void readSignatureMarkerAndVersionIfNeeded() throws IOException {
99         if (readSignatureMarker) {
100             readSignatureMarker = false;
101
102             final byte marker = input.readByte();
103             if (marker != TokenTypes.SIGNATURE_MARKER) {
104                 throw new InvalidNormalizedNodeStreamException(String.format(
105                         "Invalid signature marker: %d", marker));
106             }
107
108             final short version = input.readShort();
109             if (version != TokenTypes.LITHIUM_VERSION) {
110                 throw new InvalidNormalizedNodeStreamException(String.format("Unhandled stream version %s", version));
111             }
112         }
113     }
114
115     private NormalizedNode<?, ?> readNormalizedNodeInternal() throws IOException {
116         // each node should start with a byte
117         byte nodeType = input.readByte();
118
119         if (nodeType == NodeTypes.END_NODE) {
120             LOG.trace("End node reached. return");
121             return null;
122         }
123
124         switch (nodeType) {
125             case NodeTypes.AUGMENTATION_NODE :
126                 YangInstanceIdentifier.AugmentationIdentifier augIdentifier =
127                     new YangInstanceIdentifier.AugmentationIdentifier(readQNameSet());
128
129                 LOG.trace("Reading augmentation node {} ", augIdentifier);
130
131                 return addDataContainerChildren(Builders.augmentationBuilder()
132                         .withNodeIdentifier(augIdentifier)).build();
133
134             case NodeTypes.LEAF_SET_ENTRY_NODE :
135                 QName name = lastLeafSetQName;
136                 if (name == null) {
137                     name = readQName();
138                 }
139
140                 Object value = readObject();
141                 NodeWithValue<Object> leafIdentifier = new NodeWithValue<>(name, value);
142
143                 LOG.trace("Reading leaf set entry node {}, value {}", leafIdentifier, value);
144
145                 return leafSetEntryBuilder().withNodeIdentifier(leafIdentifier).withValue(value).build();
146
147             case NodeTypes.MAP_ENTRY_NODE :
148                 NodeIdentifierWithPredicates entryIdentifier = new NodeIdentifierWithPredicates(
149                         readQName(), readKeyValueMap());
150
151                 LOG.trace("Reading map entry node {} ", entryIdentifier);
152
153                 return addDataContainerChildren(Builders.mapEntryBuilder()
154                         .withNodeIdentifier(entryIdentifier)).build();
155
156             default :
157                 return readNodeIdentifierDependentNode(nodeType, new NodeIdentifier(readQName()));
158         }
159     }
160
161     private NormalizedNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier,
162                                       Object, LeafNode<Object>> leafBuilder() {
163         if (leafBuilder == null) {
164             leafBuilder = Builders.leafBuilder();
165         }
166
167         return leafBuilder;
168     }
169
170     @SuppressWarnings("rawtypes")
171     private NormalizedNodeAttrBuilder<NodeWithValue, Object,
172                                       LeafSetEntryNode<Object>> leafSetEntryBuilder() {
173         if (leafSetEntryBuilder == null) {
174             leafSetEntryBuilder = Builders.leafSetEntryBuilder();
175         }
176
177         return leafSetEntryBuilder;
178     }
179
180     private NormalizedNode<?, ?> readNodeIdentifierDependentNode(final byte nodeType, final NodeIdentifier identifier)
181         throws IOException {
182
183         switch (nodeType) {
184             case NodeTypes.LEAF_NODE :
185                 LOG.trace("Read leaf node {}", identifier);
186                 // Read the object value
187                 return leafBuilder().withNodeIdentifier(identifier).withValue(readObject()).build();
188
189             case NodeTypes.ANY_XML_NODE :
190                 LOG.trace("Read xml node");
191                 return Builders.anyXmlBuilder().withNodeIdentifier(identifier).withValue(readDOMSource()).build();
192
193             case NodeTypes.MAP_NODE :
194                 LOG.trace("Read map node {}", identifier);
195                 return addDataContainerChildren(Builders.mapBuilder().withNodeIdentifier(identifier)).build();
196
197             case NodeTypes.CHOICE_NODE:
198                 LOG.trace("Read choice node {}", identifier);
199                 return addDataContainerChildren(Builders.choiceBuilder().withNodeIdentifier(identifier)).build();
200
201             case NodeTypes.ORDERED_MAP_NODE:
202                 LOG.trace("Reading ordered map node {}", identifier);
203                 return addDataContainerChildren(Builders.orderedMapBuilder().withNodeIdentifier(identifier)).build();
204
205             case NodeTypes.UNKEYED_LIST:
206                 LOG.trace("Read unkeyed list node {}", identifier);
207                 return addDataContainerChildren(Builders.unkeyedListBuilder().withNodeIdentifier(identifier)).build();
208
209             case NodeTypes.UNKEYED_LIST_ITEM:
210                 LOG.trace("Read unkeyed list item node {}", identifier);
211                 return addDataContainerChildren(Builders.unkeyedListEntryBuilder()
212                         .withNodeIdentifier(identifier)).build();
213
214             case NodeTypes.CONTAINER_NODE:
215                 LOG.trace("Read container node {}", identifier);
216                 return addDataContainerChildren(Builders.containerBuilder().withNodeIdentifier(identifier)).build();
217
218             case NodeTypes.LEAF_SET :
219                 LOG.trace("Read leaf set node {}", identifier);
220                 return addLeafSetChildren(identifier.getNodeType(),
221                         Builders.leafSetBuilder().withNodeIdentifier(identifier)).build();
222
223             case NodeTypes.ORDERED_LEAF_SET:
224                 LOG.trace("Read ordered leaf set node {}", identifier);
225                 ListNodeBuilder<Object, LeafSetEntryNode<Object>> orderedLeafSetBuilder =
226                         Builders.orderedLeafSetBuilder().withNodeIdentifier(identifier);
227                 orderedLeafSetBuilder = addLeafSetChildren(identifier.getNodeType(), orderedLeafSetBuilder);
228                 return orderedLeafSetBuilder.build();
229
230             default :
231                 return null;
232         }
233     }
234
235     private DOMSource readDOMSource() throws IOException {
236         String xml = readObject().toString();
237         try {
238             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
239             factory.setNamespaceAware(true);
240             Element node = factory.newDocumentBuilder().parse(
241                     new InputSource(new StringReader(xml))).getDocumentElement();
242             return new DOMSource(node);
243         } catch (SAXException | ParserConfigurationException e) {
244             throw new IOException("Error parsing XML: " + xml, e);
245         }
246     }
247
248     private QName readQName() throws IOException {
249         // Read in the same sequence of writing
250         String localName = readCodedString();
251         String namespace = readCodedString();
252         String revision = readCodedString();
253
254         String qname;
255         if (!Strings.isNullOrEmpty(revision)) {
256             qname = reusableStringBuilder.append('(').append(namespace).append(REVISION_ARG).append(revision)
257                     .append(')').append(localName).toString();
258         } else {
259             qname = reusableStringBuilder.append('(').append(namespace).append(')').append(localName).toString();
260         }
261
262         reusableStringBuilder.delete(0, reusableStringBuilder.length());
263         return QNameFactory.create(qname);
264     }
265
266
267     private String readCodedString() throws IOException {
268         byte valueType = input.readByte();
269         if (valueType == TokenTypes.IS_CODE_VALUE) {
270             return codedStringMap.get(input.readInt());
271         } else if (valueType == TokenTypes.IS_STRING_VALUE) {
272             String value = input.readUTF().intern();
273             codedStringMap.put(Integer.valueOf(codedStringMap.size()), value);
274             return value;
275         }
276
277         return null;
278     }
279
280     private Set<QName> readQNameSet() throws IOException {
281         // Read the children count
282         int count = input.readInt();
283         Set<QName> children = new HashSet<>(count);
284         for (int i = 0; i < count; i++) {
285             children.add(readQName());
286         }
287         return children;
288     }
289
290     private Map<QName, Object> readKeyValueMap() throws IOException {
291         int count = input.readInt();
292         Map<QName, Object> keyValueMap = new HashMap<>(count);
293
294         for (int i = 0; i < count; i++) {
295             keyValueMap.put(readQName(), readObject());
296         }
297
298         return keyValueMap;
299     }
300
301     private Object readObject() throws IOException {
302         byte objectType = input.readByte();
303         switch (objectType) {
304             case ValueTypes.BITS_TYPE:
305                 return readObjSet();
306
307             case ValueTypes.BOOL_TYPE :
308                 return Boolean.valueOf(input.readBoolean());
309
310             case ValueTypes.BYTE_TYPE :
311                 return Byte.valueOf(input.readByte());
312
313             case ValueTypes.INT_TYPE :
314                 return Integer.valueOf(input.readInt());
315
316             case ValueTypes.LONG_TYPE :
317                 return Long.valueOf(input.readLong());
318
319             case ValueTypes.QNAME_TYPE :
320                 return readQName();
321
322             case ValueTypes.SHORT_TYPE :
323                 return Short.valueOf(input.readShort());
324
325             case ValueTypes.STRING_TYPE :
326                 return input.readUTF();
327
328             case ValueTypes.STRING_BYTES_TYPE:
329                 return readStringBytes();
330
331             case ValueTypes.BIG_DECIMAL_TYPE :
332                 return new BigDecimal(input.readUTF());
333
334             case ValueTypes.BIG_INTEGER_TYPE :
335                 return new BigInteger(input.readUTF());
336
337             case ValueTypes.BINARY_TYPE :
338                 byte[] bytes = new byte[input.readInt()];
339                 input.readFully(bytes);
340                 return bytes;
341
342             case ValueTypes.YANG_IDENTIFIER_TYPE :
343                 return readYangInstanceIdentifierInternal();
344
345             default :
346                 return null;
347         }
348     }
349
350     private String readStringBytes() throws IOException {
351         byte[] bytes = new byte[input.readInt()];
352         input.readFully(bytes);
353         return new String(bytes, StandardCharsets.UTF_8);
354     }
355
356     @Override
357     public SchemaPath readSchemaPath() throws IOException {
358         readSignatureMarkerAndVersionIfNeeded();
359
360         final boolean absolute = input.readBoolean();
361         final int size = input.readInt();
362         final Collection<QName> qnames = new ArrayList<>(size);
363         for (int i = 0; i < size; ++i) {
364             qnames.add(readQName());
365         }
366
367         return SchemaPath.create(qnames, absolute);
368     }
369
370     @Override
371     public YangInstanceIdentifier readYangInstanceIdentifier() throws IOException {
372         readSignatureMarkerAndVersionIfNeeded();
373         return readYangInstanceIdentifierInternal();
374     }
375
376     private YangInstanceIdentifier readYangInstanceIdentifierInternal() throws IOException {
377         int size = input.readInt();
378
379         List<PathArgument> pathArguments = new ArrayList<>(size);
380
381         for (int i = 0; i < size; i++) {
382             pathArguments.add(readPathArgument());
383         }
384         return YangInstanceIdentifier.create(pathArguments);
385     }
386
387     private Set<String> readObjSet() throws IOException {
388         int count = input.readInt();
389         Set<String> children = new HashSet<>(count);
390         for (int i = 0; i < count; i++) {
391             children.add(readCodedString());
392         }
393         return children;
394     }
395
396     @Override
397     public PathArgument readPathArgument() throws IOException {
398         // read Type
399         int type = input.readByte();
400
401         switch (type) {
402
403             case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
404                 return new YangInstanceIdentifier.AugmentationIdentifier(readQNameSet());
405
406             case PathArgumentTypes.NODE_IDENTIFIER :
407                 return new NodeIdentifier(readQName());
408
409             case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES :
410                 return new NodeIdentifierWithPredicates(readQName(), readKeyValueMap());
411
412             case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
413                 return new NodeWithValue<>(readQName(), readObject());
414
415             default :
416                 return null;
417         }
418     }
419
420     @SuppressWarnings("unchecked")
421     private ListNodeBuilder<Object, LeafSetEntryNode<Object>> addLeafSetChildren(final QName nodeType,
422             final ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder) throws IOException {
423
424         LOG.trace("Reading children of leaf set");
425
426         lastLeafSetQName = nodeType;
427
428         LeafSetEntryNode<Object> child = (LeafSetEntryNode<Object>)readNormalizedNodeInternal();
429
430         while (child != null) {
431             builder.withChild(child);
432             child = (LeafSetEntryNode<Object>)readNormalizedNodeInternal();
433         }
434         return builder;
435     }
436
437     @SuppressWarnings({ "unchecked", "rawtypes" })
438     private NormalizedNodeContainerBuilder addDataContainerChildren(
439             final NormalizedNodeContainerBuilder builder) throws IOException {
440         LOG.trace("Reading data container (leaf nodes) nodes");
441
442         NormalizedNode<?, ?> child = readNormalizedNodeInternal();
443
444         while (child != null) {
445             builder.addChild(child);
446             child = readNormalizedNodeInternal();
447         }
448         return builder;
449     }
450
451     @Override
452     public void readFully(final byte[] value) throws IOException {
453         readSignatureMarkerAndVersionIfNeeded();
454         input.readFully(value);
455     }
456
457     @Override
458     public void readFully(final byte[] str, final int off, final int len) throws IOException {
459         readSignatureMarkerAndVersionIfNeeded();
460         input.readFully(str, off, len);
461     }
462
463     @Override
464     public int skipBytes(final int num) throws IOException {
465         readSignatureMarkerAndVersionIfNeeded();
466         return input.skipBytes(num);
467     }
468
469     @Override
470     public boolean readBoolean() throws IOException {
471         readSignatureMarkerAndVersionIfNeeded();
472         return input.readBoolean();
473     }
474
475     @Override
476     public byte readByte() throws IOException {
477         readSignatureMarkerAndVersionIfNeeded();
478         return input.readByte();
479     }
480
481     @Override
482     public int readUnsignedByte() throws IOException {
483         readSignatureMarkerAndVersionIfNeeded();
484         return input.readUnsignedByte();
485     }
486
487     @Override
488     public short readShort() throws IOException {
489         readSignatureMarkerAndVersionIfNeeded();
490         return input.readShort();
491     }
492
493     @Override
494     public int readUnsignedShort() throws IOException {
495         readSignatureMarkerAndVersionIfNeeded();
496         return input.readUnsignedShort();
497     }
498
499     @Override
500     public char readChar() throws IOException {
501         readSignatureMarkerAndVersionIfNeeded();
502         return input.readChar();
503     }
504
505     @Override
506     public int readInt() throws IOException {
507         readSignatureMarkerAndVersionIfNeeded();
508         return input.readInt();
509     }
510
511     @Override
512     public long readLong() throws IOException {
513         readSignatureMarkerAndVersionIfNeeded();
514         return input.readLong();
515     }
516
517     @Override
518     public float readFloat() throws IOException {
519         readSignatureMarkerAndVersionIfNeeded();
520         return input.readFloat();
521     }
522
523     @Override
524     public double readDouble() throws IOException {
525         readSignatureMarkerAndVersionIfNeeded();
526         return input.readDouble();
527     }
528
529     @Override
530     public String readLine() throws IOException {
531         readSignatureMarkerAndVersionIfNeeded();
532         return input.readLine();
533     }
534
535     @Override
536     public String readUTF() throws IOException {
537         readSignatureMarkerAndVersionIfNeeded();
538         return input.readUTF();
539     }
540 }