1 package org.opendaylight.controller.remote.rpc;
3 import akka.actor.ActorRef;
4 import com.google.common.util.concurrent.Futures;
5 import com.google.common.util.concurrent.ListenableFuture;
6 import org.opendaylight.controller.remote.rpc.messages.ErrorResponse;
7 import org.opendaylight.controller.remote.rpc.messages.InvokeRoutedRpc;
8 import org.opendaylight.controller.remote.rpc.messages.InvokeRpc;
9 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
10 import org.opendaylight.controller.remote.rpc.utils.ActorUtil;
11 import org.opendaylight.controller.remote.rpc.utils.XmlUtils;
12 import org.opendaylight.controller.sal.core.api.RoutedRpcDefaultImplementation;
13 import org.opendaylight.controller.sal.core.api.RpcImplementation;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.common.RpcResult;
16 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
17 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import java.util.Collections;
26 public class RemoteRpcImplementation implements RpcImplementation,
27 RoutedRpcDefaultImplementation {
28 private static final Logger LOG = LoggerFactory.getLogger(RemoteRpcImplementation.class);
29 private ActorRef rpcBroker;
30 private SchemaContext schemaContext;
32 public RemoteRpcImplementation(ActorRef rpcBroker, SchemaContext schemaContext) {
33 this.rpcBroker = rpcBroker;
34 this.schemaContext = schemaContext;
38 public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, YangInstanceIdentifier identifier, CompositeNode input) {
39 InvokeRoutedRpc rpcMsg = new InvokeRoutedRpc(rpc, identifier, input);
41 return executeMsg(rpcMsg);
45 public Set<QName> getSupportedRpcs() {
46 // TODO : check if we need to get this from routing registry
47 return Collections.emptySet();
51 public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, CompositeNode input) {
52 InvokeRpc rpcMsg = new InvokeRpc(rpc, input);
53 return executeMsg(rpcMsg);
56 private ListenableFuture<RpcResult<CompositeNode>> executeMsg(Object rpcMsg) {
57 ListenableFuture<RpcResult<CompositeNode>> listenableFuture = null;
60 Object response = ActorUtil.executeLocalOperation(rpcBroker, rpcMsg, ActorUtil.ASK_DURATION, ActorUtil.AWAIT_DURATION);
61 if(response instanceof RpcResponse) {
63 RpcResponse rpcResponse = (RpcResponse) response;
64 CompositeNode result = XmlUtils.xmlToCompositeNode(rpcResponse.getResultCompositeNode());
65 listenableFuture = Futures.immediateFuture(RpcResultBuilder.success(result).build());
67 } else if(response instanceof ErrorResponse) {
69 ErrorResponse errorResponse = (ErrorResponse) response;
70 Exception e = errorResponse.getException();
71 final RpcResultBuilder<CompositeNode> failed = RpcResultBuilder.failed();
72 failed.withError(null, null, e.getMessage(), null, null, e.getCause());
73 listenableFuture = Futures.immediateFuture(failed.build());
76 } catch (Exception e) {
77 LOG.error("Error occurred while invoking RPC actor {}", e.toString());
79 final RpcResultBuilder<CompositeNode> failed = RpcResultBuilder.failed();
80 failed.withError(null, null, e.getMessage(), null, null, e.getCause());
81 listenableFuture = Futures.immediateFuture(failed.build());
84 return listenableFuture;