2 * Copyright (c) 2013, 2014 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.io.IOException;
13 import java.util.HashMap;
15 import java.util.Properties;
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20 import javax.servlet.http.HttpSession;
22 import org.apache.commons.io.IOUtils;
23 import org.opendaylight.controller.configuration.IConfigurationContainerService;
24 import org.opendaylight.controller.configuration.IConfigurationService;
25 import org.opendaylight.controller.containermanager.IContainerAuthorization;
26 import org.opendaylight.controller.sal.authorization.Privilege;
27 import org.opendaylight.controller.sal.authorization.Resource;
28 import org.opendaylight.controller.sal.authorization.UserLevel;
29 import org.opendaylight.controller.sal.utils.ServiceHelper;
30 import org.opendaylight.controller.sal.utils.Status;
31 import org.opendaylight.controller.sal.utils.StatusCode;
32 import org.opendaylight.controller.usermanager.IUserManager;
33 import org.springframework.stereotype.Controller;
34 import org.springframework.ui.Model;
35 import org.springframework.web.bind.annotation.PathVariable;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.bind.annotation.RequestMethod;
38 import org.springframework.web.bind.annotation.ResponseBody;
42 public class DaylightWeb {
43 @RequestMapping(value = "")
44 public String index(Model model, HttpServletRequest request) {
45 IUserManager userManager = (IUserManager) ServiceHelper
46 .getGlobalInstance(IUserManager.class, this);
47 if (userManager == null) {
48 return "User Manager is not available";
51 String username = request.getUserPrincipal().getName();
53 model.addAttribute("username", username);
54 model.addAttribute("role", userManager.getUserLevel(username)
61 * Read the version.properties file for the property
64 * @return String value configured in the version.properties file
66 @RequestMapping(value="/versionProperty/{property}", method = RequestMethod.GET)
68 public String getVersion(HttpServletRequest request, @PathVariable("property") String property) {
69 Properties prop = new Properties();
70 FileInputStream propertiesFile = null;
72 propertiesFile = new FileInputStream("version.properties");
73 prop.load(propertiesFile);
74 return prop.getProperty(property+".version");
75 } catch (IOException e) {
76 // TODO: We should be logging the exception here
77 // "Failed to open version.properties."
80 IOUtils.closeQuietly(propertiesFile);
83 @RequestMapping(value = "web.json")
85 public Map<String, Map<String, Object>> bundles(HttpServletRequest request) {
86 Object[] instances = ServiceHelper.getGlobalInstances(
87 IDaylightWeb.class, this, null);
88 Map<String, Map<String, Object>> bundles = new HashMap<String, Map<String, Object>>();
89 Map<String, Object> entry;
91 String username = request.getUserPrincipal().getName();
92 IUserManager userManger = (IUserManager) ServiceHelper
93 .getGlobalInstance(IUserManager.class, this);
94 for (Object instance : instances) {
95 bundle = (IDaylightWeb) instance;
96 if (userManger != null
97 && bundle.isAuthorized(userManger.getUserLevel(username))) {
98 entry = new HashMap<String, Object>();
99 entry.put("name", bundle.getWebName());
100 entry.put("order", bundle.getWebOrder());
101 bundles.put(bundle.getWebId(), entry);
107 @RequestMapping(value = "save", method = RequestMethod.POST)
109 public String save(HttpServletRequest request) {
110 String username = request.getUserPrincipal().getName();
111 IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
112 if (userManager == null) {
113 return "User Manager is not available";
115 UserLevel level = userManager.getUserLevel(username);
120 IConfigurationService configService = (IConfigurationService) ServiceHelper.getGlobalInstance(
121 IConfigurationService.class, this);
122 if (configService != null) {
123 status = configService.saveConfigurations();
125 status = new Status(StatusCode.NOSERVICE, "Configuration Service is not available");
128 case NETWORKOPERATOR:
130 IContainerAuthorization containerAuth = (IContainerAuthorization) ServiceHelper.getGlobalInstance(
131 IContainerAuthorization.class, this);
132 if (containerAuth != null) {
133 boolean oneSaved = false;
134 Set<Resource> authorizedContainers = containerAuth.getAllResourcesforUser(username);
135 if (authorizedContainers.isEmpty()) {
136 status = new Status(StatusCode.UNAUTHORIZED, "User is not authorized for any container");
138 for (Resource container : authorizedContainers) {
139 if (container.getPrivilege() == Privilege.WRITE) {
140 String containerName = (String)container.getResource();
141 IConfigurationContainerService containerConfigService = (IConfigurationContainerService) ServiceHelper
142 .getInstance(IConfigurationContainerService.class, containerName, this);
143 if (containerConfigService != null) {
144 status = containerConfigService.saveConfigurations();
145 if (status.isSuccess()) {
152 status = new Status(StatusCode.SUCCESS);
154 status = new Status(StatusCode.UNAUTHORIZED, "Operation not allowed for current user");
158 status = new Status(StatusCode.NOSERVICE, "Container Authorization Service is not available");
164 status = new Status(StatusCode.UNAUTHORIZED, "Operation not allowed for current user");
167 // This function will eventually return a Status
168 return status.getDescription();
171 @RequestMapping(value = "logout")
172 public String logout(Map<String, Object> model, final HttpServletRequest request) {
174 IUserManager userManager = (IUserManager) ServiceHelper
175 .getGlobalInstance(IUserManager.class, this);
176 if (userManager == null) {
177 return "User Manager is not available";
179 String username = request.getUserPrincipal().getName();
180 HttpSession session = request.getSession(false);
181 if (session != null) {
182 if (username != null) {
183 userManager.userLogout(username);
185 session.invalidate();
188 return "redirect:" + "/";
191 @RequestMapping(value = "login")
192 public String login(Model model, final HttpServletRequest request,
193 final HttpServletResponse response) {
194 // response.setHeader("X-Page-Location", "/login");
195 IUserManager userManager = (IUserManager) ServiceHelper
196 .getGlobalInstance(IUserManager.class, this);
197 if (userManager == null) {
198 return "User Manager is not available";
201 String username = request.getUserPrincipal().getName();
203 model.addAttribute("username", username);
204 model.addAttribute("role", userManager.getUserLevel(username)
206 return "forward:" + "/";