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
9 package org.opendaylight.netconf.console.commands;
11 import com.google.common.annotations.VisibleForTesting;
12 import org.apache.karaf.shell.commands.Command;
13 import org.apache.karaf.shell.commands.Option;
14 import org.apache.karaf.shell.console.AbstractAction;
15 import org.opendaylight.netconf.console.api.NetconfCommands;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
25 @Command(name = "netconf:connect-device", scope = "netconf", description = "Connect to a netconf device.")
26 public class NetconfConnectDeviceCommand extends AbstractAction {
28 protected final NetconfCommands service;
30 public NetconfConnectDeviceCommand(final NetconfCommands service) {
31 this.service = service;
35 NetconfConnectDeviceCommand(final NetconfCommands service, final String deviceIp, final String devicePort) {
36 this.service = service;
37 this.deviceIp = deviceIp;
38 this.devicePort = devicePort;
42 aliases = { "--ipaddress" },
43 description = "IP address of the netconf device",
46 private String deviceIp;
49 aliases = { "--port" },
50 description = "Port of the netconf device",
53 private String devicePort;
56 aliases = { "--username" },
57 description = "Username for netconf connection",
60 private String username;
63 aliases = { "--password" },
64 description = "Password for netconf connection",
67 private String password;
70 aliases = { "--tcp-only" },
71 description = "Type of connection, true for tcp only",
74 private String connectionType = "false";
77 aliases = { "--schemaless" },
78 description = "Schemaless surpport, true for schemaless",
81 private String schemaless = "false";
84 aliases = { "--identifier" },
85 description = "Node Identifier of the netconf device",
88 private String deviceId;
91 protected Object doExecute() throws Exception {
92 if (!NetconfCommandUtils.isIpValid(deviceIp) || !NetconfCommandUtils.isPortValid(devicePort)) {
93 return "Invalid IP:" + deviceIp + " or Port:" + devicePort + "Please enter a valid entry to proceed.";
96 final boolean isTcpOnly = connectionType.equals("true");
97 final boolean isSchemaless = schemaless.equals("true");
98 final Credentials credentials =
99 new LoginPasswordBuilder().setPassword(password).setUsername(username).build();
101 final NetconfNode netconfNode = new NetconfNodeBuilder()
102 .setHost(new Host(new IpAddress(new Ipv4Address(deviceIp))))
103 .setPort(new PortNumber(Integer.decode(devicePort)))
104 .setTcpOnly(isTcpOnly)
105 .setSchemaless(isSchemaless)
106 .setCredentials(credentials)
109 service.connectDevice(netconfNode, deviceId);
110 final String message = "Netconf connector added succesfully";