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 b2c9ea5..ac65b5b 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 d96be31..926b552 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 895de3a..3f179cf 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 fa4fca5..d2cf5fd 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 738a74f..69deff4 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 bc59c54..f49394a 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 3e32233..549a274 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 2b800c8..4207cd5 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 036cb58..cd94795 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 ec38f74..634d049 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 7d1bcdb..6dec833 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 e68724a..7eab816 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 c2709d7..7837493 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 72186af..747ec99 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 636fa5a..475ef3a 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 17b93f4..e1f96ca 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() + "]";
+    }
 }

©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.