Refactor VersionedExternalizableMessage messages 71/33871/2
authorGary Wu <gary.wu1@huawei.com>
Tue, 2 Feb 2016 00:45:25 +0000 (16:45 -0800)
committerGary Wu <gary.wu1@huawei.com>
Fri, 5 Feb 2016 20:51:21 +0000 (12:51 -0800)
Refactor VersionedExternalizableMessage messages
to centralize version checking in toSerializable().

Change-Id: I6ad4477eb16e104ab801205842875efa164817af
Signed-off-by: Gary Wu <gary.wu1@huawei.com>
17 files changed:
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/AbortTransactionReply.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/AbstractThreePhaseCommitMessage.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/BatchedModificationsReply.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/CanCommitTransactionReply.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/CloseTransaction.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/CloseTransactionChain.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/CloseTransactionReply.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/CommitTransactionReply.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/CreateTransaction.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/CreateTransactionReply.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/DataExists.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/DataExistsReply.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/EmptyReply.java [deleted file]
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/ReadData.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/ReadDataReply.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/ReadyTransactionReply.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/VersionedExternalizableMessage.java

index b2c9ea563f37ac828908aa4cd173188922b99cfe..ac65b5b4898ed9c72c6bd2a631a670f301889ed6 100644 (file)
@@ -11,7 +11,7 @@ package org.opendaylight.controller.cluster.datastore.messages;
 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
 import org.opendaylight.controller.protobuff.messages.cohort3pc.ThreePhaseCommitCohortMessages;
 
