Do not format QNames to string on input
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / NormalizedNodeInputStreamReader.java
index 3b912e6cb9064e2bd7ff50a5bbaa72e629eea131..d5ac1580acc8dee0af2a9846586857859ac15f8d 100755 (executable)
@@ -10,6 +10,8 @@ package org.opendaylight.controller.cluster.datastore.node.utils.stream;
 
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableList.Builder;
 import java.io.DataInput;
 import java.io.IOException;
 import java.io.StringReader;
@@ -17,16 +19,15 @@ 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.util.ImmutableOffsetMapTemplate;
+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;
@@ -56,11 +57,9 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
 
     private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeInputStreamReader.class);
 
-    private static final String REVISION_ARG = "?revision=";
-
     private final DataInput input;
 
-    private final Map<Integer, String> codedStringMap = new HashMap<>();
+    private final List<String> codedStringMap = new ArrayList<>();
 
     private QName lastLeafSetQName;
 
@@ -70,8 +69,6 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
     @SuppressWarnings("rawtypes")
     private NormalizedNodeAttrBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafSetEntryBuilder;
 
-    private final StringBuilder reusableStringBuilder = new StringBuilder(50);
-
     private boolean readSignatureMarker = true;
 
     NormalizedNodeInputStreamReader(final DataInput input, final boolean versionChecked) {
@@ -136,8 +133,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
                 return leafSetEntryBuilder().withNodeIdentifier(leafIdentifier).withValue(value).build();
 
             case NodeTypes.MAP_ENTRY_NODE :
-                NodeIdentifierWithPredicates entryIdentifier = new NodeIdentifierWithPredicates(
-                        readQName(), readKeyValueMap());
+                NodeIdentifierWithPredicates entryIdentifier = readNormalizedNodeWithPredicates();
 
                 LOG.trace("Reading map entry node {} ", entryIdentifier);
 
@@ -213,12 +209,10 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
 
             case NodeTypes.ORDERED_LEAF_SET:
                 LOG.trace("Read ordered leaf set node {}", identifier);
-                ListNodeBuilder<Object, LeafSetEntryNode<Object>> orderedLeafSetBuilder =
-                        Builders.orderedLeafSetBuilder().withNodeIdentifier(identifier);
-                orderedLeafSetBuilder = addLeafSetChildren(identifier.getNodeType(), orderedLeafSetBuilder);
-                return orderedLeafSetBuilder.build();
+                return addLeafSetChildren(identifier.getNodeType(),
+                        Builders.orderedLeafSetBuilder().withNodeIdentifier(identifier)).build();
 
-            default :
+            default:
                 return null;
         }
     }
@@ -240,32 +234,31 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
         // 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();
-        } else {
-            qname = reusableStringBuilder.append('(').append(namespace).append(')').append(localName).toString();
-        }
+        String revision = Strings.emptyToNull(readCodedString());
 
-        reusableStringBuilder.delete(0, reusableStringBuilder.length());
-        return QNameFactory.create(qname);
+        return QNameFactory.create(new QNameFactory.Key(localName, namespace, revision));
     }
 
 
     private String readCodedString() throws IOException {
-        byte valueType = input.readByte();
-        if (valueType == TokenTypes.IS_CODE_VALUE) {
-            return codedStringMap.get(input.readInt());
-        } else if (valueType == TokenTypes.IS_STRING_VALUE) {
-            String value = input.readUTF().intern();
-            codedStringMap.put(codedStringMap.size(), value);
-            return value;
+        final byte valueType = input.readByte();
+        switch (valueType) {
+            case TokenTypes.IS_NULL_VALUE:
+                return null;
+            case TokenTypes.IS_CODE_VALUE:
+                final int code = input.readInt();
+                try {
+                    return codedStringMap.get(code);
+                } catch (IndexOutOfBoundsException e) {
+                    throw new IOException("String code " + code + " was not found", e);
+                }
+            case TokenTypes.IS_STRING_VALUE:
+                final String value = input.readUTF().intern();
+                codedStringMap.add(value);
+                return value;
+            default:
+                throw new IOException("Unhandled string value type " + valueType);
         }
-
-        return null;
     }
 
     private Set<QName> readQNameSet() throws IOException {
@@ -278,15 +271,26 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
         return children;
     }
 
-    private Map<QName, Object> readKeyValueMap() throws IOException {
-        int count = input.readInt();
-        Map<QName, Object> keyValueMap = new HashMap<>(count);
+    private NodeIdentifierWithPredicates readNormalizedNodeWithPredicates() throws IOException {
+        final QName qname = readQName();
+        final int count = input.readInt();
+        switch (count) {
+            case 0:
+                return new NodeIdentifierWithPredicates(qname);
+            case 1:
+                return new NodeIdentifierWithPredicates(qname, readQName(), readObject());
+            default:
+                // ImmutableList is used by ImmutableOffsetMapTemplate for lookups, hence we use that.
+                final Builder<QName> keys = ImmutableList.builderWithExpectedSize(count);
+                final Object[] values = new Object[count];
+                for (int i = 0; i < count; i++) {
+                    keys.add(readQName());
+                    values[i] = readObject();
+                }
 
-        for (int i = 0; i < count; i++) {
-            keyValueMap.put(readQName(), readObject());
+                return new NodeIdentifierWithPredicates(qname, ImmutableOffsetMapTemplate.ordered(keys.build())
+                    .instantiateWithValues(values));
         }
-
-        return keyValueMap;
     }
 
     private Object readObject() throws IOException {
@@ -333,6 +337,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;
         }
@@ -350,12 +363,12 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
 
         final boolean absolute = input.readBoolean();
         final int size = input.readInt();
-        final Collection<QName> qnames = new ArrayList<>(size);
+
+        final Builder<QName> qnames = ImmutableList.builderWithExpectedSize(size);
         for (int i = 0; i < size; ++i) {
             qnames.add(readQName());
         }
-
-        return SchemaPath.create(qnames, absolute);
+        return SchemaPath.create(qnames.build(), absolute);
     }
 
     @Override
@@ -366,13 +379,11 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
 
     private YangInstanceIdentifier readYangInstanceIdentifierInternal() throws IOException {
         int size = input.readInt();
-
-        List<PathArgument> pathArguments = new ArrayList<>(size);
-
+        final Builder<PathArgument> pathArguments = ImmutableList.builderWithExpectedSize(size);
         for (int i = 0; i < size; i++) {
             pathArguments.add(readPathArgument());
         }
-        return YangInstanceIdentifier.create(pathArguments);
+        return YangInstanceIdentifier.create(pathArguments.build());
     }
 
     private Set<String> readObjSet() throws IOException {
@@ -398,7 +409,7 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeDataInput
                 return new NodeIdentifier(readQName());
 
             case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES :
-                return new NodeIdentifierWithPredicates(readQName(), readKeyValueMap());
+                return readNormalizedNodeWithPredicates();
 
             case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
                 return new NodeWithValue<>(readQName(), readObject());