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 akka.japi.Creator;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Throwables;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.FutureCallback;
18 import com.google.common.util.concurrent.Futures;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
25 import org.opendaylight.controller.remote.rpc.messages.ExecuteRpc;
26 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
27 import org.opendaylight.yangtools.yang.common.RpcError;
28 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
29 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34 * Actor to initiate execution of remote RPC on other nodes of the cluster.
37 public class RpcBroker extends AbstractUntypedActor {
38 private final DOMRpcService rpcService;
40 private RpcBroker(final DOMRpcService rpcService) {
41 this.rpcService = rpcService;
44 public static Props props(final DOMRpcService rpcService) {
45 Preconditions.checkNotNull(rpcService, "DOMRpcService can not be null");
46 return Props.create(new RpcBrokerCreator(rpcService));
50 protected void handleReceive(final Object message) throws Exception {
51 if (message instanceof ExecuteRpc) {
52 executeRpc((ExecuteRpc) message);
56 @SuppressWarnings("checkstyle:IllegalCatch")
57 private void executeRpc(final ExecuteRpc msg) {
58 LOG.debug("Executing rpc {}", msg.getRpc());
59 final NormalizedNode<?, ?> input = RemoteRpcInput.from(msg.getInputNormalizedNode());
60 final SchemaPath schemaPath = SchemaPath.create(true, msg.getRpc());
61 final ActorRef sender = getSender();
62 final ActorRef self = self();
65 final CheckedFuture<DOMRpcResult, DOMRpcException> future = rpcService.invokeRpc(schemaPath, input);
67 Futures.addCallback(future, new FutureCallback<DOMRpcResult>() {
69 public void onSuccess(final DOMRpcResult result) {
70 if (result.getErrors() != null && !result.getErrors().isEmpty()) {
71 final String message = String.format("Execution of RPC %s failed", msg.getRpc());
72 Collection<RpcError> errors = result.getErrors();
73 if (errors == null || errors.size() == 0) {
74 errors = Arrays.asList(RpcResultBuilder.newError(ErrorType.RPC, null, message));
77 sender.tell(new akka.actor.Status.Failure(new RpcErrorsException(message, errors)), self);
79 LOG.debug("Sending response for execute rpc : {}", msg.getRpc());
81 sender.tell(new RpcResponse(result.getResult()), self);
86 public void onFailure(final Throwable failure) {
88 "executeRpc for {} failed with root cause: {}. For exception details, enable Debug logging.",
89 msg.getRpc(), Throwables.getRootCause(failure));
90 if (LOG.isDebugEnabled()) {
91 LOG.debug("Detailed exception for execute RPC failure :{}", failure);
93 sender.tell(new akka.actor.Status.Failure(failure), self);
96 } catch (final RuntimeException e) {
97 sender.tell(new akka.actor.Status.Failure(e), sender);
101 private static class RpcBrokerCreator implements Creator<RpcBroker> {
102 private static final long serialVersionUID = 1L;
104 final DOMRpcService rpcService;
106 RpcBrokerCreator(final DOMRpcService rpcService) {
107 this.rpcService = rpcService;
111 public RpcBroker create() throws Exception {
112 return new RpcBroker(rpcService);