Move shaded components to third-party/
[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             censor = true,
51             multiValued = false)
52     private String username;
53
54     @Option(name = "-P",
55             aliases = { "--password" },
56             description = "Password for NETCONF connection",
57             required = true,
58             censor = true,
59             multiValued = false)
60     private String password;
61
62     @Option(name = "-ni",
63             aliases = { "--new-ipaddress" },
64             description = "New IP address of NETCONF device",
65             required = false,
66             multiValued = false)
67     private String newIp;
68
69     @Option(name = "-np",
70             aliases = { "--new-port" },
71             description = "New Port of NETCONF device",
72             required = false,
73             multiValued = false)
74     private String newPort;
75
76     @Option(name = "-nU",
77             aliases = { "--new-username" },
78             description = "New Username for NETCONF connection",
79             required = false,
80             multiValued = false)
81     private String newUsername;
82
83     @Option(name = "-nP",
84             aliases = { "--new-password" },
85             description = "New Password for NETCONF connection",
86             required = false,
87             multiValued = false)
88     private String newPassword;
89
90     @Option(name = "-t",
91             aliases = { "--tcp-only" },
92             description = "Type of connection, true for tcp only",
93             required = false,
94             multiValued = false)
95     private String newConnectionType = "false";
96
97     @Option(name = "-sl",
98             aliases = { "--schemaless" },
99             description = "Schemaless surpport, true for schemaless",
100             required = false,
101             multiValued = false)
102     private String newSchemaless = "false";
103
104     @Override
105     public Object execute() {
106         Map<String, String> updated = new HashMap<>();
107         updated.put(NetconfConsoleConstants.NETCONF_IP, newIp);
108         updated.put(NetconfConsoleConstants.NETCONF_PORT, newPort);
109         updated.put(NetconfConsoleConstants.USERNAME, newUsername);
110         updated.put(NetconfConsoleConstants.PASSWORD, newPassword);
111         updated.put(NetconfConsoleConstants.TCP_ONLY, newConnectionType);
112         updated.put(NetconfConsoleConstants.SCHEMALESS,newSchemaless);
113         updated.values().remove(null);
114
115         if (updated.isEmpty()) {
116             return "Nothing to update.";
117         }
118
119         return service.updateDevice(deviceId, username, password, updated);
120     }
121 }