1d4211a9267e25a1399f48d99c145cdb6bf5b6fa
[controller.git] / opendaylight / 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.util.HashMap;
12 import java.util.Map;
13 import java.util.Set;
14
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17 import javax.servlet.http.HttpSession;
18
19 import org.opendaylight.controller.configuration.IConfigurationContainerService;
20 import org.opendaylight.controller.configuration.IConfigurationService;
21 import org.opendaylight.controller.containermanager.IContainerAuthorization;
22 import org.opendaylight.controller.sal.authorization.Privilege;
23 import org.opendaylight.controller.sal.authorization.Resource;
24 import org.opendaylight.controller.sal.authorization.UserLevel;
25 import org.opendaylight.controller.sal.utils.ServiceHelper;
26 import org.opendaylight.controller.sal.utils.Status;
27 import org.opendaylight.controller.sal.utils.StatusCode;
28 import org.opendaylight.controller.usermanager.IUserManager;
29 import org.springframework.stereotype.Controller;
30 import org.springframework.ui.Model;
31 import org.springframework.web.bind.annotation.RequestMapping;
32 import org.springframework.web.bind.annotation.RequestMethod;
33 import org.springframework.web.bind.annotation.ResponseBody;
34
35 @Controller
36 @RequestMapping("/")
37 public class DaylightWeb {
38     @RequestMapping(value = "")
39     public String index(Model model, HttpServletRequest request) {
40         IUserManager userManager = (IUserManager) ServiceHelper
41                 .getGlobalInstance(IUserManager.class, this);
42         if (userManager == null) {
43             return "User Manager is not available";
44         }
45
46         String username = request.getUserPrincipal().getName();
47
48         model.addAttribute("username", username);
49         model.addAttribute("role", userManager.getUserLevel(username)
50                 .toNumber());
51
52         return "main";
53     }
54
55     @RequestMapping(value = "web.json")
56     @ResponseBody
57     public Map<String, Map<String, Object>> bundles(HttpServletRequest request) {
58         Object[] instances = ServiceHelper.getGlobalInstances(
59                 IDaylightWeb.class, this, null);
60         Map<String, Map<String, Object>> bundles = new HashMap<String, Map<String, Object>>();
61         Map<String, Object> entry;
62         IDaylightWeb bundle;
63         String username = request.getUserPrincipal().getName();
64         IUserManager userManger = (IUserManager) ServiceHelper
65                 .getGlobalInstance(IUserManager.class, this);
66         for (Object instance : instances) {
67             bundle = (IDaylightWeb) instance;
68             if (userManger != null
69                     && bundle.isAuthorized(userManger.getUserLevel(username))) {
70                 entry = new HashMap<String, Object>();
71                 entry.put("name", bundle.getWebName());
72                 entry.put("order", bundle.getWebOrder());
73                 bundles.put(bundle.getWebId(), entry);
74             }
75         }
76         return bundles;
77     }
78
79     @RequestMapping(value = "save", method = RequestMethod.POST)
80     @ResponseBody
81     public String save(HttpServletRequest request) {
82         String username = request.getUserPrincipal().getName();
83         IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
84         if (userManager == null) {
85             return "User Manager is not available";
86         }
87         UserLevel level = userManager.getUserLevel(username);
88         Status status;
89         switch (level) {
90         case SYSTEMADMIN:
91         case NETWORKADMIN:
92             IConfigurationService configService = (IConfigurationService) ServiceHelper.getGlobalInstance(
93                     IConfigurationService.class, this);
94             if (configService != null) {
95                 status = configService.saveConfigurations();
96             } else {
97                 status = new Status(StatusCode.NOSERVICE, "Configuration Service is not available");
98             }
99             break;
100         case NETWORKOPERATOR:
101         case CONTAINERUSER:
102             IContainerAuthorization containerAuth = (IContainerAuthorization) ServiceHelper.getGlobalInstance(
103                     IContainerAuthorization.class, this);
104             if (containerAuth != null) {
105                 boolean oneSaved = false;
106                 Set<Resource> authorizedContainers = containerAuth.getAllResourcesforUser(username);
107                 if (authorizedContainers.isEmpty()) {
108                     status = new Status(StatusCode.UNAUTHORIZED, "User is not authorized for any container");
109                 } else {
110                     for (Resource container : authorizedContainers) {
111                         if (container.getPrivilege() == Privilege.WRITE) {
112                             String containerName = (String)container.getResource();
113                             IConfigurationContainerService containerConfigService = (IConfigurationContainerService) ServiceHelper
114                                     .getInstance(IConfigurationContainerService.class, containerName, this);
115                             if (containerConfigService != null) {
116                                 status = containerConfigService.saveConfigurations();
117                                 if (status.isSuccess()) {
118                                     oneSaved = true;
119                                 }
120                             }
121                         }
122                     }
123                     if (oneSaved) {
124                         status = new Status(StatusCode.SUCCESS);
125                     } else {
126                         status = new Status(StatusCode.UNAUTHORIZED, "Operation not allowed for current user");
127                     }
128                 }
129             } else {
130                 status = new Status(StatusCode.NOSERVICE, "Container Authorization Service is not available");
131             }
132             break;
133         case APPUSER:
134         case NOUSER:
135         default:
136             status = new Status(StatusCode.UNAUTHORIZED, "Operation not allowed for current user");
137             break;
138         }
139         // This function will eventually return a Status
140         return status.getDescription();
141     }
142
143     @RequestMapping(value = "logout")
144     public String logout(Map<String, Object> model, final HttpServletRequest request) {
145
146         IUserManager userManager = (IUserManager) ServiceHelper
147                 .getGlobalInstance(IUserManager.class, this);
148         if (userManager == null) {
149             return "User Manager is not available";
150         }
151         String username = request.getUserPrincipal().getName();
152         HttpSession session = request.getSession(false);
153         if (session != null) {
154             if (username != null) {
155                 userManager.userLogout(username);
156             }
157             session.invalidate();
158
159         }
160         return "redirect:" + "/";
161     }
162
163     @RequestMapping(value = "login")
164     public String login(Model model, final HttpServletRequest request,
165             final HttpServletResponse response) {
166         // response.setHeader("X-Page-Location", "/login");
167         IUserManager userManager = (IUserManager) ServiceHelper
168                 .getGlobalInstance(IUserManager.class, this);
169         if (userManager == null) {
170             return "User Manager is not available";
171         }
172
173         String username = request.getUserPrincipal().getName();
174
175         model.addAttribute("username", username);
176         model.addAttribute("role", userManager.getUserLevel(username)
177                 .toNumber());
178         return "forward:" + "/";
179     }
180
181 }