2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.remote.rpc;
10 import static java.util.Objects.requireNonNull;
12 import akka.dispatch.OnComplete;
13 import com.google.common.util.concurrent.AbstractFuture;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import scala.concurrent.ExecutionContext;
22 import scala.concurrent.Future;
24 abstract class AbstractRemoteFuture<T, O, E extends Exception> extends AbstractFuture<O> {
25 private static final Logger LOG = LoggerFactory.getLogger(AbstractRemoteFuture.class);
27 private final @NonNull T type;
29 AbstractRemoteFuture(final @NonNull T type, final Future<Object> requestFuture) {
30 this.type = requireNonNull(type);
31 requestFuture.onComplete(new FutureUpdater(), ExecutionContext.Implicits$.MODULE$.global());
35 public final O get() throws InterruptedException, ExecutionException {
38 } catch (ExecutionException e) {
39 throw mapException(e);
44 public final O get(final long timeout, final TimeUnit unit)
45 throws InterruptedException, ExecutionException, TimeoutException {
47 return super.get(timeout, unit);
48 } catch (final ExecutionException e) {
49 throw mapException(e);
54 protected final boolean set(final O value) {
55 final boolean ret = super.set(value);
57 LOG.debug("Future {} for action {} successfully completed", this, type);
62 final void failNow(final Throwable error) {
63 LOG.debug("Failing future {} for operation {}", this, type, error);
67 abstract @Nullable O processReply(Object reply);
69 abstract @NonNull Class<E> exceptionClass();
71 abstract @NonNull E wrapCause(Throwable cause);
73 private ExecutionException mapException(final ExecutionException ex) {
74 final Throwable cause = ex.getCause();
75 return exceptionClass().isInstance(cause) ? ex : new ExecutionException(ex.getMessage(), wrapCause(cause));
78 private final class FutureUpdater extends OnComplete<Object> {
80 public void onComplete(final Throwable error, final Object reply) {
82 final O result = processReply(reply);
84 LOG.debug("Received response for operation {}: result is {}", type, result);
87 failNow(new IllegalStateException("Incorrect reply type " + reply + " from Akka"));