Handle empty type in NormalizedNode streaming
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / NormalizedNodeInputStreamReader.java
old mode 100644 (file)
new mode 100755 (executable)
index 3f6cbb7..cc74443
@@ -11,20 +11,23 @@ package org.opendaylight.controller.cluster.datastore.node.utils.stream;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import java.io.DataInput;
-import java.io.DataInputStream;
 import java.io.IOException;
-import java.io.InputStream;
+import java.io.StringReader;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.dom.DOMSource;
 import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
+import org.opendaylight.yangtools.yang.common.Empty;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
@@ -38,16 +41,18 @@ import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.w3c.dom.Element;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
 
 /**
- * NormalizedNodeInputStreamReader reads the byte stream and constructs the normalized node including its children nodes.
- * This process goes in recursive manner, where each NodeTypes object signifies the start of the object, except END_NODE.
- * If a node can have children, then that node's end is calculated based on appearance of END_NODE.
- *
+ * NormalizedNodeInputStreamReader reads the byte stream and constructs the normalized node including its children
+ * nodes. This process goes in recursive manner, where each NodeTypes object signifies the start of the object, except
+ * END_NODE. If a node can have children, then that node's end is calculated based on appearance of END_NODE.
  */
-
 public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput {
 
     private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeInputStreamReader.class);
@@ -63,28 +68,13 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
     private NormalizedNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier,
                                       Object, LeafNode<Object>> leafBuilder;
 
+    @SuppressWarnings("rawtypes")
     private NormalizedNodeAttrBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafSetEntryBuilder;
 
     private final StringBuilder reusableStringBuilder = new StringBuilder(50);
 
     private boolean readSignatureMarker = true;
 
