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.util.HashMap;
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17 import javax.servlet.http.HttpSession;
19 import org.opendaylight.controller.configuration.IConfigurationContainerService;
20 import org.opendaylight.controller.configuration.IConfigurationService;
21 import org.opendaylight.controller.containermanager.IContainerAuthorization;
22 import org.opendaylight.controller.sal.authorization.Privilege;
23 import org.opendaylight.controller.sal.authorization.Resource;
24 import org.opendaylight.controller.sal.authorization.UserLevel;
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.usermanager.IUserManager;
29 import org.springframework.stereotype.Controller;
30 import org.springframework.ui.Model;
31 import org.springframework.web.bind.annotation.RequestMapping;
32 import org.springframework.web.bind.annotation.RequestMethod;
33 import org.springframework.web.bind.annotation.ResponseBody;
37 public class DaylightWeb {
38 @RequestMapping(value = "")
39 public String index(Model model, HttpServletRequest request) {
40 IUserManager userManager = (IUserManager) ServiceHelper
41 .getGlobalInstance(IUserManager.class, this);
42 if (userManager == null) {
43 return "User Manager is not available";
46 String username = request.getUserPrincipal().getName();
48 model.addAttribute("username", username);
49 model.addAttribute("role", userManager.getUserLevel(username)
55 @RequestMapping(value = "web.json")
57 public Map<String, Map<String, Object>> bundles(HttpServletRequest request) {
58 Object[] instances = ServiceHelper.getGlobalInstances(
59 IDaylightWeb.class, this, null);
60 Map<String, Map<String, Object>> bundles = new HashMap<String, Map<String, Object>>();
61 Map<String, Object> entry;
63 String username = request.getUserPrincipal().getName();
64 IUserManager userManger = (IUserManager) ServiceHelper
65 .getGlobalInstance(IUserManager.class, this);
66 for (Object instance : instances) {
67 bundle = (IDaylightWeb) instance;
68 if (userManger != null
69 && bundle.isAuthorized(userManger.getUserLevel(username))) {
70 entry = new HashMap<String, Object>();
71 entry.put("name", bundle.getWebName());
72 entry.put("order", bundle.getWebOrder());
73 bundles.put(bundle.getWebId(), entry);
79 @RequestMapping(value = "save", method = RequestMethod.POST)
81 public String save(HttpServletRequest request) {
82 String username = request.getUserPrincipal().getName();
83 IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
84 if (userManager == null) {
85 return "User Manager is not available";
87 UserLevel level = userManager.getUserLevel(username);
92 IConfigurationService configService = (IConfigurationService) ServiceHelper.getGlobalInstance(
93 IConfigurationService.class, this);
94 if (configService != null) {
95 status = configService.saveConfigurations();
97 status = new Status(StatusCode.NOSERVICE, "Configuration Service is not available");
100 case NETWORKOPERATOR:
102 IContainerAuthorization containerAuth = (IContainerAuthorization) ServiceHelper.getGlobalInstance(
103 IContainerAuthorization.class, this);
104 if (containerAuth != null) {
105 boolean oneSaved = false;
106 Set<Resource> authorizedContainers = containerAuth.getAllResourcesforUser(username);
107 if (authorizedContainers.isEmpty()) {
108 status = new Status(StatusCode.UNAUTHORIZED, "User is not authorized for any container");
110 for (Resource container : authorizedContainers) {
111 if (container.getPrivilege() == Privilege.WRITE) {
112 String containerName = (String)container.getResource();
113 IConfigurationContainerService containerConfigService = (IConfigurationContainerService) ServiceHelper
114 .getInstance(IConfigurationContainerService.class, containerName, this);
115 if (containerConfigService != null) {
116 status = containerConfigService.saveConfigurations();
117 if (status.isSuccess()) {
124 status = new Status(StatusCode.SUCCESS);
126 status = new Status(StatusCode.UNAUTHORIZED, "Operation not allowed for current user");
130 status = new Status(StatusCode.NOSERVICE, "Container Authorization Service is not available");
136 status = new Status(StatusCode.UNAUTHORIZED, "Operation not allowed for current user");
139 // This function will eventually return a Status
140 return status.getDescription();
143 @RequestMapping(value = "logout")
144 public String logout(Map<String, Object> model, final HttpServletRequest request) {
146 IUserManager userManager = (IUserManager) ServiceHelper
147 .getGlobalInstance(IUserManager.class, this);
148 if (userManager == null) {
149 return "User Manager is not available";
151 String username = request.getUserPrincipal().getName();
152 HttpSession session = request.getSession(false);
153 if (session != null) {
154 if (username != null) {
155 userManager.userLogout(username);
157 session.invalidate();
160 return "redirect:" + "/";
163 @RequestMapping(value = "login")
164 public String login(Model model, final HttpServletRequest request,
165 final HttpServletResponse response) {
166 // response.setHeader("X-Page-Location", "/login");
167 IUserManager userManager = (IUserManager) ServiceHelper
168 .getGlobalInstance(IUserManager.class, this);
169 if (userManager == null) {
170 return "User Manager is not available";
173 String username = request.getUserPrincipal().getName();
175 model.addAttribute("username", username);
176 model.addAttribute("role", userManager.getUserLevel(username)
178 return "forward:" + "/";