import akka.actor.ActorRef;
import akka.actor.Props;
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
import com.google.common.io.ByteSource;
import com.google.common.util.concurrent.Uninterruptibles;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
import org.apache.commons.lang3.SerializationUtils;
import org.opendaylight.controller.cluster.DataPersistenceProvider;
import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
+import org.opendaylight.controller.cluster.raft.messages.Payload;
import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
-import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
import org.opendaylight.yangtools.concepts.Identifier;
public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort, RaftActorSnapshotCohort {
protected MockRaftActor(final AbstractBuilder<?, ?> builder) {
super(builder.id, builder.peerAddresses != null ? builder.peerAddresses :
- Collections.<String, String>emptyMap(), Optional.fromNullable(builder.config), PAYLOAD_VERSION);
- state = new ArrayList<>();
- this.actorDelegate = mock(RaftActor.class);
- this.recoveryCohortDelegate = mock(RaftActorRecoveryCohort.class);
+ Collections.emptyMap(), Optional.ofNullable(builder.config), PAYLOAD_VERSION);
+ state = Collections.synchronizedList(new ArrayList<>());
+ actorDelegate = mock(RaftActor.class);
+ recoveryCohortDelegate = mock(RaftActorRecoveryCohort.class);
- this.snapshotCohortDelegate = builder.snapshotCohort != null ? builder.snapshotCohort :
+ snapshotCohortDelegate = builder.snapshotCohort != null ? builder.snapshotCohort :
mock(RaftActorSnapshotCohort.class);
if (builder.dataPersistenceProvider == null) {
- setPersistence(builder.persistent.isPresent() ? builder.persistent.get() : true);
+ setPersistence(builder.persistent.isPresent() ? builder.persistent.orElseThrow() : true);
} else {
setPersistence(builder.dataPersistenceProvider);
}
}
private void applySnapshotState(final Snapshot.State newState) {
- if (newState instanceof MockSnapshotState) {
+ if (newState instanceof MockSnapshotState mockState) {
state.clear();
- state.addAll(((MockSnapshotState)newState).getState());
+ state.addAll(mockState.getState());
}
}
@Override
- public void createSnapshot(final ActorRef actorRef, final java.util.Optional<OutputStream> installSnapshotStream) {
+ public void createSnapshot(final ActorRef actorRef, final Optional<OutputStream> installSnapshotStream) {
LOG.info("{}: createSnapshot called", persistenceId());
snapshotCohortDelegate.createSnapshot(actorRef, installSnapshotStream);
}
@Override
protected Optional<ActorRef> getRoleChangeNotifier() {
- return Optional.fromNullable(roleChangeNotifier);
+ return Optional.ofNullable(roleChangeNotifier);
}
@Override public String persistenceId() {
- return this.getId();
+ return getId();
}
protected void newBehavior(final RaftActorBehavior newBehavior) {
}
public static List<Object> fromState(final Snapshot.State from) {
- if (from instanceof MockSnapshotState) {
- return ((MockSnapshotState)from).getState();
+ if (from instanceof MockSnapshotState mockState) {
+ return mockState.getState();
}
throw new IllegalStateException("Unexpected snapshot State: " + from);
}
public ReplicatedLog getReplicatedLog() {
- return this.getRaftActorContext().getReplicatedLog();
+ return getRaftActorContext().getReplicatedLog();
}
@Override
private ActorRef roleChangeNotifier;
private RaftActorSnapshotMessageSupport snapshotMessageSupport;
private Snapshot restoreFromSnapshot;
- private Optional<Boolean> persistent = Optional.absent();
+ private Optional<Boolean> persistent = Optional.empty();
private final Class<A> actorClass;
private Function<Runnable, Void> pauseLeaderFunction;
private RaftActorSnapshotCohort snapshotCohort;
}
public T id(final String newId) {
- this.id = newId;
+ id = newId;
return self();
}
public T peerAddresses(final Map<String, String> newPeerAddresses) {
- this.peerAddresses = newPeerAddresses;
+ peerAddresses = newPeerAddresses;
return self();
}
public T config(final ConfigParams newConfig) {
- this.config = newConfig;
+ config = newConfig;
return self();
}
public T dataPersistenceProvider(final DataPersistenceProvider newDataPersistenceProvider) {
- this.dataPersistenceProvider = newDataPersistenceProvider;
+ dataPersistenceProvider = newDataPersistenceProvider;
return self();
}
public T roleChangeNotifier(final ActorRef newRoleChangeNotifier) {
- this.roleChangeNotifier = newRoleChangeNotifier;
+ roleChangeNotifier = newRoleChangeNotifier;
return self();
}
public T snapshotMessageSupport(final RaftActorSnapshotMessageSupport newSnapshotMessageSupport) {
- this.snapshotMessageSupport = newSnapshotMessageSupport;
+ snapshotMessageSupport = newSnapshotMessageSupport;
return self();
}
public T restoreFromSnapshot(final Snapshot newRestoreFromSnapshot) {
- this.restoreFromSnapshot = newRestoreFromSnapshot;
+ restoreFromSnapshot = newRestoreFromSnapshot;
return self();
}
public T persistent(final Optional<Boolean> newPersistent) {
- this.persistent = newPersistent;
+ persistent = newPersistent;
return self();
}
public T pauseLeaderFunction(final Function<Runnable, Void> newPauseLeaderFunction) {
- this.pauseLeaderFunction = newPauseLeaderFunction;
+ pauseLeaderFunction = newPauseLeaderFunction;
return self();
}
public T snapshotCohort(final RaftActorSnapshotCohort newSnapshotCohort) {
- this.snapshotCohort = newSnapshotCohort;
+ snapshotCohort = newSnapshotCohort;
return self();
}
@Override
public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + (state == null ? 0 : state.hashCode());
- return result;
+ return Objects.hash(state);
}
@Override
return false;
}
MockSnapshotState other = (MockSnapshotState) obj;
- if (state == null) {
- if (other.state != null) {
- return false;
- }
- } else if (!state.equals(other.state)) {
+ if (!Objects.equals(state, other.state)) {
return false;
}
return true;