-    /**
-     * @deprecated Use {@link #NormalizedNodeInputStreamReader(DataInput)} instead.
-     */
-    @Deprecated
-    public NormalizedNodeInputStreamReader(final InputStream stream) throws IOException {
-        this((DataInput) new DataInputStream(Preconditions.checkNotNull(stream)));
-    }
-
-    /**
-     * @deprecated Use {@link NormalizedNodeInputOutput#newDataInput(DataInput)} instead.
-     */
-    @Deprecated
-    public NormalizedNodeInputStreamReader(final DataInput input) {
-        this(input, false);
-    }
-
     NormalizedNodeInputStreamReader(final DataInput input, final boolean versionChecked) {
         this.input = Preconditions.checkNotNull(input);
         readSignatureMarker = !versionChecked;
@@ -97,7 +87,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
     }
 
     private void readSignatureMarkerAndVersionIfNeeded() throws IOException {
-        if(readSignatureMarker) {
+        if (readSignatureMarker) {
             readSignatureMarker = false;
 
             final byte marker = input.readByte();
@@ -117,31 +107,32 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
         // each node should start with a byte
         byte nodeType = input.readByte();
 
-        if(nodeType == NodeTypes.END_NODE) {
-            LOG.debug("End node reached. return");
+        if (nodeType == NodeTypes.END_NODE) {
+            LOG.trace("End node reached. return");
+            lastLeafSetQName = null;
             return null;
         }
 
-        switch(nodeType) {
+        switch (nodeType) {
             case NodeTypes.AUGMENTATION_NODE :
                 YangInstanceIdentifier.AugmentationIdentifier augIdentifier =
                     new YangInstanceIdentifier.AugmentationIdentifier(readQNameSet());
 
-                LOG.debug("Reading augmentation node {} ", augIdentifier);
+                LOG.trace("Reading augmentation node {} ", augIdentifier);
 
-                return addDataContainerChildren(Builders.augmentationBuilder().
-                        withNodeIdentifier(augIdentifier)).build();
+                return addDataContainerChildren(Builders.augmentationBuilder()
+                        .withNodeIdentifier(augIdentifier)).build();
 
             case NodeTypes.LEAF_SET_ENTRY_NODE :
                 QName name = lastLeafSetQName;
-                if(name == null) {
+                if (name == null) {
                     name = readQName();
                 }
 
                 Object value = readObject();
                 NodeWithValue<Object> leafIdentifier = new NodeWithValue<>(name, value);
 
-                LOG.debug("Reading leaf set entry node {}, value {}", leafIdentifier, value);
+                LOG.trace("Reading leaf set entry node {}, value {}", leafIdentifier, value);
 
                 return leafSetEntryBuilder().withNodeIdentifier(leafIdentifier).withValue(value).build();
 
@@ -149,10 +140,10 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
                 NodeIdentifierWithPredicates entryIdentifier = new NodeIdentifierWithPredicates(
                         readQName(), readKeyValueMap());
 
-                LOG.debug("Reading map entry node {} ", entryIdentifier);
+                LOG.trace("Reading map entry node {} ", entryIdentifier);
 
-                return addDataContainerChildren(Builders.mapEntryBuilder().
-                        withNodeIdentifier(entryIdentifier)).build();
+                return addDataContainerChildren(Builders.mapEntryBuilder()
+                        .withNodeIdentifier(entryIdentifier)).build();
 
             default :
                 return readNodeIdentifierDependentNode(nodeType, new NodeIdentifier(readQName()));
@@ -161,16 +152,17 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
 
     private NormalizedNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier,
                                       Object, LeafNode<Object>> leafBuilder() {
-        if(leafBuilder == null) {
+        if (leafBuilder == null) {
             leafBuilder = Builders.leafBuilder();
         }
 
         return leafBuilder;
     }
 
+    @SuppressWarnings("rawtypes")
     private NormalizedNodeAttrBuilder<NodeWithValue, Object,
                                       LeafSetEntryNode<Object>> leafSetEntryBuilder() {
-        if(leafSetEntryBuilder == null) {
+        if (leafSetEntryBuilder == null) {
             leafSetEntryBuilder = Builders.leafSetEntryBuilder();
         }
 
@@ -180,53 +172,48 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
     private NormalizedNode<?, ?> readNodeIdentifierDependentNode(final byte nodeType, final NodeIdentifier identifier)
         throws IOException {
 
-        switch(nodeType) {
+        switch (nodeType) {
             case NodeTypes.LEAF_NODE :
-                LOG.debug("Read leaf node {}", identifier);
+                LOG.trace("Read leaf node {}", identifier);
                 // Read the object value
                 return leafBuilder().withNodeIdentifier(identifier).withValue(readObject()).build();
 
             case NodeTypes.ANY_XML_NODE :
-                LOG.debug("Read xml node");
-                return Builders.anyXmlBuilder().withValue((DOMSource) readObject()).build();
+                LOG.trace("Read xml node");
+                return Builders.anyXmlBuilder().withNodeIdentifier(identifier).withValue(readDOMSource()).build();
 
             case NodeTypes.MAP_NODE :
-                LOG.debug("Read map node {}", identifier);
-                return addDataContainerChildren(Builders.mapBuilder().
-                        withNodeIdentifier(identifier)).build();
-
-            case NodeTypes.CHOICE_NODE :
-                LOG.debug("Read choice node {}", identifier);
-                return addDataContainerChildren(Builders.choiceBuilder().
-                        withNodeIdentifier(identifier)).build();
-
-            case NodeTypes.ORDERED_MAP_NODE :
-                LOG.debug("Reading ordered map node {}", identifier);
-                return addDataContainerChildren(Builders.orderedMapBuilder().
-                        withNodeIdentifier(identifier)).build();
-
-            case NodeTypes.UNKEYED_LIST :
-                LOG.debug("Read unkeyed list node {}", identifier);
-                return addDataContainerChildren(Builders.unkeyedListBuilder().
-                        withNodeIdentifier(identifier)).build();
-
-            case NodeTypes.UNKEYED_LIST_ITEM :
-                LOG.debug("Read unkeyed list item node {}", identifier);
-                return addDataContainerChildren(Builders.unkeyedListEntryBuilder().
-                        withNodeIdentifier(identifier)).build();
-
-            case NodeTypes.CONTAINER_NODE :
-                LOG.debug("Read container node {}", identifier);
-                return addDataContainerChildren(Builders.containerBuilder().
-                        withNodeIdentifier(identifier)).build();
+                LOG.trace("Read map node {}", identifier);
+                return addDataContainerChildren(Builders.mapBuilder().withNodeIdentifier(identifier)).build();
+
+            case NodeTypes.CHOICE_NODE:
+                LOG.trace("Read choice node {}", identifier);
+                return addDataContainerChildren(Builders.choiceBuilder().withNodeIdentifier(identifier)).build();
+
+            case NodeTypes.ORDERED_MAP_NODE:
+                LOG.trace("Reading ordered map node {}", identifier);
+                return addDataContainerChildren(Builders.orderedMapBuilder().withNodeIdentifier(identifier)).build();
+
+            case NodeTypes.UNKEYED_LIST:
+                LOG.trace("Read unkeyed list node {}", identifier);
+                return addDataContainerChildren(Builders.unkeyedListBuilder().withNodeIdentifier(identifier)).build();
+
+            case NodeTypes.UNKEYED_LIST_ITEM:
+                LOG.trace("Read unkeyed list item node {}", identifier);
+                return addDataContainerChildren(Builders.unkeyedListEntryBuilder()
+                        .withNodeIdentifier(identifier)).build();
+
+            case NodeTypes.CONTAINER_NODE:
+                LOG.trace("Read container node {}", identifier);
+                return addDataContainerChildren(Builders.containerBuilder().withNodeIdentifier(identifier)).build();
 
             case NodeTypes.LEAF_SET :
-                LOG.debug("Read leaf set node {}", identifier);
+                LOG.trace("Read leaf set node {}", identifier);
                 return addLeafSetChildren(identifier.getNodeType(),
                         Builders.leafSetBuilder().withNodeIdentifier(identifier)).build();
 
             case NodeTypes.ORDERED_LEAF_SET:
-                LOG.debug("Read ordered leaf set node {}", identifier);
+                LOG.trace("Read ordered leaf set node {}", identifier);
                 ListNodeBuilder<Object, LeafSetEntryNode<Object>> orderedLeafSetBuilder =
                         Builders.orderedLeafSetBuilder().withNodeIdentifier(identifier);
                 orderedLeafSetBuilder = addLeafSetChildren(identifier.getNodeType(), orderedLeafSetBuilder);
@@ -237,44 +224,56 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
         }
     }
 
+    private DOMSource readDOMSource() throws IOException {
+        String xml = readObject().toString();
+        try {
+            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+            factory.setNamespaceAware(true);
+            Element node = factory.newDocumentBuilder().parse(
+                    new InputSource(new StringReader(xml))).getDocumentElement();
+            return new DOMSource(node);
+        } catch (SAXException | ParserConfigurationException e) {
+            throw new IOException("Error parsing XML: " + xml, e);
+        }
+    }
+
     private QName readQName() throws IOException {
         // Read in the same sequence of writing
         String localName = readCodedString();
         String namespace = readCodedString();
         String revision = readCodedString();
 
-        String qName;
-        if(!Strings.isNullOrEmpty(revision)) {
-            qName = reusableStringBuilder.append('(').append(namespace).append(REVISION_ARG).
-                        append(revision).append(')').append(localName).toString();
+        String qname;
+        if (!Strings.isNullOrEmpty(revision)) {
+            qname = reusableStringBuilder.append('(').append(namespace).append(REVISION_ARG).append(revision)
+                    .append(')').append(localName).toString();
         } else {
-            qName = reusableStringBuilder.append('(').append(namespace).append(')').
-                        append(localName).toString();
+            qname = reusableStringBuilder.append('(').append(namespace).append(')').append(localName).toString();
         }
 
         reusableStringBuilder.delete(0, reusableStringBuilder.length());
-        return QNameFactory.create(qName);
+        return QNameFactory.create(qname);
     }
 
 
     private String readCodedString() throws IOException {
         byte valueType = input.readByte();
-        if(valueType == TokenTypes.IS_CODE_VALUE) {
+        if (valueType == TokenTypes.IS_CODE_VALUE) {
             return codedStringMap.get(input.readInt());
-        } else if(valueType == TokenTypes.IS_STRING_VALUE) {
+        } else if (valueType == TokenTypes.IS_STRING_VALUE) {
             String value = input.readUTF().intern();
-            codedStringMap.put(Integer.valueOf(codedStringMap.size()), value);
+            codedStringMap.put(codedStringMap.size(), value);
             return value;
         }
 
         return null;
     }
 
-    private Set<QName> readQNameSet() throws IOException{
+    private Set<QName> readQNameSet() throws IOException {
         // Read the children count
         int count = input.readInt();
         Set<QName> children = new HashSet<>(count);
-        for(int i = 0; i < count; i++) {
+        for (int i = 0; i < count; i++) {
             children.add(readQName());
         }
         return children;
@@ -284,7 +283,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
         int count = input.readInt();
         Map<QName, Object> keyValueMap = new HashMap<>(count);
 
-        for(int i = 0; i < count; i++) {
+        for (int i = 0; i < count; i++) {
             keyValueMap.put(readQName(), readObject());
         }
 
@@ -293,27 +292,27 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
 
     private Object readObject() throws IOException {
         byte objectType = input.readByte();
-        switch(objectType) {
+        switch (objectType) {
             case ValueTypes.BITS_TYPE:
                 return readObjSet();
 
             case ValueTypes.BOOL_TYPE :
-                return Boolean.valueOf(input.readBoolean());
+                return input.readBoolean();
 
             case ValueTypes.BYTE_TYPE :
-                return Byte.valueOf(input.readByte());
+                return input.readByte();
 
             case ValueTypes.INT_TYPE :
-                return Integer.valueOf(input.readInt());
+                return input.readInt();
 
             case ValueTypes.LONG_TYPE :
-                return Long.valueOf(input.readLong());
+                return input.readLong();
 
             case ValueTypes.QNAME_TYPE :
                 return readQName();
 
             case ValueTypes.SHORT_TYPE :
-                return Short.valueOf(input.readShort());
+                return input.readShort();
 
             case ValueTypes.STRING_TYPE :
                 return input.readUTF();
@@ -335,6 +334,15 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
             case ValueTypes.YANG_IDENTIFIER_TYPE :
                 return readYangInstanceIdentifierInternal();
 
+            case ValueTypes.EMPTY_TYPE:
+            // Leaf nodes no longer allow null values and thus we no longer emit null values. Previously, the "empty"
+            // yang type was represented as null so we translate an incoming null value to Empty. It was possible for
+            // a BI user to set a string leaf to null and we're rolling the dice here but the chances for that are
+            // very low. We'd have to know the yang type but, even if we did, we can't let a null value pass upstream
+            // so we'd have to drop the leaf which might cause other issues.
+            case ValueTypes.NULL_TYPE:
+                return Empty.getInstance();
+
             default :
                 return null;
         }
@@ -346,6 +354,20 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
         return new String(bytes, StandardCharsets.UTF_8);
     }
 
+    @Override
+    public SchemaPath readSchemaPath() throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+
+        final boolean absolute = input.readBoolean();
+        final int size = input.readInt();
+        final Collection<QName> qnames = new ArrayList<>(size);
+        for (int i = 0; i < size; ++i) {
+            qnames.add(readQName());
+        }
+
+        return SchemaPath.create(qnames, absolute);
+    }
+
     @Override
     public YangInstanceIdentifier readYangInstanceIdentifier() throws IOException {
         readSignatureMarkerAndVersionIfNeeded();
@@ -357,7 +379,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
 
         List<PathArgument> pathArguments = new ArrayList<>(size);
 
-        for(int i = 0; i < size; i++) {
+        for (int i = 0; i < size; i++) {
             pathArguments.add(readPathArgument());
         }
         return YangInstanceIdentifier.create(pathArguments);
@@ -366,7 +388,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
     private Set<String> readObjSet() throws IOException {
         int count = input.readInt();
         Set<String> children = new HashSet<>(count);
-        for(int i = 0; i < count; i++) {
+        for (int i = 0; i < count; i++) {
             children.add(readCodedString());
         }
         return children;
@@ -377,7 +399,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
         // read Type
         int type = input.readByte();
 
-        switch(type) {
+        switch (type) {
 
             case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
                 return new YangInstanceIdentifier.AugmentationIdentifier(readQNameSet());
@@ -400,13 +422,13 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
     private ListNodeBuilder<Object, LeafSetEntryNode<Object>> addLeafSetChildren(final QName nodeType,
             final ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder) throws IOException {
 
-        LOG.debug("Reading children of leaf set");
+        LOG.trace("Reading children of leaf set");
 
         lastLeafSetQName = nodeType;
 
         LeafSetEntryNode<Object> child = (LeafSetEntryNode<Object>)readNormalizedNodeInternal();
 
-        while(child != null) {
+        while (child != null) {
             builder.withChild(child);
             child = (LeafSetEntryNode<Object>)readNormalizedNodeInternal();
         }
@@ -416,11 +438,11 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
     @SuppressWarnings({ "unchecked", "rawtypes" })
     private NormalizedNodeContainerBuilder addDataContainerChildren(
             final NormalizedNodeContainerBuilder builder) throws IOException {
-        LOG.debug("Reading data container (leaf nodes) nodes");
+        LOG.trace("Reading data container (leaf nodes) nodes");
 
         NormalizedNode<?, ?> child = readNormalizedNodeInternal();
 
-        while(child != null) {
+        while (child != null) {
             builder.addChild(child);
             child = readNormalizedNodeInternal();
         }
@@ -428,21 +450,21 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
     }
 
     @Override
-    public void readFully(final byte[] b) throws IOException {
+    public void readFully(final byte[] value) throws IOException {
         readSignatureMarkerAndVersionIfNeeded();
-        input.readFully(b);
+        input.readFully(value);
     }
 
     @Override
-    public void readFully(final byte[] b, final int off, final int len) throws IOException {
+    public void readFully(final byte[] str, final int off, final int len) throws IOException {
         readSignatureMarkerAndVersionIfNeeded();
-        input.readFully(b, off, len);
+        input.readFully(str, off, len);
     }
 
     @Override
-    public int skipBytes(final int n) throws IOException {
+    public int skipBytes(final int num) throws IOException {
         readSignatureMarkerAndVersionIfNeeded();
-        return input.skipBytes(n);
+        return input.skipBytes(num);
     }
 
     @Override