2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.web;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.ArrayList;
14 import java.util.List;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpSession;
20 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
21 import org.opendaylight.controller.connectionmanager.IConnectionManager;
22 import org.opendaylight.controller.sal.authorization.UserLevel;
23 import org.opendaylight.controller.sal.core.Description;
24 import org.opendaylight.controller.sal.core.Node;
25 import org.opendaylight.controller.sal.utils.GlobalConstants;
26 import org.opendaylight.controller.sal.utils.ServiceHelper;
27 import org.opendaylight.controller.sal.utils.Status;
28 import org.opendaylight.controller.sal.utils.StatusCode;
29 import org.opendaylight.controller.switchmanager.ISwitchManager;
30 import org.opendaylight.controller.usermanager.IUserManager;
31 import org.opendaylight.controller.usermanager.UserConfig;
32 import org.springframework.stereotype.Controller;
33 import org.springframework.web.bind.annotation.PathVariable;
34 import org.springframework.web.bind.annotation.RequestMapping;
35 import org.springframework.web.bind.annotation.RequestMethod;
36 import org.springframework.web.bind.annotation.RequestParam;
37 import org.springframework.web.bind.annotation.ResponseBody;
39 import com.google.gson.Gson;
42 @RequestMapping("/admin")
43 public class DaylightWebAdmin {
44 Gson gson = new Gson();
47 * Returns list of clustered controllers. Highlights "this" controller and
48 * if controller is coordinator
50 * @return List<ClusterBean>
52 @RequestMapping("/cluster")
54 public String getClusteredControllers() {
55 IClusterGlobalServices clusterServices = (IClusterGlobalServices) ServiceHelper.getGlobalInstance(
56 IClusterGlobalServices.class, this);
57 if (clusterServices == null) {
60 IConnectionManager connectionManager = (IConnectionManager) ServiceHelper.getGlobalInstance(
61 IConnectionManager.class, this);
62 if (connectionManager == null) {
66 List<ClusterNodeBean> clusterNodes = new ArrayList<ClusterNodeBean>();
68 List<InetAddress> controllers = clusterServices.getClusteredControllers();
69 for (InetAddress controller : controllers) {
70 ClusterNodeBean.Builder clusterBeanBuilder = new ClusterNodeBean.Builder(controller);
72 // get number of connected nodes
73 Set<Node> connectedNodes = connectionManager.getNodes(controller);
74 int numNodes = connectedNodes == null ? 0 : connectedNodes.size();
75 clusterBeanBuilder.nodesConnected(numNodes);
77 // determine if this is the executing controller
78 if (controller.equals(clusterServices.getMyAddress())) {
79 clusterBeanBuilder.highlightMe();
82 // determine whether this is coordinator
83 if (clusterServices.getCoordinatorAddress().equals(controller)) {
84 clusterBeanBuilder.iAmCoordinator();
86 clusterNodes.add(clusterBeanBuilder.build());
89 return gson.toJson(clusterNodes);
93 * Return nodes connected to controller {controller}
96 * - byte[] of the address of the controller
97 * @return List<NodeBean>
99 @RequestMapping("/cluster/controller/{controller}")
101 public String getNodesConnectedToController(@PathVariable("controller") String controller) {
102 IClusterGlobalServices clusterServices = (IClusterGlobalServices) ServiceHelper.getGlobalInstance(
103 IClusterGlobalServices.class, this);
104 if (clusterServices == null) {
107 IConnectionManager connectionManager = (IConnectionManager) ServiceHelper.getGlobalInstance(
108 IConnectionManager.class, this);
109 if (connectionManager == null) {
112 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
113 GlobalConstants.DEFAULT.toString(), this);
114 if (switchManager == null) {
118 byte[] address = gson.fromJson(controller, byte[].class);
119 InetAddress controllerAddress = null;
121 controllerAddress = InetAddress.getByAddress(address);
122 } catch (UnknownHostException e) {
126 List<NodeBean> result = new ArrayList<NodeBean>();
128 Set<Node> nodes = connectionManager.getNodes(controllerAddress);
130 return gson.toJson(result);
132 for (Node node : nodes) {
133 Description description = (Description) switchManager.getNodeProp(node, Description.propertyName);
135 if (description == null || description.getValue().equals("None")) {
136 nodeBean = new NodeBean(node);
138 nodeBean = new NodeBean(node, description.getValue());
140 result.add(nodeBean);
143 return gson.toJson(result);
146 @RequestMapping("/users")
148 public List<UserConfig> getUsers() {
149 IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
150 if (userManager == null) {
154 List<UserConfig> userConfList = userManager.getLocalUserList();
160 * Password in clear text, moving to HTTP/SSL soon
162 @RequestMapping(value = "/users", method = RequestMethod.POST)
164 public Status saveLocalUserConfig(@RequestParam(required = true) String json,
165 @RequestParam(required = true) String action, HttpServletRequest request) {
167 IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
168 if (userManager == null) {
169 return new Status(StatusCode.NOSERVICE, "User Manager unavailable");
172 if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
173 return new Status(StatusCode.UNAUTHORIZED, "Operation not permitted");
176 Gson gson = new Gson();
177 UserConfig plainConfig = gson.fromJson(json, UserConfig.class);
178 // Recreate using the proper constructor which will hash the password
179 UserConfig config = new UserConfig(plainConfig.getUser(), plainConfig.getPassword(), plainConfig.getRoles());
181 Status result = (action.equals("add")) ? userManager.addLocalUser(config) : userManager.removeLocalUser(config);
182 if (result.isSuccess()) {
183 if (action.equals("add")) {
184 DaylightWebUtil.auditlog("User", request.getUserPrincipal().getName(), "added", config.getUser()
185 + " as " + config.getRoles().toString());
187 DaylightWebUtil.auditlog("User", request.getUserPrincipal().getName(), "removed", config.getUser());
193 @RequestMapping(value = "/user/modify", method = RequestMethod.POST)
195 public Status modifyUser(@RequestParam(required = true) String json,
196 @RequestParam(required = true) String action, HttpServletRequest request) {
198 IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
199 if (userManager == null) {
200 return new Status(StatusCode.NOSERVICE, "User Manager unavailable");
203 if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
204 return new Status(StatusCode.UNAUTHORIZED, "Operation not permitted");
207 UserConfig newConfig = gson.fromJson(json, UserConfig.class);
208 List<UserConfig> currentUserConfig = userManager.getLocalUserList();
209 String password = null;
210 String user = newConfig.getUser();
211 for (UserConfig userConfig : currentUserConfig) {
212 if(userConfig.getUser().equals(user)){
213 password = userConfig.getPassword();
217 if (password == null) {
218 String msg = String.format("User %s not found in configuration database", user);
219 return new Status(StatusCode.NOTFOUND, msg);
222 //While modifying a user role, the password is not provided from GUI for any user.
223 //The password is stored in hash mode, hence it cannot be retrieved and added to UserConfig object
224 //The hashed password is injected below to the json string containing username and new roles before
225 //converting to UserConfig object.
226 json = json.replace("\"roles\"", "\"password\":\""+ password + "\",\"roles\"");
227 Gson gson = new Gson();
228 newConfig = gson.fromJson(json, UserConfig.class);
230 Status result = userManager.modifyLocalUser(newConfig);
231 if (result.isSuccess()) {
232 DaylightWebUtil.auditlog("Roles of", request.getUserPrincipal().getName(), "updated", newConfig.getUser()
233 + " to " + newConfig.getRoles().toString());
239 @RequestMapping(value = "/users/{username}", method = RequestMethod.POST)
241 public Status removeLocalUser(@PathVariable("username") String userName, HttpServletRequest request) {
243 String loggedInUser = request.getUserPrincipal().getName();
244 if (loggedInUser.equals(userName)) {
245 String msg = "Invalid Request: User cannot delete itself";
246 return new Status(StatusCode.NOTALLOWED, msg);
249 IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
250 if (userManager == null) {
251 return new Status(StatusCode.NOSERVICE, "User Manager unavailable");
254 if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
255 return new Status(StatusCode.UNAUTHORIZED, "Operation not permitted");
258 Status status = userManager.removeLocalUser(userName);
259 if (status.isSuccess()) {
260 DaylightWebUtil.auditlog("User", request.getUserPrincipal().getName(), "removed", userName);
266 @RequestMapping(value = "/users/password/{username}", method = RequestMethod.POST)
268 public Status changePassword(
269 @PathVariable("username") String username, HttpServletRequest request,
270 @RequestParam(value = "currentPassword", required=false) String currentPassword,
271 @RequestParam("newPassword") String newPassword) {
272 IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
273 if (userManager == null) {
274 return new Status(StatusCode.NOSERVICE, "User Manager unavailable");
278 String requestingUser = request.getUserPrincipal().getName();
280 //changing own password
281 if (requestingUser.equals(username) ) {
282 status = userManager.changeLocalUserPassword(username, currentPassword, newPassword);
283 //enforce the user to re-login with new password
284 if (status.isSuccess() && !newPassword.equals(currentPassword)) {
285 userManager.userLogout(username);
286 HttpSession session = request.getSession(false);
287 if ( session != null) {
288 session.invalidate();
292 //admin level user resetting other's password
293 } else if (authorize(userManager, UserLevel.NETWORKADMIN, request)) {
295 //Since User Manager doesn't have an unprotected password change API,
296 //we re-create the user with the new password (and current roles).
297 List<String> roles = userManager.getUserRoles(username);
298 UserConfig newConfig = new UserConfig(username, newPassword, roles);
300 //validate before removing existing config, so we don't remove but fail to add
301 status = newConfig.validate();
302 if (!status.isSuccess()) {
306 userManager.userLogout(username);
307 status = userManager.removeLocalUser(username);
308 if (!status.isSuccess()) {
311 if (userManager.addLocalUser(newConfig).isSuccess()) {
312 status = new Status(StatusCode.SUCCESS, "Password for user " + username + " reset successfully.");
315 status = new Status(StatusCode.INTERNALERROR, "Failed resetting password for user " + username + ". User is now removed.");
320 status = new Status(StatusCode.UNAUTHORIZED, "Operation not permitted");
323 if (status.isSuccess()) {
324 DaylightWebUtil.auditlog("User", request.getUserPrincipal().getName(), "changed password for",
331 * Is the operation permitted for the given level
335 private boolean authorize(IUserManager userManager, UserLevel level, HttpServletRequest request) {
336 String username = request.getUserPrincipal().getName();
337 UserLevel userLevel = userManager.getUserLevel(username);
338 return userLevel.toNumber() <= level.toNumber();