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