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.Description;
24 import org.opendaylight.controller.sal.core.Name;
25 import org.opendaylight.controller.sal.core.Node;
26 import org.opendaylight.controller.sal.core.NodeConnector;
27 import org.opendaylight.controller.sal.utils.GlobalConstants;
28 import org.opendaylight.controller.sal.utils.ServiceHelper;
29 import org.opendaylight.controller.sal.utils.Status;
30 import org.opendaylight.controller.sal.utils.StatusCode;
31 import org.opendaylight.controller.switchmanager.ISwitchManager;
32 import org.opendaylight.controller.switchmanager.Switch;
33 import org.opendaylight.controller.switchmanager.SwitchConfig;
34 import org.opendaylight.controller.web.DaylightWebUtil;
35 import org.opendaylight.controller.web.IDaylightWeb;
36 import org.springframework.stereotype.Controller;
37 import org.springframework.web.bind.annotation.PathVariable;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.bind.annotation.RequestMethod;
40 import org.springframework.web.bind.annotation.RequestParam;
41 import org.springframework.web.bind.annotation.ResponseBody;
43 import com.google.gson.Gson;
47 public class Flows implements IDaylightWeb {
48 private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER;
49 private static final String WEB_NAME = "Flows";
50 private static final String WEB_ID = "flows";
51 private static final short WEB_ORDER = 2;
54 ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null);
58 public String getWebName() {
63 public String getWebId() {
68 public short getWebOrder() {
73 public boolean isAuthorized(UserLevel userLevel) {
74 return userLevel.ordinal() <= AUTH_LEVEL.ordinal();
77 @RequestMapping(value = "/main")
79 public Map<String, Object> getFlows(HttpServletRequest request, @RequestParam(required = false) String container) {
80 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
82 // Derive the privilege this user has on the current container
83 String userName = request.getUserPrincipal().getName();
84 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
85 if (privilege == Privilege.NONE) {
90 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
91 .getInstance(IForwardingRulesManager.class, containerName, this);
97 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
98 .getInstance(ISwitchManager.class, containerName, this);
99 if (switchManager == null) {
103 // get static flow list
104 List<FlowConfig> staticFlowList = frm.getStaticFlows();
105 Set<Map<String, Object>> flowSet = new HashSet<Map<String, Object>>();
106 for (FlowConfig flowConfig : staticFlowList) {
107 Map<String, Object> entry = new HashMap<String, Object>();
108 entry.put("flow", flowConfig);
109 entry.put("name", flowConfig.getName());
110 Node node = flowConfig.getNode();
111 entry.put("node", getNodeDesc(node, switchManager));
112 entry.put("nodeId", node.toString());
116 Map <String, Object> output = new HashMap<String, Object>(2);
117 output.put("flows", flowSet);
118 output.put("privilege", privilege);
122 @RequestMapping(value = "/node-ports")
124 public Map<String, Object> getNodePorts(HttpServletRequest request, @RequestParam(required = false) String container) {
125 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
127 // Derive the privilege this user has on the current container
128 String userName = request.getUserPrincipal().getName();
129 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
133 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
134 .getInstance(ISwitchManager.class, containerName, this);
135 if (switchManager == null) {
139 Map<String, Object> nodes = new HashMap<String, Object>();
140 Map<Short, String> port;
142 for (Switch node : switchManager.getNetworkDevices()) {
143 port = new HashMap<Short, String>(); // new port
144 Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
146 if (nodeConnectorSet != null) {
147 for (NodeConnector nodeConnector : nodeConnectorSet) {
148 String nodeConnectorName = ((Name) switchManager
149 .getNodeConnectorProp(nodeConnector,
150 Name.NamePropName)).getValue();
151 port.put((Short) nodeConnector.getID(), nodeConnectorName
152 + "(" + nodeConnector.getNodeConnectorIDString()
158 Map<String, Object> entry = new HashMap<String, Object>();
159 entry.put("ports", port);
162 entry.put("name", getNodeDesc(node.getNode(), switchManager));
165 nodes.put(node.getNode().toString(), entry);
171 @RequestMapping(value = "/node-flows")
173 public Map<String, Object> getNodeFlows(HttpServletRequest request, @RequestParam(required = false) String container) {
174 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
176 // Derive the privilege this user has on the current container
177 String userName = request.getUserPrincipal().getName();
178 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
182 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
183 .getInstance(ISwitchManager.class, containerName, this);
184 if (switchManager == null) {
187 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
188 .getInstance(IForwardingRulesManager.class, containerName, this);
193 Map<String, Object> nodes = new HashMap<String, Object>();
195 for (Switch sw : switchManager.getNetworkDevices()) {
196 Node node = sw.getNode();
198 List<FlowConfig> flows = frm.getStaticFlows(node);
200 String nodeDesc = node.toString();
201 SwitchConfig config = switchManager.getSwitchConfig(node
203 if ((config != null) && (config.getProperty(Description.propertyName) != null)) {
204 nodeDesc = ((Description) config.getProperty(Description.propertyName)).getValue();
207 nodes.put(nodeDesc, flows.size());
213 @RequestMapping(value = "/flow", method = RequestMethod.POST)
215 public String actionFlow(@RequestParam(required = true) String action,
216 @RequestParam(required = false) String body,
217 @RequestParam(required = true) String nodeId,
218 HttpServletRequest request, @RequestParam(required = false) String container) {
219 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
221 // Authorization check
222 String userName = request.getUserPrincipal().getName();
223 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
224 return "Operation not authorized";
227 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
228 .getInstance(IForwardingRulesManager.class, containerName, this);
233 Gson gson = new Gson();
234 FlowConfig flow = gson.fromJson(body, FlowConfig.class);
235 Node node = Node.fromString(nodeId);
237 Status result = new Status(StatusCode.BADREQUEST, "Invalid request");
238 if (action.equals("add")) {
239 result = frm.addStaticFlow(flow);
242 return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result
246 @RequestMapping(value = "/flow/{nodeId}/{name:.*}", method = RequestMethod.POST)
248 public String removeFlow(@PathVariable("nodeId") String nodeId,
249 @PathVariable("name") String name,
250 @RequestParam(required = true) String action,
251 HttpServletRequest request, @RequestParam(required = false) String container) {
252 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
254 // Authorization check
255 String userName = request.getUserPrincipal().getName();
256 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
257 return "Operation not authorized";
260 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
261 .getInstance(IForwardingRulesManager.class, containerName, this);
266 Status result = null;
267 Node node = Node.fromString(nodeId);
271 if (action.equals("remove")) {
272 result = frm.removeStaticFlow(name, node);
273 } else if (action.equals("toggle")) {
274 result = frm.toggleStaticFlowStatus(name, node);
276 result = new Status(StatusCode.BADREQUEST, "Unknown action");
279 return (result.isSuccess()) ? StatusCode.SUCCESS.toString() : result
283 private String getNodeDesc(Node node, ISwitchManager switchManager) {
284 Description desc = (Description) switchManager.getNodeProp(node, Description.propertyName);
285 String description = (desc == null) ? "" : desc.getValue();
286 return (description.isEmpty() || description.equalsIgnoreCase("none")) ? node.toString() : description;