Bump persisted PayloadVersion
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / AbstractVersionedShardDataTreeSnapshot.java
index 1d9b58f27049046fdee8f62d2e690e2344439821..21d018df12da01b24cd2191491c139318b0cec6d 100644 (file)
@@ -8,14 +8,11 @@
 package org.opendaylight.controller.cluster.datastore.persisted;
 
 import com.google.common.base.Verify;
-import java.io.DataInputStream;
-import java.io.DataOutputStream;
 import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.OutputStream;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
 import java.util.Optional;
-import javax.annotation.Nonnull;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -30,27 +27,30 @@ abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnaps
     private static final Logger LOG = LoggerFactory.getLogger(AbstractVersionedShardDataTreeSnapshot.class);
 
     @SuppressWarnings("checkstyle:FallThrough")
-    static ShardDataTreeSnapshot deserialize(final DataInputStream is) throws IOException {
-        final PayloadVersion version = PayloadVersion.readFrom(is);
+    static @NonNull ShardSnapshotState versionedDeserialize(final ObjectInput in) throws IOException {
+        final PayloadVersion version = PayloadVersion.readFrom(in);
         switch (version) {
             case BORON:
-                // Boron snapshots use Java Serialization
-                try (final ObjectInputStream ois = new ObjectInputStream(is)) {
-                    return (ShardDataTreeSnapshot) ois.readObject();
-                } catch (ClassNotFoundException e) {
-                    LOG.error("Failed to serialize data tree snapshot", e);
-                    throw new IOException("Snapshot failed to deserialize", e);
-                }
+                return new ShardSnapshotState(readSnapshot(in), true);
+            case SODIUM:
+                return new ShardSnapshotState(readSnapshot(in), false);
             case TEST_FUTURE_VERSION:
             case TEST_PAST_VERSION:
                 // These versions are never returned and this code is effectively dead
-                break;
             default:
-                throw new IOException("Invalid payload version in snapshot");
+                // Not included as default in above switch to ensure we get warnings when new versions are added
+                throw new IOException("Encountered unhandled version" + version);
         }
+    }
 
-        // Not included as default in above switch to ensure we get warnings when new versions are added
-        throw new IOException("Encountered unhandled version" + version);
+    // Boron and Sodium snapshots use Java Serialization, but differ in stream format
+    private static @NonNull ShardDataTreeSnapshot readSnapshot(final ObjectInput in) throws IOException {
+        try {
+            return (ShardDataTreeSnapshot) in.readObject();
+        } catch (ClassNotFoundException e) {
+            LOG.error("Failed to serialize data tree snapshot", e);
+            throw new IOException("Snapshot failed to deserialize", e);
+        }
     }
 
     @Override
@@ -63,24 +63,21 @@ abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnaps
      *
      * @return The root node.
      */
-    @Nonnull
-    abstract NormalizedNode<?, ?> rootNode();
+    abstract @NonNull NormalizedNode<?, ?> rootNode();
 
     /**
      * Return the snapshot payload version. Implementations of this method should return a constant.
      *
      * @return Snapshot payload version
      */
-    @Nonnull
-    abstract PayloadVersion version();
+    abstract @NonNull PayloadVersion version();
 
-    private void versionedSerialize(final DataOutputStream dos, final PayloadVersion version) throws IOException {
+    private void versionedSerialize(final ObjectOutput out, final PayloadVersion version) throws IOException {
         switch (version) {
             case BORON:
-                // Boron snapshots use Java Serialization
-                try (ObjectOutputStream oos = new ObjectOutputStream(dos)) {
-                    oos.writeObject(this);
-                }
+            case SODIUM:
+                // Boron and Sodium snapshots use Java Serialization, but differ in stream format
+                out.writeObject(this);
                 return;
             case TEST_FUTURE_VERSION:
             case TEST_PAST_VERSION:
@@ -93,11 +90,9 @@ abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnaps
     }
 
     @Override
-    public void serialize(final OutputStream os) throws IOException {
-        try (final DataOutputStream dos = new DataOutputStream(os)) {
-            final PayloadVersion version = version();
-            version.writeTo(dos);
-            versionedSerialize(dos, version);
-        }
+    public void serialize(final ObjectOutput out) throws IOException {
+        final PayloadVersion version = version();
+        version.writeTo(out);
+        versionedSerialize(out, version);
     }
 }