be70b8155e473d28e95567129dcdd87ea97d9c08
[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
9 package org.opendaylight.netconf.console.commands;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Strings;
13
14 import java.util.Arrays;
15
16 import org.apache.karaf.shell.commands.Command;
17 import org.apache.karaf.shell.commands.Option;
18 import org.apache.karaf.shell.console.AbstractAction;
19 import org.opendaylight.netconf.console.api.NetconfCommands;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.Protocol.Name;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.ProtocolBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.protocol.specification.TlsCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.protocol.specification.TlsCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.protocol.specification.tls._case.TlsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
32
33 @Command(name = "netconf:connect-device", scope = "netconf", description = "Connect to a netconf device.")
34 public class NetconfConnectDeviceCommand extends AbstractAction {
35
36     protected final NetconfCommands service;
37
38     public NetconfConnectDeviceCommand(final NetconfCommands service) {
39         this.service = service;
40     }
41
42     @VisibleForTesting
43     NetconfConnectDeviceCommand(final NetconfCommands service, final String deviceIp, final String devicePort) {
44         this.service = service;
45         this.deviceIp = deviceIp;
46         this.devicePort = devicePort;
47     }
48
49     @Option(name = "-i",
50             aliases = { "--ipaddress" },
51             description = "IP address of the netconf device",
52             required = true,
53             multiValued = false)
54     private String deviceIp;
55
56     @Option(name = "-p",
57             aliases = { "--port" },
58             description = "Port of the netconf device",
59             required = true,
60             multiValued = false)
61     private String devicePort;
62
63     @Option(name = "-U",
64             aliases = { "--username" },
65             description = "Username for netconf connection",
66             required = false,
67             multiValued = false)
68     private String username;
69
70     @Option(name = "-P",
71             aliases = { "--password" },
72             description = "Password for netconf connection",
73             required = false,
74             multiValued = false)
75     private String password;
76
77     @Option(name = "-t",
78             aliases = { "--tcp-only" },
79             description = "Type of connection, true for tcp only",
80             required = false,
81             multiValued = false)
82     private String connectionType = "false";
83
84     @Option(name = "-pr",
85             aliases = { "--protocol" },
86             description = "Which protocol to be used, ssh or tls",
87             required = false,
88             multiValued = false)
89     private String protocol = "ssh";
90
91     @Option(name = "-ev",
92             aliases = { "--excluded-versions" },
93             description = "TLS versions not supported by target device",
94             required = false,
95             multiValued = false)
96     private String excludedTlsVersions;
97
98     @Option(name = "-sl",
99             aliases = { "--schemaless" },
100             description = "Schemaless surpport, true for schemaless",
101             required = false,
102             multiValued = false)
103     private String schemaless = "false";
104
105     @Option(name = "-id",
106             aliases = { "--identifier" },
107             description = "Node Identifier of the netconf device",
108             required = false,
109             multiValued = false)
110     private String deviceId;
111
112     @Override
113     protected Object doExecute() throws Exception {
114         if (!NetconfCommandUtils.isIpValid(deviceIp) || !NetconfCommandUtils.isPortValid(devicePort)) {
115             return "Invalid IP:" + deviceIp + " or Port:" + devicePort + "Please enter a valid entry to proceed.";
116         }
117
118         final boolean isTcpOnly = connectionType.equals("true");
119         final boolean isSchemaless = schemaless.equals("true");
120
121         final NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
122         netconfNodeBuilder.setHost(new Host(new IpAddress(new Ipv4Address(deviceIp))))
123                           .setPort(new PortNumber(Integer.decode(devicePort)))
124                           .setTcpOnly(isTcpOnly)
125                           .setSchemaless(isSchemaless);
126
127         if (isTcpOnly || protocol.equalsIgnoreCase("ssh")) {
128             if (Strings.isNullOrEmpty(username) || Strings.isNullOrEmpty(password)) {
129                 return "Empty Username:" + username + " or Password:" + password
130                         + ". In TCP or SSH mode, you must provide valid username and password.";
131             }
132             final Credentials credentials =
133                     new LoginPasswordBuilder().setPassword(password).setUsername(username).build();
134             netconfNodeBuilder.setCredentials(credentials);
135             if (!isTcpOnly) {
136                 netconfNodeBuilder.setProtocol(new ProtocolBuilder().setName(Name.SSH).build());
137             }
138         } else if (protocol.equalsIgnoreCase("tls")) {
139             TlsCase tlsCase = null;
140             if (!Strings.isNullOrEmpty(excludedTlsVersions)) {
141                 tlsCase = new TlsCaseBuilder()
142                             .setTls(new TlsBuilder()
143                                     .setExcludedVersions(Arrays.asList(excludedTlsVersions.split(","))).build())
144                             .build();
145             }
146             netconfNodeBuilder.setProtocol(new ProtocolBuilder()
147                                             .setName(Name.TLS)
148                                             .setSpecification(tlsCase)
149                                             .build());
150         } else {
151             return "Invalid protocol: " + protocol + ". Only SSH and TLS are supported.";
152         }
153
154         service.connectDevice(netconfNodeBuilder.build(), deviceId);
155         final String message = "Netconf connector added succesfully";
156         return message;
157     }
158 }