ca37f4b7c19658ca14facf9af29d6d83d2cd07cb
[controller.git] / opendaylight / adsal / web / root / src / main / java / org / opendaylight / controller / web / DaylightWeb.java
1 /*
2  * Copyright (c) 2013 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.util.HashMap;
13 import java.util.Map;
14 import java.util.Properties;
15 import java.util.Set;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19 import javax.servlet.http.HttpSession;
20
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;
37
38 @Controller
39 @RequestMapping("/")
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";
47         }
48
49         String username = request.getUserPrincipal().getName();
50
51         model.addAttribute("username", username);
52         model.addAttribute("role", userManager.getUserLevel(username)
53                 .toNumber());
54
55         return "main";
56     }
57
58     /**
59      * Read the version.properties file for the property
60      *
61      * @param request
62      * @return String value configured in the version.properties file
63      */
64     @RequestMapping(value="/versionProperty/{property}", method = RequestMethod.GET)
65     @ResponseBody
66     public String getVersion(HttpServletRequest request, @PathVariable("property") String property) {
67         Properties prop = new Properties();
68         try {
69             prop.load(new FileInputStream("version.properties"));
70             return prop.getProperty(property+".version");
71         } catch (Exception e) {
72             return null;
73         }
74     }
75     @RequestMapping(value = "web.json")
76     @ResponseBody
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;
82         IDaylightWeb bundle;
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);
94             }
95         }
96         return bundles;
97     }
98
99     @RequestMapping(value = "save", method = RequestMethod.POST)
100     @ResponseBody
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";
106         }
107         UserLevel level = userManager.getUserLevel(username);
108         Status status;
109         switch (level) {
110         case SYSTEMADMIN:
111         case NETWORKADMIN:
112             IConfigurationService configService = (IConfigurationService) ServiceHelper.getGlobalInstance(
113                     IConfigurationService.class, this);
114             if (configService != null) {
115                 status = configService.saveConfigurations();
116             } else {
117                 status = new Status(StatusCode.NOSERVICE, "Configuration Service is not available");
118             }
119             break;
120         case NETWORKOPERATOR:
121         case CONTAINERUSER:
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");
129                 } else {
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()) {
138                                     oneSaved = true;
139                                 }
140                             }
141                         }
142                     }
143                     if (oneSaved) {
144                         status = new Status(StatusCode.SUCCESS);
145                     } else {
146                         status = new Status(StatusCode.UNAUTHORIZED, "Operation not allowed for current user");
147                     }
148                 }
149             } else {
150                 status = new Status(StatusCode.NOSERVICE, "Container Authorization Service is not available");
151             }
152             break;
153         case APPUSER:
154         case NOUSER:
155         default:
156             status = new Status(StatusCode.UNAUTHORIZED, "Operation not allowed for current user");
157             break;
158         }
159         // This function will eventually return a Status
160         return status.getDescription();
161     }
162
163     @RequestMapping(value = "logout")
164     public String logout(Map<String, Object> model, final HttpServletRequest request) {
165
166         IUserManager userManager = (IUserManager) ServiceHelper
167                 .getGlobalInstance(IUserManager.class, this);
168         if (userManager == null) {
169             return "User Manager is not available";
170         }
171         String username = request.getUserPrincipal().getName();
172         HttpSession session = request.getSession(false);
173         if (session != null) {
174             if (username != null) {
175                 userManager.userLogout(username);
176             }
177             session.invalidate();
178
179         }
180         return "redirect:" + "/";
181     }
182
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";
191         }
192
193         String username = request.getUserPrincipal().getName();
194
195         model.addAttribute("username", username);
196         model.addAttribute("role", userManager.getUserLevel(username)
197                 .toNumber());
198         return "forward:" + "/";
199     }
200
201 }