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.devices.web;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.Map.Entry;
17 import java.util.TreeMap;
18 import java.util.concurrent.ConcurrentMap;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
22 import org.codehaus.jackson.map.ObjectMapper;
23 import org.opendaylight.controller.usermanager.IUserManager;
24 import org.opendaylight.controller.web.IDaylightWeb;
25 import org.springframework.stereotype.Controller;
26 import org.springframework.web.bind.annotation.RequestMapping;
27 import org.springframework.web.bind.annotation.RequestMethod;
28 import org.springframework.web.bind.annotation.RequestParam;
29 import org.springframework.web.bind.annotation.ResponseBody;
30 import org.opendaylight.controller.forwarding.staticrouting.IForwardingStaticRouting;
31 import org.opendaylight.controller.forwarding.staticrouting.StaticRouteConfig;
32 import org.opendaylight.controller.sal.authorization.UserLevel;
33 import org.opendaylight.controller.sal.core.Config;
34 import org.opendaylight.controller.sal.core.Name;
35 import org.opendaylight.controller.sal.core.Node;
36 import org.opendaylight.controller.sal.core.NodeConnector;
37 import org.opendaylight.controller.sal.core.Tier;
38 import org.opendaylight.controller.sal.utils.GlobalConstants;
39 import org.opendaylight.controller.sal.utils.HexEncode;
40 import org.opendaylight.controller.sal.utils.ServiceHelper;
41 import org.opendaylight.controller.sal.utils.Status;
42 import org.opendaylight.controller.sal.utils.TierHelper;
43 import org.opendaylight.controller.switchmanager.ISwitchManager;
44 import org.opendaylight.controller.switchmanager.SpanConfig;
45 import org.opendaylight.controller.switchmanager.SubnetConfig;
46 import org.opendaylight.controller.switchmanager.Switch;
47 import org.opendaylight.controller.switchmanager.SwitchConfig;
49 import com.google.gson.Gson;
53 public class Devices implements IDaylightWeb {
54 private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER;
55 private final String WEB_NAME = "Devices";
56 private final String WEB_ID = "devices";
57 private final short WEB_ORDER = 1;
58 private final String containerName = GlobalConstants.DEFAULT.toString();
61 ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null);
65 public String getWebName() {
70 public String getWebId() {
75 public short getWebOrder() {
80 public boolean isAuthorized(UserLevel userLevel) {
81 return userLevel.ordinal() <= AUTH_LEVEL.ordinal();
84 @RequestMapping(value = "/nodesLearnt", method = RequestMethod.GET)
86 public DevicesJsonBean getNodesLearnt() {
87 Gson gson = new Gson();
88 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
89 .getInstance(ISwitchManager.class, containerName, this);
90 List<Map<String, String>> nodeData = new ArrayList<Map<String, String>>();
91 for (Switch device : switchManager.getNetworkDevices()) {
92 HashMap<String, String> nodeDatum = new HashMap<String, String>();
93 Node node = device.getNode();
94 Tier tier = (Tier) switchManager.getNodeProp(node,
97 nodeDatum.put("containerName", containerName);
98 nodeDatum.put("nodeName", switchManager.getNodeDescription(node));
99 nodeDatum.put("nodeId", node.toString());
100 int tierNumber = (tier == null) ? TierHelper.unknownTierNumber
102 nodeDatum.put("tierName", TierHelper.getTierName(tierNumber)
103 + " (Tier-" + tierNumber + ")");
104 nodeDatum.put("tier", tierNumber + "");
105 SwitchConfig sc = switchManager.getSwitchConfig(device.getNode()
107 String modeStr = (sc != null) ? sc.getMode() : "0";
108 nodeDatum.put("mode", modeStr);
110 nodeDatum.put("json", gson.toJson(nodeDatum));
112 HexEncode.bytesToHexString(device.getDataLayerAddress()));
113 StringBuffer sb1 = new StringBuffer();
114 Set<NodeConnector> nodeConnectorSet = device.getNodeConnectors();
115 if (nodeConnectorSet != null && nodeConnectorSet.size() > 0) {
116 Map<Short, String> portList = new HashMap<Short, String>();
117 for (NodeConnector nodeConnector : nodeConnectorSet) {
118 String nodeConnectorNumberToStr = nodeConnector.getID().toString();
119 Name ncName = ((Name) switchManager.getNodeConnectorProp(
120 nodeConnector, Name.NamePropName));
121 Config portStatus = ((Config) switchManager
122 .getNodeConnectorProp(nodeConnector,
123 Config.ConfigPropName));
125 String nodeConnectorName = (ncName != null) ? ncName.getValue()
127 nodeConnectorName += " ("+nodeConnector.getID()+")";
128 if (portStatus.getValue() == Config.ADMIN_UP) {
129 nodeConnectorName = "<span style='color:green;'>"+nodeConnectorName+"</span>";
130 } else if (portStatus.getValue() == Config.ADMIN_DOWN) {
131 nodeConnectorName = "<span style='color:red;'>"+nodeConnectorName+"</span>";
134 portList.put(Short.parseShort(nodeConnectorNumberToStr),
138 Map<Short, String> sortedPortList = new TreeMap<Short, String>(portList);
140 for (Entry<Short, String> e : sortedPortList.entrySet()) {
141 sb1.append(e.getValue());
145 nodeDatum.put("ports", sb1.toString());
146 nodeData.add(nodeDatum);
148 DevicesJsonBean result = new DevicesJsonBean();
149 result.setNodeData(nodeData);
150 List<String> columnNames = new ArrayList<String>();
151 columnNames.add("Node ID");
152 columnNames.add("Node Name");
153 columnNames.add("Tier");
154 columnNames.add("Mac Address");
155 columnNames.add("Ports");
156 columnNames.add("Port Status");
158 result.setColumnNames(columnNames);
162 @RequestMapping(value = "/tiers", method = RequestMethod.GET)
164 public List<String> getTiers() {
165 return TierHelper.getTiers();
168 @RequestMapping(value = "/nodesLearnt/update", method = RequestMethod.GET)
170 public StatusJsonBean updateLearntNode(
171 @RequestParam("nodeName") String nodeName,
172 @RequestParam("nodeId") String nodeId,
173 @RequestParam("tier") String tier,
174 @RequestParam("operationMode") String operationMode,
175 HttpServletRequest request) {
176 if (!authorize(UserLevel.NETWORKADMIN, request)) {
177 return unauthorizedMessage();
180 StatusJsonBean resultBean = new StatusJsonBean();
182 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
183 .getInstance(ISwitchManager.class, containerName, this);
184 SwitchConfig cfg = new SwitchConfig(nodeId, nodeName, tier,
186 switchManager.updateSwitchConfig(cfg);
187 resultBean.setStatus(true);
188 resultBean.setMessage("Updated node information successfully");
189 } catch (Exception e) {
190 resultBean.setStatus(false);
191 resultBean.setMessage("Error updating node information. "
197 @RequestMapping(value = "/staticRoutes", method = RequestMethod.GET)
199 public DevicesJsonBean getStaticRoutes() {
200 Gson gson = new Gson();
201 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
202 .getInstance(IForwardingStaticRouting.class, containerName,
204 List<Map<String, String>> staticRoutes = new ArrayList<Map<String, String>>();
205 ConcurrentMap<String, StaticRouteConfig> routeConfigs = staticRouting
206 .getStaticRouteConfigs();
207 if (routeConfigs == null) {
210 for (StaticRouteConfig conf : routeConfigs.values()) {
211 Map<String, String> staticRoute = new HashMap<String, String>();
212 staticRoute.put("name", conf.getName());
213 staticRoute.put("staticRoute", conf.getStaticRoute());
214 staticRoute.put("nextHopType", conf.getNextHopType());
215 staticRoute.put("nextHop", conf.getNextHop());
216 staticRoute.put("json", gson.toJson(conf));
217 staticRoutes.add(staticRoute);
219 DevicesJsonBean result = new DevicesJsonBean();
220 result.setColumnNames(StaticRouteConfig.getGuiFieldsNames());
221 result.setNodeData(staticRoutes);
225 @RequestMapping(value = "/staticRoute/add", method = RequestMethod.GET)
227 public StatusJsonBean addStaticRoute(
228 @RequestParam("routeName") String routeName,
229 @RequestParam("staticRoute") String staticRoute,
230 @RequestParam("nextHop") String nextHop, HttpServletRequest request) {
231 if (!authorize(UserLevel.NETWORKADMIN, request)) {
232 return unauthorizedMessage();
235 StatusJsonBean result = new StatusJsonBean();
237 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
238 .getInstance(IForwardingStaticRouting.class, containerName,
240 StaticRouteConfig config = new StaticRouteConfig();
241 config.setName(routeName);
242 config.setStaticRoute(staticRoute);
243 config.setNextHop(nextHop);
244 Status addStaticRouteResult = staticRouting.addStaticRoute(config);
245 if (addStaticRouteResult.isSuccess()) {
246 result.setStatus(true);
247 result.setMessage("Static Route saved successfully");
249 result.setStatus(false);
250 result.setMessage(addStaticRouteResult.getDescription());
252 } catch (Exception e) {
253 result.setStatus(false);
254 result.setMessage("Error - " + e.getMessage());
259 @RequestMapping(value = "/staticRoute/delete", method = RequestMethod.GET)
261 public StatusJsonBean deleteStaticRoute(
262 @RequestParam("routesToDelete") String routesToDelete,
263 HttpServletRequest request) {
264 if (!authorize(UserLevel.NETWORKADMIN, request)) {
265 return unauthorizedMessage();
268 StatusJsonBean resultBean = new StatusJsonBean();
270 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
271 .getInstance(IForwardingStaticRouting.class, containerName,
273 String[] routes = routesToDelete.split(",");
275 resultBean.setStatus(true);
277 .setMessage("Successfully deleted selected static routes");
278 for (String route : routes) {
279 result = staticRouting.removeStaticRoute(route);
280 if (!result.isSuccess()) {
281 resultBean.setStatus(false);
282 resultBean.setMessage(result.getDescription());
286 } catch (Exception e) {
287 resultBean.setStatus(false);
289 .setMessage("Error occurred while deleting static routes. "
295 @RequestMapping(value = "/subnets", method = RequestMethod.GET)
297 public DevicesJsonBean getSubnetGateways() {
298 Gson gson = new Gson();
299 List<Map<String, String>> subnets = new ArrayList<Map<String, String>>();
300 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
301 .getInstance(ISwitchManager.class, containerName, this);
302 for (SubnetConfig conf : switchManager.getSubnetsConfigList()) {
303 Map<String, String> subnet = new HashMap<String, String>();
304 subnet.put("name", conf.getName());
305 subnet.put("subnet", conf.getSubnet());
306 subnet.put("json", gson.toJson(conf));
309 DevicesJsonBean result = new DevicesJsonBean();
310 result.setColumnNames(SubnetConfig.getGuiFieldsNames());
311 result.setNodeData(subnets);
315 @RequestMapping(value = "/subnetGateway/add", method = RequestMethod.GET)
317 public StatusJsonBean addSubnetGateways(
318 @RequestParam("gatewayName") String gatewayName,
319 @RequestParam("gatewayIPAddress") String gatewayIPAddress,
320 HttpServletRequest request) {
321 if (!authorize(UserLevel.NETWORKADMIN, request)) {
322 return unauthorizedMessage();
325 StatusJsonBean resultBean = new StatusJsonBean();
327 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
328 .getInstance(ISwitchManager.class, containerName, this);
329 SubnetConfig cfgObject = new SubnetConfig(gatewayName,
330 gatewayIPAddress, new ArrayList<String>());
331 Status result = switchManager.addSubnet(cfgObject);
332 if (result.isSuccess()) {
333 resultBean.setStatus(true);
334 resultBean.setMessage("Added gateway address successfully");
336 resultBean.setStatus(false);
337 resultBean.setMessage(result.getDescription());
339 } catch (Exception e) {
340 resultBean.setStatus(false);
341 resultBean.setMessage(e.getMessage());
346 @RequestMapping(value = "/subnetGateway/delete", method = RequestMethod.GET)
348 public StatusJsonBean deleteSubnetGateways(
349 @RequestParam("gatewaysToDelete") String gatewaysToDelete,
350 HttpServletRequest request) {
351 if (!authorize(UserLevel.NETWORKADMIN, request)) {
352 return unauthorizedMessage();
355 StatusJsonBean resultBean = new StatusJsonBean();
357 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
358 .getInstance(ISwitchManager.class, containerName, this);
359 String[] subnets = gatewaysToDelete.split(",");
360 resultBean.setStatus(true);
361 resultBean.setMessage("Added gateway address successfully");
362 for (String subnet : subnets) {
363 Status result = switchManager.removeSubnet(subnet);
364 if (!result.isSuccess()) {
365 resultBean.setStatus(false);
366 resultBean.setMessage(result.getDescription());
370 } catch (Exception e) {
371 resultBean.setStatus(false);
372 resultBean.setMessage(e.getMessage());
377 @RequestMapping(value = "/subnetGateway/ports/add", method = RequestMethod.GET)
379 public StatusJsonBean addSubnetGatewayPort(
380 @RequestParam("portsName") String portsName,
381 @RequestParam("ports") String ports,
382 @RequestParam("nodeId") String nodeId, HttpServletRequest request) {
383 if (!authorize(UserLevel.NETWORKADMIN, request)) {
384 return unauthorizedMessage();
387 StatusJsonBean resultBean = new StatusJsonBean();
389 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
390 .getInstance(ISwitchManager.class, containerName, this);
391 Status result = switchManager.addPortsToSubnet(portsName, nodeId
394 if (result.isSuccess()) {
395 resultBean.setStatus(true);
397 .setMessage("Added ports to subnet gateway address successfully");
399 resultBean.setStatus(false);
400 resultBean.setMessage(result.getDescription());
402 } catch (Exception e) {
403 resultBean.setStatus(false);
404 resultBean.setMessage(e.getMessage());
409 @RequestMapping(value = "/subnetGateway/ports/delete", method = RequestMethod.GET)
411 public StatusJsonBean deleteSubnetGatewayPort(
412 @RequestParam("gatewayName") String gatewayName,
413 @RequestParam("nodePort") String nodePort,
414 HttpServletRequest request) {
415 if (!authorize(UserLevel.NETWORKADMIN, request)) {
416 return unauthorizedMessage();
419 StatusJsonBean resultBean = new StatusJsonBean();
421 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
422 .getInstance(ISwitchManager.class, containerName, this);
423 Status result = switchManager.removePortsFromSubnet(gatewayName,
426 if (result.isSuccess()) {
427 resultBean.setStatus(true);
429 .setMessage("Deleted port from subnet gateway address successfully");
431 resultBean.setStatus(false);
432 resultBean.setMessage(result.getDescription());
434 } catch (Exception e) {
435 resultBean.setStatus(false);
436 resultBean.setMessage(e.getMessage());
441 @RequestMapping(value = "/spanPorts", method = RequestMethod.GET)
443 public DevicesJsonBean getSpanPorts() {
444 Gson gson = new Gson();
445 List<String> spanConfigs_json = new ArrayList<String>();
446 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
447 .getInstance(ISwitchManager.class, containerName, this);
448 for (SpanConfig conf : switchManager.getSpanConfigList()) {
449 spanConfigs_json.add(gson.toJson(conf));
451 ObjectMapper mapper = new ObjectMapper();
452 List<Map<String, String>> spanConfigs = new ArrayList<Map<String, String>>();
453 for (String config_json : spanConfigs_json) {
455 @SuppressWarnings("unchecked")
456 Map<String, String> config_data = mapper.readValue(config_json,
458 Map<String, String> config = new HashMap<String, String>();
459 for (String name : config_data.keySet()) {
460 config.put(name, config_data.get(name));
461 // Add switch name value (non-configuration field)
462 config.put("nodeName",
463 getNodeDesc(config_data.get("nodeId")));
465 config.put("json", config_json);
466 spanConfigs.add(config);
467 } catch (Exception e) {
468 // TODO: Handle the exception.
471 DevicesJsonBean result = new DevicesJsonBean();
472 result.setColumnNames(SpanConfig.getGuiFieldsNames());
473 result.setNodeData(spanConfigs);
477 @RequestMapping(value = "/nodeports")
479 public Map<String, Object> getNodePorts() {
480 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
481 .getInstance(ISwitchManager.class, containerName, this);
482 if (switchManager == null)
485 Map<String, Object> nodes = new HashMap<String, Object>();
486 Map<Short, String> port;
488 for (Switch node : switchManager.getNetworkDevices()) {
489 port = new HashMap<Short, String>(); // new port
490 Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
492 if (nodeConnectorSet != null)
493 for (NodeConnector nodeConnector : nodeConnectorSet) {
494 String nodeConnectorName = ((Name) switchManager
495 .getNodeConnectorProp(nodeConnector,
496 Name.NamePropName)).getValue();
497 port.put((Short) nodeConnector.getID(), nodeConnectorName
498 + "(" + nodeConnector.getID() + ")");
501 nodes.put(node.getNode().toString(), port);
507 @RequestMapping(value = "/spanPorts/add", method = RequestMethod.GET)
509 public StatusJsonBean addSpanPort(
510 @RequestParam("jsonData") String jsonData,
511 HttpServletRequest request) {
512 if (!authorize(UserLevel.NETWORKADMIN, request)) {
513 return unauthorizedMessage();
516 StatusJsonBean resultBean = new StatusJsonBean();
518 Gson gson = new Gson();
519 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
520 .getInstance(ISwitchManager.class, containerName, this);
521 SpanConfig cfgObject = gson.fromJson(jsonData, SpanConfig.class);
522 Status result = switchManager.addSpanConfig(cfgObject);
523 if (result.isSuccess()) {
524 resultBean.setStatus(true);
525 resultBean.setMessage("SPAN Port added successfully");
527 resultBean.setStatus(false);
528 resultBean.setMessage(result.getDescription());
530 } catch (Exception e) {
531 resultBean.setStatus(false);
532 resultBean.setMessage("Error occurred while adding span port. "
538 @RequestMapping(value = "/spanPorts/delete", method = RequestMethod.GET)
540 public StatusJsonBean deleteSpanPorts(
541 @RequestParam("spanPortsToDelete") String spanPortsToDelete,
542 HttpServletRequest request) {
543 if (!authorize(UserLevel.NETWORKADMIN, request)) {
544 return unauthorizedMessage();
547 StatusJsonBean resultBean = new StatusJsonBean();
549 Gson gson = new Gson();
550 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
551 .getInstance(ISwitchManager.class, containerName, this);
552 String[] spans = spanPortsToDelete.split("###");
553 resultBean.setStatus(true);
554 resultBean.setMessage("SPAN Port(s) deleted successfully");
555 for (String span : spans) {
556 if (!span.isEmpty()) {
557 SpanConfig cfgObject = gson
558 .fromJson(span, SpanConfig.class);
559 Status result = switchManager.removeSpanConfig(cfgObject);
560 if (!result.isSuccess()) {
561 resultBean.setStatus(false);
562 resultBean.setMessage(result.getDescription());
567 } catch (Exception e) {
568 resultBean.setStatus(false);
569 resultBean.setMessage("Error occurred while deleting span port. "
575 private String getNodeDesc(String nodeId) {
576 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
577 .getInstance(ISwitchManager.class, containerName, this);
578 String description = "";
579 if (switchManager != null) {
580 description = switchManager.getNodeDescription(Node
581 .fromString(nodeId));
583 return (description.isEmpty() || description.equalsIgnoreCase("none")) ? nodeId
588 * Is the operation permitted for the given level
592 private boolean authorize(UserLevel level, HttpServletRequest request) {
593 IUserManager userManager = (IUserManager) ServiceHelper
594 .getGlobalInstance(IUserManager.class, this);
595 if (userManager == null) {
599 String username = request.getUserPrincipal().getName();
600 UserLevel userLevel = userManager.getUserLevel(username);
601 if (userLevel.toNumber() <= level.toNumber()) {
607 private StatusJsonBean unauthorizedMessage() {
608 StatusJsonBean message = new StatusJsonBean();
609 message.setStatus(false);
610 message.setMessage("Operation not authorized");
614 @RequestMapping(value = "login")
615 public String login(final HttpServletRequest request,
616 final HttpServletResponse response) {
617 // response.setHeader("X-Page-Location", "/login");
619 * IUserManager userManager = (IUserManager) ServiceHelper
620 * .getGlobalInstance(IUserManager.class, this); if (userManager ==
621 * null) { return "User Manager is not available"; }
623 * String username = request.getUserPrincipal().getName();
626 * model.addAttribute("username", username); model.addAttribute("role",
627 * userManager.getUserLevel(username).toNumber());
629 return "forward:" + "/";