2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.flows.web;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
17 import javax.servlet.http.HttpServletRequest;
19 import org.opendaylight.controller.forwardingrulesmanager.FlowConfig;
20 import org.opendaylight.controller.forwardingrulesmanager.IForwardingRulesManager;
21 import org.opendaylight.controller.sal.authorization.UserLevel;
22 import org.opendaylight.controller.sal.core.Name;
23 import org.opendaylight.controller.sal.core.Node;
24 import org.opendaylight.controller.sal.core.NodeConnector;
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.switchmanager.ISwitchManager;
29 import org.opendaylight.controller.switchmanager.Switch;
30 import org.opendaylight.controller.switchmanager.SwitchConfig;
31 import org.opendaylight.controller.usermanager.IUserManager;
32 import org.opendaylight.controller.web.DaylightWebUtil;
33 import org.opendaylight.controller.web.IDaylightWeb;
34 import org.springframework.stereotype.Controller;
35 import org.springframework.web.bind.annotation.PathVariable;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.bind.annotation.RequestMethod;
38 import org.springframework.web.bind.annotation.RequestParam;
39 import org.springframework.web.bind.annotation.ResponseBody;
41 import com.google.gson.Gson;
45 public class Flows implements IDaylightWeb {
46 private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER;
47 private static final String WEB_NAME = "Flows";
48 private static final String WEB_ID = "flows";
49 private static final short WEB_ORDER = 2;
52 ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null);
56 public String getWebName() {
61 public String getWebId() {
66 public short getWebOrder() {
71 public boolean isAuthorized(UserLevel userLevel) {
72 return userLevel.ordinal() <= AUTH_LEVEL.ordinal();
75 @RequestMapping(value = "/main")
77 public Set<Map<String, Object>> getFlows(HttpServletRequest request, @RequestParam(required = false) String container) {
78 String containerName = DaylightWebUtil.getAuthorizedContainer(request, container, this);
81 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
82 .getInstance(IForwardingRulesManager.class, containerName, this);
88 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
89 .getInstance(ISwitchManager.class, containerName, this);
90 if (switchManager == null) {
94 // get static flow list
95 List<FlowConfig> staticFlowList = frm.getStaticFlows();
96 Set<Map<String, Object>> output = new HashSet<Map<String, Object>>();
97 for (FlowConfig flowConfig : staticFlowList) {
98 Map<String, Object> entry = new HashMap<String, Object>();
99 entry.put("flow", flowConfig);
100 entry.put("name", flowConfig.getName());
101 Node node = flowConfig.getNode();
102 String description = switchManager.getNodeDescription(node);
103 entry.put("node", (description.isEmpty() || description
104 .equalsIgnoreCase("none")) ? node.toString() : description);
105 entry.put("nodeId", node.toString());
112 @RequestMapping(value = "/node-ports")
114 public Map<String, Object> getNodePorts(HttpServletRequest request, @RequestParam(required = false) String container) {
115 String containerName = DaylightWebUtil.getAuthorizedContainer(request, container, this);
117 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
118 .getInstance(ISwitchManager.class, containerName, this);
119 if (switchManager == null) {
123 Map<String, Object> nodes = new HashMap<String, Object>();
124 Map<Short, String> port;
126 for (Switch node : switchManager.getNetworkDevices()) {
127 port = new HashMap<Short, String>(); // new port
128 Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
130 if (nodeConnectorSet != null) {
131 for (NodeConnector nodeConnector : nodeConnectorSet) {
132 String nodeConnectorName = ((Name) switchManager
133 .getNodeConnectorProp(nodeConnector,
134 Name.NamePropName)).getValue();
135 port.put((Short) nodeConnector.getID(), nodeConnectorName
136 + "(" + nodeConnector.getNodeConnectorIDString()
142 Map<String, Object> entry = new HashMap<String, Object>();
143 entry.put("ports", port);
146 String description = switchManager.getNodeDescription(node
148 entry.put("name", (description.isEmpty() || description
149 .equalsIgnoreCase("none")) ? node.getNode().toString()
153 nodes.put(node.getNode().toString(), entry);
159 @RequestMapping(value = "/node-flows")
161 public Map<String, Object> getNodeFlows(HttpServletRequest request, @RequestParam(required = false) String container) {
162 String containerName = DaylightWebUtil.getAuthorizedContainer(request, container, this);
164 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
165 .getInstance(ISwitchManager.class, containerName, this);
166 if (switchManager == null) {
169 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
170 .getInstance(IForwardingRulesManager.class, containerName, this);
175 Map<String, Object> nodes = new HashMap<String, Object>();
177 for (Switch sw : switchManager.getNetworkDevices()) {
178 Node node = sw.getNode();
180 List<FlowConfig> flows = frm.getStaticFlows(node);
182 String nodeDesc = node.toString();
183 SwitchConfig config = switchManager.getSwitchConfig(node
185 if (config != null) {
186 nodeDesc = config.getNodeDescription();
189 nodes.put(nodeDesc, flows.size());
195 @RequestMapping(value = "/flow", method = RequestMethod.POST)
197 public String actionFlow(@RequestParam(required = true) String action,
198 @RequestParam(required = false) String body,
199 @RequestParam(required = true) String nodeId,
200 HttpServletRequest request, @RequestParam(required = false) String container) {
201 if (!isUserAuthorized(UserLevel.NETWORKADMIN, request)) {
202 return "Operation not authorized";
205 String containerName = DaylightWebUtil.getAuthorizedContainer(request, container, this);
207 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
208 .getInstance(IForwardingRulesManager.class, containerName, this);
213 Gson gson = new Gson();
214 FlowConfig flow = gson.fromJson(body, FlowConfig.class);
215 Node node = Node.fromString(nodeId);
217 Status result = new Status(StatusCode.BADREQUEST, "Invalid request");
218 if (action.equals("add")) {
219 result = frm.addStaticFlow(flow, false);
222 return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result
226 @RequestMapping(value = "/flow/{nodeId}/{name}", method = RequestMethod.POST)
228 public String removeFlow(@PathVariable("nodeId") String nodeId,
229 @PathVariable("name") String name,
230 @RequestParam(required = true) String action,
231 HttpServletRequest request, @RequestParam(required = false) String container) {
232 if (!isUserAuthorized(UserLevel.NETWORKADMIN, request)) {
233 return "Operation not authorized";
236 String containerName = DaylightWebUtil.getAuthorizedContainer(request, container, this);
238 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
239 .getInstance(IForwardingRulesManager.class, containerName, this);
244 Status result = null;
245 Node node = Node.fromString(nodeId);
249 if (action.equals("remove")) {
250 result = frm.removeStaticFlow(name, node);
251 } else if (action.equals("toggle")) {
252 result = frm.toggleStaticFlowStatus(name, node);
254 result = new Status(StatusCode.BADREQUEST, "Unknown action");
257 return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result
262 * Returns whether the current user's level is same or above the required
263 * authorization level.
265 * @param requiredLevel
266 * the authorization level required
268 private boolean isUserAuthorized(UserLevel requiredLevel,
269 HttpServletRequest request) {
270 IUserManager userManager = (IUserManager) ServiceHelper
271 .getGlobalInstance(IUserManager.class, this);
272 if (userManager == null) {
276 String username = request.getUserPrincipal().getName();
277 UserLevel userLevel = userManager.getUserLevel(username);
278 return (userLevel.ordinal() <= requiredLevel.ordinal());