Add support for reusable streaming
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RemoteRpcImplementation.java
1 /*
2  * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.remote.rpc;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorRef;
13 import akka.pattern.Patterns;
14 import akka.util.Timeout;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import org.opendaylight.controller.remote.rpc.messages.ExecuteRpc;
17 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
18 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
19 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21
22 /**
23  * A {@link DOMRpcImplementation} which routes invocation requests to a remote invoker actor.
24  *
25  * @author Robert Varga
26  */
27 final class RemoteRpcImplementation implements DOMRpcImplementation {
28     // 0 for local, 1 for binding, 2 for remote
29     private static final long COST = 2;
30
31     private final ActorRef remoteInvoker;
32     private final Timeout askDuration;
33
34     RemoteRpcImplementation(final ActorRef remoteInvoker, final RemoteRpcProviderConfig config) {
35         this.remoteInvoker = requireNonNull(remoteInvoker);
36         this.askDuration = config.getAskDuration();
37     }
38
39     @Override
40     public ListenableFuture<DOMRpcResult> invokeRpc(final DOMRpcIdentifier rpc,
41             final NormalizedNode<?, ?> input) {
42         final RemoteDOMRpcFuture ret = RemoteDOMRpcFuture.create(rpc.getType().getLastComponent());
43         ret.completeWith(Patterns.ask(remoteInvoker, ExecuteRpc.from(rpc, input), askDuration));
44         return ret;
45     }
46
47     @Override
48     public long invocationCost() {
49         return COST;
50     }
51 }