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 class RemoteDOMRpcFuture extends AbstractFuture<DOMRpcResult> implements CheckedFuture<DOMRpcResult, DOMRpcException> {
30 private static final Logger LOG = LoggerFactory.getLogger(RemoteDOMRpcFuture.class);
32 private final QName rpcName;
34 private RemoteDOMRpcFuture(final QName rpcName) {
35 this.rpcName = Preconditions.checkNotNull(rpcName, "rpcName");
38 public static RemoteDOMRpcFuture create(final QName rpcName) {
39 return new RemoteDOMRpcFuture(rpcName);
42 protected void failNow(final Throwable error) {
43 LOG.debug("Failing future {} for rpc {}", this, rpcName, error);
47 protected void completeWith(final Future<Object> future) {
48 future.onComplete(new FutureUpdater(), ExecutionContext.Implicits$.MODULE$.global());
52 public DOMRpcResult checkedGet() throws DOMRpcException {
55 } catch (final ExecutionException e) {
56 throw mapException(e);
57 } catch (final InterruptedException e) {
58 throw new RemoteDOMRpcException("Interruped while invoking RPC", e);
63 public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, DOMRpcException {
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);
73 private static DOMRpcException mapException(final ExecutionException ex) {
74 final Throwable cause = ex.getCause();
75 if (cause instanceof DOMRpcException) {
76 return (DOMRpcException) cause;
78 return new RemoteDOMRpcException("Exception during invoking RPC", ex);
81 private final class FutureUpdater extends OnComplete<Object> {
84 public void onComplete(final Throwable error, final Object reply) throws Throwable {
86 RemoteDOMRpcFuture.this.failNow(error);
87 } else if (reply instanceof RpcResponse) {
88 final RpcResponse rpcReply = (RpcResponse) reply;
89 final NormalizedNode<?, ?> result = rpcReply.getResultNormalizedNode();
91 LOG.debug("Received response for rpc {}: result is {}", rpcName, result);
93 RemoteDOMRpcFuture.this.set(new DefaultDOMRpcResult(result));
95 LOG.debug("Future {} for rpc {} successfully completed", RemoteDOMRpcFuture.this, rpcName);
97 RemoteDOMRpcFuture.this.failNow(new IllegalStateException("Incorrect reply type " + reply