Simplify code using Java 8 features
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / AbstractShardBackendResolver.java
index a1ddcc344953f991b34450d34afb5615bc6c4149..faffe0a3764985f5439e13867928a1ee2a1f2511 100644 (file)
@@ -65,9 +65,9 @@ abstract class AbstractShardBackendResolver extends BackendInfoResolver<ShardBac
             return result;
         }
 
-        private synchronized void onStageResolved(final ShardBackendInfo result, final Throwable failure) {
+        private synchronized void onStageResolved(final ShardBackendInfo info, final Throwable failure) {
             if (failure == null) {
-                this.result = Preconditions.checkNotNull(result);
+                this.result = Preconditions.checkNotNull(info);
             } else {
                 LOG.warn("Failed to resolve shard", failure);
             }
@@ -136,28 +136,31 @@ abstract class AbstractShardBackendResolver extends BackendInfoResolver<ShardBac
         LOG.debug("Shard {} resolved to {}, attempting to connect", shardName, info);
 
         FutureConverters.toJava(ExplicitAsk.ask(info.getPrimaryShardActor(), connectFunction, CONNECT_TIMEOUT))
-            .whenComplete((response, failure) -> {
-                if (failure != null) {
-                    LOG.debug("Connect attempt to {} failed, will retry", shardName, failure);
-                    future.completeExceptionally(wrap("Connection attempt failed", failure));
-                    return;
-                }
-                if (response instanceof RequestFailure) {
-                    final Throwable cause = ((RequestFailure<?, ?>) response).getCause().unwrap();
-                    LOG.debug("Connect attempt to {} failed to process", shardName, cause);
-                    final Throwable result = cause instanceof NotLeaderException
-                            ? wrap("Leader moved during establishment", cause) : cause;
-                    future.completeExceptionally(result);
-                    return;
-                }
-
-                LOG.debug("Resolved backend information to {}", response);
-                Preconditions.checkArgument(response instanceof ConnectClientSuccess, "Unhandled response {}",
-                    response);
-                final ConnectClientSuccess success = (ConnectClientSuccess) response;
-                future.complete(new ShardBackendInfo(success.getBackend(), nextSessionId.getAndIncrement(),
-                    success.getVersion(), shardName, UnsignedLong.fromLongBits(cookie), success.getDataTree(),
-                    success.getMaxMessages()));
-            });
+            .whenComplete((response, failure) -> onConnectResponse(shardName, cookie, future, response, failure));
+    }
+
+    private void onConnectResponse(final String shardName, final long cookie,
+            final CompletableFuture<ShardBackendInfo> future, final Object response, final Throwable failure) {
+        if (failure != null) {
+            LOG.debug("Connect attempt to {} failed, will retry", shardName, failure);
+            future.completeExceptionally(wrap("Connection attempt failed", failure));
+            return;
+        }
+        if (response instanceof RequestFailure) {
+            final Throwable cause = ((RequestFailure<?, ?>) response).getCause().unwrap();
+            LOG.debug("Connect attempt to {} failed to process", shardName, cause);
+            final Throwable result = cause instanceof NotLeaderException
+                    ? wrap("Leader moved during establishment", cause) : cause;
+            future.completeExceptionally(result);
+            return;
+        }
+
+        LOG.debug("Resolved backend information to {}", response);
+        Preconditions.checkArgument(response instanceof ConnectClientSuccess, "Unhandled response %s",
+            response);
+        final ConnectClientSuccess success = (ConnectClientSuccess) response;
+        future.complete(new ShardBackendInfo(success.getBackend(), nextSessionId.getAndIncrement(),
+            success.getVersion(), shardName, UnsignedLong.fromLongBits(cookie), success.getDataTree(),
+            success.getMaxMessages()));
     }
 }