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 org.apache.karaf.shell.commands.Command;
12 import org.apache.karaf.shell.commands.Option;
13 import org.apache.karaf.shell.console.AbstractAction;
14 import org.opendaylight.netconf.console.api.NetconfCommands;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
24 @Command(name = "netconf:connect-device", scope = "netconf", description = "Connect to a netconf device.")
25 public class NetconfConnectDeviceCommand extends AbstractAction {
27 protected final NetconfCommands service;
29 public NetconfConnectDeviceCommand(final NetconfCommands service) {
30 this.service = service;
34 aliases = { "--ipaddress" },
35 description = "IP address of the netconf device",
38 private String deviceIp;
41 aliases = { "--port" },
42 description = "Port of the netconf device",
45 private String devicePort;
48 aliases = { "--username" },
49 description = "Username for netconf connection",
52 private String username;
55 aliases = { "--password" },
56 description = "Password for netconf connection",
59 private String password;
62 aliases = { "--tcp-only" },
63 description = "Type of connection, true for tcp only",
66 private String connectionType = "false";
69 aliases = { "--identifier" },
70 description = "Node Identifier of the netconf device",
73 private String deviceId;
76 protected Object doExecute() throws Exception {
77 if (!NetconfCommandUtils.isIpValid(deviceIp) || !NetconfCommandUtils.isPortValid(devicePort)) {
78 return "Invalid IP:" + deviceIp + " or Port:" + devicePort + "Please enter a valid entry to proceed.";
81 final boolean isTcpOnly = (connectionType.equals("true")) ? true : false;
82 final Credentials credentials = new LoginPasswordBuilder().setPassword(password).setUsername(username).build();
84 final NetconfNode netconfNode = new NetconfNodeBuilder()
85 .setHost(new Host(new IpAddress(new Ipv4Address(deviceIp))))
86 .setPort(new PortNumber(Integer.decode(devicePort)))
87 .setTcpOnly(isTcpOnly)
88 .setCredentials(credentials)
91 service.connectDevice(netconfNode, deviceId);
92 final String message = "Netconf connector added succesfully";