Removed uncessary calls to RpcBroker to find routes.
[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.base.Throwables;
13 import com.google.common.util.concurrent.AbstractFuture;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
21 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
22 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import scala.concurrent.ExecutionContext;
28 import scala.concurrent.Future;
29
30 /**
31  * @author tony
32  *
33  */
34 class RemoteDOMRpcFuture extends AbstractFuture<DOMRpcResult> implements CheckedFuture<DOMRpcResult, DOMRpcException> {
35
36     private static final Logger LOG = LoggerFactory.getLogger(RemoteDOMRpcFuture.class);
37
38     private final QName rpcName;
39
40     private RemoteDOMRpcFuture(final QName rpcName) {
41         this.rpcName = Preconditions.checkNotNull(rpcName,"rpcName");
42     }
43
44     public static RemoteDOMRpcFuture create(final QName rpcName) {
45         return new RemoteDOMRpcFuture(rpcName);
46     }
47
48     protected void failNow(final Throwable error) {
49         LOG.debug("Failing future {} for rpc {}", this, rpcName, error);
50         setException(error);
51     }
52
53     protected void completeWith(final Future<Object> future) {
54         future.onComplete(new FutureUpdater(), ExecutionContext.Implicits$.MODULE$.global());
55     }
56
57     @Override
58     public DOMRpcResult checkedGet() throws DOMRpcException {
59         try {
60             return get();
61         } catch (final ExecutionException e) {
62             throw mapException(e);
63         } catch (final InterruptedException e) {
64             throw Throwables.propagate(e);
65         }
66     }
67
68     @Override
69     public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, DOMRpcException {
70         try {
71             return get(timeout, unit);
72         } catch (final ExecutionException e) {
73             throw mapException(e);
74         } catch (final InterruptedException e) {
75             throw Throwables.propagate(e);
76         }
77     }
78
79     private DOMRpcException mapException(final ExecutionException e) {
80         final Throwable cause = e.getCause();
81         if (cause instanceof DOMRpcException) {
82             return (DOMRpcException) cause;
83         }
84         return new RemoteDOMRpcException("Exception during invoking RPC", e);
85     }
86
87     private final class FutureUpdater extends OnComplete<Object> {
88
89         @Override
90         public void onComplete(final Throwable error, final Object reply) throws Throwable {
91             if (error != null) {
92                 RemoteDOMRpcFuture.this.failNow(error);
93             } else if (reply instanceof RpcResponse) {
94                 final RpcResponse rpcReply = (RpcResponse) reply;
95                 final NormalizedNode<?, ?> result;
96                 if (rpcReply.getResultNormalizedNode() == null) {
97                     result = null;
98                     LOG.debug("Received response for rpc {}: result is null", rpcName);
99                 } else {
100                     result = NormalizedNodeSerializer.deSerialize(rpcReply.getResultNormalizedNode());
101                     LOG.debug("Received response for rpc {}: result is {}", rpcName, result);
102                 }
103                 RemoteDOMRpcFuture.this.set(new DefaultDOMRpcResult(result));
104                 LOG.debug("Future {} for rpc {} successfully completed", RemoteDOMRpcFuture.this, rpcName);
105             }
106             RemoteDOMRpcFuture.this.failNow(new IllegalStateException("Incorrect reply type " + reply
107                     + "from Akka"));
108         }
109     }
110
111 }