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.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
21 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
22 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import scala.concurrent.ExecutionContext;
28 import scala.concurrent.Future;
34 class RemoteDOMRpcFuture extends AbstractFuture<DOMRpcResult> implements CheckedFuture<DOMRpcResult, DOMRpcException> {
36 private static final Logger LOG = LoggerFactory.getLogger(RemoteDOMRpcFuture.class);
38 private final QName rpcName;
40 private RemoteDOMRpcFuture(final QName rpcName) {
41 this.rpcName = Preconditions.checkNotNull(rpcName,"rpcName");
44 public static RemoteDOMRpcFuture create(final QName rpcName) {
45 return new RemoteDOMRpcFuture(rpcName);
48 protected void failNow(final Throwable error) {
49 LOG.debug("Failing future {} for rpc {}", this, rpcName, error);
53 protected void completeWith(final Future<Object> future) {
54 future.onComplete(new FutureUpdater(), ExecutionContext.Implicits$.MODULE$.global());
58 public DOMRpcResult checkedGet() throws DOMRpcException {
61 } catch (final ExecutionException e) {
62 throw mapException(e);
63 } catch (final InterruptedException e) {
64 throw Throwables.propagate(e);
69 public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, DOMRpcException {
71 return get(timeout, unit);
72 } catch (final ExecutionException e) {
73 throw mapException(e);
74 } catch (final InterruptedException e) {
75 throw Throwables.propagate(e);
79 private DOMRpcException mapException(final ExecutionException e) {
80 final Throwable cause = e.getCause();
81 if (cause instanceof DOMRpcException) {
82 return (DOMRpcException) cause;
84 return new RemoteDOMRpcException("Exception during invoking RPC", e);
87 private final class FutureUpdater extends OnComplete<Object> {
90 public void onComplete(final Throwable error, final Object reply) throws Throwable {
92 RemoteDOMRpcFuture.this.failNow(error);
93 } else if (reply instanceof RpcResponse) {
94 final RpcResponse rpcReply = (RpcResponse) reply;
95 final NormalizedNode<?, ?> result;
96 if (rpcReply.getResultNormalizedNode() == null) {
98 LOG.debug("Received response for rpc {}: result is null", rpcName);
100 result = NormalizedNodeSerializer.deSerialize(rpcReply.getResultNormalizedNode());
101 LOG.debug("Received response for rpc {}: result is {}", rpcName, result);
103 RemoteDOMRpcFuture.this.set(new DefaultDOMRpcResult(result));
104 LOG.debug("Future {} for rpc {} successfully completed", RemoteDOMRpcFuture.this, rpcName);
106 RemoteDOMRpcFuture.this.failNow(new IllegalStateException("Incorrect reply type " + reply