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