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.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.md.sal.dom.api.DOMRpcException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
20 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
21 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import scala.concurrent.ExecutionContext;
27 import scala.concurrent.Future;
33 class RemoteDOMRpcFuture extends AbstractFuture<DOMRpcResult> implements CheckedFuture<DOMRpcResult, DOMRpcException> {
35 private static final Logger LOG = LoggerFactory.getLogger(RemoteDOMRpcFuture.class);
37 private final QName rpcName;
39 private RemoteDOMRpcFuture(final QName rpcName) {
40 this.rpcName = Preconditions.checkNotNull(rpcName,"rpcName");
43 public static RemoteDOMRpcFuture create(final QName rpcName) {
44 return new RemoteDOMRpcFuture(rpcName);
47 protected void failNow(final Throwable error) {
48 LOG.debug("Failing future {} for rpc {}", this, rpcName, error);
52 protected void completeWith(final Future<Object> future) {
53 future.onComplete(new FutureUpdater(), ExecutionContext.Implicits$.MODULE$.global());
57 public DOMRpcResult checkedGet() throws DOMRpcException {
60 } catch (final ExecutionException e) {
61 throw mapException(e);
62 } catch (final InterruptedException e) {
63 throw Throwables.propagate(e);
68 public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, DOMRpcException {
70 return get(timeout, unit);
71 } catch (final ExecutionException e) {
72 throw mapException(e);
73 } catch (final InterruptedException e) {
74 throw Throwables.propagate(e);
78 private DOMRpcException mapException(final ExecutionException e) {
79 final Throwable cause = e.getCause();
80 if (cause instanceof DOMRpcException) {
81 return (DOMRpcException) cause;
83 return new RemoteDOMRpcException("Exception during invoking RPC", e);
86 private final class FutureUpdater extends OnComplete<Object> {
89 public void onComplete(final Throwable error, final Object reply) throws Throwable {
91 RemoteDOMRpcFuture.this.failNow(error);
92 } else if (reply instanceof RpcResponse) {
93 final RpcResponse rpcReply = (RpcResponse) reply;
94 final NormalizedNode<?, ?> result = rpcReply.getResultNormalizedNode();
96 LOG.debug("Received response for rpc {}: result is {}", rpcName, result);
98 RemoteDOMRpcFuture.this.set(new DefaultDOMRpcResult(result));
100 LOG.debug("Future {} for rpc {} successfully completed", RemoteDOMRpcFuture.this, rpcName);
102 RemoteDOMRpcFuture.this.failNow(new IllegalStateException("Incorrect reply type " + reply