Bug 3570: Persist snapshot on follower ApplySnapshot
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / MockRaftActor.java
index 53110b3583a4f7353e3d17b1ec7ac91bf8ec7891..c1aa75a12de5028331a818b7c4fb6a9c5a426b38 100644 (file)
@@ -29,6 +29,8 @@ import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payloa
 
 public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort, RaftActorSnapshotCohort {
 
+    public static final short PAYLOAD_VERSION = 5;
+
     final RaftActor actorDelegate;
     final RaftActorRecoveryCohort recoveryCohortDelegate;
     final RaftActorSnapshotCohort snapshotCohortDelegate;
@@ -70,7 +72,7 @@ public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort,
 
     public MockRaftActor(String id, Map<String, String> peerAddresses, Optional<ConfigParams> config,
                          DataPersistenceProvider dataPersistenceProvider) {
-        super(id, peerAddresses, config);
+        super(id, peerAddresses, config, PAYLOAD_VERSION);
         state = new ArrayList<>();
         this.actorDelegate = mock(RaftActor.class);
         this.recoveryCohortDelegate = mock(RaftActorRecoveryCohort.class);
@@ -154,10 +156,11 @@ public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort,
         return Props.create(new MockRaftActorCreator(peerAddresses, id, config, dataPersistenceProvider, roleChangeNotifier));
     }
 
-
     @Override protected void applyState(ActorRef clientActor, String identifier, Object data) {
         actorDelegate.applyState(clientActor, identifier, data);
-        LOG.info("{}: applyState called", persistenceId());
+        LOG.info("{}: applyState called: {}", persistenceId(), data);
+
+        state.add(data);
     }
 
     @Override
@@ -199,6 +202,10 @@ public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort,
     @Override
     public void applyRecoverySnapshot(byte[] bytes) {
         recoveryCohortDelegate.applyRecoverySnapshot(bytes);
+        applySnapshotBytes(bytes);
+    }
+
+    private void applySnapshotBytes(byte[] bytes) {
         try {
             Object data = toObject(bytes);
             if (data instanceof List) {
@@ -219,6 +226,7 @@ public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort,
     public void applySnapshot(byte [] snapshot) {
         LOG.info("{}: applySnapshot called", persistenceId());
         snapshotCohortDelegate.applySnapshot(snapshot);
+        applySnapshotBytes(snapshot);
     }
 
     @Override
@@ -235,7 +243,7 @@ public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort,
         return this.getId();
     }
 
-    private Object toObject(byte[] bs) throws ClassNotFoundException, IOException {
+    public static Object toObject(byte[] bs) throws ClassNotFoundException, IOException {
         Object obj = null;
         ByteArrayInputStream bis = null;
         ObjectInputStream ois = null;