Bug 7521: Convert DatastoreSnapshot.ShardSnapshot to store Snapshot
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / AbstractVersionedShardDataTreeSnapshot.java
index 190c815dd3508b69549f9ca9d50c6edc1027a829..c12403f81913f4b372c5e9ccfc533ebc7be5d6d6 100644 (file)
@@ -9,11 +9,10 @@ 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.ObjectInput;
 import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.OutputStream;
+import java.io.ObjectOutput;
 import java.util.Optional;
 import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -30,7 +29,8 @@ 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 {
+    @Deprecated
+    static ShardDataTreeSnapshot deserializePreCarbon(final DataInputStream is) throws IOException {
         final PayloadVersion version = PayloadVersion.readFrom(is);
         switch (version) {
             case BORON:
@@ -53,6 +53,30 @@ abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnaps
         throw new IOException("Encountered unhandled version" + version);
     }
 
+    @SuppressWarnings("checkstyle:FallThrough")
+    static ShardDataTreeSnapshot versionedDeserialize(final ObjectInput in) throws IOException {
+        final PayloadVersion version = PayloadVersion.readFrom(in);
+        switch (version) {
+            case BORON:
+                // Boron snapshots use Java Serialization
+                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);
+                }
+            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);
+    }
+
     @Override
     public final Optional<NormalizedNode<?, ?>> getRootNode() {
         return Optional.of(Verify.verifyNotNull(rootNode(), "Snapshot %s returned non-present root node", getClass()));
@@ -74,13 +98,11 @@ abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnaps
     @Nonnull
     abstract 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);
-                }
+                out.writeObject(this);
                 return;
             case TEST_FUTURE_VERSION:
             case TEST_PAST_VERSION:
@@ -93,11 +115,9 @@ abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnaps
     }
 
     @Override
-    public void serialize(final OutputStream os) throws IOException {
-        try (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);
     }
 }