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