BUG-3128: rework sal-remoterpc-connector
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RpcInvoker.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.remote.rpc;
10
11 import akka.actor.ActorRef;
12 import akka.actor.Props;
13 import com.google.common.base.Preconditions;
14 import com.google.common.base.Throwables;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.FutureCallback;
17 import com.google.common.util.concurrent.Futures;
18 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
19 import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
23 import org.opendaylight.controller.remote.rpc.messages.ExecuteRpc;
24 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27
28 /**
29  * Actor receiving invocation requests from remote nodes, routing them to
30  * {@link DOMRpcService#invokeRpc(SchemaPath, NormalizedNode)}.
31  */
32 final class RpcInvoker extends AbstractUntypedActor {
33     private final DOMRpcService rpcService;
34
35     private RpcInvoker(final DOMRpcService rpcService) {
36         this.rpcService = Preconditions.checkNotNull(rpcService);
37     }
38
39     public static Props props(final DOMRpcService rpcService) {
40         Preconditions.checkNotNull(rpcService, "DOMRpcService can not be null");
41         return Props.create(RpcInvoker.class, rpcService);
42     }
43
44     @Override
45     protected void handleReceive(final Object message) {
46         if (message instanceof ExecuteRpc) {
47             executeRpc((ExecuteRpc) message);
48         } else {
49             unknownMessage(message);
50         }
51     }
52
53     @SuppressWarnings("checkstyle:IllegalCatch")
54     private void executeRpc(final ExecuteRpc msg) {
55         LOG.debug("Executing rpc {}", msg.getRpc());
56         final SchemaPath schemaPath = SchemaPath.create(true, msg.getRpc());
57         final ActorRef sender = getSender();
58         final ActorRef self = self();
59
60         final CheckedFuture<DOMRpcResult, DOMRpcException> future;
61         try {
62             final NormalizedNode<?, ?> input = msg.getInputNormalizedNode() == null ?  null
63                     : NormalizedNodeSerializer.deSerialize(msg.getInputNormalizedNode());
64
65             future = rpcService.invokeRpc(schemaPath, input);
66         } catch (final RuntimeException e) {
67             LOG.debug("Failed to invoke RPC {}", msg.getRpc(), e);
68             sender.tell(new akka.actor.Status.Failure(e), sender);
69             return;
70         }
71
72         Futures.addCallback(future, new FutureCallback<DOMRpcResult>() {
73             @Override
74             public void onSuccess(final DOMRpcResult result) {
75                 if (result == null) {
76                     // This shouldn't happen but the FutureCallback annotates the result param with Nullable so
77                     // handle null here to avoid FindBugs warning.
78                     LOG.debug("Got null DOMRpcResult - sending null response for execute rpc : {}", msg.getRpc());
79                     sender.tell(new RpcResponse(null), self);
80                     return;
81                 }
82
83                 if (!result.getErrors().isEmpty()) {
84                     final String message = String.format("Execution of RPC %s failed", msg.getRpc());
85                     sender.tell(new akka.actor.Status.Failure(new RpcErrorsException(message, result.getErrors())),
86                         self);
87                 } else {
88                     LOG.debug("Sending response for execute rpc : {}", msg.getRpc());
89                     sender.tell(new RpcResponse(result.getResult() == null ? null :
90                         NormalizedNodeSerializer.serialize(result.getResult())), self);
91                 }
92             }
93
94             @Override
95             public void onFailure(final Throwable failure) {
96                 LOG.debug("Failed to execute RPC {}", msg.getRpc(), failure);
97                 LOG.error("Failed to execute RPC {} due to {}. More details are available on DEBUG level.",
98                     msg.getRpc(), Throwables.getRootCause(failure));
99                 sender.tell(new akka.actor.Status.Failure(failure), self);
100             }
101         });
102     }
103 }