From: Robert Varga Date: Thu, 26 Nov 2015 11:46:59 +0000 (+0100) Subject: BUG-4626: Split out stream token types X-Git-Tag: release/beryllium~100 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=3ee741416f6f67699392f8d9de9142f828d8981e BUG-4626: Split out stream token types 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 --- diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java index 0260de1663..03bd56eae7 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeInputStreamReader.java @@ -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; diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeOutputStreamWriter.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeOutputStreamWriter.java index 9ff7457298..a266a2f220 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeOutputStreamWriter.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeOutputStreamWriter.java @@ -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 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 index 0000000000..90f0472eb2 --- /dev/null +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/TokenTypes.java @@ -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; +}