BUG-7594: Expand NormalizedNodeData{Input,Output} to handle SchemaPath
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / NormalizedNodeInputStreamReader.java
index cea8fad4ea15d57bc57a4e3e8d1da820e2dab17f..831c5162628fdb7cc8af80690c539868c305513f 100644 (file)
@@ -11,18 +11,20 @@ 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.QName;
@@ -38,17 +40,19 @@ 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 NormalizedNodeStreamReader {
+public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput {
 
     private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeInputStreamReader.class);
 
@@ -63,20 +67,26 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
     private NormalizedNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier,
                                       Object, LeafNode<Object>> leafBuilder;
 
-    private NormalizedNodeAttrBuilder<NodeWithValue, Object,
-                                      LeafSetEntryNode<Object>> leafSetEntryBuilder;
+    @SuppressWarnings("rawtypes")
+    private NormalizedNodeAttrBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafSetEntryBuilder;
 
     private final StringBuilder reusableStringBuilder = new StringBuilder(50);
 
     private boolean readSignatureMarker = true;
 
-    public NormalizedNodeInputStreamReader(final InputStream stream) throws IOException {
-        Preconditions.checkNotNull(stream);
-        input = new DataInputStream(stream);
+    /**
+     * Constructs an instance.
+     *
+     * @deprecated Use {@link NormalizedNodeInputOutput#newDataInput(DataInput)} instead.
+     */
+    @Deprecated
+    public NormalizedNodeInputStreamReader(final DataInput input) {
+        this(input, false);
     }
 
-    public NormalizedNodeInputStreamReader(final DataInput input) {
+    NormalizedNodeInputStreamReader(final DataInput input, final boolean versionChecked) {
         this.input = Preconditions.checkNotNull(input);
+        readSignatureMarker = !versionChecked;
     }
 
     @Override
@@ -86,16 +96,19 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
     }
 
     private void readSignatureMarkerAndVersionIfNeeded() throws IOException {
-        if(readSignatureMarker) {
+        if (readSignatureMarker) {
             readSignatureMarker = false;
 
-            byte marker = input.readByte();
-            if(marker != NormalizedNodeOutputStreamWriter.SIGNATURE_MARKER) {
+            final byte marker = input.readByte();
+            if (marker != TokenTypes.SIGNATURE_MARKER) {
                 throw new InvalidNormalizedNodeStreamException(String.format(
                         "Invalid signature marker: %d", marker));
             }
 
-            input.readShort(); // read the version - not currently used/needed.
+            final short version = input.readShort();
+            if (version != TokenTypes.LITHIUM_VERSION) {
+                throw new InvalidNormalizedNodeStreamException(String.format("Unhandled stream version %s", version));
+            }
         }
     }
 
@@ -103,24 +116,29 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
         // each node should start with a byte
         byte nodeType = input.readByte();
 
-        if(nodeType == NodeTypes.END_NODE) {
+        if (nodeType == NodeTypes.END_NODE) {
             LOG.debug("End node reached. return");
             return null;
         }
 
-        switch(nodeType) {
+        switch (nodeType) {
             case NodeTypes.AUGMENTATION_NODE :
                 YangInstanceIdentifier.AugmentationIdentifier augIdentifier =
                     new YangInstanceIdentifier.AugmentationIdentifier(readQNameSet());
 
                 LOG.debug("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) {
+                    name = readQName();
+                }
+
                 Object value = readObject();
-                NodeWithValue leafIdentifier = new NodeWithValue(lastLeafSetQName, value);
+                NodeWithValue<Object> leafIdentifier = new NodeWithValue<>(name, value);
 
                 LOG.debug("Reading leaf set entry node {}, value {}", leafIdentifier, value);
 
@@ -132,8 +150,8 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
 
                 LOG.debug("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()));
@@ -142,16 +160,17 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
 
     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();
         }
 
@@ -161,7 +180,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
     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);
                 // Read the object value
@@ -169,73 +188,87 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
 
             case NodeTypes.ANY_XML_NODE :
                 LOG.debug("Read xml node");
-                return Builders.anyXmlBuilder().withValue((DOMSource) readObject()).build();
+                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();
+                return addDataContainerChildren(Builders.mapBuilder().withNodeIdentifier(identifier)).build();
 
-            case NodeTypes.CHOICE_NODE :
+            case NodeTypes.CHOICE_NODE:
                 LOG.debug("Read choice node {}", identifier);
-                return addDataContainerChildren(Builders.choiceBuilder().
-                        withNodeIdentifier(identifier)).build();
+                return addDataContainerChildren(Builders.choiceBuilder().withNodeIdentifier(identifier)).build();
 
-            case NodeTypes.ORDERED_MAP_NODE :
+            case NodeTypes.ORDERED_MAP_NODE:
                 LOG.debug("Reading ordered map node {}", identifier);
-                return addDataContainerChildren(Builders.orderedMapBuilder().
-                        withNodeIdentifier(identifier)).build();
+                return addDataContainerChildren(Builders.orderedMapBuilder().withNodeIdentifier(identifier)).build();
 
-            case NodeTypes.UNKEYED_LIST :
+            case NodeTypes.UNKEYED_LIST:
                 LOG.debug("Read unkeyed list node {}", identifier);
-                return addDataContainerChildren(Builders.unkeyedListBuilder().
-                        withNodeIdentifier(identifier)).build();
+                return addDataContainerChildren(Builders.unkeyedListBuilder().withNodeIdentifier(identifier)).build();
 
-            case NodeTypes.UNKEYED_LIST_ITEM :
+            case NodeTypes.UNKEYED_LIST_ITEM:
                 LOG.debug("Read unkeyed list item node {}", identifier);
-                return addDataContainerChildren(Builders.unkeyedListEntryBuilder().
-                        withNodeIdentifier(identifier)).build();
+                return addDataContainerChildren(Builders.unkeyedListEntryBuilder()
+                        .withNodeIdentifier(identifier)).build();
 
-            case NodeTypes.CONTAINER_NODE :
+            case NodeTypes.CONTAINER_NODE:
                 LOG.debug("Read container node {}", identifier);
-                return addDataContainerChildren(Builders.containerBuilder().
-                        withNodeIdentifier(identifier)).build();
+                return addDataContainerChildren(Builders.containerBuilder().withNodeIdentifier(identifier)).build();
 
             case NodeTypes.LEAF_SET :
                 LOG.debug("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);
+                ListNodeBuilder<Object, LeafSetEntryNode<Object>> orderedLeafSetBuilder =
+                        Builders.orderedLeafSetBuilder().withNodeIdentifier(identifier);
+                orderedLeafSetBuilder = addLeafSetChildren(identifier.getNodeType(), orderedLeafSetBuilder);
+                return orderedLeafSetBuilder.build();
+
             default :
                 return null;
         }
     }
 
+    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 == NormalizedNodeOutputStreamWriter.IS_CODE_VALUE) {
+        if (valueType == TokenTypes.IS_CODE_VALUE) {
             return codedStringMap.get(input.readInt());
-        } else if(valueType == NormalizedNodeOutputStreamWriter.IS_STRING_VALUE) {
+        } else if (valueType == TokenTypes.IS_STRING_VALUE) {
             String value = input.readUTF().intern();
             codedStringMap.put(Integer.valueOf(codedStringMap.size()), value);
             return value;
@@ -244,11 +277,11 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
         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;
@@ -258,7 +291,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
         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());
         }
 
@@ -267,7 +300,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
 
     private Object readObject() throws IOException {
         byte objectType = input.readByte();
-        switch(objectType) {
+        switch (objectType) {
             case ValueTypes.BITS_TYPE:
                 return readObjSet();
 
@@ -320,6 +353,21 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
         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();
         return readYangInstanceIdentifierInternal();
@@ -330,7 +378,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
 
         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);
@@ -339,17 +387,18 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
     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;
     }
 
+    @Override
     public PathArgument readPathArgument() throws IOException {
         // read Type
         int type = input.readByte();
 
-        switch(type) {
+        switch (type) {
 
             case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
                 return new YangInstanceIdentifier.AugmentationIdentifier(readQNameSet());
@@ -361,7 +410,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
                 return new NodeIdentifierWithPredicates(readQName(), readKeyValueMap());
 
             case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
-                return new NodeWithValue(readQName(), readObject());
+                return new NodeWithValue<>(readQName(), readObject());
 
             default :
                 return null;
@@ -378,7 +427,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
 
         LeafSetEntryNode<Object> child = (LeafSetEntryNode<Object>)readNormalizedNodeInternal();
 
-        while(child != null) {
+        while (child != null) {
             builder.withChild(child);
             child = (LeafSetEntryNode<Object>)readNormalizedNodeInternal();
         }
@@ -392,10 +441,100 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
 
         NormalizedNode<?, ?> child = readNormalizedNodeInternal();
 
-        while(child != null) {
+        while (child != null) {
             builder.addChild(child);
             child = readNormalizedNodeInternal();
         }
         return builder;
     }
+
+    @Override
+    public void readFully(final byte[] value) throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        input.readFully(value);
+    }
+
+    @Override
+    public void readFully(final byte[] str, final int off, final int len) throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        input.readFully(str, off, len);
+    }
+
+    @Override
+    public int skipBytes(final int num) throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        return input.skipBytes(num);
+    }
+
+    @Override
+    public boolean readBoolean() throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        return input.readBoolean();
+    }
+
+    @Override
+    public byte readByte() throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        return input.readByte();
+    }
+
+    @Override
+    public int readUnsignedByte() throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        return input.readUnsignedByte();
+    }
+
+    @Override
+    public short readShort() throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        return input.readShort();
+    }
+
+    @Override
+    public int readUnsignedShort() throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        return input.readUnsignedShort();
+    }
+
+    @Override
+    public char readChar() throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        return input.readChar();
+    }
+
+    @Override
+    public int readInt() throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        return input.readInt();
+    }
+
+    @Override
+    public long readLong() throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        return input.readLong();
+    }
+
+    @Override
+    public float readFloat() throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        return input.readFloat();
+    }
+
+    @Override
+    public double readDouble() throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        return input.readDouble();
+    }
+
+    @Override
+    public String readLine() throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        return input.readLine();
+    }
+
+    @Override
+    public String readUTF() throws IOException {
+        readSignatureMarkerAndVersionIfNeeded();
+        return input.readUTF();
+    }
 }