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;
10 import com.google.common.base.Preconditions;
11 import java.io.IOException;
12 import java.net.InetAddress;
13 import java.net.InetSocketAddress;
14 import java.net.UnknownHostException;
15 import net.sourceforge.argparse4j.ArgumentParsers;
16 import net.sourceforge.argparse4j.inf.ArgumentGroup;
17 import net.sourceforge.argparse4j.inf.ArgumentParser;
18 import net.sourceforge.argparse4j.inf.ArgumentParserException;
19 import net.sourceforge.argparse4j.inf.Namespace;
20 import org.opendaylight.controller.netconf.cli.commands.CommandDispatcher;
21 import org.opendaylight.controller.netconf.cli.commands.local.Connect;
22 import org.opendaylight.controller.netconf.cli.io.ConsoleIO;
23 import org.opendaylight.controller.netconf.cli.io.ConsoleIOImpl;
24 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
25 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
26 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 * Parse arguments, start remote device connection and start CLI after the
31 * connection is fully up
35 public static void main(final String[] args) {
36 final CliArgumentParser cliArgs = new CliArgumentParser();
39 } catch (final ArgumentParserException e) {
40 // Just end the cli, exception was handled by the CliArgumentParser
44 final ConsoleIO consoleIO;
46 consoleIO = new ConsoleIOImpl();
47 } catch (final IOException e) {
48 handleStartupException(e);
52 final SchemaContext localSchema = CommandDispatcher.parseSchema(CommandDispatcher.LOCAL_SCHEMA_PATHS);
53 final SchemaContextRegistry schemaContextRegistry = new SchemaContextRegistry(localSchema);
55 final CommandDispatcher commandDispatcher = new CommandDispatcher();
56 final CommandArgHandlerRegistry argumentHandlerRegistry = new CommandArgHandlerRegistry(consoleIO,
57 schemaContextRegistry);
58 final NetconfDeviceConnectionManager connectionManager = new NetconfDeviceConnectionManager(commandDispatcher,
59 argumentHandlerRegistry, schemaContextRegistry, consoleIO);
61 commandDispatcher.addLocalCommands(connectionManager, localSchema, cliArgs.getConnectionTimeoutMs());
63 switch (cliArgs.connectionArgsPresent()) {
65 // FIXME support pure TCP
66 handleRunningException(new UnsupportedOperationException("PURE TCP CONNECTIONS ARE NOT SUPPORTED YET, USE SSH INSTEAD BY PROVIDING USERNAME AND PASSWORD AS WELL"));
70 writeStatus(consoleIO, "Connecting to %s via SSH. Please wait.", cliArgs.getAddress());
71 connectionManager.connectBlocking(cliArgs.getAddress(), getClientSshConfig(cliArgs));
74 case NONE: {/* Do not connect initially */
75 writeStatus(consoleIO, "No initial connection. To connect use the connect command");
80 new Cli(consoleIO, commandDispatcher, argumentHandlerRegistry, schemaContextRegistry).run();
81 } catch (final Exception e) {
82 // TODO Running exceptions have to be handled properly
83 handleRunningException(e);
88 private static NetconfClientConfigurationBuilder getClientConfig(final CliArgumentParser cliArgs) {
89 return NetconfClientConfigurationBuilder.create().withAddress(cliArgs.getServerAddress())
90 .withConnectionTimeoutMillis(cliArgs.getConnectionTimeoutMs())
91 .withReconnectStrategy(Connect.getReconnectStrategy())
92 .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TCP);
95 private static NetconfClientConfigurationBuilder getClientSshConfig(final CliArgumentParser cliArgs) {
96 return NetconfClientConfigurationBuilder.create().withAddress(cliArgs.getServerAddress())
97 .withConnectionTimeoutMillis(cliArgs.getConnectionTimeoutMs())
98 .withReconnectStrategy(Connect.getReconnectStrategy())
99 .withAuthHandler(cliArgs.getCredentials())
100 .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH);
103 private static void handleStartupException(final IOException e) {
104 handleException(e, "Unable to initialize CLI");
107 private static void handleException(final Exception e, final String message) {
108 // FIXME syserr the exception and stacktrace
111 private static void writeStatus(final ConsoleIO io, final String blueprint, final Object... args) {
113 io.formatLn(blueprint, args);
114 } catch (final IOException e) {
115 handleStartupException(e);
119 private static void handleRunningException(final Exception e) {
120 handleException(e, "Unexpected CLI runtime exception");
123 private static final class CliArgumentParser {
125 public static final String USERNAME = "username";
126 public static final String PASSWORD = "password";
127 public static final String SERVER = "server";
128 public static final String PORT = "port";
130 public static final String CONNECT_TIMEOUT = "connectionTimeout";
131 public static final int DEFAULT_CONNECTION_TIMEOUT_MS = 50000;
133 private final ArgumentParser parser;
134 private Namespace parsed;
136 private CliArgumentParser() {
137 parser = ArgumentParsers.newArgumentParser("Netconf cli").defaultHelp(true)
138 .description("Generic cli for netconf devices")
139 .usage("Submit address + port for initial TCP connection (PURE TCP CONNECTIONS ARE NOT SUPPORTED YET)\n" +
140 "Submit username + password in addition to address + port for initial SSH connection\n" +
141 "If no arguments(or unexpected combination) is submitted, cli will be started without initial connection\n" +
142 "To use with ODL controller, run with: java -jar netconf-cli-0.2.5-SNAPSHOT-executable.jar --server localhost --port 1830 --username admin --password admin");
144 final ArgumentGroup tcpGroup = parser.addArgumentGroup("TCP")
145 .description("Base arguments to initiate TCP connection right away");
147 tcpGroup.addArgument("--" + SERVER).help("Netconf device ip-address/domain name");
148 tcpGroup.addArgument("--" + PORT).type(Integer.class).help("Netconf device port");
149 tcpGroup.addArgument("--" + CONNECT_TIMEOUT)
151 .setDefault(DEFAULT_CONNECTION_TIMEOUT_MS)
152 .help("Timeout(in ms) for connection to succeed, if the connection is not fully established by the time is up, " +
153 "connection attempt is considered a failure. This attribute is not working as expected yet");
155 final ArgumentGroup sshGroup = parser.addArgumentGroup("SSH")
156 .description("SSH credentials, if provided, initial connection will be attempted using SSH");
158 sshGroup.addArgument("--" + USERNAME).help("Username for SSH connection");
159 sshGroup.addArgument("--" + PASSWORD).help("Password for SSH connection");
162 public void parse(final String[] args) throws ArgumentParserException {
164 this.parsed = parser.parseArgs(args);
165 } catch (final ArgumentParserException e) {
166 parser.handleError(e);
171 public InetSocketAddress getServerAddress() {
173 return new InetSocketAddress(InetAddress.getByName(getAddress()), getPort());
174 } catch (final UnknownHostException e) {
175 throw new IllegalArgumentException(e);
179 private Integer getPort() {
181 return parsed.getInt(PORT);
184 private String getAddress() {
186 return getString(SERVER);
189 private Integer getConnectionTimeoutMs() {
191 return parsed.getInt(CONNECT_TIMEOUT);
194 private void checkParsed() {
195 Preconditions.checkState(parsed != null, "No arguments were parsed yet");
198 public String getUsername() {
200 return getString(USERNAME);
203 private String getString(final String key) {
204 return parsed.getString(key);
207 public LoginPassword getCredentials() {
208 return new LoginPassword(getUsername(), getPassword());
211 public String getPassword() {
213 return getString(PASSWORD);
216 public InitialConnectionType connectionArgsPresent() {
217 if(getAddress() != null && getPort() != null) {
218 if(getUsername() != null && getPassword() != null) {
219 return InitialConnectionType.SSH;
221 return InitialConnectionType.TCP;
223 return InitialConnectionType.NONE;
226 enum InitialConnectionType {