+ @Override
+ public Future<RpcResult<Void>> makeLeaderLocal(final MakeLeaderLocalInput input) {
+ final String shardName = input.getShardName();
+ if (Strings.isNullOrEmpty(shardName)) {
+ return newFailedRpcResultFuture("A valid shard name must be specified");
+ }
+
+ DataStoreType dataStoreType = input.getDataStoreType();
+ if (dataStoreType == null) {
+ return newFailedRpcResultFuture("A valid DataStoreType must be specified");
+ }
+
+ LOG.info("Moving leader to local node for shard {}, datastoreType {}", shardName, dataStoreType);
+
+ ActorContext actorContext = dataStoreType == DataStoreType.Config
+ ? configDataStore.getActorContext()
+ : operDataStore.getActorContext();
+
+ final scala.concurrent.Future<ActorRef> localShardReply =
+ actorContext.findLocalShardAsync(shardName);
+
+ final scala.concurrent.Promise<Object> makeLeaderLocalAsk = akka.dispatch.Futures.promise();
+ localShardReply.onComplete(new OnComplete<ActorRef>() {
+ @Override
+ public void onComplete(final Throwable failure, final ActorRef actorRef) throws Throwable {
+ if (failure != null) {
+ LOG.warn("No local shard found for {} datastoreType {} - Cannot request leadership transfer to"
+ + " local shard.", shardName, failure);
+ makeLeaderLocalAsk.failure(failure);
+ } else {
+ makeLeaderLocalAsk
+ .completeWith(actorContext
+ .executeOperationAsync(actorRef, MakeLeaderLocal.INSTANCE, makeLeaderLocalTimeout));
+ }
+ }
+ }, actorContext.getClientDispatcher());
+
+ final SettableFuture<RpcResult<Void>> future = SettableFuture.create();
+ makeLeaderLocalAsk.future().onComplete(new OnComplete<Object>() {
+ @Override
+ public void onComplete(final Throwable failure, final Object success) throws Throwable {
+ if (failure != null) {
+ LOG.error("Leadership transfer failed for shard {}.", shardName, failure);
+ future.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION,
+ "leadership transfer failed", failure).build());
+ return;
+ }
+
+ LOG.debug("Leadership transfer complete {}.", success);
+ future.set(RpcResultBuilder.<Void>success().build());
+ }
+ }, actorContext.getClientDispatcher());
+
+ return future;
+ }
+
+ @Override
+ public Future<RpcResult<Void>> addPrefixShardReplica(final AddPrefixShardReplicaInput input) {
+
+ final InstanceIdentifier<?> identifier = input.getShardPrefix();
+ if (identifier == null) {
+ return newFailedRpcResultFuture("A valid shard identifier must be specified");
+ }
+
+ final DataStoreType dataStoreType = input.getDataStoreType();
+ if (dataStoreType == null) {
+ return newFailedRpcResultFuture("A valid DataStoreType must be specified");
+ }
+
+ LOG.info("Adding replica for shard {}, datastore type {}", identifier, dataStoreType);
+
+ final YangInstanceIdentifier prefix = serializer.toYangInstanceIdentifier(identifier);
+ final SettableFuture<RpcResult<Void>> returnFuture = SettableFuture.create();
+ ListenableFuture<Success> future = sendMessageToShardManager(dataStoreType, new AddPrefixShardReplica(prefix));
+ Futures.addCallback(future, new FutureCallback<Success>() {
+ @Override
+ public void onSuccess(Success success) {
+ LOG.info("Successfully added replica for shard {}", prefix);
+ returnFuture.set(newSuccessfulResult());
+ }
+
+ @Override
+ public void onFailure(Throwable failure) {
+ onMessageFailure(String.format("Failed to add replica for shard %s", prefix),
+ returnFuture, failure);
+ }
+ });
+
+ return returnFuture;
+ }
+
+ @Override
+ public Future<RpcResult<Void>> removePrefixShardReplica(final RemovePrefixShardReplicaInput input) {
+
+ final InstanceIdentifier<?> identifier = input.getShardPrefix();
+ if (identifier == null) {
+ return newFailedRpcResultFuture("A valid shard identifier must be specified");
+ }
+
+ final DataStoreType dataStoreType = input.getDataStoreType();
+ if (dataStoreType == null) {
+ return newFailedRpcResultFuture("A valid DataStoreType must be specified");
+ }
+
+ final String memberName = input.getMemberName();
+ if (Strings.isNullOrEmpty(memberName)) {
+ return newFailedRpcResultFuture("A valid member name must be specified");
+ }
+
+ LOG.info("Removing replica for shard {} memberName {}, datastoreType {}",
+ identifier, memberName, dataStoreType);
+ final YangInstanceIdentifier prefix = serializer.toYangInstanceIdentifier(identifier);
+
+ final SettableFuture<RpcResult<Void>> returnFuture = SettableFuture.create();
+ final ListenableFuture<Success> future = sendMessageToShardManager(dataStoreType,
+ new RemovePrefixShardReplica(prefix, MemberName.forName(memberName)));
+ Futures.addCallback(future, new FutureCallback<Success>() {
+ @Override
+ public void onSuccess(final Success success) {
+ LOG.info("Successfully removed replica for shard {}", prefix);
+ returnFuture.set(newSuccessfulResult());
+ }
+
+ @Override
+ public void onFailure(final Throwable failure) {
+ onMessageFailure(String.format("Failed to remove replica for shard %s", prefix),
+ returnFuture, failure);
+ }
+ });
+
+ return returnFuture;
+ }
+