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.GlobalConstants;
26 import org.opendaylight.controller.sal.utils.ServiceHelper;
27 import org.opendaylight.controller.sal.utils.Status;
28 import org.opendaylight.controller.sal.utils.StatusCode;
29 import org.opendaylight.controller.switchmanager.ISwitchManager;
30 import org.opendaylight.controller.switchmanager.Switch;
31 import org.opendaylight.controller.switchmanager.SwitchConfig;
32 import org.opendaylight.controller.usermanager.IUserManager;
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;
50 private final String containerName = GlobalConstants.DEFAULT.toString();
53 ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null);
57 public String getWebName() {
62 public String getWebId() {
67 public short getWebOrder() {
72 public boolean isAuthorized(UserLevel userLevel) {
73 return userLevel.ordinal() <= AUTH_LEVEL.ordinal();
76 @RequestMapping(value = "/main")
78 public Set<Map<String, Object>> getFlows() {
80 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
81 .getInstance(IForwardingRulesManager.class, containerName, this);
87 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
88 .getInstance(ISwitchManager.class, containerName, this);
89 if (switchManager == null) {
93 // get static flow list
94 List<FlowConfig> staticFlowList = frm.getStaticFlows();
95 Set<Map<String, Object>> output = new HashSet<Map<String, Object>>();
96 for (FlowConfig flowConfig : staticFlowList) {
97 Map<String, Object> entry = new HashMap<String, Object>();
98 entry.put("flow", flowConfig);
99 entry.put("name", flowConfig.getName());
100 Node node = flowConfig.getNode();
101 String description = switchManager.getNodeDescription(node);
102 entry.put("node", (description.isEmpty() || description
103 .equalsIgnoreCase("none")) ? node.toString() : description);
104 entry.put("nodeId", node.toString());
111 @RequestMapping(value = "/node-ports")
113 public Map<String, Object> getNodePorts() {
114 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
115 .getInstance(ISwitchManager.class, containerName, this);
116 if (switchManager == null) {
120 Map<String, Object> nodes = new HashMap<String, Object>();
121 Map<Short, String> port;
123 for (Switch node : switchManager.getNetworkDevices()) {
124 port = new HashMap<Short, String>(); // new port
125 Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
127 if (nodeConnectorSet != null) {
128 for (NodeConnector nodeConnector : nodeConnectorSet) {
129 String nodeConnectorName = ((Name) switchManager
130 .getNodeConnectorProp(nodeConnector,
131 Name.NamePropName)).getValue();
132 port.put((Short) nodeConnector.getID(), nodeConnectorName
133 + "(" + nodeConnector.getNodeConnectorIDString()
139 Map<String, Object> entry = new HashMap<String, Object>();
140 entry.put("ports", port);
143 String description = switchManager.getNodeDescription(node
145 entry.put("name", (description.isEmpty() || description
146 .equalsIgnoreCase("none")) ? node.getNode().toString()
150 nodes.put(node.getNode().toString(), entry);
156 @RequestMapping(value = "/node-flows")
158 public Map<String, Object> getNodeFlows() {
159 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
160 .getInstance(ISwitchManager.class, containerName, this);
161 if (switchManager == null) {
164 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
165 .getInstance(IForwardingRulesManager.class, "default", this);
170 Map<String, Object> nodes = new HashMap<String, Object>();
172 for (Switch sw : switchManager.getNetworkDevices()) {
173 Node node = sw.getNode();
175 List<FlowConfig> flows = frm.getStaticFlows(node);
177 String nodeDesc = node.toString();
178 SwitchConfig config = switchManager.getSwitchConfig(node
180 if (config != null) {
181 nodeDesc = config.getNodeDescription();
184 nodes.put(nodeDesc, flows.size());
190 @RequestMapping(value = "/flow", method = RequestMethod.POST)
192 public String actionFlow(@RequestParam(required = true) String action,
193 @RequestParam(required = false) String body,
194 @RequestParam(required = true) String nodeId,
195 HttpServletRequest request) {
196 if (!isUserAuthorized(UserLevel.NETWORKADMIN, request)) {
197 return "Operation not authorized";
200 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
201 .getInstance(IForwardingRulesManager.class, containerName, this);
206 Gson gson = new Gson();
207 FlowConfig flow = gson.fromJson(body, FlowConfig.class);
208 Node node = Node.fromString(nodeId);
210 Status result = new Status(StatusCode.BADREQUEST, "Invalid request");
211 if (action.equals("add")) {
212 result = frm.addStaticFlow(flow, false);
215 return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result
219 @RequestMapping(value = "/flow/{nodeId}/{name}", method = RequestMethod.POST)
221 public String removeFlow(@PathVariable("nodeId") String nodeId,
222 @PathVariable("name") String name,
223 @RequestParam(required = true) String action,
224 HttpServletRequest request) {
225 if (!isUserAuthorized(UserLevel.NETWORKADMIN, request)) {
227 return "Operation not authorized";
230 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
231 .getInstance(IForwardingRulesManager.class, containerName, this);
236 Status result = null;
237 Node node = Node.fromString(nodeId);
241 if (action.equals("remove")) {
242 result = frm.removeStaticFlow(name, node);
243 } else if (action.equals("toggle")) {
244 result = frm.toggleStaticFlowStatus(name, node);
246 result = new Status(StatusCode.BADREQUEST, "Unknown action");
249 return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result
254 * Returns whether the current user's level is same or above the required
255 * authorization level.
257 * @param requiredLevel
258 * the authorization level required
260 private boolean isUserAuthorized(UserLevel requiredLevel,
261 HttpServletRequest request) {
262 IUserManager userManager = (IUserManager) ServiceHelper
263 .getGlobalInstance(IUserManager.class, this);
264 if (userManager == null) {
268 String username = request.getUserPrincipal().getName();
269 UserLevel userLevel = userManager.getUserLevel(username);
270 return (userLevel.ordinal() <= requiredLevel.ordinal());