Removing references to a prior branded controller
[controller.git] / opendaylight / web / root / src / main / java / org / opendaylight / controller / web / DaylightWeb.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.web;
11
12 import java.util.HashMap;
13 import java.util.Map;
14
15 import javax.servlet.http.HttpServletResponse;
16
17 import org.opendaylight.controller.configuration.IConfigurationService;
18 import org.opendaylight.controller.sal.authorization.UserLevel;
19 import org.opendaylight.controller.sal.utils.ServiceHelper;
20 import org.opendaylight.controller.sal.utils.Status;
21 import org.opendaylight.controller.sal.utils.StatusCode;
22 import org.opendaylight.controller.usermanager.IUserManager;
23 import org.springframework.security.core.context.SecurityContextHolder;
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) {
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 = SecurityContextHolder.getContext().getAuthentication().getName();
42         model.addAttribute("username", username);
43         model.addAttribute("role", userManager.getUserLevel(username).toNumber());
44         
45         return "main";
46     }
47
48     @RequestMapping(value = "web.json")
49     @ResponseBody
50     public Map<String, Map<String, Object>> bundles() {
51         Object[] instances = ServiceHelper.getGlobalInstances(IDaylightWeb.class,
52                 this, null);
53         Map<String, Map<String, Object>> bundles = new HashMap<String, Map<String, Object>>();
54         Map<String, Object> entry;
55         IDaylightWeb bundle;
56         String userName = SecurityContextHolder.getContext().getAuthentication().getName();
57         IUserManager userManger = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
58         for (Object instance : instances) {
59             bundle = (IDaylightWeb) instance;
60             if (userManger != null &&
61                         bundle.isAuthorized(userManger.getUserLevel(userName))) {
62                     entry = new HashMap<String, Object>();
63                     entry.put("name", bundle.getWebName());
64                     entry.put("order", bundle.getWebOrder());
65                     bundles.put(bundle.getWebId(), entry);
66             }
67         }
68         return bundles;
69     }
70     
71     @RequestMapping(value = "save", method = RequestMethod.POST)
72     @ResponseBody
73     public String save() {
74         String username = SecurityContextHolder.getContext().getAuthentication().getName();
75         IUserManager userManager = (IUserManager) ServiceHelper
76                 .getGlobalInstance(IUserManager.class, this);
77         if (userManager == null) return "User Manager is not available";
78         
79         UserLevel level = userManager.getUserLevel(username);
80         if (level == UserLevel.NETWORKOPERATOR) {
81                 return "Save not permitted for Operator";
82         }
83         
84         Status status = new Status(StatusCode.UNAUTHORIZED, 
85                         "Operation not allowed for current user");
86             if (level == UserLevel.NETWORKADMIN || level == UserLevel.SYSTEMADMIN) {
87                 IConfigurationService configService = (IConfigurationService) ServiceHelper
88                         .getGlobalInstance(IConfigurationService.class, this);
89                 if (configService != null) {
90                         status = configService.saveConfigurations();
91                 }
92             }
93         
94         return status.getDescription();
95     }
96     
97     @RequestMapping(value = "login")
98         public String login(Map<String, Object> model, final HttpServletResponse response) {
99                 response.setHeader("X-Page-Location", "/login");
100                 return "login";
101         }
102
103 }