3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.flows.web;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
18 import org.opendaylight.controller.forwardingrulesmanager.FlowConfig;
19 import org.opendaylight.controller.forwardingrulesmanager.IForwardingRulesManager;
20 import org.opendaylight.controller.sal.authorization.UserLevel;
21 import org.opendaylight.controller.sal.core.Name;
22 import org.opendaylight.controller.sal.core.Node;
23 import org.opendaylight.controller.sal.core.NodeConnector;
24 import org.opendaylight.controller.sal.utils.ServiceHelper;
25 import org.opendaylight.controller.sal.utils.Status;
26 import org.opendaylight.controller.sal.utils.StatusCode;
27 import org.opendaylight.controller.switchmanager.ISwitchManager;
28 import org.opendaylight.controller.switchmanager.Switch;
29 import org.opendaylight.controller.switchmanager.SwitchConfig;
30 import org.opendaylight.controller.usermanager.IUserManager;
31 import org.opendaylight.controller.web.IOneWeb;
32 import org.springframework.security.core.context.SecurityContextHolder;
33 import org.springframework.stereotype.Controller;
34 import org.springframework.web.bind.annotation.PathVariable;
35 import org.springframework.web.bind.annotation.RequestMapping;
36 import org.springframework.web.bind.annotation.RequestMethod;
37 import org.springframework.web.bind.annotation.RequestParam;
38 import org.springframework.web.bind.annotation.ResponseBody;
40 import com.google.gson.Gson;
44 public class Flows implements IOneWeb {
45 private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER;
46 private final String WEB_NAME = "Flows";
47 private final String WEB_ID = "flows";
48 private final short WEB_ORDER = 2;
51 ServiceHelper.registerGlobalService(IOneWeb.class, this, null);
55 public String getWebName() {
60 public String getWebId() {
65 public short getWebOrder() {
70 public boolean isAuthorized(UserLevel userLevel) {
71 return userLevel.ordinal() <= AUTH_LEVEL.ordinal();
74 @RequestMapping(value = "/main")
76 public Set<Map<String, Object>> getFlows() {
78 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
79 .getInstance(IForwardingRulesManager.class, "default", this);
84 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
85 .getInstance(ISwitchManager.class, "default", this);
86 if (switchManager == null)
89 // get static flow list
90 List<FlowConfig> staticFlowList = frm.getStaticFlows();
91 Set<Map<String, Object>> output = new HashSet<Map<String, Object>>();
92 for (FlowConfig flowConfig : staticFlowList) {
93 Map<String, Object> entry = new HashMap<String, Object>();
94 entry.put("flow", flowConfig);
95 entry.put("name", flowConfig.getName());
97 Node node = flowConfig.getNode();
98 SwitchConfig switchConfig = switchManager.getSwitchConfig(node.getNodeIDString());
99 String nodeName = node.toString();
100 if (switchConfig != null) { nodeName = switchConfig.getNodeName(); }
101 entry.put("node", nodeName);
102 entry.put("nodeId", node.toString());
109 @RequestMapping(value = "/node-ports")
111 public Map<String, Object> getNodePorts() {
112 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
113 .getInstance(ISwitchManager.class, "default", this);
114 if (switchManager == null)
117 Map<String, Object> nodes = new HashMap<String, Object>();
118 Map<Short, String> port;
120 for (Switch node : switchManager.getNetworkDevices()) {
121 port = new HashMap<Short, String>(); // new port
122 Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
124 if (nodeConnectorSet != null)
125 for (NodeConnector nodeConnector : nodeConnectorSet) {
126 String nodeConnectorName = ((Name) switchManager
127 .getNodeConnectorProp(nodeConnector,
128 Name.NamePropName)).getValue();
129 port.put((Short) nodeConnector.getID(),
130 nodeConnectorName + "("
131 + nodeConnector.getNodeConnectorIDString() + ")");
135 Map<String, Object> entry = new HashMap<String, Object>();
136 entry.put("ports", port);
139 String nodeName = node.getNode().toString();
140 SwitchConfig config = switchManager.getSwitchConfig(node.getNode().getNodeIDString());
141 if (config != null) {
142 nodeName = config.getNodeName();
144 entry.put("name", nodeName);
147 nodes.put(node.getNode().toString(), entry);
153 @RequestMapping(value = "/node-flows")
155 public Map<String, Object> getNodeFlows() {
156 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
157 .getInstance(ISwitchManager.class, "default", this);
158 if (switchManager == null) { return null; }
159 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
160 .getInstance(IForwardingRulesManager.class, "default", this);
161 if (frm == null) { return null; }
163 Map<String, Object> nodes = new HashMap<String, Object>();
165 for (Switch sw : switchManager.getNetworkDevices()) {
166 Node node = sw.getNode();
168 List<FlowConfig> flows = frm.getStaticFlows(node);
170 String nodeName = node.toString();
171 SwitchConfig config = switchManager.getSwitchConfig(node.getNodeIDString());
172 if (config != null) {
173 nodeName = config.getNodeName();
176 nodes.put(nodeName, flows.size());
182 @RequestMapping(value = "/flow", method = RequestMethod.POST)
184 public String actionFlow(@RequestParam(required = true) String action,
185 @RequestParam(required = false) String body, @RequestParam(required = true) String nodeId) {
186 if (!authorize(UserLevel.NETWORKADMIN)) {
187 return "Operation not authorized";
190 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
191 .getInstance(IForwardingRulesManager.class, "default", this);
192 if (frm == null) { return null; }
194 Gson gson = new Gson();
195 FlowConfig flow = gson.fromJson(body, FlowConfig.class);
196 Node node = Node.fromString(nodeId);
198 Status result = null;
199 if (action.equals("add")) {
200 result = frm.addStaticFlow(flow, false);
203 return result.getDescription();
206 @RequestMapping(value = "/flow/{nodeId}/{name}", method = RequestMethod.POST)
208 public String removeFlow(@PathVariable("nodeId") String nodeId, @PathVariable("name") String name,
209 @RequestParam(required = true) String action) {
210 if (!authorize(UserLevel.NETWORKADMIN)) { return "Operation not authorized"; }
212 IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
213 .getInstance(IForwardingRulesManager.class, "default", this);
214 if (frm == null) { return null; }
216 Status result = null;
217 Node node = Node.fromString(nodeId);
221 if (action.equals("remove")) {
222 result = frm.removeStaticFlow(name, node);
223 } else if (action.equals("toggle")) {
224 FlowConfig config = frm.getStaticFlow(name, node);
225 result = frm.toggleStaticFlowStatus(config);
227 result = new Status(StatusCode.BADREQUEST, "Unknown action");
230 return result.getDescription();
234 * Is the operation permitted for the given level
238 private boolean authorize(UserLevel level) {
239 IUserManager userManager = (IUserManager) ServiceHelper
240 .getGlobalInstance(IUserManager.class, this);
241 if (userManager == null) {
245 String username = SecurityContextHolder.getContext().getAuthentication().getName();
246 UserLevel userLevel = userManager.getUserLevel(username);
247 if (userLevel.toNumber() <= level.toNumber()) {