Upgrade ietf-{inet,yang}-types to 2013-07-15
[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 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;
23
24 @Command(name = "netconf:connect-device", scope = "netconf", description = "Connect to a netconf device.")
25 public class NetconfConnectDeviceCommand extends AbstractAction {
26
27     protected final NetconfCommands service;
28
29     public NetconfConnectDeviceCommand(final NetconfCommands service) {
30         this.service = service;
31     }
32
33     @Option(name = "-i",
34             aliases = { "--ipaddress" },
35             description = "IP address of the netconf device",
36             required = true,
37             multiValued = false)
38     private String deviceIp;
39
40     @Option(name = "-p",
41             aliases = { "--port" },
42             description = "Port of the netconf device",
43             required = true,
44             multiValued = false)
45     private String devicePort;
46
47     @Option(name = "-U",
48             aliases = { "--username" },
49             description = "Username for netconf connection",
50             required = true,
51             multiValued = false)
52     private String username;
53
54     @Option(name = "-P",
55             aliases = { "--password" },
56             description = "Password for netconf connection",
57             required = true,
58             multiValued = false)
59     private String password;
60
61     @Option(name = "-t",
62             aliases = { "--tcp-only" },
63             description = "Type of connection, true for tcp only",
64             required = false,
65             multiValued = false)
66     private String connectionType = "false";
67
68     @Option(name = "-id",
69             aliases = { "--identifier" },
70             description = "Node Identifier of the netconf device",
71             required = false,
72             multiValued = false)
73     private String deviceId;
74
75     @Override
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.";
79         }
80
81         final boolean isTcpOnly = (connectionType.equals("true")) ? true : false;
82         final Credentials credentials = new LoginPasswordBuilder().setPassword(password).setUsername(username).build();
83
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)
89                                         .build();
90
91         service.connectDevice(netconfNode, deviceId);
92         final String message = "Netconf connector added succesfully";
93         return message;
94     }
95 }