Bump upstreams for Silicon
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / AbstractRemoteFuture.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.remote.rpc;
9
10 import static java.util.Objects.requireNonNull;
11
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;
23
24 abstract class AbstractRemoteFuture<T, O, E extends Exception> extends AbstractFuture<O> {
25     private static final Logger LOG = LoggerFactory.getLogger(AbstractRemoteFuture.class);
26
27     private final @NonNull T type;
28
29     AbstractRemoteFuture(final @NonNull T type, final Future<Object> requestFuture) {
30         this.type = requireNonNull(type);
31         requestFuture.onComplete(new FutureUpdater(), ExecutionContext.Implicits$.MODULE$.global());
32     }
33
34     @Override
35     public final O get() throws InterruptedException, ExecutionException {
36         try {
37             return super.get();
38         } catch (ExecutionException e) {
39             throw mapException(e);
40         }
41     }
42
43     @Override
44     public final O get(final long timeout, final TimeUnit unit)
45             throws InterruptedException, ExecutionException, TimeoutException {
46         try {
47             return super.get(timeout, unit);
48         } catch (final ExecutionException e) {
49             throw mapException(e);
50         }
51     }
52
53     @Override
54     protected final boolean set(final O value) {
55         final boolean ret = super.set(value);
56         if (ret) {
57             LOG.debug("Future {} for action {} successfully completed", this, type);
58         }
59         return ret;
60     }
61
62     final void failNow(final Throwable error) {
63         LOG.debug("Failing future {} for operation {}", this, type, error);
64         setException(error);
65     }
66
67     abstract @Nullable O processReply(Object reply);
68
69     abstract @NonNull Class<E> exceptionClass();
70
71     abstract @NonNull E wrapCause(Throwable cause);
72
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));
76     }
77
78     private final class FutureUpdater extends OnComplete<Object> {
79         @Override
80         public void onComplete(final Throwable error, final Object reply) {
81             if (error == null) {
82                 final O result = processReply(reply);
83                 if (result != null) {
84                     LOG.debug("Received response for operation {}: result is {}", type, result);
85                     set(result);
86                 } else {
87                     failNow(new IllegalStateException("Incorrect reply type " + reply + " from Akka"));
88                 }
89             } else {
90                 failNow(error);
91             }
92         }
93     }
94 }