Define DataStoreVersions.MAGNESIUM_VERSION
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / VersionedExternalizableMessage.java
index 2a660fa..f28250b 100644 (file)
@@ -11,6 +11,9 @@ import java.io.Externalizable;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
+import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeStreamVersion;
 
 /**
  * Abstract base class for a versioned Externalizable message.
@@ -20,26 +23,54 @@ import java.io.ObjectOutput;
 public abstract class VersionedExternalizableMessage implements Externalizable, SerializableMessage {
     private static final long serialVersionUID = 1L;
 
-    private short version;
+    private short version = DataStoreVersions.CURRENT_VERSION;
 
     public VersionedExternalizableMessage() {
     }
 
-    public VersionedExternalizableMessage(short version) {
-        this.version = version;
+    public VersionedExternalizableMessage(final short version) {
+        this.version = version <= DataStoreVersions.CURRENT_VERSION ? version : DataStoreVersions.CURRENT_VERSION;
     }
 
     public short getVersion() {
         return version;
     }
 
+    protected final @NonNull NormalizedNodeStreamVersion getStreamVersion() {
+        if (version >= DataStoreVersions.MAGNESIUM_VERSION) {
+            return NormalizedNodeStreamVersion.MAGNESIUM;
+        } else if (version == DataStoreVersions.SODIUM_SR1_VERSION) {
+            return NormalizedNodeStreamVersion.SODIUM_SR1;
+        } else if (version == DataStoreVersions.NEON_SR2_VERSION) {
+            return NormalizedNodeStreamVersion.NEON_SR2;
+        } else {
+            return NormalizedNodeStreamVersion.LITHIUM;
+        }
+    }
+
     @Override
-    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+    public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
         version = in.readShort();
     }
 
     @Override
-    public void writeExternal(ObjectOutput out) throws IOException {
+    public void writeExternal(final ObjectOutput out) throws IOException {
         out.writeShort(version);
     }
+
+    @Override
+    public final Object toSerializable() {
+        final short ver = getVersion();
+        if (ver < DataStoreVersions.BORON_VERSION) {
+            throw new UnsupportedOperationException("Version " + ver
+                + " is older than the oldest version supported version " + DataStoreVersions.BORON_VERSION);
+        }
+
+        return this;
+    }
+
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + " [version=" + getVersion() + "]";
+    }
 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.