Javadoc for controller.web
[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
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import javax.servlet.http.HttpSession;
17
18 import org.opendaylight.controller.configuration.IConfigurationService;
19 import org.opendaylight.controller.sal.authorization.UserLevel;
20 import org.opendaylight.controller.sal.utils.ServiceHelper;
21 import org.opendaylight.controller.sal.utils.Status;
22 import org.opendaylight.controller.sal.utils.StatusCode;
23 import org.opendaylight.controller.usermanager.IUserManager;
24 import org.springframework.stereotype.Controller;
25 import org.springframework.ui.Model;
26 import org.springframework.web.bind.annotation.RequestMapping;
27 import org.springframework.web.bind.annotation.RequestMethod;
28 import org.springframework.web.bind.annotation.ResponseBody;
29
30 @Controller
31 @RequestMapping("/")
32 public class DaylightWeb {
33     @RequestMapping(value = "")
34     public String index(Model model, HttpServletRequest request) {
35         IUserManager userManager = (IUserManager) ServiceHelper
36                 .getGlobalInstance(IUserManager.class, this);
37         if (userManager == null) {
38             return "User Manager is not available";
39         }
40
41         String username = request.getUserPrincipal().getName();
42
43         model.addAttribute("username", username);
44         model.addAttribute("role", userManager.getUserLevel(username)
45                 .toNumber());
46
47         return "main";
48     }
49
50     @RequestMapping(value = "web.json")
51     @ResponseBody
52     public Map<String, Map<String, Object>> bundles(HttpServletRequest request) {
53         Object[] instances = ServiceHelper.getGlobalInstances(
54                 IDaylightWeb.class, this, null);
55         Map<String, Map<String, Object>> bundles = new HashMap<String, Map<String, Object>>();
56         Map<String, Object> entry;
57         IDaylightWeb bundle;
58         String username = request.getUserPrincipal().getName();
59         IUserManager userManger = (IUserManager) ServiceHelper
60                 .getGlobalInstance(IUserManager.class, this);
61         for (Object instance : instances) {
62             bundle = (IDaylightWeb) instance;
63             if (userManger != null
64                     && bundle.isAuthorized(userManger.getUserLevel(username))) {
65                 entry = new HashMap<String, Object>();
66                 entry.put("name", bundle.getWebName());
67                 entry.put("order", bundle.getWebOrder());
68                 bundles.put(bundle.getWebId(), entry);
69             }
70         }
71         return bundles;
72     }
73
74     @RequestMapping(value = "save", method = RequestMethod.POST)
75     @ResponseBody
76     public String save(HttpServletRequest request) {
77         String username = request.getUserPrincipal().getName();
78         IUserManager userManager = (IUserManager) ServiceHelper
79                 .getGlobalInstance(IUserManager.class, this);
80         if (userManager == null) {
81             return "User Manager is not available";
82         }
83
84         UserLevel level = userManager.getUserLevel(username);
85         if (level == UserLevel.NETWORKOPERATOR) {
86             return "Save not permitted for Operator";
87         }
88
89         Status status = new Status(StatusCode.UNAUTHORIZED,
90                 "Operation not allowed for current user");
91         if (level == UserLevel.NETWORKADMIN || level == UserLevel.SYSTEMADMIN) {
92             IConfigurationService configService = (IConfigurationService) ServiceHelper
93                     .getGlobalInstance(IConfigurationService.class, this);
94             if (configService != null) {
95                 status = configService.saveConfigurations();
96             }
97         }
98
99         return status.getDescription();
100     }
101
102     @RequestMapping(value = "logout")
103     public String login(Map<String, Object> model,
104             final HttpServletRequest request) {
105
106         IUserManager userManager = (IUserManager) ServiceHelper
107                 .getGlobalInstance(IUserManager.class, this);
108         if (userManager == null) {
109             return "User Manager is not available";
110         }
111         String username = request.getUserPrincipal().getName();
112         HttpSession session = request.getSession(false);
113         if (session != null) {
114             if (username != null) {
115                 userManager.userLogout(username);
116             }
117             session.invalidate();
118
119         }
120         return "redirect:" + "/";
121     }
122
123     @RequestMapping(value = "login")
124     public String login(Model model, final HttpServletRequest request,
125             final HttpServletResponse response) {
126         // response.setHeader("X-Page-Location", "/login");
127         IUserManager userManager = (IUserManager) ServiceHelper
128                 .getGlobalInstance(IUserManager.class, this);
129         if (userManager == null) {
130             return "User Manager is not available";
131         }
132
133         String username = request.getUserPrincipal().getName();
134
135         model.addAttribute("username", username);
136         model.addAttribute("role", userManager.getUserLevel(username)
137                 .toNumber());
138         return "forward:" + "/";
139     }
140
141 }