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