Convert sal-remoterpc to mdsal APIs
[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 java.util.concurrent.ExecutionException;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.TimeoutException;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
18 import org.opendaylight.mdsal.dom.api.DOMRpcException;
19 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
20 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
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 final class RemoteDOMRpcFuture extends AbstractFuture<DOMRpcResult> {
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 get() throws InterruptedException, ExecutionException {
53         try {
54             return super.get();
55         } catch (ExecutionException e) {
56             throw mapException(e);
57         }
58     }
59
60     @Override
61     public DOMRpcResult get(final long timeout, @Nonnull final TimeUnit unit)
62             throws InterruptedException, ExecutionException, TimeoutException {
63         try {
64             return super.get(timeout, unit);
65         } catch (final ExecutionException e) {
66             throw mapException(e);
67         }
68     }
69
70     private static ExecutionException mapException(final ExecutionException ex) {
71         final Throwable cause = ex.getCause();
72         if (cause instanceof DOMRpcException) {
73             return ex;
74         }
75         return new ExecutionException(ex.getMessage(),
76                 new RemoteDOMRpcException("Exception during invoking RPC", ex.getCause()));
77     }
78
79     private final class FutureUpdater extends OnComplete<Object> {
80
81         @Override
82         public void onComplete(final Throwable error, final Object reply) {
83             if (error != null) {
84                 RemoteDOMRpcFuture.this.failNow(error);
85             } else if (reply instanceof RpcResponse) {
86                 final RpcResponse rpcReply = (RpcResponse) reply;
87                 final NormalizedNode<?, ?> result = rpcReply.getResultNormalizedNode();
88
89                 LOG.debug("Received response for rpc {}: result is {}", rpcName, result);
90
91                 RemoteDOMRpcFuture.this.set(new DefaultDOMRpcResult(result));
92
93                 LOG.debug("Future {} for rpc {} successfully completed", RemoteDOMRpcFuture.this, rpcName);
94             } else {
95                 RemoteDOMRpcFuture.this.failNow(new IllegalStateException("Incorrect reply type " + reply
96                         + "from Akka"));
97             }
98         }
99     }
100 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.