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;
19 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
20 import org.opendaylight.controller.connectionmanager.IConnectionManager;
21 import org.opendaylight.controller.sal.authorization.UserLevel;
22 import org.opendaylight.controller.sal.core.Description;
23 import org.opendaylight.controller.sal.core.Node;
24 import org.opendaylight.controller.sal.utils.GlobalConstants;
25 import org.opendaylight.controller.sal.utils.ServiceHelper;
26 import org.opendaylight.controller.sal.utils.Status;
27 import org.opendaylight.controller.sal.utils.StatusCode;
28 import org.opendaylight.controller.switchmanager.ISwitchManager;
29 import org.opendaylight.controller.usermanager.IUserManager;
30 import org.opendaylight.controller.usermanager.UserConfig;
31 import org.springframework.stereotype.Controller;
32 import org.springframework.web.bind.annotation.PathVariable;
33 import org.springframework.web.bind.annotation.RequestMapping;
34 import org.springframework.web.bind.annotation.RequestMethod;
35 import org.springframework.web.bind.annotation.RequestParam;
36 import org.springframework.web.bind.annotation.ResponseBody;
38 import com.google.gson.Gson;
41 @RequestMapping("/admin")
42 public class DaylightWebAdmin {
43 Gson gson = new Gson();
46 * Returns list of clustered controllers. Highlights "this" controller and
47 * if controller is coordinator
48 * @return List<ClusterBean>
50 @RequestMapping("/cluster")
52 public String getClusteredControllers() {
53 IClusterGlobalServices clusterServices = (IClusterGlobalServices) ServiceHelper.getGlobalInstance(
54 IClusterGlobalServices.class, this);
55 if (clusterServices == null) {
59 List<ClusterNodeBean> clusterNodes = new ArrayList<ClusterNodeBean>();
61 List<InetAddress> controllers = clusterServices.getClusteredControllers();
62 for (InetAddress controller : controllers) {
63 ClusterNodeBean.Builder clusterBeanBuilder = new ClusterNodeBean.Builder(controller);
64 if (controller.equals(clusterServices.getMyAddress())) {
65 clusterBeanBuilder.highlightMe();
67 if (clusterServices.getCoordinatorAddress().equals(controller)) {
68 clusterBeanBuilder.iAmCoordinator();
70 clusterNodes.add(clusterBeanBuilder.build());
73 return gson.toJson(clusterNodes);
77 * Return nodes connected to controller {controller}
79 * - byte[] of the address of the controller
80 * @return List<NodeBean>
82 @RequestMapping("/cluster/controller/{controller}")
84 public String getNodesConnectedToController(@PathVariable("controller") String controller) {
85 IClusterGlobalServices clusterServices = (IClusterGlobalServices) ServiceHelper.getGlobalInstance(
86 IClusterGlobalServices.class, this);
87 if (clusterServices == null) {
90 IConnectionManager connectionManager = (IConnectionManager) ServiceHelper.getGlobalInstance(
91 IConnectionManager.class, this);
92 if (connectionManager == null) {
95 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
96 GlobalConstants.DEFAULT.toString(), this);
97 if (switchManager == null) {
101 byte[] address = gson.fromJson(controller, byte[].class);
102 InetAddress controllerAddress = null;
104 controllerAddress = InetAddress.getByAddress(address);
105 } catch (UnknownHostException e) {
109 List<NodeBean> result = new ArrayList<NodeBean>();
111 Set<Node> nodes = connectionManager.getNodes(controllerAddress);
113 return gson.toJson(result);
115 for (Node node : nodes) {
116 Description description = (Description) switchManager.getNodeProp(node, Description.propertyName);
118 if (description == null || description.getValue().equals("None")) {
119 nodeBean = new NodeBean(node);
121 nodeBean = new NodeBean(node, description.getValue());
123 result.add(nodeBean);
126 return gson.toJson(result);
129 @RequestMapping("/users")
131 public List<UserConfig> getUsers() {
132 IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
133 if (userManager == null) {
137 List<UserConfig> userConfList = userManager.getLocalUserList();
143 * Password in clear text, moving to HTTP/SSL soon
145 @RequestMapping(value = "/users", method = RequestMethod.POST)
147 public String saveLocalUserConfig(@RequestParam(required = true) String json,
148 @RequestParam(required = true) String action, HttpServletRequest request) {
150 IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
151 if (userManager == null) {
152 return "Internal Error";
155 if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
156 return "Operation not permitted";
159 Gson gson = new Gson();
160 UserConfig config = gson.fromJson(json, UserConfig.class);
162 Status result = (action.equals("add")) ? userManager.addLocalUser(config) : userManager.removeLocalUser(config);
163 if (result.isSuccess()) {
164 String userAction = (action.equals("add")) ? "added" : "removed";
165 DaylightWebUtil.auditlog("User", request.getUserPrincipal().getName(), userAction, config.getUser());
168 return result.getDescription();
171 @RequestMapping(value = "/users/{username}", method = RequestMethod.POST)
173 public String removeLocalUser(@PathVariable("username") String userName, HttpServletRequest request) {
175 String username = request.getUserPrincipal().getName();
176 if (username.equals(userName)) {
177 return "Invalid Request: User cannot delete itself";
180 IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
181 if (userManager == null) {
182 return "Internal Error";
185 if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
186 return "Operation not permitted";
189 Status result = userManager.removeLocalUser(userName);
190 if (result.isSuccess()) {
191 DaylightWebUtil.auditlog("User", request.getUserPrincipal().getName(), "removed", userName);
194 return result.getDescription();
197 @RequestMapping(value = "/users/password/{username}", method = RequestMethod.POST)
199 public Status changePassword(@PathVariable("username") String username, HttpServletRequest request,
200 @RequestParam("currentPassword") String currentPassword, @RequestParam("newPassword") String newPassword) {
201 IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
202 if (userManager == null) {
203 return new Status(StatusCode.GONE, "User Manager not found");
206 if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
207 return new Status(StatusCode.FORBIDDEN, "Operation not permitted");
210 if (newPassword.isEmpty()) {
211 return new Status(StatusCode.BADREQUEST, "Empty passwords not allowed");
214 Status status = userManager.changeLocalUserPassword(username, currentPassword, newPassword);
215 if (status.isSuccess()) {
216 DaylightWebUtil.auditlog("User", request.getUserPrincipal().getName(), "changed password for", username);
222 * Is the operation permitted for the given level
225 private boolean authorize(IUserManager userManager, UserLevel level, HttpServletRequest request) {
226 String username = request.getUserPrincipal().getName();
227 UserLevel userLevel = userManager.getUserLevel(username);
228 return userLevel.toNumber() <= level.toNumber();