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.InvokeRpc;
8 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
9 import org.opendaylight.controller.remote.rpc.utils.ActorUtil;
10 import org.opendaylight.controller.xml.codec.XmlUtils;
11 import org.opendaylight.controller.sal.core.api.RoutedRpcDefaultImplementation;
12 import org.opendaylight.controller.sal.core.api.RpcImplementation;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.common.RpcResult;
15 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
16 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import java.util.Collections;
25 public class RemoteRpcImplementation implements RpcImplementation,
26 RoutedRpcDefaultImplementation {
27 private static final Logger LOG = LoggerFactory.getLogger(RemoteRpcImplementation.class);
28 private ActorRef rpcBroker;
29 private SchemaContext schemaContext;
31 public RemoteRpcImplementation(ActorRef rpcBroker, SchemaContext schemaContext) {
32 this.rpcBroker = rpcBroker;
33 this.schemaContext = schemaContext;
37 public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, YangInstanceIdentifier identifier, CompositeNode input) {
38 InvokeRpc rpcMsg = new InvokeRpc(rpc, identifier, input);
40 return executeMsg(rpcMsg);
44 public Set<QName> getSupportedRpcs() {
45 // TODO : check if we need to get this from routing registry
46 return Collections.emptySet();
50 public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, CompositeNode input) {
51 InvokeRpc rpcMsg = new InvokeRpc(rpc, null, input);
52 return executeMsg(rpcMsg);
55 private ListenableFuture<RpcResult<CompositeNode>> executeMsg(Object rpcMsg) {
56 ListenableFuture<RpcResult<CompositeNode>> listenableFuture = null;
59 Object response = ActorUtil.executeOperation(rpcBroker, rpcMsg, ActorUtil.ASK_DURATION, ActorUtil.AWAIT_DURATION);
60 if(response instanceof RpcResponse) {
62 RpcResponse rpcResponse = (RpcResponse) response;
63 CompositeNode result = XmlUtils.xmlToCompositeNode(rpcResponse.getResultCompositeNode());
64 listenableFuture = Futures.immediateFuture(RpcResultBuilder.success(result).build());
66 } else if(response instanceof ErrorResponse) {
68 ErrorResponse errorResponse = (ErrorResponse) response;
69 Exception e = errorResponse.getException();
70 final RpcResultBuilder<CompositeNode> failed = RpcResultBuilder.failed();
71 failed.withError(null, null, e.getMessage(), null, null, e.getCause());
72 listenableFuture = Futures.immediateFuture(failed.build());
75 } catch (Exception e) {
76 LOG.error("Error occurred while invoking RPC actor {}", e);
78 final RpcResultBuilder<CompositeNode> failed = RpcResultBuilder.failed();
79 failed.withError(null, null, e.getMessage(), null, null, e.getCause());
80 listenableFuture = Futures.immediateFuture(failed.build());
83 return listenableFuture;