Simplify boolean expressions
[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 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;
24
25 @Command(name = "netconf:connect-device", scope = "netconf", description = "Connect to a netconf device.")
26 public class NetconfConnectDeviceCommand extends AbstractAction {
27
28     protected final NetconfCommands service;
29
30     public NetconfConnectDeviceCommand(final NetconfCommands service) {
31         this.service = service;
32     }
33
34     @VisibleForTesting
35     NetconfConnectDeviceCommand(final NetconfCommands service, final String deviceIp, final String devicePort) {
36         this.service = service;
37         this.deviceIp = deviceIp;
38         this.devicePort = devicePort;
39     }
40
41     @Option(name = "-i",
42             aliases = { "--ipaddress" },
43             description = "IP address of the netconf device",
44             required = true,
45             multiValued = false)
46     private String deviceIp;
47
48     @Option(name = "-p",
49             aliases = { "--port" },
50             description = "Port of the netconf device",
51             required = true,
52             multiValued = false)
53     private String devicePort;
54
55     @Option(name = "-U",
56             aliases = { "--username" },
57             description = "Username for netconf connection",
58             required = true,
59             multiValued = false)
60     private String username;
61
62     @Option(name = "-P",
63             aliases = { "--password" },
64             description = "Password for netconf connection",
65             required = true,
66             multiValued = false)
67     private String password;
68
69     @Option(name = "-t",
70             aliases = { "--tcp-only" },
71             description = "Type of connection, true for tcp only",
72             required = false,
73             multiValued = false)
74     private String connectionType = "false";
75
76     @Option(name = "-sl",
77             aliases = { "--schemaless" },
78             description = "Schemaless surpport, true for schemaless",
79             required = false,
80             multiValued = false)
81     private String schemaless = "false";
82
83     @Option(name = "-id",
84             aliases = { "--identifier" },
85             description = "Node Identifier of the netconf device",
86             required = false,
87             multiValued = false)
88     private String deviceId;
89
90     @Override
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.";
94         }
95
96         final boolean isTcpOnly = connectionType.equals("true");
97         final boolean isSchemaless = schemaless.equals("true");
98         final Credentials credentials = new LoginPasswordBuilder().setPassword(password).setUsername(username).build();
99
100         final NetconfNode netconfNode = new NetconfNodeBuilder()
101                                         .setHost(new Host(new IpAddress(new Ipv4Address(deviceIp))))
102                                         .setPort(new PortNumber(Integer.decode(devicePort)))
103                                         .setTcpOnly(isTcpOnly)
104                                         .setSchemaless(isSchemaless)
105                                         .setCredentials(credentials)
106                                         .build();
107
108         service.connectDevice(netconfNode, deviceId);
109         final String message = "Netconf connector added succesfully";
110         return message;
111     }
112 }