2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.remote.rpc;
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.md.sal.dom.api.DOMRpcException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
22 import org.opendaylight.controller.remote.rpc.messages.ExecuteRpc;
23 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 * Actor receiving invocation requests from remote nodes, routing them to
29 * {@link DOMRpcService#invokeRpc(SchemaPath, NormalizedNode)}.
31 final class RpcInvoker extends AbstractUntypedActor {
32 private final DOMRpcService rpcService;
34 private RpcInvoker(final DOMRpcService rpcService) {
35 this.rpcService = Preconditions.checkNotNull(rpcService);
38 public static Props props(final DOMRpcService rpcService) {
39 Preconditions.checkNotNull(rpcService, "DOMRpcService can not be null");
40 return Props.create(RpcInvoker.class, rpcService);
44 protected void handleReceive(final Object message) {
45 if (message instanceof ExecuteRpc) {
46 executeRpc((ExecuteRpc) message);
48 unknownMessage(message);
52 @SuppressWarnings("checkstyle:IllegalCatch")
53 private void executeRpc(final ExecuteRpc msg) {
54 LOG.debug("Executing rpc {}", msg.getRpc());
55 final SchemaPath schemaPath = SchemaPath.create(true, msg.getRpc());
56 final ActorRef sender = getSender();
57 final ActorRef self = self();
59 final CheckedFuture<DOMRpcResult, DOMRpcException> future;
61 future = rpcService.invokeRpc(schemaPath, msg.getInputNormalizedNode());
62 } catch (final RuntimeException e) {
63 LOG.debug("Failed to invoke RPC {}", msg.getRpc(), e);
64 sender.tell(new akka.actor.Status.Failure(e), sender);
68 Futures.addCallback(future, new FutureCallback<DOMRpcResult>() {
70 public void onSuccess(final DOMRpcResult result) {
72 // This shouldn't happen but the FutureCallback annotates the result param with Nullable so
73 // handle null here to avoid FindBugs warning.
74 LOG.debug("Got null DOMRpcResult - sending null response for execute rpc : {}", msg.getRpc());
75 sender.tell(new RpcResponse(null), self);
79 if (!result.getErrors().isEmpty()) {
80 final String message = String.format("Execution of RPC %s failed", msg.getRpc());
81 sender.tell(new akka.actor.Status.Failure(new RpcErrorsException(message, result.getErrors())),
84 LOG.debug("Sending response for execute rpc : {}", msg.getRpc());
85 sender.tell(new RpcResponse(result.getResult()), self);
90 public void onFailure(final Throwable failure) {
91 LOG.debug("Failed to execute RPC {}", msg.getRpc(), failure);
92 LOG.error("Failed to execute RPC {} due to {}. More details are available on DEBUG level.",
93 msg.getRpc(), Throwables.getRootCause(failure));
94 sender.tell(new akka.actor.Status.Failure(failure), self);