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=31aac92051f1ee6948cbefcf4c68e49d926c5da0;hb=4258ff0d490fc27658ab53dd71bf96c7aa981b13;hpb=b09693b984ad221a7effeeec0d72ef20be3c4e65 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 31aac92051..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,210 +10,89 @@ package org.opendaylight.controller.remote.rpc; import akka.actor.ActorRef; import akka.actor.Props; -import akka.dispatch.OnComplete; -import akka.japi.Creator; -import akka.japi.Pair; - +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 com.google.common.util.concurrent.JdkFutureAdapters; -import com.google.common.util.concurrent.ListenableFuture; - 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.InvokeRpc; import org.opendaylight.controller.remote.rpc.messages.RpcResponse; -import org.opendaylight.controller.remote.rpc.messages.UpdateSchemaContext; -import org.opendaylight.controller.remote.rpc.registry.RpcRegistry; -import org.opendaylight.controller.remote.rpc.utils.LatestEntryRoutingLogic; -import org.opendaylight.controller.remote.rpc.utils.RoutingLogic; -import org.opendaylight.controller.sal.connector.api.RpcRouter; -import org.opendaylight.controller.sal.core.api.Broker; -import org.opendaylight.controller.sal.core.api.Broker.ProviderSession; -import org.opendaylight.controller.xml.codec.XmlUtils; -import org.opendaylight.yangtools.yang.common.RpcError; -import org.opendaylight.yangtools.yang.common.RpcError.ErrorType; -import org.opendaylight.yangtools.yang.common.RpcResult; -import org.opendaylight.yangtools.yang.common.RpcResultBuilder; -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.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.concurrent.Future; - -import static akka.pattern.Patterns.ask; +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 final RemoteRpcProviderConfig config; - - private RpcBroker(Broker.ProviderSession brokerSession, ActorRef rpcRegistry, - SchemaContext schemaContext) { - this.brokerSession = brokerSession; - this.rpcRegistry = rpcRegistry; - this.schemaContext = schemaContext; - config = new RemoteRpcProviderConfig(getContext().system().settings().config()); + private RpcBroker(final DOMRpcService rpcService) { + this.rpcService = rpcService; } - public static Props props(Broker.ProviderSession brokerSession, ActorRef rpcRegistry, - SchemaContext schemaContext) { - return Props.create(new RpcBrokerCreator(brokerSession, rpcRegistry, schemaContext)); + public static Props props(final DOMRpcService rpcService) { + Preconditions.checkNotNull(rpcService, "DOMRpcService can not be null"); + return Props.create(RpcBroker.class, rpcService); } @Override - protected void handleReceive(Object message) throws Exception { - if(message instanceof InvokeRpc) { - invokeRemoteRpc((InvokeRpc) message); - } else if(message instanceof ExecuteRpc) { + protected void handleReceive(final Object message) throws Exception { + if (message instanceof ExecuteRpc) { executeRpc((ExecuteRpc) message); - } else if(message instanceof UpdateSchemaContext) { - updateSchemaContext((UpdateSchemaContext) message); } } - private void updateSchemaContext(UpdateSchemaContext message) { - this.schemaContext = message.getSchemaContext(); - } - - private void invokeRemoteRpc(final InvokeRpc msg) { - if(LOG.isDebugEnabled()) { - LOG.debug("Looking up the remote actor for rpc {}", msg.getRpc()); - } - RpcRouter.RouteIdentifier routeId = new RouteIdentifierImpl( - null, msg.getRpc(), msg.getIdentifier()); - RpcRegistry.Messages.FindRouters findMsg = new RpcRegistry.Messages.FindRouters(routeId); - - scala.concurrent.Future future = ask(rpcRegistry, findMsg, config.getAskDuration()); - + @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(); - OnComplete onComplete = new OnComplete() { - @Override - public void onComplete(Throwable failure, Object reply) throws Throwable { - if(failure != null) { - LOG.error("FindRouters failed", failure); - sender.tell(new akka.actor.Status.Failure(failure), self); - return; - } - - RpcRegistry.Messages.FindRoutersReply findReply = - (RpcRegistry.Messages.FindRoutersReply)reply; - - List> actorRefList = findReply.getRouterWithUpdateTime(); - - if(actorRefList == null || actorRefList.isEmpty()) { - String message = String.format( - "No remote implementation found for rpc %s", msg.getRpc()); - sender.tell(new akka.actor.Status.Failure(new RpcErrorsException( - message, Arrays.asList(RpcResultBuilder.newError(ErrorType.RPC, - "operation-not-supported", message)))), self); - return; - } - - finishInvokeRpc(actorRefList, msg, sender, self); - } - }; - - future.onComplete(onComplete, getContext().dispatcher()); - } - - protected void finishInvokeRpc(final List> actorRefList, - final InvokeRpc msg, final ActorRef sender, final ActorRef self) { - - RoutingLogic logic = new LatestEntryRoutingLogic(actorRefList); - - ExecuteRpc executeMsg = new ExecuteRpc(XmlUtils.inputCompositeNodeToXml(msg.getInput(), - schemaContext), msg.getRpc()); + 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; + } - scala.concurrent.Future future = ask(logic.select(), executeMsg, config.getAskDuration()); + 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()); - OnComplete onComplete = new OnComplete() { - @Override - public void onComplete(Throwable failure, Object reply) throws Throwable { - if(failure != null) { - LOG.error("ExecuteRpc failed", failure); - sender.tell(new akka.actor.Status.Failure(failure), self); - return; + sender.tell(new RpcResponse(result.getResult()), self); + } } - sender.tell(reply, self); - } - }; - - future.onComplete(onComplete, getContext().dispatcher()); - } - - private void executeRpc(final ExecuteRpc msg) { - if(LOG.isDebugEnabled()) { - LOG.debug("Executing rpc {}", msg.getRpc()); - } - Future> future = brokerSession.rpc(msg.getRpc(), - XmlUtils.inputXmlToCompositeNode(msg.getRpc(), msg.getInputCompositeNode(), - schemaContext)); - - ListenableFuture> listenableFuture = - JdkFutureAdapters.listenInPoolThread(future); - - final ActorRef sender = getSender(); - final ActorRef self = self(); - - Futures.addCallback(listenableFuture, new FutureCallback>() { - @Override - public void onSuccess(RpcResult result) { - if(result.isSuccessful()) { - sender.tell(new RpcResponse(XmlUtils.outputCompositeNodeToXml(result.getResult(), - schemaContext)), self); - } else { - String message = String.format("Execution of RPC %s failed", msg.getRpc()); - Collection errors = result.getErrors(); - if(errors == null || errors.size() == 0) { - errors = Arrays.asList(RpcResultBuilder.newError(ErrorType.RPC, - null, message)); + @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(new RpcErrorsException( - message, errors)), self); + sender.tell(new akka.actor.Status.Failure(failure), self); } - } - - @Override - public void onFailure(Throwable t) { - LOG.error("executeRpc for {} failed: {}", msg.getRpc(), t); - sender.tell(new akka.actor.Status.Failure(t), self); - } - }); - } - - private static class RpcBrokerCreator implements Creator { - private static final long serialVersionUID = 1L; - - final Broker.ProviderSession brokerSession; - final ActorRef rpcRegistry; - final SchemaContext schemaContext; - - RpcBrokerCreator(ProviderSession brokerSession, ActorRef rpcRegistry, - SchemaContext schemaContext) { - this.brokerSession = brokerSession; - this.rpcRegistry = rpcRegistry; - this.schemaContext = schemaContext; - } - - @Override - public RpcBroker create() throws Exception { - return new RpcBroker(brokerSession, rpcRegistry, schemaContext); + }); + } catch (final RuntimeException e) { + sender.tell(new akka.actor.Status.Failure(e), sender); } } }