Merge "Fix duplicate dependency warnings"
[controller.git] / opendaylight / adsal / web / root / src / main / java / org / opendaylight / controller / web / DaylightWeb.java
1 /*
2  * Copyright (c) 2013, 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.web;
10
11 import java.io.FileInputStream;
12 import java.io.IOException;
13 import java.util.HashMap;
14 import java.util.Map;
15 import java.util.Properties;
16 import java.util.Set;
17
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20 import javax.servlet.http.HttpSession;
21
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;
39
40 @Controller
41 @RequestMapping("/")
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";
49         }
50
51         String username = request.getUserPrincipal().getName();
52
53         model.addAttribute("username", username);
54         model.addAttribute("role", userManager.getUserLevel(username)
55                 .toNumber());
56
57         return "main";
58     }
59
60     /**
61      * Read the version.properties file for the property
62      *
63      * @param request
64      * @return String value configured in the version.properties file
65      */
66     @RequestMapping(value="/versionProperty/{property}", method = RequestMethod.GET)
67     @ResponseBody
68     public String getVersion(HttpServletRequest request, @PathVariable("property") String property) {
69         Properties prop = new Properties();
70         FileInputStream propertiesFile = null;
71         try {
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."
78             return null;
79         } finally {
80             IOUtils.closeQuietly(propertiesFile);
81         }
82     }
83     @RequestMapping(value = "web.json")
84     @ResponseBody
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;
90         IDaylightWeb bundle;
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);
102             }
103         }
104         return bundles;
105     }
106
107     @RequestMapping(value = "save", method = RequestMethod.POST)
108     @ResponseBody
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";
114         }
115         UserLevel level = userManager.getUserLevel(username);
116         Status status;
117         switch (level) {
118         case SYSTEMADMIN:
119         case NETWORKADMIN:
120             IConfigurationService configService = (IConfigurationService) ServiceHelper.getGlobalInstance(
121                     IConfigurationService.class, this);
122             if (configService != null) {
123                 status = configService.saveConfigurations();
124             } else {
125                 status = new Status(StatusCode.NOSERVICE, "Configuration Service is not available");
126             }
127             break;
128         case NETWORKOPERATOR:
129         case CONTAINERUSER:
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");
137                 } else {
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()) {
146                                     oneSaved = true;
147                                 }
148                             }
149                         }
150                     }
151                     if (oneSaved) {
152                         status = new Status(StatusCode.SUCCESS);
153                     } else {
154                         status = new Status(StatusCode.UNAUTHORIZED, "Operation not allowed for current user");
155                     }
156                 }
157             } else {
158                 status = new Status(StatusCode.NOSERVICE, "Container Authorization Service is not available");
159             }
160             break;
161         case APPUSER:
162         case NOUSER:
163         default:
164             status = new Status(StatusCode.UNAUTHORIZED, "Operation not allowed for current user");
165             break;
166         }
167         // This function will eventually return a Status
168         return status.getDescription();
169     }
170
171     @RequestMapping(value = "logout")
172     public String logout(Map<String, Object> model, final HttpServletRequest request) {
173
174         IUserManager userManager = (IUserManager) ServiceHelper
175                 .getGlobalInstance(IUserManager.class, this);
176         if (userManager == null) {
177             return "User Manager is not available";
178         }
179         String username = request.getUserPrincipal().getName();
180         HttpSession session = request.getSession(false);
181         if (session != null) {
182             if (username != null) {
183                 userManager.userLogout(username);
184             }
185             session.invalidate();
186
187         }
188         return "redirect:" + "/";
189     }
190
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";
199         }
200
201         String username = request.getUserPrincipal().getName();
202
203         model.addAttribute("username", username);
204         model.addAttribute("role", userManager.getUserLevel(username)
205                 .toNumber());
206         return "forward:" + "/";
207     }
208
209 }