Adding container authorization code in web bundles
[controller.git] / opendaylight / web / flows / src / main / java / org / opendaylight / controller / flows / web / Flows.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.flows.web;
10
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16
17 import javax.servlet.http.HttpServletRequest;
18
19 import org.opendaylight.controller.forwardingrulesmanager.FlowConfig;
20 import org.opendaylight.controller.forwardingrulesmanager.IForwardingRulesManager;
21 import org.opendaylight.controller.sal.authorization.Privilege;
22 import org.opendaylight.controller.sal.authorization.UserLevel;
23 import org.opendaylight.controller.sal.core.Name;
24 import org.opendaylight.controller.sal.core.Node;
25 import org.opendaylight.controller.sal.core.NodeConnector;
26 import org.opendaylight.controller.sal.utils.GlobalConstants;
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.switchmanager.ISwitchManager;
31 import org.opendaylight.controller.switchmanager.Switch;
32 import org.opendaylight.controller.switchmanager.SwitchConfig;
33 import org.opendaylight.controller.web.DaylightWebUtil;
34 import org.opendaylight.controller.web.IDaylightWeb;
35 import org.springframework.stereotype.Controller;
36 import org.springframework.web.bind.annotation.PathVariable;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.bind.annotation.RequestMethod;
39 import org.springframework.web.bind.annotation.RequestParam;
40 import org.springframework.web.bind.annotation.ResponseBody;
41
42 import com.google.gson.Gson;
43
44 @Controller
45 @RequestMapping("/")
46 public class Flows implements IDaylightWeb {
47     private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER;
48     private static final String WEB_NAME = "Flows";
49     private static final String WEB_ID = "flows";
50     private static final short WEB_ORDER = 2;
51
52     public Flows() {
53         ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null);
54     }
55
56     @Override
57     public String getWebName() {
58         return WEB_NAME;
59     }
60
61     @Override
62     public String getWebId() {
63         return WEB_ID;
64     }
65
66     @Override
67     public short getWebOrder() {
68         return WEB_ORDER;
69     }
70
71     @Override
72     public boolean isAuthorized(UserLevel userLevel) {
73         return userLevel.ordinal() <= AUTH_LEVEL.ordinal();
74     }
75
76     @RequestMapping(value = "/main")
77     @ResponseBody
78     public Set<Map<String, Object>> getFlows(HttpServletRequest request, @RequestParam(required = false) String container) {
79         String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
80
81         // Derive the privilege this user has on the current container
82         String userName = request.getUserPrincipal().getName();
83         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
84             return null;
85         }
86
87         // fetch frm
88         IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
89                 .getInstance(IForwardingRulesManager.class, containerName, this);
90         if (frm == null) {
91             return null;
92         }
93
94         // fetch sm
95         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
96                 .getInstance(ISwitchManager.class, containerName, this);
97         if (switchManager == null) {
98             return null;
99         }
100
101         // get static flow list
102         List<FlowConfig> staticFlowList = frm.getStaticFlows();
103         Set<Map<String, Object>> output = new HashSet<Map<String, Object>>();
104         for (FlowConfig flowConfig : staticFlowList) {
105             Map<String, Object> entry = new HashMap<String, Object>();
106             entry.put("flow", flowConfig);
107             entry.put("name", flowConfig.getName());
108             Node node = flowConfig.getNode();
109             String description = switchManager.getNodeDescription(node);
110             entry.put("node", (description.isEmpty() || description
111                     .equalsIgnoreCase("none")) ? node.toString() : description);
112             entry.put("nodeId", node.toString());
113             output.add(entry);
114         }
115
116         return output;
117     }
118
119     @RequestMapping(value = "/node-ports")
120     @ResponseBody
121     public Map<String, Object> getNodePorts(HttpServletRequest request, @RequestParam(required = false) String container) {
122         String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
123
124         // Derive the privilege this user has on the current container
125         String userName = request.getUserPrincipal().getName();
126         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
127             return null;
128         }
129
130         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
131                 .getInstance(ISwitchManager.class, containerName, this);
132         if (switchManager == null) {
133             return null;
134         }
135
136         Map<String, Object> nodes = new HashMap<String, Object>();
137         Map<Short, String> port;
138
139         for (Switch node : switchManager.getNetworkDevices()) {
140             port = new HashMap<Short, String>(); // new port
141             Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
142
143             if (nodeConnectorSet != null) {
144                 for (NodeConnector nodeConnector : nodeConnectorSet) {
145                     String nodeConnectorName = ((Name) switchManager
146                             .getNodeConnectorProp(nodeConnector,
147                                     Name.NamePropName)).getValue();
148                     port.put((Short) nodeConnector.getID(), nodeConnectorName
149                             + "(" + nodeConnector.getNodeConnectorIDString()
150                             + ")");
151                 }
152             }
153
154             // add ports
155             Map<String, Object> entry = new HashMap<String, Object>();
156             entry.put("ports", port);
157
158             // add name
159             String description = switchManager.getNodeDescription(node
160                     .getNode());
161             entry.put("name", (description.isEmpty() || description
162                     .equalsIgnoreCase("none")) ? node.getNode().toString()
163                     : description);
164
165             // add to the node
166             nodes.put(node.getNode().toString(), entry);
167         }
168
169         return nodes;
170     }
171
172     @RequestMapping(value = "/node-flows")
173     @ResponseBody
174     public Map<String, Object> getNodeFlows(HttpServletRequest request, @RequestParam(required = false) String container) {
175         String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
176
177         // Derive the privilege this user has on the current container
178         String userName = request.getUserPrincipal().getName();
179         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
180             return null;
181         }
182
183         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
184                 .getInstance(ISwitchManager.class, containerName, this);
185         if (switchManager == null) {
186             return null;
187         }
188         IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
189                 .getInstance(IForwardingRulesManager.class, containerName, this);
190         if (frm == null) {
191             return null;
192         }
193
194         Map<String, Object> nodes = new HashMap<String, Object>();
195
196         for (Switch sw : switchManager.getNetworkDevices()) {
197             Node node = sw.getNode();
198
199             List<FlowConfig> flows = frm.getStaticFlows(node);
200
201             String nodeDesc = node.toString();
202             SwitchConfig config = switchManager.getSwitchConfig(node
203                     .toString());
204             if (config != null) {
205                 nodeDesc = config.getNodeDescription();
206             }
207
208             nodes.put(nodeDesc, flows.size());
209         }
210
211         return nodes;
212     }
213
214     @RequestMapping(value = "/flow", method = RequestMethod.POST)
215     @ResponseBody
216     public String actionFlow(@RequestParam(required = true) String action,
217             @RequestParam(required = false) String body,
218             @RequestParam(required = true) String nodeId,
219             HttpServletRequest request, @RequestParam(required = false) String container) {
220         String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
221
222         // Authorization check
223         String userName = request.getUserPrincipal().getName();
224         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
225             return "Operation not authorized";
226         }
227
228         IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
229                 .getInstance(IForwardingRulesManager.class, containerName, this);
230         if (frm == null) {
231             return null;
232         }
233
234         Gson gson = new Gson();
235         FlowConfig flow = gson.fromJson(body, FlowConfig.class);
236         Node node = Node.fromString(nodeId);
237         flow.setNode(node);
238         Status result = new Status(StatusCode.BADREQUEST, "Invalid request");
239         if (action.equals("add")) {
240             result = frm.addStaticFlow(flow, false);
241         }
242
243         return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result
244                 .getDescription();
245     }
246
247     @RequestMapping(value = "/flow/{nodeId}/{name}", method = RequestMethod.POST)
248     @ResponseBody
249     public String removeFlow(@PathVariable("nodeId") String nodeId,
250             @PathVariable("name") String name,
251             @RequestParam(required = true) String action,
252             HttpServletRequest request, @RequestParam(required = false) String container) {
253         String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
254
255         // Authorization check
256         String userName = request.getUserPrincipal().getName();
257         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
258             return "Operation not authorized";
259         }
260
261         IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
262                 .getInstance(IForwardingRulesManager.class, containerName, this);
263         if (frm == null) {
264             return null;
265         }
266
267         Status result = null;
268         Node node = Node.fromString(nodeId);
269         if (node == null) {
270             return null;
271         }
272         if (action.equals("remove")) {
273             result = frm.removeStaticFlow(name, node);
274         } else if (action.equals("toggle")) {
275             result = frm.toggleStaticFlowStatus(name, node);
276         } else {
277             result = new Status(StatusCode.BADREQUEST, "Unknown action");
278         }
279
280         return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result
281                 .getDescription();
282     }
283 }