BUG-6937: Add ReachableMember case to Gossiper
[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.Preconditions;
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.md.sal.dom.api.DOMRpcException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
19 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
20 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
21 import org.opendaylight.yangtools.yang.common.QName;
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 final QName rpcName;
33
34     private RemoteDOMRpcFuture(final QName rpcName) {
35         this.rpcName = Preconditions.checkNotNull(rpcName, "rpcName");
36     }
37
38     public static RemoteDOMRpcFuture create(final QName rpcName) {
39         return new RemoteDOMRpcFuture(rpcName);
40     }
41
42     protected void failNow(final Throwable error) {
43         LOG.debug("Failing future {} for rpc {}", this, rpcName, error);
44         setException(error);
45     }
46
47     protected void completeWith(final Future<Object> future) {
48         future.onComplete(new FutureUpdater(), ExecutionContext.Implicits$.MODULE$.global());
49     }
50
51     @Override
52     public DOMRpcResult checkedGet() throws DOMRpcException {
53         try {
54             return get();
55         } catch (final ExecutionException e) {
56             throw mapException(e);
57         } catch (final InterruptedException e) {
58             throw new RemoteDOMRpcException("Interruped while invoking RPC", e);
59         }
60     }
61
62     @Override
63     public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, DOMRpcException {
64         try {
65             return get(timeout, unit);
66         } catch (final ExecutionException e) {
67             throw mapException(e);
68         } catch (final InterruptedException e) {
69             throw new RemoteDOMRpcException("Interruped while invoking RPC", e);
70         }
71     }
72
73     private static DOMRpcException mapException(final ExecutionException ex) {
74         final Throwable cause = ex.getCause();
75         if (cause instanceof DOMRpcException) {
76             return (DOMRpcException) cause;
77         }
78         return new RemoteDOMRpcException("Exception during invoking RPC", ex);
79     }
80
81     private final class FutureUpdater extends OnComplete<Object> {
82
83         @Override
84         public void onComplete(final Throwable error, final Object reply) throws Throwable {
85             if (error != null) {
86                 RemoteDOMRpcFuture.this.failNow(error);
87             } else if (reply instanceof RpcResponse) {
88                 final RpcResponse rpcReply = (RpcResponse) reply;
89                 final NormalizedNode<?, ?> result = rpcReply.getResultNormalizedNode();
90
91                 LOG.debug("Received response for rpc {}: result is {}", rpcName, result);
92
93                 RemoteDOMRpcFuture.this.set(new DefaultDOMRpcResult(result));
94
95                 LOG.debug("Future {} for rpc {} successfully completed", RemoteDOMRpcFuture.this, rpcName);
96             } else {
97                 RemoteDOMRpcFuture.this.failNow(new IllegalStateException("Incorrect reply type " + reply
98                         + "from Akka"));
99             }
100         }
101     }
102
103 }