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.io.FileInputStream;
12 import java.util.HashMap;
14 import java.util.Properties;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19 import javax.servlet.http.HttpSession;
21 import org.opendaylight.controller.configuration.IConfigurationContainerService;
22 import org.opendaylight.controller.configuration.IConfigurationService;
23 import org.opendaylight.controller.containermanager.IContainerAuthorization;
24 import org.opendaylight.controller.sal.authorization.Privilege;
25 import org.opendaylight.controller.sal.authorization.Resource;
26 import org.opendaylight.controller.sal.authorization.UserLevel;
27 import org.opendaylight.controller.sal.utils.ServiceHelper;
28 import org.opendaylight.controller.sal.utils.Status;
29 import org.opendaylight.controller.sal.utils.StatusCode;
30 import org.opendaylight.controller.usermanager.IUserManager;
31 import org.springframework.stereotype.Controller;
32 import org.springframework.ui.Model;
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.ResponseBody;
40 public class DaylightWeb {
41 @RequestMapping(value = "")
42 public String index(Model model, HttpServletRequest request) {
43 IUserManager userManager = (IUserManager) ServiceHelper
44 .getGlobalInstance(IUserManager.class, this);
45 if (userManager == null) {
46 return "User Manager is not available";
49 String username = request.getUserPrincipal().getName();
51 model.addAttribute("username", username);
52 model.addAttribute("role", userManager.getUserLevel(username)
59 * Read the version.properties file for the property
62 * @return String value configured in the version.properties file
64 @RequestMapping(value="/versionProperty/{property}", method = RequestMethod.GET)
66 public String getVersion(HttpServletRequest request, @PathVariable("property") String property) {
67 Properties prop = new Properties();
69 prop.load(new FileInputStream("version.properties"));
70 return prop.getProperty(property+".version");
71 } catch (Exception e) {
75 @RequestMapping(value = "web.json")
77 public Map<String, Map<String, Object>> bundles(HttpServletRequest request) {
78 Object[] instances = ServiceHelper.getGlobalInstances(
79 IDaylightWeb.class, this, null);
80 Map<String, Map<String, Object>> bundles = new HashMap<String, Map<String, Object>>();
81 Map<String, Object> entry;
83 String username = request.getUserPrincipal().getName();
84 IUserManager userManger = (IUserManager) ServiceHelper
85 .getGlobalInstance(IUserManager.class, this);
86 for (Object instance : instances) {
87 bundle = (IDaylightWeb) instance;
88 if (userManger != null
89 && bundle.isAuthorized(userManger.getUserLevel(username))) {
90 entry = new HashMap<String, Object>();
91 entry.put("name", bundle.getWebName());
92 entry.put("order", bundle.getWebOrder());
93 bundles.put(bundle.getWebId(), entry);
99 @RequestMapping(value = "save", method = RequestMethod.POST)
101 public String save(HttpServletRequest request) {
102 String username = request.getUserPrincipal().getName();
103 IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
104 if (userManager == null) {
105 return "User Manager is not available";
107 UserLevel level = userManager.getUserLevel(username);
112 IConfigurationService configService = (IConfigurationService) ServiceHelper.getGlobalInstance(
113 IConfigurationService.class, this);
114 if (configService != null) {
115 status = configService.saveConfigurations();
117 status = new Status(StatusCode.NOSERVICE, "Configuration Service is not available");
120 case NETWORKOPERATOR:
122 IContainerAuthorization containerAuth = (IContainerAuthorization) ServiceHelper.getGlobalInstance(
123 IContainerAuthorization.class, this);
124 if (containerAuth != null) {
125 boolean oneSaved = false;
126 Set<Resource> authorizedContainers = containerAuth.getAllResourcesforUser(username);
127 if (authorizedContainers.isEmpty()) {
128 status = new Status(StatusCode.UNAUTHORIZED, "User is not authorized for any container");
130 for (Resource container : authorizedContainers) {
131 if (container.getPrivilege() == Privilege.WRITE) {
132 String containerName = (String)container.getResource();
133 IConfigurationContainerService containerConfigService = (IConfigurationContainerService) ServiceHelper
134 .getInstance(IConfigurationContainerService.class, containerName, this);
135 if (containerConfigService != null) {
136 status = containerConfigService.saveConfigurations();
137 if (status.isSuccess()) {
144 status = new Status(StatusCode.SUCCESS);
146 status = new Status(StatusCode.UNAUTHORIZED, "Operation not allowed for current user");
150 status = new Status(StatusCode.NOSERVICE, "Container Authorization Service is not available");
156 status = new Status(StatusCode.UNAUTHORIZED, "Operation not allowed for current user");
159 // This function will eventually return a Status
160 return status.getDescription();
163 @RequestMapping(value = "logout")
164 public String logout(Map<String, Object> model, final HttpServletRequest request) {
166 IUserManager userManager = (IUserManager) ServiceHelper
167 .getGlobalInstance(IUserManager.class, this);
168 if (userManager == null) {
169 return "User Manager is not available";
171 String username = request.getUserPrincipal().getName();
172 HttpSession session = request.getSession(false);
173 if (session != null) {
174 if (username != null) {
175 userManager.userLogout(username);
177 session.invalidate();
180 return "redirect:" + "/";
183 @RequestMapping(value = "login")
184 public String login(Model model, final HttpServletRequest request,
185 final HttpServletResponse response) {
186 // response.setHeader("X-Page-Location", "/login");
187 IUserManager userManager = (IUserManager) ServiceHelper
188 .getGlobalInstance(IUserManager.class, this);
189 if (userManager == null) {
190 return "User Manager is not available";
193 String username = request.getUserPrincipal().getName();
195 model.addAttribute("username", username);
196 model.addAttribute("role", userManager.getUserLevel(username)
198 return "forward:" + "/";