Merge changes I0587299e,I81a92c4e
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / ProxyDOMRpcService.java
index 33fa7b96e7443f83e4e0bb1fe133c5b4699a8657..b6e56fbef4e1b867aa02d88bba327a9615f39267 100644 (file)
@@ -13,7 +13,6 @@ import akka.actor.ActorSystem;
 import akka.dispatch.OnComplete;
 import akka.pattern.Patterns;
 import akka.util.Timeout;
-import com.google.common.base.Function;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.SettableFuture;
@@ -33,6 +32,7 @@ import org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessage
 import org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessageReply;
 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyResultResponse;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -45,6 +45,14 @@ public class ProxyDOMRpcService implements DOMRpcService {
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyManager.class);
 
+    private final ExceptionMapper<DOMRpcException> domRpcExceptionMapper =
+        new ExceptionMapper<DOMRpcException>("invokeRpc", DOMRpcException.class) {
+            @Override
+            protected DOMRpcException newWithCause(String message, Throwable cause) {
+                return new ClusteringRpcException(id + ": Exception during remote rpc invocation.", cause);
+            }
+        };
+
     private final ActorRef masterActorRef;
     private final ActorSystem actorSystem;
     private final RemoteDeviceId id;
@@ -64,60 +72,40 @@ public class ProxyDOMRpcService implements DOMRpcService {
                                                                   @Nullable final NormalizedNode<?, ?> input) {
         LOG.trace("{}: Rpc operation invoked with schema type: {} and node: {}.", id, type, input);
 
-        final NormalizedNodeMessage normalizedNodeMessage =
-                new NormalizedNodeMessage(YangInstanceIdentifier.EMPTY, input);
-        final Future<Object> scalaFuture =
-                Patterns.ask(masterActorRef,
-                        new InvokeRpcMessage(new SchemaPathMessage(type), normalizedNodeMessage),
-                        actorResponseWaitTime);
+        final NormalizedNodeMessage normalizedNodeMessage = input != null
+                ? new NormalizedNodeMessage(YangInstanceIdentifier.EMPTY, input) : null;
+        final Future<Object> scalaFuture = Patterns.ask(masterActorRef,
+                new InvokeRpcMessage(new SchemaPathMessage(type), normalizedNodeMessage), actorResponseWaitTime);
 
         final SettableFuture<DOMRpcResult> settableFuture = SettableFuture.create();
 
-        final CheckedFuture<DOMRpcResult, DOMRpcException> checkedFuture = Futures.makeChecked(settableFuture,
-                new Function<Exception, DOMRpcException>() {
-
-                @Nullable
-                @Override
-                public DOMRpcException apply(@Nullable final Exception exception) {
-                    return new ClusteringRpcException(id + ": Exception during remote rpc invocation.",
-                            exception);
-                }
-            });
-
         scalaFuture.onComplete(new OnComplete<Object>() {
             @Override
-            public void onComplete(final Throwable failure, final Object success) throws Throwable {
+            public void onComplete(final Throwable failure, final Object response) {
                 if (failure != null) {
                     settableFuture.setException(failure);
                     return;
                 }
-                if (success instanceof Throwable) {
-                    settableFuture.setException((Throwable) success);
-                    return;
-                }
-                if (success instanceof EmptyResultResponse || success == null) {
+
+                if (response instanceof EmptyResultResponse) {
                     settableFuture.set(null);
                     return;
                 }
-                final Collection<RpcError> errors = ((InvokeRpcMessageReply) success).getRpcErrors();
+
+                final Collection<RpcError> errors = ((InvokeRpcMessageReply) response).getRpcErrors();
                 final NormalizedNodeMessage normalizedNodeMessageResult =
-                        ((InvokeRpcMessageReply) success).getNormalizedNodeMessage();
+                        ((InvokeRpcMessageReply) response).getNormalizedNodeMessage();
                 final DOMRpcResult result;
                 if (normalizedNodeMessageResult == null) {
                     result = new DefaultDOMRpcResult(errors);
                 } else {
-                    if (errors == null) {
-                        result = new DefaultDOMRpcResult(normalizedNodeMessageResult.getNode());
-                    } else {
-                        result = new DefaultDOMRpcResult(normalizedNodeMessageResult.getNode(), errors);
-                    }
+                    result = new DefaultDOMRpcResult(normalizedNodeMessageResult.getNode(), errors);
                 }
                 settableFuture.set(result);
             }
         }, actorSystem.dispatcher());
 
-        return checkedFuture;
-
+        return Futures.makeChecked(settableFuture, domRpcExceptionMapper);
     }
 
     @Nonnull