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.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
18 import javax.servlet.http.HttpServletRequest;
20 import org.opendaylight.controller.forwardingrulesmanager.FlowConfig;
21 import org.opendaylight.controller.forwardingrulesmanager.IForwardingRulesManager;
22 import org.opendaylight.controller.sal.authorization.Privilege;
23 import org.opendaylight.controller.sal.authorization.UserLevel;
24 import org.opendaylight.controller.sal.core.Description;
25 import org.opendaylight.controller.sal.core.Name;
26 import org.opendaylight.controller.sal.core.Node;
27 import org.opendaylight.controller.sal.core.NodeConnector;
28 import org.opendaylight.controller.sal.utils.GlobalConstants;
29 import org.opendaylight.controller.sal.utils.ServiceHelper;
30 import org.opendaylight.controller.sal.utils.Status;
31 import org.opendaylight.controller.sal.utils.StatusCode;
32 import org.opendaylight.controller.switchmanager.ISwitchManager;
33 import org.opendaylight.controller.switchmanager.Switch;
34 import org.opendaylight.controller.switchmanager.SwitchConfig;
35 import org.opendaylight.controller.web.DaylightWebUtil;
36 import org.opendaylight.controller.web.IDaylightWeb;
37 import org.springframework.stereotype.Controller;
38 import org.springframework.web.bind.annotation.PathVariable;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RequestMethod;
41 import org.springframework.web.bind.annotation.RequestParam;
42 import org.springframework.web.bind.annotation.ResponseBody;
44 import com.google.gson.Gson;
48 public class Flows implements IDaylightWeb {
49 private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER;
50 private static final String WEB_NAME = "Flows";
52 private static final String WEB_ID = "flows";
53 private static final short WEB_ORDER = 2;
55 private final Gson gson;
58 ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null);
63 public String getWebName() {
68 public String getWebId() {
73 public short getWebOrder() {
78 public boolean isAuthorized(UserLevel userLevel) {
79 return userLevel.ordinal() <= AUTH_LEVEL.ordinal();
82 @RequestMapping(value = "/main")
84 public Map<String, Object> getFlows(HttpServletRequest request, @RequestParam(required = false) String container) {
85 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
87 // Derive the privilege this user has on the current container
88 String userName = request.getUserPrincipal().getName();
89 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
90 if (privilege == Privilege.NONE) {
95 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper.getInstance(
96 IForwardingRulesManager.class, containerName, this);
102 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
104 if (switchManager == null) {
108 // get static flow list
109 List<FlowConfig> staticFlowList = frm.getStaticFlows();
110 Set<Map<String, Object>> flowSet = new HashSet<Map<String, Object>>();
111 for (FlowConfig flowConfig : staticFlowList) {
112 Map<String, Object> entry = new HashMap<String, Object>();
113 entry.put("flow", flowConfig);
114 entry.put("name", flowConfig.getName());
115 Node node = flowConfig.getNode();
116 entry.put("node", getNodeDesc(node, switchManager));
117 entry.put("nodeId", node.toString());
121 Map<String, Object> output = new HashMap<String, Object>(2);
122 output.put("flows", flowSet);
123 output.put("privilege", privilege);
127 @RequestMapping(value = "/node-ports")
129 public Map<String, Object> getNodePorts(HttpServletRequest request, @RequestParam(required = false) String container) {
130 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
132 // Derive the privilege this user has on the current container
133 String userName = request.getUserPrincipal().getName();
134 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
138 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
140 if (switchManager == null) {
144 Map<String, Object> nodes = new HashMap<String, Object>();
145 Map<Short, String> port;
147 for (Switch node : switchManager.getNetworkDevices()) {
148 port = new HashMap<Short, String>(); // new port
149 Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
151 if (nodeConnectorSet != null) {
152 for (NodeConnector nodeConnector : nodeConnectorSet) {
153 String nodeConnectorName = ((Name) switchManager.getNodeConnectorProp(nodeConnector,
154 Name.NamePropName)).getValue();
155 port.put((Short) nodeConnector.getID(),
156 nodeConnectorName + "(" + nodeConnector.getNodeConnectorIDString() + ")");
161 Map<String, Object> entry = new HashMap<String, Object>();
162 entry.put("ports", port);
165 entry.put("name", getNodeDesc(node.getNode(), switchManager));
168 nodes.put(node.getNode().toString(), entry);
174 @RequestMapping(value = "/node-flows")
176 public Map<String, Object> getNodeFlows(HttpServletRequest request, @RequestParam(required = false) String container) {
177 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
179 // Derive the privilege this user has on the current container
180 String userName = request.getUserPrincipal().getName();
181 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
185 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
187 if (switchManager == null) {
190 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper.getInstance(
191 IForwardingRulesManager.class, containerName, this);
196 Map<String, Object> nodes = new HashMap<String, Object>();
198 for (Switch sw : switchManager.getNetworkDevices()) {
199 Node node = sw.getNode();
201 List<FlowConfig> flows = frm.getStaticFlows(node);
203 String nodeDesc = node.toString();
204 SwitchConfig config = switchManager.getSwitchConfig(node.toString());
205 if ((config != null) && (config.getProperty(Description.propertyName) != null)) {
206 nodeDesc = ((Description) config.getProperty(Description.propertyName)).getValue();
209 nodes.put(nodeDesc, flows.size());
215 @RequestMapping(value = "/flow", method = RequestMethod.POST)
217 public String actionFlow(@RequestParam(required = true) String action, @RequestParam(required = false) String body,
218 @RequestParam(required = true) String nodeId, HttpServletRequest request,
219 @RequestParam(required = false) String container) {
220 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
222 // Authorization check
223 String userName = request.getUserPrincipal().getName();
224 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
225 return "Operation not authorized";
228 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper.getInstance(
229 IForwardingRulesManager.class, containerName, this);
234 FlowConfig flow = gson.fromJson(body, FlowConfig.class);
236 Node node = Node.fromString(nodeId);
239 Status result = new Status(StatusCode.BADREQUEST, "Invalid request");
240 if (action.equals("add")) {
241 result = frm.addStaticFlow(flow);
242 if (result.isSuccess()) {
243 DaylightWebUtil.auditlog("Flow Entry", userName, "added", flow.getName() + " on Node "
244 + DaylightWebUtil.getNodeDesc(node, containerName, this), containerName);
248 return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result.getDescription();
251 @RequestMapping(value = "/flow/{nodeId}/{name:.*}", method = RequestMethod.POST)
253 public String removeFlow(@PathVariable("nodeId") String nodeId, @PathVariable("name") String name,
254 @RequestParam(required = true) String action, HttpServletRequest request,
255 @RequestParam(required = false) String container) {
256 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
258 // Authorization check
259 String userName = request.getUserPrincipal().getName();
260 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
261 return "Operation not authorized";
264 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper.getInstance(
265 IForwardingRulesManager.class, containerName, this);
270 Status result = null;
271 Node node = Node.fromString(nodeId);
275 if (action.equals("remove")) {
276 result = frm.removeStaticFlow(name, node);
277 if (result.isSuccess()) {
278 DaylightWebUtil.auditlog("Flow Entry", userName, "removed",
279 name + " on Node " + DaylightWebUtil.getNodeDesc(node, containerName, this), containerName);
281 } else if (action.equals("toggle")) {
282 result = frm.toggleStaticFlowStatus(name, node);
283 if (result.isSuccess()) {
284 DaylightWebUtil.auditlog("Flow Entry", userName, "toggled",
285 name + " on Node " + DaylightWebUtil.getNodeDesc(node, containerName, this), containerName);
288 result = new Status(StatusCode.BADREQUEST, "Unknown action");
291 return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result.getDescription();
294 @SuppressWarnings("unchecked")
295 @RequestMapping(value = "/flow/deleteFlows", method = RequestMethod.POST)
297 public String removeSelectedFlows(@RequestParam(required = false) String body, HttpServletRequest request,
298 @RequestParam(required = false) String container) {
299 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
301 // Authorization check
302 String userName = request.getUserPrincipal().getName();
303 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
304 return "Operation not authorized";
306 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper.getInstance(
307 IForwardingRulesManager.class, containerName, this);
309 return "Forwarding Rules Manager is not available";
312 List<Map<String, String>> flowList = new ArrayList<Map<String, String>>();
313 flowList = gson.fromJson(body, flowList.getClass());
314 Status result = new Status(StatusCode.BADREQUEST, "Invalid request");
316 for (Map<String, String> flowEntry : flowList) {
317 Node node = Node.fromString(flowEntry.get("node"));
318 result = frm.removeStaticFlow(flowEntry.get("name"), node);
319 if (result.isSuccess()) {
320 DaylightWebUtil.auditlog("Flow Entry", userName, "removed", flowEntry.get("name") + " on Node "
321 + DaylightWebUtil.getNodeDesc(node, containerName, this), containerName);
323 status = flowEntry.get("name") + ", " + status;
326 if (!status.equals("")) {
327 return "Could not remove " + status.substring(0, status.length() - 2) + " Flow(s)";
333 private String getNodeDesc(Node node, ISwitchManager switchManager) {
334 Description desc = (Description) switchManager.getNodeProp(node, Description.propertyName);
335 String description = (desc == null) ? "" : desc.getValue();
336 return (description.isEmpty() || description.equalsIgnoreCase("none")) ? node.toString() : description;