2 * Copyright (c) 2016 Inocybe Technologies 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.netconf.console.commands;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.Strings;
14 import com.google.common.collect.ImmutableSet;
15 import org.apache.karaf.shell.api.action.Action;
16 import org.apache.karaf.shell.api.action.Command;
17 import org.apache.karaf.shell.api.action.Option;
18 import org.apache.karaf.shell.api.action.lifecycle.Reference;
19 import org.apache.karaf.shell.api.action.lifecycle.Service;
20 import org.opendaylight.netconf.console.api.NetconfCommands;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.connection.parameters.Protocol.Name;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.connection.parameters.ProtocolBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.connection.parameters.protocol.specification.TlsCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.connection.parameters.protocol.specification.TlsCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.connection.parameters.protocol.specification.tls._case.TlsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.credentials.credentials.LoginPasswordBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNodeBuilder;
32 import org.opendaylight.yangtools.yang.common.Uint16;
35 @Command(name = "connect-device", scope = "netconf", description = "Connect to a netconf device.")
36 public class NetconfConnectDeviceCommand implements Action {
39 private NetconfCommands service;
41 public NetconfConnectDeviceCommand() {
46 NetconfConnectDeviceCommand(final NetconfCommands service) {
47 this.service = service;
51 NetconfConnectDeviceCommand(final NetconfCommands service, final String deviceIp, final String devicePort,
52 final String username, final String password) {
53 this.service = requireNonNull(service);
54 this.deviceIp = requireNonNull(deviceIp);
55 this.devicePort = requireNonNull(devicePort);
56 this.username = requireNonNull(username);
57 this.password = requireNonNull(password);
61 aliases = { "--ipaddress" },
62 description = "IP address of the netconf device",
65 private String deviceIp;
68 aliases = { "--port" },
69 description = "Port of the netconf device",
72 private String devicePort;
75 aliases = { "--username" },
76 description = "Username for netconf connection",
80 private String username;
83 aliases = { "--password" },
84 description = "Password for netconf connection",
88 private String password;
91 aliases = { "--tcp-only" },
92 description = "Type of connection, true for tcp only",
95 private String connectionType = "false";
98 aliases = { "--protocol" },
99 description = "Which protocol to be used, ssh or tls",
102 private String protocol = "ssh";
104 @Option(name = "-ev",
105 aliases = { "--excluded-versions" },
106 description = "TLS versions not supported by target device",
109 private String excludedTlsVersions;
111 @Option(name = "-sl",
112 aliases = { "--schemaless" },
113 description = "Schemaless surpport, true for schemaless",
116 private String schemaless = "false";
118 @Option(name = "-id",
119 aliases = { "--identifier" },
120 description = "Node Identifier of the netconf device",
123 private String deviceId;
126 public Object execute() {
127 if (!NetconfCommandUtils.isIpValid(deviceIp) || !NetconfCommandUtils.isPortValid(devicePort)) {
128 return "Invalid IP:" + deviceIp + " or Port:" + devicePort + "Please enter a valid entry to proceed.";
131 final boolean isTcpOnly = connectionType.equals("true");
132 final boolean isSchemaless = schemaless.equals("true");
134 final NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
135 netconfNodeBuilder.setHost(new Host(new IpAddress(new Ipv4Address(deviceIp))))
136 .setPort(new PortNumber(Uint16.valueOf(Integer.decode(devicePort))))
137 .setTcpOnly(isTcpOnly)
138 .setSchemaless(isSchemaless);
140 if (isTcpOnly || protocol.equalsIgnoreCase("ssh")) {
141 if (Strings.isNullOrEmpty(username) || Strings.isNullOrEmpty(password)) {
142 return "Empty Username:" + username + " or Password:" + password
143 + ". In TCP or SSH mode, you must provide valid username and password.";
145 netconfNodeBuilder.setCredentials(new LoginPasswordBuilder()
146 .setUsername(username)
147 .setPassword(password)
150 netconfNodeBuilder.setProtocol(new ProtocolBuilder().setName(Name.SSH).build());
152 } else if (protocol.equalsIgnoreCase("tls")) {
153 TlsCase tlsCase = null;
154 if (!Strings.isNullOrEmpty(excludedTlsVersions)) {
155 tlsCase = new TlsCaseBuilder()
156 .setTls(new TlsBuilder()
157 .setExcludedVersions(ImmutableSet.copyOf(excludedTlsVersions.split(","))).build())
160 netconfNodeBuilder.setProtocol(new ProtocolBuilder()
162 .setSpecification(tlsCase)
165 return "Invalid protocol: " + protocol + ". Only SSH and TLS are supported.";
168 service.connectDevice(netconfNodeBuilder.build(), deviceId);
169 final String message = "Netconf connector added succesfully";