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

©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.