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