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.opendaylight.yangtools.yang.model.api.SchemaPath;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import scala.concurrent.ExecutionContext;
23 import scala.concurrent.Future;
25 abstract class AbstractRemoteFuture<T, E extends Exception> extends AbstractFuture<T> {
26 private static final Logger LOG = LoggerFactory.getLogger(AbstractRemoteFuture.class);
28 private final @NonNull SchemaPath type;
30 AbstractRemoteFuture(final @NonNull SchemaPath type, final Future<Object> requestFuture) {
31 this.type = requireNonNull(type);
32 requestFuture.onComplete(new FutureUpdater(), ExecutionContext.Implicits$.MODULE$.global());
36 public final T get() throws InterruptedException, ExecutionException {
39 } catch (ExecutionException e) {
40 throw mapException(e);
45 public final T get(final long timeout, final TimeUnit unit)
46 throws InterruptedException, ExecutionException, TimeoutException {
48 return super.get(timeout, unit);
49 } catch (final ExecutionException e) {
50 throw mapException(e);
55 protected final boolean set(final T value) {
56 final boolean ret = super.set(value);
58 LOG.debug("Future {} for action {} successfully completed", this, type);
63 final void failNow(final Throwable error) {
64 LOG.debug("Failing future {} for operation {}", this, type, error);
68 abstract @Nullable T processReply(Object reply);
70 abstract @NonNull Class<E> exceptionClass();
72 abstract @NonNull E wrapCause(Throwable cause);
74 private ExecutionException mapException(final ExecutionException ex) {
75 final Throwable cause = ex.getCause();
76 return exceptionClass().isInstance(cause) ? ex : new ExecutionException(ex.getMessage(), wrapCause(cause));
79 private final class FutureUpdater extends OnComplete<Object> {
81 public void onComplete(final Throwable error, final Object reply) {
83 final T result = processReply(reply);
85 LOG.debug("Received response for operation {}: result is {}", type, result);
88 failNow(new IllegalStateException("Incorrect reply type " + reply + " from Akka"));