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.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;
42 import com.google.gson.Gson;
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;
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 Map<String, Object> getFlows(HttpServletRequest request, @RequestParam(required = false) String container) {
79 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
81 // Derive the privilege this user has on the current container
82 String userName = request.getUserPrincipal().getName();
83 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
84 if (privilege == Privilege.NONE) {
89 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
90 .getInstance(IForwardingRulesManager.class, containerName, this);
96 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
97 .getInstance(ISwitchManager.class, containerName, this);
98 if (switchManager == null) {
102 // get static flow list
103 List<FlowConfig> staticFlowList = frm.getStaticFlows();
104 Set<Map<String, Object>> flowSet = new HashSet<Map<String, Object>>();
105 for (FlowConfig flowConfig : staticFlowList) {
106 Map<String, Object> entry = new HashMap<String, Object>();
107 entry.put("flow", flowConfig);
108 entry.put("name", flowConfig.getName());
109 Node node = flowConfig.getNode();
110 String description = switchManager.getNodeDescription(node);
111 entry.put("node", (description.isEmpty() || description
112 .equalsIgnoreCase("none")) ? node.toString() : description);
113 entry.put("nodeId", node.toString());
117 Map <String, Object> output = new HashMap<String, Object>(2);
118 output.put("flows", flowSet);
119 output.put("privilege", privilege);
123 @RequestMapping(value = "/node-ports")
125 public Map<String, Object> getNodePorts(HttpServletRequest request, @RequestParam(required = false) String container) {
126 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
128 // Derive the privilege this user has on the current container
129 String userName = request.getUserPrincipal().getName();
130 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
134 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
135 .getInstance(ISwitchManager.class, containerName, this);
136 if (switchManager == null) {
140 Map<String, Object> nodes = new HashMap<String, Object>();
141 Map<Short, String> port;
143 for (Switch node : switchManager.getNetworkDevices()) {
144 port = new HashMap<Short, String>(); // new port
145 Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
147 if (nodeConnectorSet != null) {
148 for (NodeConnector nodeConnector : nodeConnectorSet) {
149 String nodeConnectorName = ((Name) switchManager
150 .getNodeConnectorProp(nodeConnector,
151 Name.NamePropName)).getValue();
152 port.put((Short) nodeConnector.getID(), nodeConnectorName
153 + "(" + nodeConnector.getNodeConnectorIDString()
159 Map<String, Object> entry = new HashMap<String, Object>();
160 entry.put("ports", port);
163 String description = switchManager.getNodeDescription(node
165 entry.put("name", (description.isEmpty() || description
166 .equalsIgnoreCase("none")) ? node.getNode().toString()
170 nodes.put(node.getNode().toString(), entry);
176 @RequestMapping(value = "/node-flows")
178 public Map<String, Object> getNodeFlows(HttpServletRequest request, @RequestParam(required = false) String container) {
179 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
181 // Derive the privilege this user has on the current container
182 String userName = request.getUserPrincipal().getName();
183 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
187 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
188 .getInstance(ISwitchManager.class, containerName, this);
189 if (switchManager == null) {
192 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
193 .getInstance(IForwardingRulesManager.class, containerName, this);
198 Map<String, Object> nodes = new HashMap<String, Object>();
200 for (Switch sw : switchManager.getNetworkDevices()) {
201 Node node = sw.getNode();
203 List<FlowConfig> flows = frm.getStaticFlows(node);
205 String nodeDesc = node.toString();
206 SwitchConfig config = switchManager.getSwitchConfig(node
208 if (config != null) {
209 nodeDesc = config.getNodeDescription();
212 nodes.put(nodeDesc, flows.size());
218 @RequestMapping(value = "/flow", method = RequestMethod.POST)
220 public String actionFlow(@RequestParam(required = true) String action,
221 @RequestParam(required = false) String body,
222 @RequestParam(required = true) String nodeId,
223 HttpServletRequest request, @RequestParam(required = false) String container) {
224 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
226 // Authorization check
227 String userName = request.getUserPrincipal().getName();
228 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
229 return "Operation not authorized";
232 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
233 .getInstance(IForwardingRulesManager.class, containerName, this);
238 Gson gson = new Gson();
239 FlowConfig flow = gson.fromJson(body, FlowConfig.class);
240 Node node = Node.fromString(nodeId);
242 Status result = new Status(StatusCode.BADREQUEST, "Invalid request");
243 if (action.equals("add")) {
244 result = frm.addStaticFlow(flow, false);
247 return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result
251 @RequestMapping(value = "/flow/{nodeId}/{name}", method = RequestMethod.POST)
253 public String removeFlow(@PathVariable("nodeId") String nodeId,
254 @PathVariable("name") String name,
255 @RequestParam(required = true) String action,
256 HttpServletRequest request, @RequestParam(required = false) String container) {
257 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
259 // Authorization check
260 String userName = request.getUserPrincipal().getName();
261 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
262 return "Operation not authorized";
265 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
266 .getInstance(IForwardingRulesManager.class, containerName, this);
271 Status result = null;
272 Node node = Node.fromString(nodeId);
276 if (action.equals("remove")) {
277 result = frm.removeStaticFlow(name, node);
278 } else if (action.equals("toggle")) {
279 result = frm.toggleStaticFlowStatus(name, node);
281 result = new Status(StatusCode.BADREQUEST, "Unknown action");
284 return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result