Remote RPC Broker: Make Futures non-blocking.
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RemoteDOMRpcFuture.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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 akka.dispatch.OnComplete;
11 import com.google.common.base.Throwables;
12 import com.google.common.util.concurrent.AbstractFuture;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
20 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
21 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import scala.concurrent.ExecutionContext;
26 import scala.concurrent.Future;
27
28 class RemoteDOMRpcFuture extends AbstractFuture<DOMRpcResult> implements CheckedFuture<DOMRpcResult, DOMRpcException> {
29
30     private static final Logger LOG = LoggerFactory.getLogger(RemoteDOMRpcFuture.class);
31
32     private RemoteDOMRpcFuture(final Future<Object> future) {
33         future.onComplete(new FutureUpdater(), ExecutionContext.Implicits$.MODULE$.global());
34     }
35
36     public static CheckedFuture<DOMRpcResult, DOMRpcException> from(final Future<Object> future) {
37         return new RemoteDOMRpcFuture(future);
38     }
39
40     @Override
41     public DOMRpcResult checkedGet() throws DOMRpcException {
42         try {
43             return get();
44         } catch (final ExecutionException e) {
45             throw mapException(e);
46         } catch (final InterruptedException e) {
47             throw Throwables.propagate(e);
48         }
49     }
50
51     @Override
52     public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, DOMRpcException {
53         try {
54             return get(timeout, unit);
55         } catch (final ExecutionException e) {
56             throw mapException(e);
57         } catch (final InterruptedException e) {
58             throw Throwables.propagate(e);
59         }
60     }
61
62     private DOMRpcException mapException(final ExecutionException e) {
63         final Throwable cause = e.getCause();
64         if (cause instanceof DOMRpcException) {
65             return (DOMRpcException) cause;
66         }
67         return new RemoteDOMRpcException("Exception during invoking RPC", e);
68     }
69
70     private final class FutureUpdater extends OnComplete<Object> {
71
72         @Override
73         public void onComplete(final Throwable error, final Object reply) throws Throwable {
74             if (error != null) {
75                 RemoteDOMRpcFuture.this.setException(error);
76             } else if (reply instanceof RpcResponse) {
77                 final RpcResponse rpcReply = (RpcResponse) reply;
78                 final NormalizedNode<?, ?> result;
79                 if (rpcReply.getResultNormalizedNode() == null) {
80                     result = null;
81                     LOG.debug("Received response for invoke rpc: result is null");
82                 } else {
83                     result = NormalizedNodeSerializer.deSerialize(rpcReply.getResultNormalizedNode());
84                     LOG.debug("Received response for invoke rpc: result is {}", result);
85                 }
86                 RemoteDOMRpcFuture.this.set(new DefaultDOMRpcResult(result));
87             }
88             RemoteDOMRpcFuture.this.setException(new IllegalStateException("Incorrect reply type " + reply
89                     + "from Akka"));
90         }
91     }
92
93 }