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 Set<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 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
88 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
89 .getInstance(IForwardingRulesManager.class, containerName, this);
95 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
96 .getInstance(ISwitchManager.class, containerName, this);
97 if (switchManager == null) {
101 // get static flow list
102 List<FlowConfig> staticFlowList = frm.getStaticFlows();
103 Set<Map<String, Object>> output = new HashSet<Map<String, Object>>();
104 for (FlowConfig flowConfig : staticFlowList) {
105 Map<String, Object> entry = new HashMap<String, Object>();
106 entry.put("flow", flowConfig);
107 entry.put("name", flowConfig.getName());
108 Node node = flowConfig.getNode();
109 String description = switchManager.getNodeDescription(node);
110 entry.put("node", (description.isEmpty() || description
111 .equalsIgnoreCase("none")) ? node.toString() : description);
112 entry.put("nodeId", node.toString());
119 @RequestMapping(value = "/node-ports")
121 public Map<String, Object> getNodePorts(HttpServletRequest request, @RequestParam(required = false) String container) {
122 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
124 // Derive the privilege this user has on the current container
125 String userName = request.getUserPrincipal().getName();
126 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
130 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
131 .getInstance(ISwitchManager.class, containerName, this);
132 if (switchManager == null) {
136 Map<String, Object> nodes = new HashMap<String, Object>();
137 Map<Short, String> port;
139 for (Switch node : switchManager.getNetworkDevices()) {
140 port = new HashMap<Short, String>(); // new port
141 Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
143 if (nodeConnectorSet != null) {
144 for (NodeConnector nodeConnector : nodeConnectorSet) {
145 String nodeConnectorName = ((Name) switchManager
146 .getNodeConnectorProp(nodeConnector,
147 Name.NamePropName)).getValue();
148 port.put((Short) nodeConnector.getID(), nodeConnectorName
149 + "(" + nodeConnector.getNodeConnectorIDString()
155 Map<String, Object> entry = new HashMap<String, Object>();
156 entry.put("ports", port);
159 String description = switchManager.getNodeDescription(node
161 entry.put("name", (description.isEmpty() || description
162 .equalsIgnoreCase("none")) ? node.getNode().toString()
166 nodes.put(node.getNode().toString(), entry);
172 @RequestMapping(value = "/node-flows")
174 public Map<String, Object> getNodeFlows(HttpServletRequest request, @RequestParam(required = false) String container) {
175 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
177 // Derive the privilege this user has on the current container
178 String userName = request.getUserPrincipal().getName();
179 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
183 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
184 .getInstance(ISwitchManager.class, containerName, this);
185 if (switchManager == null) {
188 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
189 .getInstance(IForwardingRulesManager.class, containerName, this);
194 Map<String, Object> nodes = new HashMap<String, Object>();
196 for (Switch sw : switchManager.getNetworkDevices()) {
197 Node node = sw.getNode();
199 List<FlowConfig> flows = frm.getStaticFlows(node);
201 String nodeDesc = node.toString();
202 SwitchConfig config = switchManager.getSwitchConfig(node
204 if (config != null) {
205 nodeDesc = config.getNodeDescription();
208 nodes.put(nodeDesc, flows.size());
214 @RequestMapping(value = "/flow", method = RequestMethod.POST)
216 public String actionFlow(@RequestParam(required = true) String action,
217 @RequestParam(required = false) String body,
218 @RequestParam(required = true) String nodeId,
219 HttpServletRequest request, @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
229 .getInstance(IForwardingRulesManager.class, containerName, this);
234 Gson gson = new Gson();
235 FlowConfig flow = gson.fromJson(body, FlowConfig.class);
236 Node node = Node.fromString(nodeId);
238 Status result = new Status(StatusCode.BADREQUEST, "Invalid request");
239 if (action.equals("add")) {
240 result = frm.addStaticFlow(flow, false);
243 return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result
247 @RequestMapping(value = "/flow/{nodeId}/{name}", method = RequestMethod.POST)
249 public String removeFlow(@PathVariable("nodeId") String nodeId,
250 @PathVariable("name") String name,
251 @RequestParam(required = true) String action,
252 HttpServletRequest request, @RequestParam(required = false) String container) {
253 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
255 // Authorization check
256 String userName = request.getUserPrincipal().getName();
257 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
258 return "Operation not authorized";
261 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
262 .getInstance(IForwardingRulesManager.class, containerName, this);
267 Status result = null;
268 Node node = Node.fromString(nodeId);
272 if (action.equals("remove")) {
273 result = frm.removeStaticFlow(name, node);
274 } else if (action.equals("toggle")) {
275 result = frm.toggleStaticFlowStatus(name, node);
277 result = new Status(StatusCode.BADREQUEST, "Unknown action");
280 return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result