X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-remoterpc-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fremote%2Frpc%2FRpcBroker.java;h=3d870c99411917e136b2f1cad496dc0c4e9ce01f;hp=26e8e960e3ce46d5f9ea9b3047160134a1f2df36;hb=4258ff0d490fc27658ab53dd71bf96c7aa981b13;hpb=07ba9a998f0b3c3045ed8e31afda5c96de141b3b diff --git a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/RpcBroker.java b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/RpcBroker.java index 26e8e960e3..3d870c9941 100644 --- a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/RpcBroker.java +++ b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/RpcBroker.java @@ -10,132 +10,89 @@ package org.opendaylight.controller.remote.rpc; import akka.actor.ActorRef; import akka.actor.Props; -import akka.japi.Creator; -import org.opendaylight.controller.remote.rpc.messages.ErrorResponse; +import com.google.common.base.Preconditions; +import com.google.common.base.Throwables; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; +import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcException; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcService; import org.opendaylight.controller.remote.rpc.messages.ExecuteRpc; -import org.opendaylight.controller.remote.rpc.messages.GetRoutedRpc; -import org.opendaylight.controller.remote.rpc.messages.GetRoutedRpcReply; -import org.opendaylight.controller.remote.rpc.messages.GetRpc; -import org.opendaylight.controller.remote.rpc.messages.GetRpcReply; -import org.opendaylight.controller.remote.rpc.messages.InvokeRoutedRpc; -import org.opendaylight.controller.remote.rpc.messages.InvokeRpc; import org.opendaylight.controller.remote.rpc.messages.RpcResponse; -import org.opendaylight.controller.remote.rpc.utils.ActorUtil; -import org.opendaylight.controller.remote.rpc.utils.XmlUtils; -import org.opendaylight.controller.sal.core.api.Broker; -import org.opendaylight.yangtools.yang.common.RpcResult; -import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.concurrent.Future; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; /** * Actor to initiate execution of remote RPC on other nodes of the cluster. */ public class RpcBroker extends AbstractUntypedActor { + private final DOMRpcService rpcService; - private static final Logger LOG = LoggerFactory.getLogger(RpcBroker.class); - private final Broker.ProviderSession brokerSession; - private final ActorRef rpcRegistry; - private SchemaContext schemaContext; - - private RpcBroker(Broker.ProviderSession brokerSession, ActorRef rpcRegistry, SchemaContext schemaContext){ - this.brokerSession = brokerSession; - this.rpcRegistry = rpcRegistry; - this.schemaContext = schemaContext; - } - - public static Props props(final Broker.ProviderSession brokerSession, final ActorRef rpcRegistry, final SchemaContext schemaContext){ - return Props.create(new Creator(){ - - @Override - public RpcBroker create() throws Exception { - return new RpcBroker(brokerSession, rpcRegistry, schemaContext); - } - }); - } - @Override - protected void handleReceive(Object message) throws Exception { - if(message instanceof InvokeRoutedRpc) { - invokeRemoteRoutedRpc((InvokeRoutedRpc) message); - } else if(message instanceof InvokeRpc) { - invokeRemoteRpc((InvokeRpc) message); - } else if(message instanceof ExecuteRpc) { - executeRpc((ExecuteRpc) message); + private RpcBroker(final DOMRpcService rpcService) { + this.rpcService = rpcService; } - } - - private void invokeRemoteRoutedRpc(InvokeRoutedRpc msg) { - // Look up the remote actor to execute rpc - LOG.debug("Looking up the remote actor for route {}", msg); - try { - RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, msg.getRpc(), msg.getIdentifier()); - GetRoutedRpc routedRpcMsg = new GetRoutedRpc(routeId); - GetRoutedRpcReply rpcReply = (GetRoutedRpcReply) ActorUtil.executeLocalOperation(rpcRegistry, routedRpcMsg, ActorUtil.LOCAL_ASK_DURATION, ActorUtil.LOCAL_AWAIT_DURATION); - - String remoteActorPath = rpcReply.getRoutePath(); - if(remoteActorPath == null) { - LOG.debug("No remote actor found for rpc execution."); - - getSender().tell(new ErrorResponse( - new IllegalStateException("No remote actor found for rpc execution.")), self()); - } else { - ExecuteRpc executeMsg = new ExecuteRpc(XmlUtils.inputCompositeNodeToXml(msg.getInput(), schemaContext), msg.getRpc()); - - Object operationRes = ActorUtil.executeRemoteOperation(this.context().actorSelection(remoteActorPath), - executeMsg, ActorUtil.REMOTE_ASK_DURATION, ActorUtil.REMOTE_AWAIT_DURATION); - - getSender().tell(operationRes, self()); - } - } catch (Exception e) { - LOG.error(e.toString()); - getSender().tell(new ErrorResponse(e), self()); + public static Props props(final DOMRpcService rpcService) { + Preconditions.checkNotNull(rpcService, "DOMRpcService can not be null"); + return Props.create(RpcBroker.class, rpcService); } - } - - private void invokeRemoteRpc(InvokeRpc msg) { - // Look up the remote actor to execute rpc - LOG.debug("Looking up the remote actor for route {}", msg); - try { - RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, msg.getRpc(), null); - GetRpc rpcMsg = new GetRpc(routeId); - GetRpcReply rpcReply = (GetRpcReply)ActorUtil.executeLocalOperation(rpcRegistry, rpcMsg, ActorUtil.LOCAL_ASK_DURATION, ActorUtil.LOCAL_AWAIT_DURATION); - String remoteActorPath = rpcReply.getRoutePath(); - - if(remoteActorPath == null) { - LOG.debug("No remote actor found for rpc {{}}.", msg.getRpc()); - getSender().tell(new ErrorResponse( - new IllegalStateException("No remote actor found for rpc execution of : " + msg.getRpc())), self()); - } else { - - ExecuteRpc executeMsg = new ExecuteRpc(XmlUtils.inputCompositeNodeToXml(msg.getInput(), schemaContext), msg.getRpc()); - Object operationRes = ActorUtil.executeRemoteOperation(this.context().actorSelection(remoteActorPath), - executeMsg, ActorUtil.REMOTE_ASK_DURATION, ActorUtil.REMOTE_AWAIT_DURATION); - - getSender().tell(operationRes, self()); - } - } catch (Exception e) { - LOG.error(e.toString()); - getSender().tell(new ErrorResponse(e), self()); + @Override + protected void handleReceive(final Object message) throws Exception { + if (message instanceof ExecuteRpc) { + executeRpc((ExecuteRpc) message); + } } - } - private void executeRpc(ExecuteRpc msg) { - LOG.debug("Executing rpc for rpc {}", msg.getRpc()); - try { - Future> rpc = brokerSession.rpc(msg.getRpc(), XmlUtils.inputXmlToCompositeNode(msg.getRpc(), msg.getInputCompositeNode(), schemaContext)); - RpcResult rpcResult = rpc != null ? rpc.get():null; - CompositeNode result = rpcResult != null ? rpcResult.getResult() : null; - getSender().tell(new RpcResponse(XmlUtils.outputCompositeNodeToXml(result, schemaContext)), self()); - } catch (Exception e) { - LOG.error(e.toString()); - getSender().tell(new ErrorResponse(e), self()); + @SuppressWarnings("checkstyle:IllegalCatch") + private void executeRpc(final ExecuteRpc msg) { + LOG.debug("Executing rpc {}", msg.getRpc()); + final NormalizedNode input = RemoteRpcInput.from(msg.getInputNormalizedNode()); + final SchemaPath schemaPath = SchemaPath.create(true, msg.getRpc()); + final ActorRef sender = getSender(); + final ActorRef self = self(); + + try { + final CheckedFuture future = rpcService.invokeRpc(schemaPath, input); + + Futures.addCallback(future, new FutureCallback() { + @Override + public void onSuccess(final DOMRpcResult result) { + if (result == null) { + // This shouldn't happen but the FutureCallback annotates the result param with Nullable so + // handle null here to avoid FindBugs warning. + LOG.debug("Got null DOMRpcResult - sending null response for execute rpc : {}", msg.getRpc()); + sender.tell(new RpcResponse(null), self); + return; + } + + if (!result.getErrors().isEmpty()) { + final String message = String.format("Execution of RPC %s failed", msg.getRpc()); + sender.tell(new akka.actor.Status.Failure(new RpcErrorsException(message, + result.getErrors())), self); + } else { + LOG.debug("Sending response for execute rpc : {}", msg.getRpc()); + + sender.tell(new RpcResponse(result.getResult()), self); + } + } + + @Override + public void onFailure(final Throwable failure) { + LOG.error( + "executeRpc for {} failed with root cause: {}. For exception details, enable Debug logging.", + msg.getRpc(), Throwables.getRootCause(failure)); + if (LOG.isDebugEnabled()) { + LOG.debug("Detailed exception for execute RPC failure :{}", failure); + } + sender.tell(new akka.actor.Status.Failure(failure), self); + } + }); + } catch (final RuntimeException e) { + sender.tell(new akka.actor.Status.Failure(e), sender); + } } - } - }