BUG-4626: Split out stream token types 83/30283/12
authorRobert Varga <rovarga@cisco.com>
Thu, 26 Nov 2015 11:46:59 +0000 (12:46 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 10 Dec 2015 13:08:14 +0000 (13:08 +0000)
Move token types and version into a dedicated internal class, adding
some documentation and introducing planned token types.

Also adds validation of stream version on reception, so that parsing
fails predictably in case of sender doing the worng thing and sending us
an unrecognized version.

Change-Id: I85fd9615b897e0fe4addb7f1807416d7bfe1b903
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeOutputStreamWriter.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/TokenTypes.java [new file with mode: 0644]

index 0260de16639997f0a44fc50c6e2da9865d2ec78a..03bd56eae74370a8631fca1985acad59a5d4271d 100644 (file)
@@ -93,13 +93,16 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
         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));
+            }
         }
     }
 
@@ -237,9 +240,9 @@ public class NormalizedNodeInputStreamReader implements NormalizedNodeStreamRead
 
     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;
index 9ff7457298ef3c43745d0b76ed21c6b19e1f3314..a266a2f2202904c190ca873fa7764709a767ecac 100644 (file)
@@ -45,13 +45,6 @@ public class NormalizedNodeOutputStreamWriter implements NormalizedNodeStreamWri
 
     private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeOutputStreamWriter.class);
 
-    static final byte SIGNATURE_MARKER = (byte) 0xab;
-    static final short CURRENT_VERSION = (short) 1;
-
-    static final byte IS_CODE_VALUE = 1;
-    static final byte IS_STRING_VALUE = 2;
-    static final byte IS_NULL_VALUE = 3;
-
     private final DataOutput output;
 
     private final Map<String, Integer> stringCodeMap = new HashMap<>();
@@ -87,8 +80,8 @@ public class NormalizedNodeOutputStreamWriter implements NormalizedNodeStreamWri
 
     private void writeSignatureMarkerAndVersionIfNeeded() throws IOException {
         if(!wroteSignatureMarker) {
-            output.writeByte(SIGNATURE_MARKER);
-            output.writeShort(CURRENT_VERSION);
+            output.writeByte(TokenTypes.SIGNATURE_MARKER);
+            output.writeShort(TokenTypes.LITHIUM_VERSION);
             wroteSignatureMarker = true;
         }
     }
@@ -246,15 +239,15 @@ public class NormalizedNodeOutputStreamWriter implements NormalizedNodeStreamWri
     private void writeCodedString(String key) throws IOException {
         Integer value = stringCodeMap.get(key);
         if(value != null) {
-            output.writeByte(IS_CODE_VALUE);
+            output.writeByte(TokenTypes.IS_CODE_VALUE);
             output.writeInt(value);
         } else {
             if(key != null) {
-                output.writeByte(IS_STRING_VALUE);
+                output.writeByte(TokenTypes.IS_STRING_VALUE);
                 stringCodeMap.put(key, Integer.valueOf(stringCodeMap.size()));
                 output.writeUTF(key);
             } else {
-                output.writeByte(IS_NULL_VALUE);
+                output.writeByte(TokenTypes.IS_NULL_VALUE);
             }
         }
     }
diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/TokenTypes.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/TokenTypes.java
new file mode 100644 (file)
index 0000000..90f0472
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.datastore.node.utils.stream;
+
+final class TokenTypes {
+    private TokenTypes() {
+        throw new UnsupportedOperationException();
+    }
+
+    static final byte SIGNATURE_MARKER = (byte) 0xab;
+
+    /**
+     * Original stream version. Uses a per-stream dictionary for strings. QNames are serialized as three strings.
+     */
+    static final short LITHIUM_VERSION = 1;
+
+    // Tokens supported in LITHIUM_VERSION
+    static final byte IS_CODE_VALUE = 1;
+    static final byte IS_STRING_VALUE = 2;
+    static final byte IS_NULL_VALUE = 3;
+}