Bump mdsal to 5.0.2
[netconf.git] / netconf / netconf-console / src / main / java / org / opendaylight / netconf / console / commands / NetconfConnectDeviceCommand.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.console.commands;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.Strings;
14 import java.util.Arrays;
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.node.topology.rev150114.NetconfNodeBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.Protocol.Name;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.ProtocolBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.protocol.specification.TlsCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.protocol.specification.TlsCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.protocol.specification.tls._case.TlsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
33 import org.opendaylight.yangtools.yang.common.Uint16;
34
35 @Service
36 @Command(name = "connect-device", scope = "netconf", description = "Connect to a netconf device.")
37 public class NetconfConnectDeviceCommand implements Action {
38
39     @Reference
40     private NetconfCommands service;
41
42     public NetconfConnectDeviceCommand() {
43
44     }
45
46     @VisibleForTesting
47     NetconfConnectDeviceCommand(final NetconfCommands service) {
48         this.service = service;
49     }
50
51     @VisibleForTesting
52     NetconfConnectDeviceCommand(final NetconfCommands service, final String deviceIp, final String devicePort,
53             final String username, final String password) {
54         this.service = requireNonNull(service);
55         this.deviceIp = requireNonNull(deviceIp);
56         this.devicePort = requireNonNull(devicePort);
57         this.username = requireNonNull(username);
58         this.password = requireNonNull(password);
59     }
60
61     @Option(name = "-i",
62             aliases = { "--ipaddress" },
63             description = "IP address of the netconf device",
64             required = true,
65             multiValued = false)
66     private String deviceIp;
67
68     @Option(name = "-p",
69             aliases = { "--port" },
70             description = "Port of the netconf device",
71             required = true,
72             multiValued = false)
73     private String devicePort;
74
75     @Option(name = "-U",
76             aliases = { "--username" },
77             description = "Username for netconf connection",
78             required = false,
79             multiValued = false)
80     private String username;
81
82     @Option(name = "-P",
83             aliases = { "--password" },
84             description = "Password for netconf connection",
85             required = false,
86             multiValued = false)
87     private String password;
88
89     @Option(name = "-t",
90             aliases = { "--tcp-only" },
91             description = "Type of connection, true for tcp only",
92             required = false,
93             multiValued = false)
94     private String connectionType = "false";
95
96     @Option(name = "-pr",
97             aliases = { "--protocol" },
98             description = "Which protocol to be used, ssh or tls",
99             required = false,
100             multiValued = false)
101     private String protocol = "ssh";
102
103     @Option(name = "-ev",
104             aliases = { "--excluded-versions" },
105             description = "TLS versions not supported by target device",
106             required = false,
107             multiValued = false)
108     private String excludedTlsVersions;
109
110     @Option(name = "-sl",
111             aliases = { "--schemaless" },
112             description = "Schemaless surpport, true for schemaless",
113             required = false,
114             multiValued = false)
115     private String schemaless = "false";
116
117     @Option(name = "-id",
118             aliases = { "--identifier" },
119             description = "Node Identifier of the netconf device",
120             required = false,
121             multiValued = false)
122     private String deviceId;
123
124     @Override
125     public Object execute() {
126         if (!NetconfCommandUtils.isIpValid(deviceIp) || !NetconfCommandUtils.isPortValid(devicePort)) {
127             return "Invalid IP:" + deviceIp + " or Port:" + devicePort + "Please enter a valid entry to proceed.";
128         }
129
130         final boolean isTcpOnly = connectionType.equals("true");
131         final boolean isSchemaless = schemaless.equals("true");
132
133         final NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
134         netconfNodeBuilder.setHost(new Host(new IpAddress(new Ipv4Address(deviceIp))))
135                           .setPort(new PortNumber(Uint16.valueOf(Integer.decode(devicePort))))
136                           .setTcpOnly(isTcpOnly)
137                           .setSchemaless(isSchemaless);
138
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.";
143             }
144             final Credentials credentials =
145                     new LoginPasswordBuilder().setPassword(password).setUsername(username).build();
146             netconfNodeBuilder.setCredentials(credentials);
147             if (!isTcpOnly) {
148                 netconfNodeBuilder.setProtocol(new ProtocolBuilder().setName(Name.SSH).build());
149             }
150         } else if (protocol.equalsIgnoreCase("tls")) {
151             TlsCase tlsCase = null;
152             if (!Strings.isNullOrEmpty(excludedTlsVersions)) {
153                 tlsCase = new TlsCaseBuilder()
154                             .setTls(new TlsBuilder()
155                                     .setExcludedVersions(Arrays.asList(excludedTlsVersions.split(","))).build())
156                             .build();
157             }
158             netconfNodeBuilder.setProtocol(new ProtocolBuilder()
159                                             .setName(Name.TLS)
160                                             .setSpecification(tlsCase)
161                                             .build());
162         } else {
163             return "Invalid protocol: " + protocol + ". Only SSH and TLS are supported.";
164         }
165
166         service.connectDevice(netconfNodeBuilder.build(), deviceId);
167         final String message = "Netconf connector added succesfully";
168         return message;
169     }
170 }