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
8 package org.opendaylight.controller.netconf.cli.commands.remote;
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.TimeUnit;
13 import java.util.concurrent.TimeoutException;
14 import org.opendaylight.controller.netconf.cli.commands.AbstractCommand;
15 import org.opendaylight.controller.netconf.cli.commands.Command;
16 import org.opendaylight.controller.netconf.cli.commands.CommandInvocationException;
17 import org.opendaylight.controller.netconf.cli.commands.input.Input;
18 import org.opendaylight.controller.netconf.cli.commands.input.InputDefinition;
19 import org.opendaylight.controller.netconf.cli.commands.output.Output;
20 import org.opendaylight.controller.netconf.cli.commands.output.OutputDefinition;
21 import org.opendaylight.controller.sal.core.api.RpcImplementation;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
25 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
28 * Generic remote command implementation that sends the rpc xml to the remote device and waits for response
29 * Waiting is limited with TIMEOUT
31 public class RemoteCommand extends AbstractCommand {
33 // TODO make this configurable
34 private static final long DEFAULT_TIMEOUT = 10000;
35 private static final TimeUnit DEFAULT_TIMEOUT_UNIT = TimeUnit.MILLISECONDS;
36 private final RpcImplementation rpc;
38 public RemoteCommand(final QName qName, final InputDefinition args, final OutputDefinition output, final String description, final RpcImplementation rpc) {
39 super(qName, args, output, description);
44 public Output invoke(final Input inputArgs) throws CommandInvocationException {
45 final ListenableFuture<RpcResult<CompositeNode>> invokeRpc = rpc.invokeRpc(getCommandId(), inputArgs.wrap(getCommandId()));
47 return new Output(invokeRpc.get(DEFAULT_TIMEOUT, DEFAULT_TIMEOUT_UNIT).getResult());
48 } catch (final ExecutionException e) {
49 throw new CommandInvocationException(getCommandId(), e);
50 } catch (final TimeoutException e) {
51 // Request timed out, cancel request
52 invokeRpc.cancel(true);
53 throw new CommandInvocationException.CommandTimeoutException(getCommandId(), e);
54 } catch (final InterruptedException e) {
55 throw new RuntimeException(e);
59 public static Command fromRpc(final RpcDefinition rpcDefinition, final RpcImplementation rpcInvoker) {
60 final InputDefinition args = getInputDefinition(rpcDefinition);
61 final OutputDefinition retVal = getOutputDefinition(rpcDefinition);
63 return new RemoteCommand(rpcDefinition.getQName(), args, retVal, rpcDefinition.getDescription(), rpcInvoker);