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.sal.common.util.RpcErrors;
11 import org.opendaylight.controller.sal.common.util.Rpcs;
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.RpcError;
16 import org.opendaylight.yangtools.yang.common.RpcResult;
17 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
28 public class RemoteRpcImplementation implements RpcImplementation,
29 RoutedRpcDefaultImplementation {
30 private static final Logger LOG = LoggerFactory.getLogger(RemoteRpcImplementation.class);
31 private ActorRef rpcBroker;
32 private SchemaContext schemaContext;
34 public RemoteRpcImplementation(ActorRef rpcBroker, SchemaContext schemaContext) {
35 this.rpcBroker = rpcBroker;
36 this.schemaContext = schemaContext;
40 public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, InstanceIdentifier identifier, CompositeNode input) {
41 InvokeRoutedRpc rpcMsg = new InvokeRoutedRpc(rpc, identifier, input);
43 return executeMsg(rpcMsg);
47 public Set<QName> getSupportedRpcs() {
48 // TODO : check if we need to get this from routing registry
49 return Collections.emptySet();
53 public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, CompositeNode input) {
54 InvokeRpc rpcMsg = new InvokeRpc(rpc, input);
55 return executeMsg(rpcMsg);
58 private ListenableFuture<RpcResult<CompositeNode>> executeMsg(Object rpcMsg) {
59 CompositeNode result = null;
60 Collection<RpcError> errors = errors = new ArrayList<>();
62 Object response = ActorUtil.executeLocalOperation(rpcBroker, rpcMsg, ActorUtil.ASK_DURATION, ActorUtil.AWAIT_DURATION);
63 if(response instanceof RpcResponse) {
64 RpcResponse rpcResponse = (RpcResponse) response;
65 result = XmlUtils.xmlToCompositeNode(rpcResponse.getResultCompositeNode());
66 } else if(response instanceof ErrorResponse) {
67 ErrorResponse errorResponse = (ErrorResponse) response;
68 Exception e = errorResponse.getException();
69 errors.add(RpcErrors.getRpcError(null, null, null, null, e.getMessage(), null, e.getCause()));
71 } catch (Exception e) {
72 LOG.error("Error occurred while invoking RPC actor {}", e.toString());
73 errors.add(RpcErrors.getRpcError(null, null, null, null, e.getMessage(), null, e.getCause()));
75 return Futures.immediateFuture(Rpcs.getRpcResult(true, result, errors));