2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.remote.rpc;
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;
28 final class RemoteDOMRpcFuture extends AbstractFuture<DOMRpcResult>
29 implements CheckedFuture<DOMRpcResult, DOMRpcException> {
31 private static final Logger LOG = LoggerFactory.getLogger(RemoteDOMRpcFuture.class);
33 private final QName rpcName;
35 private RemoteDOMRpcFuture(final QName rpcName) {
36 this.rpcName = Preconditions.checkNotNull(rpcName, "rpcName");
39 public static RemoteDOMRpcFuture create(final QName rpcName) {
40 return new RemoteDOMRpcFuture(rpcName);
43 protected void failNow(final Throwable error) {
44 LOG.debug("Failing future {} for rpc {}", this, rpcName, error);
48 protected void completeWith(final Future<Object> future) {
49 future.onComplete(new FutureUpdater(), ExecutionContext.Implicits$.MODULE$.global());
53 public DOMRpcResult checkedGet() throws DOMRpcException {
56 } catch (final ExecutionException e) {
57 throw mapException(e);
58 } catch (final InterruptedException e) {
59 throw new RemoteDOMRpcException("Interruped while invoking RPC", e);
64 public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, DOMRpcException {
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);
74 private static DOMRpcException mapException(final ExecutionException ex) {
75 final Throwable cause = ex.getCause();
76 if (cause instanceof DOMRpcException) {
77 return (DOMRpcException) cause;
79 return new RemoteDOMRpcException("Exception during invoking RPC", ex);
82 private final class FutureUpdater extends OnComplete<Object> {
85 public void onComplete(final Throwable error, final Object reply) throws Throwable {
87 RemoteDOMRpcFuture.this.failNow(error);
88 } else if (reply instanceof RpcResponse) {
89 final RpcResponse rpcReply = (RpcResponse) reply;
90 final NormalizedNode<?, ?> result = rpcReply.getResultNormalizedNode();
92 LOG.debug("Received response for rpc {}: result is {}", rpcName, result);
94 RemoteDOMRpcFuture.this.set(new DefaultDOMRpcResult(result));
96 LOG.debug("Future {} for rpc {} successfully completed", RemoteDOMRpcFuture.this, rpcName);
98 RemoteDOMRpcFuture.this.failNow(new IllegalStateException("Incorrect reply type " + reply