Fix FindBugs warnings in sal-remoterpc-connector and enable enforcement
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RpcBroker.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.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;
26
27 /**
28  * Actor to initiate execution of remote RPC on other nodes of the cluster.
29  */
30
31 public class RpcBroker extends AbstractUntypedActor {
32     private final DOMRpcService rpcService;
33
34     private RpcBroker(final DOMRpcService rpcService) {
35         this.rpcService = rpcService;
36     }
37
38     public static Props props(final DOMRpcService rpcService) {
39         Preconditions.checkNotNull(rpcService, "DOMRpcService can not be null");
40         return Props.create(RpcBroker.class, rpcService);
41     }
42
43     @Override
44     protected void handleReceive(final Object message) throws Exception {
45         if (message instanceof ExecuteRpc) {
46             executeRpc((ExecuteRpc) message);
47         }
48     }
49
50     @SuppressWarnings("checkstyle:IllegalCatch")
51     private void executeRpc(final ExecuteRpc msg) {
52         LOG.debug("Executing rpc {}", msg.getRpc());
53         final NormalizedNode<?, ?> input = RemoteRpcInput.from(msg.getInputNormalizedNode());
54         final SchemaPath schemaPath = SchemaPath.create(true, msg.getRpc());
55         final ActorRef sender = getSender();
56         final ActorRef self = self();
57
58         try {
59             final CheckedFuture<DOMRpcResult, DOMRpcException> future = rpcService.invokeRpc(schemaPath, input);
60
61             Futures.addCallback(future, new FutureCallback<DOMRpcResult>() {
62                 @Override
63                 public void onSuccess(final DOMRpcResult result) {
64                     if (result == null) {
65                         // This shouldn't happen but the FutureCallback annotates the result param with Nullable so
66                         // handle null here to avoid FindBugs warning.
67                         LOG.debug("Got null DOMRpcResult - sending null response for execute rpc : {}", msg.getRpc());
68                         sender.tell(new RpcResponse(null), self);
69                         return;
70                     }
71
72                     if (!result.getErrors().isEmpty()) {
73                         final String message = String.format("Execution of RPC %s failed", msg.getRpc());
74                         sender.tell(new akka.actor.Status.Failure(new RpcErrorsException(message,
75                                 result.getErrors())), self);
76                     } else {
77                         LOG.debug("Sending response for execute rpc : {}", msg.getRpc());
78
79                         sender.tell(new RpcResponse(result.getResult()), self);
80                     }
81                 }
82
83                 @Override
84                 public void onFailure(final Throwable failure) {
85                     LOG.error(
86                         "executeRpc for {} failed with root cause: {}. For exception details, enable Debug logging.",
87                         msg.getRpc(), Throwables.getRootCause(failure));
88                     if (LOG.isDebugEnabled()) {
89                         LOG.debug("Detailed exception for execute RPC failure :{}", failure);
90                     }
91                     sender.tell(new akka.actor.Status.Failure(failure), self);
92                 }
93             });
94         } catch (final RuntimeException e) {
95             sender.tell(new akka.actor.Status.Failure(e), sender);
96         }
97     }
98 }