-public class AbortTransactionReply extends EmptyReply {
+public class AbortTransactionReply extends VersionedExternalizableMessage {
     private static final Object SERIALIZED_INSTANCE =
             ThreePhaseCommitCohortMessages.AbortTransactionReply.newBuilder().build();
 
index d96be31b2d1e598e494fc2030ee5f6370120072f..926b552a9ede2e601d33d767e6d8b96e09e0eb00 100644 (file)
@@ -11,7 +11,6 @@ import com.google.common.base.Preconditions;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
-import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
 
 /**
  * Base class for a 3PC message.
@@ -31,8 +30,6 @@ public abstract class AbstractThreePhaseCommitMessage extends VersionedExternali
         this.transactionID = Preconditions.checkNotNull(transactionID);
     }
 
-    protected abstract Object newLegacySerializedInstance();
-
     public String getTransactionID() {
         return transactionID;
     }
@@ -49,11 +46,6 @@ public abstract class AbstractThreePhaseCommitMessage extends VersionedExternali
         out.writeUTF(transactionID);
     }
 
-    @Override
-    public Object toSerializable() {
-        return getVersion() >= DataStoreVersions.BORON_VERSION ? this : newLegacySerializedInstance();
-    }
-
     @Override
     public String toString() {
         return getClass().getSimpleName() + " [transactionID=" + transactionID + ", version=" + getVersion()
index 895de3a62626da3d0d332a205a866b5997f2b320..3f179cf0019ac825781032588d678499e8b27061 100644 (file)
@@ -45,7 +45,8 @@ public class BatchedModificationsReply extends VersionedExternalizableMessage {
     }
 
     @Override
-    public Object toSerializable() {
+    protected Object newLegacySerializedInstance() {
+        // no legacy serialized type for this class; return self
         return this;
     }
 
index fa4fca508916725db298b4bb7bc20e1863005b3e..d2cf5fde7e5191dab033a7a1c1e0808a9eb35132 100644 (file)
@@ -53,12 +53,8 @@ public class CanCommitTransactionReply extends VersionedExternalizableMessage {
     }
 
     @Override
-    public Object toSerializable() {
-        if(getVersion() >= DataStoreVersions.BORON_VERSION) {
-            return this;
-        } else {
-            return canCommit ? YES_SERIALIZED : NO_SERIALIZED;
-        }
+    protected Object newLegacySerializedInstance() {
+        return canCommit ? YES_SERIALIZED : NO_SERIALIZED;
     }
 
     @Override
index 738a74f17b1a3d985bd0862dd93fbe3d8e861bf0..69deff49694533f28a592845647f147c6c86fd3a 100644 (file)
@@ -8,7 +8,6 @@
 
 package org.opendaylight.controller.cluster.datastore.messages;
 
-import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
 
 public class CloseTransaction extends VersionedExternalizableMessage {
@@ -25,8 +24,8 @@ public class CloseTransaction extends VersionedExternalizableMessage {
     }
 
     @Override
-    public Object toSerializable() {
-        return getVersion() >= DataStoreVersions.BORON_VERSION ? this : SERIALIZED_INSTANCE;
+    protected Object newLegacySerializedInstance() {
+        return SERIALIZED_INSTANCE;
     }
 
     public static boolean isSerializedType(Object message) {
index bc59c544ca63c3935753435b0c2d49a29f886e76..f49394a6cdab89158ff314d0751cffb45392b627 100644 (file)
@@ -44,13 +44,9 @@ public class CloseTransactionChain extends VersionedExternalizableMessage {
     }
 
     @Override
-    public Object toSerializable() {
-        if(getVersion() >= DataStoreVersions.BORON_VERSION) {
-            return this;
-        } else {
-            return ShardTransactionChainMessages.CloseTransactionChain.newBuilder()
-                .setTransactionChainId(transactionChainId).build();
-        }
+    protected Object newLegacySerializedInstance() {
+        return ShardTransactionChainMessages.CloseTransactionChain.newBuilder().setTransactionChainId(transactionChainId)
+                .build();
     }
 
     public static CloseTransactionChain fromSerializable(final Object serializable){
index 3e32233959c8c6df3a36c96e0e3da2cc3c72cf25..549a27475978aca4278fa45922bbd5a93e1fe7f9 100644 (file)
@@ -15,7 +15,8 @@ public class CloseTransactionReply extends VersionedExternalizableMessage {
     }
 
     @Override
-    public Object toSerializable() {
+    protected Object newLegacySerializedInstance() {
+        // no legacy serialized type for this class; return self
         return this;
     }
 }
index 2b800c8346657ddf5f8ce7e005302c41856c76ff..4207cd5877e756b9a6c198728a6cbc0fd0eb8e22 100644 (file)
@@ -11,7 +11,7 @@ package org.opendaylight.controller.cluster.datastore.messages;
 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
 import org.opendaylight.controller.protobuff.messages.cohort3pc.ThreePhaseCommitCohortMessages;
 
-public class CommitTransactionReply extends EmptyReply {
+public class CommitTransactionReply extends VersionedExternalizableMessage {
     private static final Object SERIALIZED_INSTANCE =
             ThreePhaseCommitCohortMessages.CommitTransactionReply.newBuilder().build();
 
index 036cb586144a92e683912292007b4f4a0b844cca..cd9479543efca935cbb4640ef29db9af7c25ab10 100644 (file)
@@ -12,7 +12,6 @@ import com.google.common.base.Preconditions;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
-import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
 
 public class CreateTransaction extends VersionedExternalizableMessage {
@@ -62,14 +61,10 @@ public class CreateTransaction extends VersionedExternalizableMessage {
     }
 
     @Override
-    public Object toSerializable() {
-        if(getVersion() >= DataStoreVersions.BORON_VERSION) {
-            return this;
-        } else {
-            return ShardTransactionMessages.CreateTransaction.newBuilder()
-                .setTransactionId(transactionId).setTransactionType(transactionType)
-                .setTransactionChainId(transactionChainId).setMessageVersion(getVersion()).build();
-        }
+    protected Object newLegacySerializedInstance() {
+        return ShardTransactionMessages.CreateTransaction.newBuilder().setTransactionId(transactionId)
+                .setTransactionType(transactionType).setTransactionChainId(transactionChainId)
+                .setMessageVersion(getVersion()).build();
     }
 
     @Override
index ec38f749e9657e5c1e633022a7a32ffba08a31e1..634d0492f9cd7110ccab8910ee3344bae1526e9f 100644 (file)
@@ -11,7 +11,6 @@ package org.opendaylight.controller.cluster.datastore.messages;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
-import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
 
 public class CreateTransactionReply extends VersionedExternalizableMessage {
@@ -52,13 +51,9 @@ public class CreateTransactionReply extends VersionedExternalizableMessage {
     }
 
     @Override
-    public Object toSerializable() {
-        if(getVersion() >= DataStoreVersions.BORON_VERSION) {
-            return this;
-        } else {
-            return ShardTransactionMessages.CreateTransactionReply.newBuilder().setTransactionActorPath(transactionPath)
-                    .setTransactionId(transactionId).setMessageVersion(getVersion()).build();
-        }
+    protected Object newLegacySerializedInstance() {
+        return ShardTransactionMessages.CreateTransactionReply.newBuilder().setTransactionActorPath(transactionPath)
+                .setTransactionId(transactionId).setMessageVersion(getVersion()).build();
     }
 
     @Override
index 7d1bcdb8f65c3273e50685eaa58c76cdc6d67d9b..6dec83308820ce6d656580382eeb34f517929d32 100644 (file)
@@ -28,13 +28,9 @@ public class DataExists extends AbstractRead<Boolean> {
     }
 
     @Override
-    public Object toSerializable() {
-        if(getVersion() >= DataStoreVersions.BORON_VERSION) {
-            return this;
-        } else {
-            return ShardTransactionMessages.DataExists.newBuilder().setInstanceIdentifierPathArguments(
-                        InstanceIdentifierUtils.toSerializable(getPath())).build();
-        }
+    protected Object newLegacySerializedInstance() {
+        return ShardTransactionMessages.DataExists.newBuilder()
+                .setInstanceIdentifierPathArguments(InstanceIdentifierUtils.toSerializable(getPath())).build();
     }
 
     @Override
index e68724a2626766091bd988f60ed54cd1b5fe9b16..7eab81695f0d2eb61341256e52df0ff182e88465 100644 (file)
@@ -49,12 +49,8 @@ public class DataExistsReply extends VersionedExternalizableMessage {
     }
 
     @Override
-    public Object toSerializable() {
-        if(getVersion() >= DataStoreVersions.BORON_VERSION) {
-            return this;
-        } else {
-            return exists ? SERIALIZABLE_TRUE : SERIALIZABLE_FALSE;
-        }
+    protected Object newLegacySerializedInstance() {
+        return exists ? SERIALIZABLE_TRUE : SERIALIZABLE_FALSE;
     }
 
     public static DataExistsReply fromSerializable(final Object serializable) {
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/EmptyReply.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/messages/EmptyReply.java
deleted file mode 100644 (file)
index 7eea727..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright (c) 2015 Brocade Communications 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.messages;
-
-import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
-
-/**
- * A reply with no data.
- *
- * @author Thomas Pantelis
- */
-public abstract class EmptyReply extends VersionedExternalizableMessage {
-    protected EmptyReply() {
-    }
-
-    protected EmptyReply(short version) {
-        super(version);
-    }
-
-    protected abstract Object newLegacySerializedInstance();
-
-    @Override
-    public Object toSerializable() {
-        return getVersion() >= DataStoreVersions.BORON_VERSION ? this : newLegacySerializedInstance();
-    }
-
-    @Override
-    public String toString() {
-        return getClass().getSimpleName() + " [version=" + getVersion() + "]";
-    }
-}
index c2709d757fdacc83ea6932337dd27ee64f7ab46f..7837493970cb2860fd7d819876f848318068f143 100644 (file)
@@ -30,13 +30,9 @@ public class ReadData extends AbstractRead<Optional<NormalizedNode<?, ?>>> {
     }
 
     @Override
-    public Object toSerializable() {
-        if(getVersion() >= DataStoreVersions.BORON_VERSION) {
-            return this;
-        } else {
-            return ShardTransactionMessages.ReadData.newBuilder().setInstanceIdentifierPathArguments(
-                    InstanceIdentifierUtils.toSerializable(getPath())).build();
-        }
+    protected Object newLegacySerializedInstance() {
+        return ShardTransactionMessages.ReadData.newBuilder()
+                .setInstanceIdentifierPathArguments(InstanceIdentifierUtils.toSerializable(getPath())).build();
     }
 
     @Override
index 72186af3f96a128f86eb090405b9152d8e8c4d1b..747ec99a715cd9e17d9801337d3d505780448197 100644 (file)
@@ -44,7 +44,8 @@ public class ReadDataReply extends VersionedExternalizableMessage {
     }
 
     @Override
-    public Object toSerializable() {
+    protected Object newLegacySerializedInstance() {
+        // no legacy serialized type for this class; return self
         return this;
     }
 
index 636fa5ac2dfb44e6ad67d1ae7afffd3ac2c9881e..475ef3a3633d49566b719c2d3e138820cb08384a 100644 (file)
@@ -47,7 +47,8 @@ public class ReadyTransactionReply extends VersionedExternalizableMessage {
     }
 
     @Override
-    public Object toSerializable() {
+    protected Object newLegacySerializedInstance() {
+        // no legacy serialized type for this class; return self
         return this;
     }
 
index 17b93f4b04d5f6d7445f6b2f1d262b7d41162d0d..e1f96ca457cede16e9459f040ad755e0e5cc4f69 100644 (file)
@@ -43,4 +43,16 @@ public abstract class VersionedExternalizableMessage implements Externalizable,
     public void writeExternal(ObjectOutput out) throws IOException {
         out.writeShort(version);
     }
+
+    protected abstract Object newLegacySerializedInstance();
+
+    @Override
+    public final Object toSerializable() {
+        return getVersion() >= DataStoreVersions.BORON_VERSION ? this : newLegacySerializedInstance();
+    }
+
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + " [version=" + getVersion() + "]";
+    }
 }