02e2d1201518b22001da9882e41ee881624a16bc
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RemoteRpcImplementation.java
1 package org.opendaylight.controller.remote.rpc;
2
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.remote.rpc.utils.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;
21
22 import java.util.Collections;
23 import java.util.Set;
24
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;
30
31   public RemoteRpcImplementation(ActorRef rpcBroker, SchemaContext schemaContext) {
32     this.rpcBroker = rpcBroker;
33     this.schemaContext = schemaContext;
34   }
35
36   @Override
37   public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, YangInstanceIdentifier identifier, CompositeNode input) {
38     InvokeRpc rpcMsg = new InvokeRpc(rpc, identifier, input);
39
40     return executeMsg(rpcMsg);
41   }
42
43   @Override
44   public Set<QName> getSupportedRpcs() {
45     // TODO : check if we need to get this from routing registry
46     return Collections.emptySet();
47   }
48
49   @Override
50   public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, CompositeNode input) {
51     InvokeRpc rpcMsg = new InvokeRpc(rpc, null, input);
52     return executeMsg(rpcMsg);
53   }
54
55   private ListenableFuture<RpcResult<CompositeNode>> executeMsg(Object rpcMsg) {
56     ListenableFuture<RpcResult<CompositeNode>> listenableFuture = null;
57
58     try {
59       Object response = ActorUtil.executeOperation(rpcBroker, rpcMsg, ActorUtil.ASK_DURATION, ActorUtil.AWAIT_DURATION);
60       if(response instanceof RpcResponse) {
61
62         RpcResponse rpcResponse = (RpcResponse) response;
63         CompositeNode result = XmlUtils.xmlToCompositeNode(rpcResponse.getResultCompositeNode());
64         listenableFuture = Futures.immediateFuture(RpcResultBuilder.success(result).build());
65
66       } else if(response instanceof ErrorResponse) {
67
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());
73
74       }
75     } catch (Exception e) {
76       LOG.error("Error occurred while invoking RPC actor {}", e);
77
78       final RpcResultBuilder<CompositeNode> failed = RpcResultBuilder.failed();
79       failed.withError(null, null, e.getMessage(), null, null, e.getCause());
80       listenableFuture = Futures.immediateFuture(failed.build());
81     }
82
83     return listenableFuture;
84   }
85 }