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.lang.reflect.Type;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.List;
17 import java.util.Map.Entry;
19 import java.util.TreeMap;
20 import java.util.concurrent.ConcurrentMap;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
25 import org.codehaus.jackson.map.ObjectMapper;
26 import org.opendaylight.controller.forwarding.staticrouting.IForwardingStaticRouting;
27 import org.opendaylight.controller.forwarding.staticrouting.StaticRouteConfig;
28 import org.opendaylight.controller.sal.authorization.Privilege;
29 import org.opendaylight.controller.sal.authorization.UserLevel;
30 import org.opendaylight.controller.sal.core.Config;
31 import org.opendaylight.controller.sal.core.Description;
32 import org.opendaylight.controller.sal.core.ForwardingMode;
33 import org.opendaylight.controller.sal.core.Name;
34 import org.opendaylight.controller.sal.core.Node;
35 import org.opendaylight.controller.sal.core.NodeConnector;
36 import org.opendaylight.controller.sal.core.Property;
37 import org.opendaylight.controller.sal.core.State;
38 import org.opendaylight.controller.sal.core.Tier;
39 import org.opendaylight.controller.sal.utils.GlobalConstants;
40 import org.opendaylight.controller.sal.utils.HexEncode;
41 import org.opendaylight.controller.sal.utils.ServiceHelper;
42 import org.opendaylight.controller.sal.utils.Status;
43 import org.opendaylight.controller.sal.utils.TierHelper;
44 import org.opendaylight.controller.switchmanager.ISwitchManager;
45 import org.opendaylight.controller.switchmanager.SpanConfig;
46 import org.opendaylight.controller.switchmanager.SubnetConfig;
47 import org.opendaylight.controller.switchmanager.Switch;
48 import org.opendaylight.controller.switchmanager.SwitchConfig;
49 import org.opendaylight.controller.web.DaylightWebUtil;
50 import org.opendaylight.controller.web.IDaylightWeb;
51 import org.springframework.stereotype.Controller;
52 import org.springframework.web.bind.annotation.RequestMapping;
53 import org.springframework.web.bind.annotation.RequestMethod;
54 import org.springframework.web.bind.annotation.RequestParam;
55 import org.springframework.web.bind.annotation.ResponseBody;
57 import com.google.gson.Gson;
58 import com.google.gson.reflect.TypeToken;
62 public class Devices implements IDaylightWeb {
63 private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER;
64 private static final String WEB_NAME = "Devices";
65 private static final String WEB_ID = "devices";
66 private static final short WEB_ORDER = 1;
69 ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null);
73 public String getWebName() {
78 public String getWebId() {
83 public short getWebOrder() {
88 public boolean isAuthorized(UserLevel userLevel) {
89 return userLevel.ordinal() <= AUTH_LEVEL.ordinal();
92 @RequestMapping(value = "/nodesLearnt", method = RequestMethod.GET)
94 public DevicesJsonBean getNodesLearnt(HttpServletRequest request,
95 @RequestParam(required = false) String container) {
96 Gson gson = new Gson();
97 String containerName = (container == null) ? GlobalConstants.DEFAULT
98 .toString() : container;
100 // Derive the privilege this user has on the current container
101 String userName = request.getUserPrincipal().getName();
102 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
104 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
106 List<Map<String, String>> nodeData = new ArrayList<Map<String, String>>();
107 if (switchManager != null && privilege != Privilege.NONE) {
108 for (Switch device : switchManager.getNetworkDevices()) {
109 HashMap<String, String> nodeDatum = new HashMap<String, String>();
110 Node node = device.getNode();
111 Tier tier = (Tier) switchManager.getNodeProp(node, Tier.TierPropName);
112 nodeDatum.put("containerName", containerName);
113 Description description = (Description) switchManager.getNodeProp(node, Description.propertyName);
114 String desc = (description == null) ? "" : description.getValue();
115 nodeDatum.put("nodeName", desc);
116 nodeDatum.put("nodeId", node.toString());
117 int tierNumber = (tier == null) ? TierHelper.unknownTierNumber : tier.getValue();
118 nodeDatum.put("tierName", TierHelper.getTierName(tierNumber) + " (Tier-" + tierNumber + ")");
119 nodeDatum.put("tier", tierNumber + "");
120 String modeStr = "0";
121 ForwardingMode mode = null;
122 if (!containerName.equals(GlobalConstants.DEFAULT.toString())) {
123 ISwitchManager switchManagerDefault = (ISwitchManager) ServiceHelper.getInstance(
124 ISwitchManager.class, GlobalConstants.DEFAULT.toString(), this);
125 mode = (ForwardingMode) switchManagerDefault.getNodeProp(node, ForwardingMode.name);
127 mode = (ForwardingMode) switchManager.getNodeProp(node, ForwardingMode.name);
130 modeStr = String.valueOf(mode.getValue());
132 nodeDatum.put("mode", modeStr);
134 nodeDatum.put("json", gson.toJson(nodeDatum));
135 nodeDatum.put("mac", HexEncode.bytesToHexString(device.getDataLayerAddress()));
136 StringBuffer sb1 = new StringBuffer();
137 Set<NodeConnector> nodeConnectorSet = device.getNodeConnectors();
138 if (nodeConnectorSet != null && nodeConnectorSet.size() > 0) {
139 Map<Short, String> portList = new HashMap<Short, String>();
140 List<String> intfList = new ArrayList<String>();
141 for (NodeConnector nodeConnector : nodeConnectorSet) {
142 String nodeConnectorNumberToStr = nodeConnector.getID().toString();
143 Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector, Name.NamePropName));
144 Config portStatus = ((Config) switchManager
145 .getNodeConnectorProp(nodeConnector,
146 Config.ConfigPropName));
147 State portState = ((State) switchManager
148 .getNodeConnectorProp(nodeConnector,
149 State.StatePropName));
150 String nodeConnectorName = (ncName != null) ? ncName
152 nodeConnectorName += " (" + nodeConnector.getID() + ")";
154 if (portStatus != null) {
155 if (portStatus.getValue() == Config.ADMIN_UP) {
156 if (portState.getValue() == State.EDGE_UP) {
157 nodeConnectorName = "<span class='admin-up'>"
158 + nodeConnectorName + "</span>";
159 } else if (portState.getValue() == State.EDGE_DOWN) {
160 nodeConnectorName = "<span class='edge-down'>"
161 + nodeConnectorName + "</span>";
163 } else if (portStatus.getValue() == Config.ADMIN_DOWN) {
164 nodeConnectorName = "<span class='admin-down'>"
165 + nodeConnectorName + "</span>";
169 Class<?> idClass = nodeConnector.getID().getClass();
170 if (idClass.equals(Short.class)) {
172 Short.parseShort(nodeConnectorNumberToStr),
175 intfList.add(nodeConnectorName);
179 if (portList.size() > 0) {
180 Map<Short, String> sortedPortList = new TreeMap<Short, String>(
183 for (Entry<Short, String> e : sortedPortList.entrySet()) {
184 sb1.append(e.getValue());
187 } else if (intfList.size() > 0) {
188 for (String intf : intfList) {
194 nodeDatum.put("ports", sb1.toString());
195 nodeData.add(nodeDatum);
199 DevicesJsonBean result = new DevicesJsonBean();
200 result.setNodeData(nodeData);
201 result.setPrivilege(privilege);
202 List<String> columnNames = new ArrayList<String>();
203 columnNames.add("Node ID");
204 columnNames.add("Node Name");
205 columnNames.add("Tier");
206 columnNames.add("Mac Address");
207 columnNames.add("Ports");
208 columnNames.add("Port Status");
210 result.setColumnNames(columnNames);
214 @RequestMapping(value = "/tiers", method = RequestMethod.GET)
216 public List<String> getTiers() {
217 return TierHelper.getTiers();
220 @RequestMapping(value = "/nodesLearnt/update", method = RequestMethod.GET)
222 public StatusJsonBean updateLearntNode(
223 @RequestParam("nodeName") String nodeName,
224 @RequestParam("nodeId") String nodeId,
225 @RequestParam("tier") String tier,
226 @RequestParam("operationMode") String operationMode,
227 HttpServletRequest request,
228 @RequestParam(required = false) String container) {
229 String containerName = (container == null) ? GlobalConstants.DEFAULT
230 .toString() : container;
232 // Authorization check
233 String userName = request.getUserPrincipal().getName();
235 .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
236 return unauthorizedMessage();
239 StatusJsonBean resultBean = new StatusJsonBean();
241 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
242 .getInstance(ISwitchManager.class, containerName, this);
243 Map<String, Property> nodeProperties = new HashMap<String, Property>();
244 Property desc = new Description(nodeName);
245 nodeProperties.put(desc.getName(), desc);
246 Property nodeTier = new Tier(Integer.parseInt(tier));
247 nodeProperties.put(nodeTier.getName(), nodeTier);
248 if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
249 Property mode = new ForwardingMode(Integer.parseInt(operationMode));
250 nodeProperties.put(mode.getName(), mode);
252 SwitchConfig cfg = new SwitchConfig(nodeId, nodeProperties);
253 Status result = switchManager.updateNodeConfig(cfg);
254 if (!result.isSuccess()) {
255 resultBean.setStatus(false);
256 resultBean.setMessage(result.getDescription());
258 resultBean.setStatus(true);
259 resultBean.setMessage("Updated node information successfully");
260 DaylightWebUtil.auditlog("Node", userName, "updated", nodeId + " to "+ nodeName, containerName);
262 } catch (Exception e) {
263 resultBean.setStatus(false);
264 resultBean.setMessage("Error updating node information. "
270 @RequestMapping(value = "/staticRoutes", method = RequestMethod.GET)
272 public DevicesJsonBean getStaticRoutes(HttpServletRequest request,
273 @RequestParam(required = false) String container) {
274 Gson gson = new Gson();
275 String containerName = (container == null) ? GlobalConstants.DEFAULT
276 .toString() : container;
278 // Derive the privilege this user has on the current container
279 String userName = request.getUserPrincipal().getName();
280 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
282 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
283 .getInstance(IForwardingStaticRouting.class, containerName,
285 if (staticRouting == null) {
288 List<Map<String, String>> staticRoutes = new ArrayList<Map<String, String>>();
289 ConcurrentMap<String, StaticRouteConfig> routeConfigs = staticRouting
290 .getStaticRouteConfigs();
291 if (routeConfigs == null) {
294 if (privilege != Privilege.NONE) {
295 for (StaticRouteConfig conf : routeConfigs.values()) {
296 Map<String, String> staticRoute = new HashMap<String, String>();
297 staticRoute.put("name", conf.getName());
298 staticRoute.put("staticRoute", conf.getStaticRoute());
299 staticRoute.put("nextHopType", conf.getNextHopType());
300 staticRoute.put("nextHop", conf.getNextHop());
301 staticRoute.put("json", gson.toJson(conf));
302 staticRoutes.add(staticRoute);
305 DevicesJsonBean result = new DevicesJsonBean();
306 result.setPrivilege(privilege);
307 result.setColumnNames(StaticRouteConfig.getGuiFieldsNames());
308 result.setNodeData(staticRoutes);
312 @RequestMapping(value = "/staticRoute/add", method = RequestMethod.GET)
314 public StatusJsonBean addStaticRoute(
315 @RequestParam("routeName") String routeName,
316 @RequestParam("staticRoute") String staticRoute,
317 @RequestParam("nextHop") String nextHop,
318 HttpServletRequest request,
319 @RequestParam(required = false) String container) {
320 String containerName = (container == null) ? GlobalConstants.DEFAULT
321 .toString() : container;
323 // Authorization check
324 String userName = request.getUserPrincipal().getName();
326 .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
327 return unauthorizedMessage();
330 StatusJsonBean result = new StatusJsonBean();
332 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
333 .getInstance(IForwardingStaticRouting.class, containerName,
335 StaticRouteConfig config = new StaticRouteConfig();
336 config.setName(routeName);
337 config.setStaticRoute(staticRoute);
338 config.setNextHop(nextHop);
339 Status addStaticRouteResult = staticRouting.addStaticRoute(config);
340 if (addStaticRouteResult.isSuccess()) {
341 result.setStatus(true);
342 result.setMessage("Static Route saved successfully");
343 DaylightWebUtil.auditlog("Static Route", userName, "added", routeName, containerName);
345 result.setStatus(false);
346 result.setMessage(addStaticRouteResult.getDescription());
348 } catch (Exception e) {
349 result.setStatus(false);
350 result.setMessage("Error - " + e.getMessage());
355 @RequestMapping(value = "/staticRoute/delete", method = RequestMethod.GET)
357 public StatusJsonBean deleteStaticRoute(
358 @RequestParam("routesToDelete") String routesToDelete,
359 HttpServletRequest request,
360 @RequestParam(required = false) String container) {
361 String containerName = (container == null) ? GlobalConstants.DEFAULT
362 .toString() : container;
364 // Authorization check
365 String userName = request.getUserPrincipal().getName();
366 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
367 return unauthorizedMessage();
370 StatusJsonBean resultBean = new StatusJsonBean();
372 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
373 .getInstance(IForwardingStaticRouting.class, containerName,
375 String[] routes = routesToDelete.split(",");
377 resultBean.setStatus(true);
379 .setMessage("Successfully deleted selected static routes");
380 for (String route : routes) {
381 result = staticRouting.removeStaticRoute(route);
382 if (!result.isSuccess()) {
383 resultBean.setStatus(false);
384 resultBean.setMessage(result.getDescription());
387 DaylightWebUtil.auditlog("Static Route", userName, "removed", route, containerName);
389 } catch (Exception e) {
390 resultBean.setStatus(false);
392 .setMessage("Error occurred while deleting static routes. "
398 @RequestMapping(value = "/subnets", method = RequestMethod.GET)
400 public DevicesJsonBean getSubnetGateways(HttpServletRequest request,
401 @RequestParam(required = false) String container) {
402 Gson gson = new Gson();
403 List<Map<String, String>> subnets = new ArrayList<Map<String, String>>();
404 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
406 // Derive the privilege this user has on the current container
407 String userName = request.getUserPrincipal().getName();
408 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
410 if (privilege != Privilege.NONE) {
411 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
412 containerName, this);
413 if (switchManager != null) {
414 for (SubnetConfig conf : switchManager.getSubnetsConfigList()) {
415 Map<String, String> subnet = new HashMap<String, String>();
416 subnet.put("name", conf.getName());
417 subnet.put("subnet", conf.getSubnet());
418 List<SubnetGatewayPortBean> portsList = new ArrayList<SubnetGatewayPortBean>();
419 Iterator<NodeConnector> itor = conf.getNodeConnectors().iterator();
420 while(itor.hasNext()) {
421 SubnetGatewayPortBean bean = new SubnetGatewayPortBean();
422 NodeConnector nodeConnector = itor.next();
423 String nodeName = getNodeDesc(nodeConnector.getNode().toString(), containerName);
424 Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector, Name.NamePropName));
425 String nodeConnectorName = (ncName != null) ? ncName.getValue() : "";
426 bean.setNodeName(nodeName);
427 bean.setNodePortName(nodeConnectorName);
428 bean.setNodeId(nodeConnector.getNode().toString());
429 bean.setNodePortId(nodeConnector.toString());
432 subnet.put("nodePorts", gson.toJson(portsList));
437 DevicesJsonBean result = new DevicesJsonBean();
438 result.setPrivilege(privilege);
439 result.setColumnNames(SubnetConfig.getGuiFieldsNames());
440 result.setNodeData(subnets);
444 @RequestMapping(value = "/subnetGateway/add", method = RequestMethod.GET)
446 public StatusJsonBean addSubnetGateways(
447 @RequestParam("gatewayName") String gatewayName,
448 @RequestParam("gatewayIPAddress") String gatewayIPAddress,
449 HttpServletRequest request,
450 @RequestParam(required = false) String container) {
451 String containerName = (container == null) ? GlobalConstants.DEFAULT
452 .toString() : container;
454 // Authorization check
455 String userName = request.getUserPrincipal().getName();
456 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
457 return unauthorizedMessage();
460 StatusJsonBean resultBean = new StatusJsonBean();
462 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
463 .getInstance(ISwitchManager.class, containerName, this);
464 SubnetConfig cfgObject = new SubnetConfig(gatewayName,
465 gatewayIPAddress, new ArrayList<String>());
466 Status result = switchManager.addSubnet(cfgObject);
467 if (result.isSuccess()) {
468 resultBean.setStatus(true);
469 resultBean.setMessage("Added gateway address successfully");
470 DaylightWebUtil.auditlog("Subnet Gateway", userName, "added", gatewayName, containerName);
472 resultBean.setStatus(false);
473 resultBean.setMessage(result.getDescription());
475 } catch (Exception e) {
476 resultBean.setStatus(false);
477 resultBean.setMessage(e.getMessage());
482 @RequestMapping(value = "/subnetGateway/delete", method = RequestMethod.GET)
484 public StatusJsonBean deleteSubnetGateways(
485 @RequestParam("gatewaysToDelete") String gatewaysToDelete,
486 HttpServletRequest request,
487 @RequestParam(required = false) String container) {
488 String containerName = (container == null) ? GlobalConstants.DEFAULT
489 .toString() : container;
491 // Authorization check
492 String userName = request.getUserPrincipal().getName();
493 if (DaylightWebUtil.getContainerPrivilege(userName, container, this) != Privilege.WRITE) {
494 return unauthorizedMessage();
497 StatusJsonBean resultBean = new StatusJsonBean();
499 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
500 .getInstance(ISwitchManager.class, containerName, this);
501 String[] subnets = gatewaysToDelete.split(",");
502 resultBean.setStatus(true);
503 resultBean.setMessage("Added gateway address successfully");
504 for (String subnet : subnets) {
505 Status result = switchManager.removeSubnet(subnet);
506 if (!result.isSuccess()) {
507 resultBean.setStatus(false);
508 resultBean.setMessage(result.getDescription());
511 DaylightWebUtil.auditlog("Subnet Gateway", userName, "removed", subnet, containerName);
513 } catch (Exception e) {
514 resultBean.setStatus(false);
515 resultBean.setMessage(e.getMessage());
520 @RequestMapping(value = "/subnetGateway/ports/add", method = RequestMethod.GET)
522 public StatusJsonBean addSubnetGatewayPort(
523 @RequestParam("portsName") String portsName,
524 @RequestParam("ports") String ports,
525 @RequestParam("nodeId") String nodeId, HttpServletRequest request,
526 @RequestParam(required = false) String container) {
527 String containerName = (container == null) ? GlobalConstants.DEFAULT
528 .toString() : container;
530 // Authorization check
531 String userName = request.getUserPrincipal().getName();
532 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
533 return unauthorizedMessage();
536 StatusJsonBean resultBean = new StatusJsonBean();
538 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
539 .getInstance(ISwitchManager.class, containerName, this);
540 List<String> toAdd = new ArrayList<String>();
541 for (String port : ports.split(",")) {
544 Status result = switchManager.addPortsToSubnet(portsName, toAdd);
546 if (result.isSuccess()) {
547 resultBean.setStatus(true);
549 .setMessage("Added ports to subnet gateway address successfully");
550 DaylightWebUtil.auditlog("Ports to Subnet Gateway", userName, "added", ports, containerName);
552 resultBean.setStatus(false);
553 resultBean.setMessage(result.getDescription());
555 } catch (Exception e) {
556 resultBean.setStatus(false);
557 resultBean.setMessage(e.getMessage());
562 @RequestMapping(value = "/subnetGateway/ports/delete", method = RequestMethod.GET)
564 public StatusJsonBean deleteSubnetGatewayPort(
565 @RequestParam("gatewayName") String gatewayName,
566 @RequestParam("nodePort") String nodePort,
567 HttpServletRequest request,
568 @RequestParam(required = false) String container) {
569 String containerName = (container == null) ? GlobalConstants.DEFAULT
570 .toString() : container;
572 // Authorization check
573 String userName = request.getUserPrincipal().getName();
574 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
575 return unauthorizedMessage();
578 StatusJsonBean resultBean = new StatusJsonBean();
580 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
581 .getInstance(ISwitchManager.class, containerName, this);
582 List<String> toRemove = new ArrayList<String>();
583 for (String port : nodePort.split(",")) {
586 Status result = switchManager.removePortsFromSubnet(gatewayName, toRemove);
588 if (result.isSuccess()) {
589 resultBean.setStatus(true);
590 resultBean.setMessage("Deleted port from subnet gateway address successfully");
591 DaylightWebUtil.auditlog("Ports from Subnet Gateway", userName, "removed", nodePort, containerName);
593 resultBean.setStatus(false);
594 resultBean.setMessage(result.getDescription());
596 } catch (Exception e) {
597 resultBean.setStatus(false);
598 resultBean.setMessage(e.getMessage());
603 @RequestMapping(value = "/spanPorts", method = RequestMethod.GET)
605 public DevicesJsonBean getSpanPorts(HttpServletRequest request,
606 @RequestParam(required = false) String container) {
607 Gson gson = new Gson();
608 List<Map<String, String>> spanConfigs = new ArrayList<Map<String, String>>();
609 String containerName = (container == null) ? GlobalConstants.DEFAULT
610 .toString() : container;
612 // Derive the privilege this user has on the current container
613 String userName = request.getUserPrincipal().getName();
614 Privilege privilege = DaylightWebUtil.getContainerPrivilege(
615 userName, containerName, this);
617 if (privilege != Privilege.NONE) {
618 List<String> spanConfigs_json = new ArrayList<String>();
619 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
620 .getInstance(ISwitchManager.class, containerName, this);
621 if (switchManager != null) {
622 for (SpanConfig conf : switchManager.getSpanConfigList()) {
623 spanConfigs_json.add(gson.toJson(conf));
626 ObjectMapper mapper = new ObjectMapper();
628 for (String config_json : spanConfigs_json) {
630 @SuppressWarnings("unchecked")
631 Map<String, String> config_data = mapper.readValue(config_json,
633 Map<String, String> config = new HashMap<String, String>();
634 for (String name : config_data.keySet()) {
635 config.put(name, config_data.get(name));
636 // Add switch portName value (non-configuration field)
637 config.put("nodeName",
638 getNodeDesc(config_data.get("nodeId"), containerName));
639 NodeConnector spanPortNodeConnector = NodeConnector.fromString(config_data.get("spanPort"));
640 Name ncName = ((Name) switchManager.getNodeConnectorProp(spanPortNodeConnector,
642 String spanPortName = (ncName != null) ? ncName.getValue() : "";
643 config.put("spanPortName", spanPortName);
645 config.put("json", config_json);
646 spanConfigs.add(config);
647 } catch (Exception e) {
648 // TODO: Handle the exception.
653 DevicesJsonBean result = new DevicesJsonBean();
654 result.setPrivilege(privilege);
655 result.setColumnNames(SpanConfig.getGuiFieldsNames());
656 result.setNodeData(spanConfigs);
660 @RequestMapping(value = "/nodeports")
662 public String getNodePorts(HttpServletRequest request, @RequestParam(required = false) String container) {
663 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
665 // Derive the privilege this user has on the current container
666 String userName = request.getUserPrincipal().getName();
667 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
671 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
672 .getInstance(ISwitchManager.class, containerName, this);
673 if (switchManager == null) {
676 List<NodeJsonBean> nodeJsonBeans = new ArrayList<NodeJsonBean>();
678 for (Switch node : switchManager.getNetworkDevices()) {
679 NodeJsonBean nodeJsonBean = new NodeJsonBean();
680 List<PortJsonBean> port = new ArrayList<PortJsonBean>();
681 Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
682 if (nodeConnectorSet != null) {
683 for (NodeConnector nodeConnector : nodeConnectorSet) {
684 String nodeConnectorName = ((Name) switchManager
685 .getNodeConnectorProp(nodeConnector, Name.NamePropName)).getValue();
686 port.add(new PortJsonBean(nodeConnector.getID().toString(), nodeConnectorName, nodeConnector.toString()));
689 nodeJsonBean.setNodeId(node.getNode().toString());
690 nodeJsonBean.setNodeName(getNodeDesc(node.getNode().toString(), containerName));
691 nodeJsonBean.setNodePorts(port);
692 nodeJsonBeans.add(nodeJsonBean);
695 return new Gson().toJson(nodeJsonBeans);
698 @RequestMapping(value = "/spanPorts/add", method = RequestMethod.GET)
700 public StatusJsonBean addSpanPort(
701 @RequestParam("jsonData") String jsonData,
702 HttpServletRequest request,
703 @RequestParam(required = false) String container) {
704 String containerName = (container == null) ? GlobalConstants.DEFAULT
705 .toString() : container;
707 // Authorization check
708 String userName = request.getUserPrincipal().getName();
709 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
710 return unauthorizedMessage();
713 StatusJsonBean resultBean = new StatusJsonBean();
715 Gson gson = new Gson();
716 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
717 .getInstance(ISwitchManager.class, containerName, this);
718 SpanConfig cfgObject = gson.fromJson(jsonData, SpanConfig.class);
719 Status result = switchManager.addSpanConfig(cfgObject);
720 if (result.isSuccess()) {
721 resultBean.setStatus(true);
722 resultBean.setMessage("SPAN Port added successfully");
723 DaylightWebUtil.auditlog("SPAN Port", userName, "added", cfgObject.getNodeId(), containerName);
725 resultBean.setStatus(false);
726 resultBean.setMessage(result.getDescription());
728 } catch (Exception e) {
729 resultBean.setStatus(false);
730 resultBean.setMessage("Error occurred while adding span port. "
736 @RequestMapping(value = "/spanPorts/delete", method = RequestMethod.GET)
738 public StatusJsonBean deleteSpanPorts(
739 @RequestParam("spanPortsToDelete") String spanPortsToDelete,
740 HttpServletRequest request,
741 @RequestParam(required = false) String container) {
742 String containerName = (container == null) ? GlobalConstants.DEFAULT
743 .toString() : container;
745 // Authorization check
746 String userName = request.getUserPrincipal().getName();
747 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
748 return unauthorizedMessage();
751 StatusJsonBean resultBean = new StatusJsonBean();
753 Gson gson = new Gson();
754 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
755 .getInstance(ISwitchManager.class, containerName, this);
756 Type collectionType = new TypeToken<List<SpanPortJsonBean>>() {}.getType();
757 List<SpanPortJsonBean> jsonBeanList = gson.fromJson(spanPortsToDelete, collectionType);
758 for (SpanPortJsonBean jsonBean : jsonBeanList) {
759 SpanConfig cfgObject = gson
760 .fromJson(gson.toJson(jsonBean), SpanConfig.class);
761 Status result = switchManager.removeSpanConfig(cfgObject);
762 if (!result.isSuccess()) {
763 resultBean.setStatus(false);
764 resultBean.setMessage(result.getDescription());
767 DaylightWebUtil.auditlog("SPAN Port", userName, "removed", cfgObject.getNodeId(), containerName);
769 resultBean.setStatus(true);
770 resultBean.setMessage("SPAN Port(s) deleted successfully");
771 } catch (Exception e) {
772 resultBean.setStatus(false);
773 resultBean.setMessage("Error occurred while deleting span port. "
779 private String getNodeDesc(String nodeId, String containerName) {
780 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
781 .getInstance(ISwitchManager.class, containerName, this);
782 String description = "";
783 if (switchManager != null) {
784 Description desc = (Description) switchManager.getNodeProp(Node.fromString(nodeId), Description.propertyName);
786 description = desc.getValue();
789 return (description.isEmpty() || description.equalsIgnoreCase("none")) ? nodeId
793 private StatusJsonBean unauthorizedMessage() {
794 StatusJsonBean message = new StatusJsonBean();
795 message.setStatus(false);
796 message.setMessage("Operation not authorized");
800 @RequestMapping(value = "login")
801 public String login(final HttpServletRequest request,
802 final HttpServletResponse response) {
803 // response.setHeader("X-Page-Location", "/login");
805 * IUserManager userManager = (IUserManager) ServiceHelper
806 * .getGlobalInstance(IUserManager.class, this); if (userManager ==
807 * null) { return "User Manager is not available"; }
809 * String username = request.getUserPrincipal().getName();
812 * model.addAttribute("username", username); model.addAttribute("role",
813 * userManager.getUserLevel(username).toNumber());
815 return "forward:" + "/";