Search in sources :

Example 1 with RpcResponse

use of org.opendaylight.controller.remote.rpc.messages.RpcResponse in project controller by opendaylight.

the class RpcBrokerTest method testExecuteRpc.

@Test
public void testExecuteRpc() {
    new TestKit(node1) {

        {
            final ContainerNode invokeRpcResult = makeRPCOutput("bar");
            final DOMRpcResult rpcResult = new DefaultDOMRpcResult(invokeRpcResult);
            when(domRpcService1.invokeRpc(eq(TEST_RPC_TYPE), Mockito.<NormalizedNode<?, ?>>any())).thenReturn(Futures.<DOMRpcResult, DOMRpcException>immediateCheckedFuture(rpcResult));
            final ExecuteRpc executeMsg = ExecuteRpc.from(TEST_RPC_ID, null);
            rpcInvoker1.tell(executeMsg, getRef());
            final RpcResponse rpcResponse = expectMsgClass(duration("5 seconds"), RpcResponse.class);
            assertEquals(rpcResult.getResult(), rpcResponse.getResultNormalizedNode());
        }
    };
}
Also used : DefaultDOMRpcResult(org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult) DOMRpcResult(org.opendaylight.controller.md.sal.dom.api.DOMRpcResult) DefaultDOMRpcResult(org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult) ExecuteRpc(org.opendaylight.controller.remote.rpc.messages.ExecuteRpc) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) TestKit(akka.testkit.javadsl.TestKit) RpcResponse(org.opendaylight.controller.remote.rpc.messages.RpcResponse) Test(org.junit.Test)

Example 2 with RpcResponse

use of org.opendaylight.controller.remote.rpc.messages.RpcResponse in project controller by opendaylight.

the class RpcInvoker method executeRpc.

@SuppressWarnings("checkstyle:IllegalCatch")
private void executeRpc(final ExecuteRpc msg) {
    LOG.debug("Executing rpc {}", msg.getRpc());
    final SchemaPath schemaPath = SchemaPath.create(true, msg.getRpc());
    final ActorRef sender = getSender();
    final ActorRef self = self();
    final ListenableFuture<DOMRpcResult> future;
    try {
        future = rpcService.invokeRpc(schemaPath, msg.getInputNormalizedNode());
    } catch (final RuntimeException e) {
        LOG.debug("Failed to invoke RPC {}", msg.getRpc(), e);
        sender.tell(new akka.actor.Status.Failure(e), sender);
        return;
    }
    Futures.addCallback(future, new FutureCallback<DOMRpcResult>() {

        @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.debug("Failed to execute RPC {}", msg.getRpc(), failure);
            LOG.error("Failed to execute RPC {} due to {}. More details are available on DEBUG level.", msg.getRpc(), Throwables.getRootCause(failure));
            sender.tell(new akka.actor.Status.Failure(failure), self);
        }
    }, MoreExecutors.directExecutor());
}
Also used : DOMRpcResult(org.opendaylight.controller.md.sal.dom.api.DOMRpcResult) ActorRef(akka.actor.ActorRef) RpcResponse(org.opendaylight.controller.remote.rpc.messages.RpcResponse) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath)

Aggregations

DOMRpcResult (org.opendaylight.controller.md.sal.dom.api.DOMRpcResult)2 RpcResponse (org.opendaylight.controller.remote.rpc.messages.RpcResponse)2 ActorRef (akka.actor.ActorRef)1 TestKit (akka.testkit.javadsl.TestKit)1 Test (org.junit.Test)1 DefaultDOMRpcResult (org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult)1 ExecuteRpc (org.opendaylight.controller.remote.rpc.messages.ExecuteRpc)1 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)1 SchemaPath (org.opendaylight.yangtools.yang.model.api.SchemaPath)1