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.rev231121.connection.parameters.Protocol.Name;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231121.connection.parameters.ProtocolBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231121.connection.parameters.protocol.specification.TlsCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231121.connection.parameters.protocol.specification.tls._case.TlsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231121.credentials.credentials.LoginPwUnencryptedBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev231121.credentials.credentials.login.pw.unencrypted.LoginPasswordUnencryptedBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev231121.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 {
38 private NetconfCommands service;
41 aliases = { "--ipaddress" },
42 description = "IP address of the netconf device",
48 aliases = { "--port" },
49 description = "Port of the netconf device",
55 aliases = { "--username" },
56 description = "Username for netconf connection",
63 aliases = { "--password" },
64 description = "Password for netconf connection",
71 aliases = { "--tcp-only" },
72 description = "Type of connection, true for tcp only",
75 String connectionType = "false";
78 aliases = { "--protocol" },
79 description = "Which protocol to be used, ssh or tls",
82 String protocol = "ssh";
85 aliases = { "--excluded-versions" },
86 description = "TLS versions not supported by target device",
89 String excludedTlsVersions;
92 aliases = { "--schemaless" },
93 description = "Schemaless surpport, true for schemaless",
96 String schemaless = "false";
99 aliases = { "--identifier" },
100 description = "Node Identifier of the netconf device",
103 private String deviceId;
105 public NetconfConnectDeviceCommand() {
106 // Nothing here, uses injection
110 NetconfConnectDeviceCommand(final NetconfCommands service) {
111 this.service = requireNonNull(service);
115 NetconfConnectDeviceCommand(final NetconfCommands service, final String deviceIp, final String devicePort,
116 final String username, final String password) {
117 this.service = requireNonNull(service);
118 this.deviceIp = requireNonNull(deviceIp);
119 this.devicePort = requireNonNull(devicePort);
120 this.username = requireNonNull(username);
121 this.password = requireNonNull(password);
125 public String execute() {
126 if (!NetconfCommandUtils.isIpValid(deviceIp) || !NetconfCommandUtils.isPortValid(devicePort)) {
127 return "Invalid IP:" + deviceIp + " or Port:" + devicePort + "Please enter a valid entry to proceed.";
130 final boolean isTcpOnly = connectionType.equals("true");
131 final boolean isSchemaless = schemaless.equals("true");
133 final var netconfNodeBuilder = new NetconfNodeBuilder()
134 .setHost(new Host(new IpAddress(new Ipv4Address(deviceIp))))
135 .setPort(new PortNumber(Uint16.valueOf(Integer.decode(devicePort))))
136 .setTcpOnly(isTcpOnly)
137 .setSchemaless(isSchemaless);
139 if (isTcpOnly || protocol.equalsIgnoreCase("ssh")) {
140 if (Strings.isNullOrEmpty(username) || Strings.isNullOrEmpty(password)) {
141 return "Empty Username:" + username + " or Password:" + password + ". "
142 + "In TCP or SSH mode, you must provide valid username and password.";
144 netconfNodeBuilder.setCredentials(new LoginPwUnencryptedBuilder()
145 .setLoginPasswordUnencrypted(new LoginPasswordUnencryptedBuilder()
146 .setUsername(username)
147 .setPassword(password)
151 netconfNodeBuilder.setProtocol(new ProtocolBuilder().setName(Name.SSH).build());
153 } else if (protocol.equalsIgnoreCase("tls")) {
154 final var tlsCase = Strings.isNullOrEmpty(excludedTlsVersions) ? null : new TlsCaseBuilder()
155 .setTls(new TlsBuilder()
156 .setExcludedVersions(ImmutableSet.copyOf(excludedTlsVersions.split(",")))
159 netconfNodeBuilder.setProtocol(new ProtocolBuilder().setName(Name.TLS).setSpecification(tlsCase).build());
161 return "Invalid protocol: " + protocol + ". Only SSH and TLS are supported.";
164 service.connectDevice(netconfNodeBuilder.build(), deviceId);
165 return "Netconf connector added succesfully";