Merge "Updated remote rpc code after integration tests. Rpc execution is failing...
[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.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;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Set;
27
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;
33
34   public RemoteRpcImplementation(ActorRef rpcBroker, SchemaContext schemaContext) {
35     this.rpcBroker = rpcBroker;
36     this.schemaContext = schemaContext;
37   }
38
39   @Override
40   public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, InstanceIdentifier identifier, CompositeNode input) {
41     InvokeRoutedRpc rpcMsg = new InvokeRoutedRpc(rpc, identifier, input);
42
43     return executeMsg(rpcMsg);
44   }
45
46   @Override
47   public Set<QName> getSupportedRpcs() {
48     // TODO : check if we need to get this from routing registry
49     return Collections.emptySet();
50   }
51
52   @Override
53   public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, CompositeNode input) {
54     InvokeRpc rpcMsg = new InvokeRpc(rpc, input);
55     return executeMsg(rpcMsg);
56   }
57
58   private ListenableFuture<RpcResult<CompositeNode>> executeMsg(Object rpcMsg) {
59     CompositeNode result = null;
60     Collection<RpcError> errors = errors = new ArrayList<>();
61     try {
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()));
70       }
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()));
74     }
75     return Futures.immediateFuture(Rpcs.getRpcResult(true, result, errors));
76   }
77 }