netconf-console unit tests added
[netconf.git] / netconf / netconf-console / src / main / java / org / opendaylight / netconf / console / commands / NetconfUpdateDeviceCommand.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 java.util.HashMap;
13 import java.util.Map;
14
15 import org.apache.karaf.shell.commands.Command;
16 import org.apache.karaf.shell.commands.Option;
17 import org.apache.karaf.shell.console.AbstractAction;
18 import org.opendaylight.netconf.console.api.NetconfCommands;
19 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
20
21 @Command(name = "netconf:update-device", scope = "netconf", description = "Update netconf device attributes.")
22 public class NetconfUpdateDeviceCommand extends AbstractAction {
23
24     protected final NetconfCommands service;
25
26     public NetconfUpdateDeviceCommand(final NetconfCommands service) {
27         this.service = service;
28     }
29
30     @VisibleForTesting
31     NetconfUpdateDeviceCommand(final NetconfCommands service, final String newIp) {
32         this.service = service;
33         this.newIp = newIp;
34     }
35
36     @Option(name = "-id",
37             aliases = { "--nodeId" },
38             description = "NETCONF node ID of the netconf device",
39             required = true,
40             multiValued = false)
41     private String deviceId;
42
43     @Option(name = "-U",
44             aliases = { "--username" },
45             description = "Username for NETCONF connection",
46             required = true,
47             multiValued = false)
48     private String username;
49
50     @Option(name = "-P",
51             aliases = { "--password" },
52             description = "Password for NETCONF connection",
53             required = true,
54             multiValued = false)
55     private String password;
56
57     @Option(name = "-ni",
58             aliases = { "--new-ipaddress" },
59             description = "New IP address of NETCONF device",
60             required = false,
61             multiValued = false)
62     private String newIp;
63
64     @Option(name = "-np",
65             aliases = { "--new-port" },
66             description = "New Port of NETCONF device",
67             required = false,
68             multiValued = false)
69     private String newPort;
70
71     @Option(name = "-nU",
72             aliases = { "--new-username" },
73             description = "New Username for NETCONF connection",
74             required = false,
75             multiValued = false)
76     private String newUsername;
77
78     @Option(name = "-nP",
79             aliases = { "--new-password" },
80             description = "New Password for NETCONF connection",
81             required = false,
82             multiValued = false)
83     private String newPassword;
84
85     @Option(name = "-t",
86             aliases = { "--tcp-only" },
87             description = "Type of connection, true for tcp only",
88             required = false,
89             multiValued = false)
90     private String newConnectionType = "false";
91
92     @Override
93     protected Object doExecute() throws Exception {
94
95         Map<String, String> updated = new HashMap<>();
96         updated.put(NetconfConsoleConstants.NETCONF_IP, newIp);
97         updated.put(NetconfConsoleConstants.NETCONF_PORT, newPort);
98         updated.put(NetconfConsoleConstants.USERNAME, newUsername);
99         updated.put(NetconfConsoleConstants.PASSWORD, newPassword);
100         updated.put(NetconfConsoleConstants.TCP_ONLY, newConnectionType);
101         updated.values().remove(null);
102
103         if (updated.isEmpty()) {
104             return "Nothing to update.";
105         } else {
106             String statusMessage = service.updateDevice(deviceId, username, password, updated);
107             return statusMessage;
108         }
109     }
110
111 }