Add support for reusable streaming
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RemoteRpcImplementation.java
index 404a109741b56a0344ec5d413342badfe41ee06c..71b275999171d04419df3a3c6744502a3fdf1791 100644 (file)
@@ -1,29 +1,51 @@
+/*
+ * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
 package org.opendaylight.controller.remote.rpc;
 
-import static akka.pattern.Patterns.ask;
+import static java.util.Objects.requireNonNull;
 
 import akka.actor.ActorRef;
-import com.google.common.util.concurrent.CheckedFuture;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
-import org.opendaylight.controller.remote.rpc.messages.InvokeRpc;
+import akka.pattern.Patterns;
+import akka.util.Timeout;
+import com.google.common.util.concurrent.ListenableFuture;
+import org.opendaylight.controller.remote.rpc.messages.ExecuteRpc;
+import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
+import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
+import org.opendaylight.mdsal.dom.api.DOMRpcResult;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
-public class RemoteRpcImplementation implements DOMRpcImplementation {
-    private final ActorRef rpcBroker;
-    private final RemoteRpcProviderConfig config;
+/**
+ * A {@link DOMRpcImplementation} which routes invocation requests to a remote invoker actor.
+ *
+ * @author Robert Varga
+ */
+final class RemoteRpcImplementation implements DOMRpcImplementation {
+    // 0 for local, 1 for binding, 2 for remote
+    private static final long COST = 2;
 
-    public RemoteRpcImplementation(final ActorRef rpcBroker, final RemoteRpcProviderConfig config) {
-        this.rpcBroker = rpcBroker;
-        this.config = config;
+    private final ActorRef remoteInvoker;
+    private final Timeout askDuration;
+
+    RemoteRpcImplementation(final ActorRef remoteInvoker, final RemoteRpcProviderConfig config) {
+        this.remoteInvoker = requireNonNull(remoteInvoker);
+        this.askDuration = config.getAskDuration();
+    }
+
+    @Override
+    public ListenableFuture<DOMRpcResult> invokeRpc(final DOMRpcIdentifier rpc,
+            final NormalizedNode<?, ?> input) {
+        final RemoteDOMRpcFuture ret = RemoteDOMRpcFuture.create(rpc.getType().getLastComponent());
+        ret.completeWith(Patterns.ask(remoteInvoker, ExecuteRpc.from(rpc, input), askDuration));
+        return ret;
     }
 
     @Override
-    public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final DOMRpcIdentifier rpc, final NormalizedNode<?, ?> input) {
-        final InvokeRpc rpcMsg = new InvokeRpc(rpc.getType().getLastComponent(), rpc.getContextReference(), input);
-        final scala.concurrent.Future<Object> future = ask(rpcBroker, rpcMsg, config.getAskDuration());
-        return RemoteDOMRpcFuture.from(future);
+    public long invocationCost() {
+        return COST;
     }
 }