Move netconf-console to apps/
[netconf.git] / apps / 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 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;
33
34 @Service
35 @Command(name = "connect-device", scope = "netconf", description = "Connect to a netconf device.")
36 public class NetconfConnectDeviceCommand implements Action {
37
38     @Reference
39     private NetconfCommands service;
40
41     public NetconfConnectDeviceCommand() {
42
43     }
44
45     @VisibleForTesting
46     NetconfConnectDeviceCommand(final NetconfCommands service) {
47         this.service = service;
48     }
49
50     @VisibleForTesting
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);
58     }
59
60     @Option(name = "-i",
61             aliases = { "--ipaddress" },
62             description = "IP address of the netconf device",
63             required = true,
64             multiValued = false)
65     private String deviceIp;
66
67     @Option(name = "-p",
68             aliases = { "--port" },
69             description = "Port of the netconf device",
70             required = true,
71             multiValued = false)
72     private String devicePort;
73
74     @Option(name = "-U",
75             aliases = { "--username" },
76             description = "Username for netconf connection",
77             required = false,
78             censor = true,
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             censor = true,
87             multiValued = false)
88     private String password;
89
90     @Option(name = "-t",
91             aliases = { "--tcp-only" },
92             description = "Type of connection, true for tcp only",
93             required = false,
94             multiValued = false)
95     private String connectionType = "false";
96
97     @Option(name = "-pr",
98             aliases = { "--protocol" },
99             description = "Which protocol to be used, ssh or tls",
100             required = false,
101             multiValued = false)
102     private String protocol = "ssh";
103
104     @Option(name = "-ev",
105             aliases = { "--excluded-versions" },
106             description = "TLS versions not supported by target device",
107             required = false,
108             multiValued = false)
109     private String excludedTlsVersions;
110
111     @Option(name = "-sl",
112             aliases = { "--schemaless" },
113             description = "Schemaless surpport, true for schemaless",
114             required = false,
115             multiValued = false)
116     private String schemaless = "false";
117
118     @Option(name = "-id",
119             aliases = { "--identifier" },
120             description = "Node Identifier of the netconf device",
121             required = false,
122             multiValued = false)
123     private String deviceId;
124
125     @Override
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.";
129         }
130
131         final boolean isTcpOnly = connectionType.equals("true");
132         final boolean isSchemaless = schemaless.equals("true");
133
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);
139
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.";
144             }
145             netconfNodeBuilder.setCredentials(new LoginPasswordBuilder()
146                 .setUsername(username)
147                 .setPassword(password)
148                 .build());
149             if (!isTcpOnly) {
150                 netconfNodeBuilder.setProtocol(new ProtocolBuilder().setName(Name.SSH).build());
151             }
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())
158                             .build();
159             }
160             netconfNodeBuilder.setProtocol(new ProtocolBuilder()
161                                             .setName(Name.TLS)
162                                             .setSpecification(tlsCase)
163                                             .build());
164         } else {
165             return "Invalid protocol: " + protocol + ". Only SSH and TLS are supported.";
166         }
167
168         service.connectDevice(netconfNodeBuilder.build(), deviceId);
169         final String message = "Netconf connector added succesfully";
170         return message;
171     }
172 }