Bug 7521: Convert Snapshot to store a State instance
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / ShardDataTreeSnapshot.java
index 6cb047fc375552e8bd77af204ca5bc7196898e00..7b8382e9c2536bf44b6dea553e8b81f5c855aeeb 100644 (file)
@@ -12,8 +12,8 @@ import java.io.ByteArrayInputStream;
 import java.io.DataInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.Optional;
-import javax.annotation.Nonnull;
 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.slf4j.Logger;
@@ -32,6 +32,7 @@ public abstract class ShardDataTreeSnapshot {
         // Hidden to prevent subclassing from outside of this package
     }
 
+    @Deprecated
     public static ShardDataTreeSnapshot deserialize(final byte[] bytes) throws IOException {
         /**
          * Unfortunately versions prior to Boron did not include any way to evolve the snapshot format and contained
@@ -50,18 +51,7 @@ public abstract class ShardDataTreeSnapshot {
 
         try {
             try (final InputStream is = new ByteArrayInputStream(bytes)) {
-                try (final DataInputStream dis = new DataInputStream(is)) {
-                    final ShardDataTreeSnapshot ret = AbstractVersionedShardDataTreeSnapshot.deserialize(dis);
-
-                    // Make sure we consume all bytes, otherwise something went very wrong
-                    final int bytesLeft = dis.available();
-                    if (bytesLeft != 0) {
-                        throw new IOException("Deserialization left " + bytesLeft + " in the buffer");
-                    }
-
-
-                    return ret;
-                }
+                return deserialize(is);
             }
         } catch (IOException e) {
             LOG.debug("Failed to deserialize versioned stream, attempting pre-Lithium ProtoBuf", e);
@@ -69,6 +59,21 @@ public abstract class ShardDataTreeSnapshot {
         }
     }
 
+    public static ShardDataTreeSnapshot deserialize(final InputStream is) throws IOException {
+        try (final DataInputStream dis = new DataInputStream(is)) {
+            final ShardDataTreeSnapshot ret = AbstractVersionedShardDataTreeSnapshot.deserialize(dis);
+
+            // Make sure we consume all bytes, otherwise something went very wrong
+            final int bytesLeft = dis.available();
+            if (bytesLeft != 0) {
+                throw new IOException("Deserialization left " + bytesLeft + " in the buffer");
+            }
+
+
+            return ret;
+        }
+    }
+
     /**
      * Get the root data node contained in this snapshot.
      *
@@ -76,14 +81,9 @@ public abstract class ShardDataTreeSnapshot {
      */
     public abstract Optional<NormalizedNode<?, ?>> getRootNode();
 
-    /**
-     * Serialize this snapshot into a byte array for persistence.
-     *
-     * @return Serialized snapshot
-     * @throws IOException when a serialization problem occurs
-     */
-    public abstract @Nonnull byte[] serialize() throws IOException;
+    public abstract void serialize(final OutputStream os) throws IOException;
 
+    @Deprecated
     private static boolean isLegacyStream(final byte[] bytes) {
         if (bytes.length < 2) {
             // Versioned streams have at least two bytes