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;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import javax.servlet.http.HttpSession;
18 import org.opendaylight.controller.configuration.IConfigurationService;
19 import org.opendaylight.controller.sal.authorization.UserLevel;
20 import org.opendaylight.controller.sal.utils.ServiceHelper;
21 import org.opendaylight.controller.sal.utils.Status;
22 import org.opendaylight.controller.sal.utils.StatusCode;
23 import org.opendaylight.controller.usermanager.IUserManager;
24 import org.springframework.stereotype.Controller;
25 import org.springframework.ui.Model;
26 import org.springframework.web.bind.annotation.RequestMapping;
27 import org.springframework.web.bind.annotation.RequestMethod;
28 import org.springframework.web.bind.annotation.ResponseBody;
32 public class DaylightWeb {
33 @RequestMapping(value = "")
34 public String index(Model model, HttpServletRequest request) {
35 IUserManager userManager = (IUserManager) ServiceHelper
36 .getGlobalInstance(IUserManager.class, this);
37 if (userManager == null) {
38 return "User Manager is not available";
41 String username = request.getUserPrincipal().getName();
43 model.addAttribute("username", username);
44 model.addAttribute("role", userManager.getUserLevel(username)
50 @RequestMapping(value = "web.json")
52 public Map<String, Map<String, Object>> bundles(HttpServletRequest request) {
53 Object[] instances = ServiceHelper.getGlobalInstances(
54 IDaylightWeb.class, this, null);
55 Map<String, Map<String, Object>> bundles = new HashMap<String, Map<String, Object>>();
56 Map<String, Object> entry;
58 String username = request.getUserPrincipal().getName();
59 IUserManager userManger = (IUserManager) ServiceHelper
60 .getGlobalInstance(IUserManager.class, this);
61 for (Object instance : instances) {
62 bundle = (IDaylightWeb) instance;
63 if (userManger != null
64 && bundle.isAuthorized(userManger.getUserLevel(username))) {
65 entry = new HashMap<String, Object>();
66 entry.put("name", bundle.getWebName());
67 entry.put("order", bundle.getWebOrder());
68 bundles.put(bundle.getWebId(), entry);
74 @RequestMapping(value = "save", method = RequestMethod.POST)
76 public String save(HttpServletRequest request) {
77 String username = request.getUserPrincipal().getName();
78 IUserManager userManager = (IUserManager) ServiceHelper
79 .getGlobalInstance(IUserManager.class, this);
80 if (userManager == null) {
81 return "User Manager is not available";
84 UserLevel level = userManager.getUserLevel(username);
85 if (level == UserLevel.NETWORKOPERATOR) {
86 return "Save not permitted for Operator";
89 Status status = new Status(StatusCode.UNAUTHORIZED,
90 "Operation not allowed for current user");
91 if (level == UserLevel.NETWORKADMIN || level == UserLevel.SYSTEMADMIN) {
92 IConfigurationService configService = (IConfigurationService) ServiceHelper
93 .getGlobalInstance(IConfigurationService.class, this);
94 if (configService != null) {
95 status = configService.saveConfigurations();
99 return status.getDescription();
102 @RequestMapping(value = "logout")
103 public String logout(Map<String, Object> model, final HttpServletRequest request) {
105 IUserManager userManager = (IUserManager) ServiceHelper
106 .getGlobalInstance(IUserManager.class, this);
107 if (userManager == null) {
108 return "User Manager is not available";
110 String username = request.getUserPrincipal().getName();
111 HttpSession session = request.getSession(false);
112 if (session != null) {
113 if (username != null) {
114 userManager.userLogout(username);
116 session.invalidate();
119 return "redirect:" + "/";
122 @RequestMapping(value = "login")
123 public String login(Model model, final HttpServletRequest request,
124 final HttpServletResponse response) {
125 // response.setHeader("X-Page-Location", "/login");
126 IUserManager userManager = (IUserManager) ServiceHelper
127 .getGlobalInstance(IUserManager.class, this);
128 if (userManager == null) {
129 return "User Manager is not available";
132 String username = request.getUserPrincipal().getName();
134 model.addAttribute("username", username);
135 model.addAttribute("role", userManager.getUserLevel(username)
137 return "forward:" + "/";