Eliminate XmlUtil.createElement(Document, String)
[netconf.git] / netconf / netconf-console / src / main / java / org / opendaylight / netconf / console / commands / NetconfDisconnectDeviceCommand.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 import org.apache.karaf.shell.api.action.Action;
14 import org.apache.karaf.shell.api.action.Command;
15 import org.apache.karaf.shell.api.action.Option;
16 import org.opendaylight.netconf.console.api.NetconfCommands;
17
18 @Command(name = "netconf:disconnect-device", scope = "netconf", description = "Disconnect netconf device.")
19 public class NetconfDisconnectDeviceCommand implements Action {
20
21     protected final NetconfCommands service;
22
23     public NetconfDisconnectDeviceCommand(final NetconfCommands service) {
24         this.service = service;
25     }
26
27     @VisibleForTesting
28     NetconfDisconnectDeviceCommand(final NetconfCommands service, final String deviceId, final String deviceIp,
29                                    final String devicePort) {
30         this.service = service;
31         this.deviceId = deviceId;
32         this.deviceIp = deviceIp;
33         this.devicePort = devicePort;
34     }
35
36     @Option(name = "-i",
37             aliases = { "--ipaddress" },
38             description = "IP address of the netconf device",
39             required = false,
40             multiValued = false)
41     private String deviceIp;
42
43     @Option(name = "-p",
44             aliases = { "--port" },
45             description = "Port of the netconf device",
46             required = false,
47             multiValued = false)
48     private String devicePort;
49
50     @Option(name = "-id",
51             aliases = { "--identifier" },
52             description = "Node Identifier of the netconf device",
53             required = false,
54             multiValued = false)
55     private String deviceId;
56
57     @Override
58     public Object execute() {
59         boolean status = false;
60         if (!Strings.isNullOrEmpty(deviceId)) {
61             status = service.disconnectDevice(deviceId);
62         } else {
63             if (!NetconfCommandUtils.isIpValid(deviceIp) || !NetconfCommandUtils.isPortValid(devicePort)) {
64                 return "Invalid IP:" + deviceIp + " or Port:" + devicePort + "Please enter a valid entry to proceed.";
65             }
66             status = service.disconnectDevice(deviceIp, devicePort);
67         }
68         final String message = status ? "Netconf connector disconnected succesfully"
69                 : "Failed to disconnect netconf connector. Refer to karaf.log for details.";
70         return message;
71     }
72 }