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,
85 @RequestParam(required = false) String container) {
86 String containerName = (container == null) ? GlobalConstants.DEFAULT
87 .toString() : container;
89 // Derive the privilege this user has on the current container
90 String userName = request.getUserPrincipal().getName();
91 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName,
93 if (privilege == Privilege.NONE) {
98 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
99 .getInstance(IForwardingRulesManager.class, containerName, this);
105 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
106 .getInstance(ISwitchManager.class, containerName, this);
107 if (switchManager == null) {
111 // get static flow list
112 List<FlowConfig> staticFlowList = frm.getStaticFlows();
113 Set<Map<String, Object>> flowSet = new HashSet<Map<String, Object>>();
114 for (FlowConfig flowConfig : staticFlowList) {
115 Map<String, Object> entry = new HashMap<String, Object>();
116 entry.put("flow", flowConfig);
117 entry.put("name", flowConfig.getName());
118 Node node = flowConfig.getNode();
119 entry.put("node", getNodeDesc(node, switchManager));
120 entry.put("nodeId", node.toString());
124 Map<String, Object> output = new HashMap<String, Object>(2);
125 output.put("flows", flowSet);
126 output.put("privilege", privilege);
130 @RequestMapping(value = "/node-ports")
132 public Map<String, Object> getNodePorts(HttpServletRequest request,
133 @RequestParam(required = false) String container) {
134 String containerName = (container == null) ? GlobalConstants.DEFAULT
135 .toString() : container;
137 // Derive the privilege this user has on the current container
138 String userName = request.getUserPrincipal().getName();
140 .getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
144 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
145 .getInstance(ISwitchManager.class, containerName, this);
146 if (switchManager == null) {
150 Map<String, Object> nodes = new HashMap<String, Object>();
151 Map<Short, String> port;
153 for (Switch node : switchManager.getNetworkDevices()) {
154 port = new HashMap<Short, String>(); // new port
155 Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
157 if (nodeConnectorSet != null) {
158 for (NodeConnector nodeConnector : nodeConnectorSet) {
159 String nodeConnectorName = ((Name) switchManager
160 .getNodeConnectorProp(nodeConnector,
161 Name.NamePropName)).getValue();
162 port.put((Short) nodeConnector.getID(), nodeConnectorName
163 + "(" + nodeConnector.getNodeConnectorIDString()
169 Map<String, Object> entry = new HashMap<String, Object>();
170 entry.put("ports", port);
173 entry.put("name", getNodeDesc(node.getNode(), switchManager));
176 nodes.put(node.getNode().toString(), entry);
182 @RequestMapping(value = "/node-flows")
184 public Map<String, Object> getNodeFlows(HttpServletRequest request,
185 @RequestParam(required = false) String container) {
186 String containerName = (container == null) ? GlobalConstants.DEFAULT
187 .toString() : container;
189 // Derive the privilege this user has on the current container
190 String userName = request.getUserPrincipal().getName();
192 .getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
196 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
197 .getInstance(ISwitchManager.class, containerName, this);
198 if (switchManager == null) {
201 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
202 .getInstance(IForwardingRulesManager.class, containerName, this);
207 Map<String, Object> nodes = new HashMap<String, Object>();
209 for (Switch sw : switchManager.getNetworkDevices()) {
210 Node node = sw.getNode();
212 List<FlowConfig> flows = frm.getStaticFlows(node);
214 String nodeDesc = node.toString();
215 SwitchConfig config = switchManager
216 .getSwitchConfig(node.toString());
218 && (config.getProperty(Description.propertyName) != null)) {
219 nodeDesc = ((Description) config
220 .getProperty(Description.propertyName)).getValue();
223 nodes.put(nodeDesc, flows.size());
229 @RequestMapping(value = "/flow", method = RequestMethod.POST)
231 public String actionFlow(@RequestParam(required = true) String action,
232 @RequestParam(required = false) String body,
233 @RequestParam(required = true) String nodeId,
234 HttpServletRequest request,
235 @RequestParam(required = false) String container) {
236 String containerName = (container == null) ? GlobalConstants.DEFAULT
237 .toString() : container;
239 // Authorization check
240 String userName = request.getUserPrincipal().getName();
242 .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
243 return "Operation not authorized";
246 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
247 .getInstance(IForwardingRulesManager.class, containerName, this);
252 FlowConfig flow = gson.fromJson(body, FlowConfig.class);
254 Node node = Node.fromString(nodeId);
256 Status result = new Status(StatusCode.BADREQUEST, "Invalid request");
257 if (action.equals("add")) {
258 result = frm.addStaticFlow(flow);
259 DaylightWebUtil.auditlog("Flow", userName, "added", flow.getName(),
263 return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result
267 @RequestMapping(value = "/flow/{nodeId}/{name:.*}", method = RequestMethod.POST)
269 public String removeFlow(@PathVariable("nodeId") String nodeId,
270 @PathVariable("name") String name,
271 @RequestParam(required = true) String action,
272 HttpServletRequest request,
273 @RequestParam(required = false) String container) {
274 String containerName = (container == null) ? GlobalConstants.DEFAULT
275 .toString() : container;
277 // Authorization check
278 String userName = request.getUserPrincipal().getName();
280 .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
281 return "Operation not authorized";
284 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
285 .getInstance(IForwardingRulesManager.class, containerName, this);
290 Status result = null;
291 Node node = Node.fromString(nodeId);
295 if (action.equals("remove")) {
296 result = frm.removeStaticFlow(name, node);
297 if (result.isSuccess()) {
298 DaylightWebUtil.auditlog("Flow", userName, "removed", name,
301 } else if (action.equals("toggle")) {
302 result = frm.toggleStaticFlowStatus(name, node);
303 if (result.isSuccess()) {
304 DaylightWebUtil.auditlog("Flow", userName, "toggled", name,
308 result = new Status(StatusCode.BADREQUEST, "Unknown action");
311 return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result
315 @SuppressWarnings("unchecked")
316 @RequestMapping(value = "/flow/deleteFlows", method = RequestMethod.POST)
318 public String removeSelectedFlows(
319 @RequestParam(required = false) String body,
320 HttpServletRequest request,
321 @RequestParam(required = false) String container) {
322 String containerName = (container == null) ? GlobalConstants.DEFAULT
323 .toString() : container;
325 // Authorization check
326 String userName = request.getUserPrincipal().getName();
328 .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
329 return "Operation not authorized";
332 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
333 .getInstance(IForwardingRulesManager.class, containerName, this);
335 return "Forwarding Rules Manager is not available";
338 List<Map<String, String>> flowList = new ArrayList<Map<String, String>>();
339 flowList = gson.fromJson(body, flowList.getClass());
340 Status result = new Status(StatusCode.BADREQUEST, "Invalid request");
342 for (Map<String, String> flowEntry : flowList) {
343 Node node = Node.fromString(flowEntry.get("node"));
344 result = frm.removeStaticFlow(flowEntry.get("name"), node);
345 if (result.isSuccess()) {
346 DaylightWebUtil.auditlog("Flow", userName, "removed",
347 flowEntry.get("name"), containerName);
349 status = flowEntry.get("name") + ", " + status;
352 if (!status.equals("")) {
353 return "Could not remove "
354 + status.substring(0, status.length() - 2) + " Flow(s)";
360 private String getNodeDesc(Node node, ISwitchManager switchManager) {
361 Description desc = (Description) switchManager.getNodeProp(node,
362 Description.propertyName);
363 String description = (desc == null) ? "" : desc.getValue();
364 return (description.isEmpty() || description.equalsIgnoreCase("none")) ? node
365 .toString() : description;