Deprecate old MD-SAL APIs for removal
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardRecoveryCoordinator.java
index 634b6f698ab25e44093498c10ab4fcfed6678cc2..aeaad4880614445d4d708353c8644c1233a4e83d 100644 (file)
@@ -7,16 +7,15 @@
  */
 package org.opendaylight.controller.cluster.datastore;
 
-import com.google.common.base.Preconditions;
-import com.google.common.base.Throwables;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
 import java.io.File;
-import java.io.IOException;
 import org.opendaylight.controller.cluster.datastore.persisted.ShardDataTreeSnapshot;
 import org.opendaylight.controller.cluster.datastore.persisted.ShardSnapshotState;
 import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeXMLOutput;
 import org.opendaylight.controller.cluster.raft.RaftActorRecoveryCohort;
 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
-import org.opendaylight.controller.cluster.raft.persisted.Snapshot.State;
 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.slf4j.Logger;
@@ -30,21 +29,51 @@ import org.slf4j.Logger;
  *
  * @author Thomas Pantelis
  */
-class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
+abstract class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
+    private static final class Simple extends ShardRecoveryCoordinator {
+        Simple(final ShardDataTree store, final String shardName, final Logger log) {
+            super(store, shardName, log);
+        }
+
+        @Override
+        public Snapshot getRestoreFromSnapshot() {
+            return null;
+        }
+    }
+
+    private static final class WithSnapshot extends ShardRecoveryCoordinator {
+        private final Snapshot restoreFromSnapshot;
+
+        WithSnapshot(final ShardDataTree store, final String shardName, final Logger log, final Snapshot snapshot) {
+            super(store, shardName, log);
+            this.restoreFromSnapshot = requireNonNull(snapshot);
+        }
+
+        @Override
+        public Snapshot getRestoreFromSnapshot() {
+            return restoreFromSnapshot;
+        }
+    }
+
     private final ShardDataTree store;
     private final String shardName;
     private final Logger log;
-    private final Snapshot restoreFromSnapshot;
 
     private boolean open;
 
-    ShardRecoveryCoordinator(final ShardDataTree store,  final Snapshot restoreFromSnapshot, final String shardName,
-            final Logger log) {
-        this.store = Preconditions.checkNotNull(store);
-        this.shardName = Preconditions.checkNotNull(shardName);
-        this.log = Preconditions.checkNotNull(log);
+    ShardRecoveryCoordinator(final ShardDataTree store, final String shardName, final Logger log) {
+        this.store = requireNonNull(store);
+        this.shardName = requireNonNull(shardName);
+        this.log = requireNonNull(log);
+    }
+
+    static ShardRecoveryCoordinator create(final ShardDataTree store, final String shardName, final Logger log) {
+        return new Simple(store, shardName, log);
+    }
 
-        this.restoreFromSnapshot = restoreFromSnapshot;
+    static ShardRecoveryCoordinator forSnapshot(final ShardDataTree store, final String shardName, final Logger log,
+            final Snapshot snapshot) {
+        return new WithSnapshot(store, shardName, log, snapshot);
     }
 
     @Override
@@ -56,7 +85,7 @@ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
     @Override
     @SuppressWarnings("checkstyle:IllegalCatch")
     public void appendRecoveredLogEntry(final Payload payload) {
-        Preconditions.checkState(open, "call startLogRecovery before calling appendRecoveredLogEntry");
+        checkState(open, "call startLogRecovery before calling appendRecoveredLogEntry");
 
         try {
             store.applyRecoveryPayload(payload);
@@ -72,7 +101,7 @@ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
      */
     @Override
     public void applyCurrentLogRecoveryBatch() {
-        Preconditions.checkState(open, "call startLogRecovery before calling applyCurrentLogRecoveryBatch");
+        checkState(open, "call startLogRecovery before calling applyCurrentLogRecoveryBatch");
         open = false;
     }
 
@@ -86,13 +115,13 @@ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
     /**
      * Applies a recovered snapshot to the data store.
      *
-     * @param snapshotBytes the serialized snapshot
+     * @param snapshotState the serialized snapshot
      */
     @Override
     @SuppressWarnings("checkstyle:IllegalCatch")
     public void applyRecoverySnapshot(final Snapshot.State snapshotState) {
         if (!(snapshotState instanceof ShardSnapshotState)) {
-            log.debug("{}: applyRecoverySnapshot ignoring snapshot: {}", snapshotState);
+            log.debug("{}: applyRecoverySnapshot ignoring snapshot: {}", shardName, snapshotState);
         }
 
         log.debug("{}: Applying recovered snapshot", shardName);
@@ -107,20 +136,4 @@ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
                     shardName, shardSnapshot, f), e);
         }
     }
-
-    @Override
-    public Snapshot getRestoreFromSnapshot() {
-        return restoreFromSnapshot;
-    }
-
-    @Override
-    @Deprecated
-    public State deserializePreCarbonSnapshot(byte[] from) {
-        try {
-            return new ShardSnapshotState(ShardDataTreeSnapshot.deserializePreCarbon(from));
-        } catch (IOException e) {
-            log.error("{}: failed to deserialize snapshot", shardName, e);
-            throw Throwables.propagate(e);
-        }
-    }
 }