94361901fdd123b96189cca32598f678d2658a06
[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 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
16 import com.google.common.base.Strings;
17
18 @Command(name = "netconf:disconnect-device", scope = "netconf", description = "Disconnect netconf device.")
19 public class NetconfDisconnectDeviceCommand extends AbstractAction {
20
21     protected final NetconfCommands service;
22
23     public NetconfDisconnectDeviceCommand(final NetconfCommands service) {
24         this.service = service;
25     }
26
27     @Option(name = "-i",
28             aliases = { "--ipaddress" },
29             description = "IP address of the netconf device",
30             required = false,
31             multiValued = false)
32     private String deviceIp;
33
34     @Option(name = "-p",
35             aliases = { "--port" },
36             description = "Port of the netconf device",
37             required = false,
38             multiValued = false)
39     private String devicePort;
40
41     @Option(name = "-id",
42             aliases = { "--identifier" },
43             description = "Node Identifier of the netconf device",
44             required = false,
45             multiValued = false)
46     private String deviceId;
47
48     @Override
49     protected Object doExecute() throws Exception {
50         boolean status = false;
51         if (!Strings.isNullOrEmpty(deviceId)) {
52             status = service.disconnectDevice(deviceId);
53         } else {
54             if (!NetconfCommandUtils.isIpValid(deviceIp) && !NetconfCommandUtils.isPortValid(devicePort)) {
55                 return "Invalid IP:" + deviceIp + " or Port:" + devicePort + "Please enter a valid entry to proceed.";
56             }
57             status = service.disconnectDevice(deviceIp, devicePort);
58         }
59         final String message = status ? "Netconf connector disconnected succesfully"
60                 : "Failed to disconnect netconf connector. Refer to karaf.log for details.";
61         return message;
62     }
63 }