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.HashSet;
14 import java.util.List;
16 import java.util.Map.Entry;
18 import java.util.TreeMap;
19 import java.util.concurrent.ConcurrentMap;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
24 import org.codehaus.jackson.map.ObjectMapper;
25 import org.opendaylight.controller.forwarding.staticrouting.IForwardingStaticRouting;
26 import org.opendaylight.controller.forwarding.staticrouting.StaticRouteConfig;
27 import org.opendaylight.controller.sal.authorization.Privilege;
28 import org.opendaylight.controller.sal.authorization.UserLevel;
29 import org.opendaylight.controller.sal.core.Config;
30 import org.opendaylight.controller.sal.core.Description;
31 import org.opendaylight.controller.sal.core.ForwardingMode;
32 import org.opendaylight.controller.sal.core.Name;
33 import org.opendaylight.controller.sal.core.Node;
34 import org.opendaylight.controller.sal.core.NodeConnector;
35 import org.opendaylight.controller.sal.core.Property;
36 import org.opendaylight.controller.sal.core.State;
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;
48 import org.opendaylight.controller.web.DaylightWebUtil;
49 import org.opendaylight.controller.web.IDaylightWeb;
50 import org.springframework.stereotype.Controller;
51 import org.springframework.web.bind.annotation.RequestMapping;
52 import org.springframework.web.bind.annotation.RequestMethod;
53 import org.springframework.web.bind.annotation.RequestParam;
54 import org.springframework.web.bind.annotation.ResponseBody;
56 import com.google.gson.Gson;
60 public class Devices implements IDaylightWeb {
61 private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER;
62 private final String WEB_NAME = "Devices";
63 private final String WEB_ID = "devices";
64 private final short WEB_ORDER = 1;
67 ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null);
71 public String getWebName() {
76 public String getWebId() {
81 public short getWebOrder() {
86 public boolean isAuthorized(UserLevel userLevel) {
87 return userLevel.ordinal() <= AUTH_LEVEL.ordinal();
90 @RequestMapping(value = "/nodesLearnt", method = RequestMethod.GET)
92 public DevicesJsonBean getNodesLearnt(HttpServletRequest request,
93 @RequestParam(required = false) String container) {
94 Gson gson = new Gson();
95 String containerName = (container == null) ? GlobalConstants.DEFAULT
96 .toString() : container;
98 // Derive the privilege this user has on the current container
99 String userName = request.getUserPrincipal().getName();
100 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
102 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
104 List<Map<String, String>> nodeData = new ArrayList<Map<String, String>>();
105 if (switchManager != null && privilege != Privilege.NONE) {
106 for (Switch device : switchManager.getNetworkDevices()) {
107 HashMap<String, String> nodeDatum = new HashMap<String, String>();
108 Node node = device.getNode();
109 Tier tier = (Tier) switchManager.getNodeProp(node, Tier.TierPropName);
110 nodeDatum.put("containerName", containerName);
111 Description description = (Description) switchManager.getNodeProp(node, Description.propertyName);
112 String desc = (description == null) ? "" : description.getValue();
113 nodeDatum.put("nodeName", desc);
114 nodeDatum.put("nodeId", node.toString());
115 int tierNumber = (tier == null) ? TierHelper.unknownTierNumber : tier.getValue();
116 nodeDatum.put("tierName", TierHelper.getTierName(tierNumber) + " (Tier-" + tierNumber + ")");
117 nodeDatum.put("tier", tierNumber + "");
118 String modeStr = "0";
119 ForwardingMode mode = (ForwardingMode) switchManager.getNodeProp(node, ForwardingMode.name);
121 modeStr = String.valueOf(mode.getValue());
123 nodeDatum.put("mode", modeStr);
125 nodeDatum.put("json", gson.toJson(nodeDatum));
126 nodeDatum.put("mac", HexEncode.bytesToHexString(device.getDataLayerAddress()));
127 StringBuffer sb1 = new StringBuffer();
128 Set<NodeConnector> nodeConnectorSet = device.getNodeConnectors();
129 if (nodeConnectorSet != null && nodeConnectorSet.size() > 0) {
130 Map<Short, String> portList = new HashMap<Short, String>();
131 List<String> intfList = new ArrayList<String>();
132 for (NodeConnector nodeConnector : nodeConnectorSet) {
133 String nodeConnectorNumberToStr = nodeConnector.getID().toString();
134 Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector, Name.NamePropName));
135 Config portStatus = ((Config) switchManager
136 .getNodeConnectorProp(nodeConnector,
137 Config.ConfigPropName));
138 State portState = ((State) switchManager
139 .getNodeConnectorProp(nodeConnector,
140 State.StatePropName));
142 String nodeConnectorName = (ncName != null) ? ncName
144 nodeConnectorName += " (" + nodeConnector.getID() + ")";
146 if (portStatus != null) {
147 if (portStatus.getValue() == Config.ADMIN_UP) {
148 if (portState.getValue() == State.EDGE_UP) {
149 nodeConnectorName = "<span class='admin-up'>"
150 + nodeConnectorName + "</span>";
151 } else if (portState.getValue() == State.EDGE_DOWN) {
152 nodeConnectorName = "<span class='edge-down'>"
153 + nodeConnectorName + "</span>";
155 } else if (portStatus.getValue() == Config.ADMIN_DOWN) {
156 nodeConnectorName = "<span class='admin-down'>"
157 + nodeConnectorName + "</span>";
161 Class<?> idClass = nodeConnector.getID().getClass();
162 if (idClass.equals(Short.class)) {
164 Short.parseShort(nodeConnectorNumberToStr),
167 intfList.add(nodeConnectorName);
171 if (portList.size() > 0) {
172 Map<Short, String> sortedPortList = new TreeMap<Short, String>(
175 for (Entry<Short, String> e : sortedPortList.entrySet()) {
176 sb1.append(e.getValue());
179 } else if (intfList.size() > 0) {
180 for (String intf : intfList) {
186 nodeDatum.put("ports", sb1.toString());
187 nodeData.add(nodeDatum);
191 DevicesJsonBean result = new DevicesJsonBean();
192 result.setNodeData(nodeData);
193 result.setPrivilege(privilege);
194 List<String> columnNames = new ArrayList<String>();
195 columnNames.add("Node ID");
196 columnNames.add("Node Name");
197 columnNames.add("Tier");
198 columnNames.add("Mac Address");
199 columnNames.add("Ports");
200 columnNames.add("Port Status");
202 result.setColumnNames(columnNames);
206 @RequestMapping(value = "/tiers", method = RequestMethod.GET)
208 public List<String> getTiers() {
209 return TierHelper.getTiers();
212 @RequestMapping(value = "/nodesLearnt/update", method = RequestMethod.GET)
214 public StatusJsonBean updateLearntNode(
215 @RequestParam("nodeName") String nodeName,
216 @RequestParam("nodeId") String nodeId,
217 @RequestParam("tier") String tier,
218 @RequestParam("operationMode") String operationMode,
219 HttpServletRequest request,
220 @RequestParam(required = false) String container) {
221 String containerName = (container == null) ? GlobalConstants.DEFAULT
222 .toString() : container;
224 // Authorization check
225 String userName = request.getUserPrincipal().getName();
227 .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
228 return unauthorizedMessage();
231 StatusJsonBean resultBean = new StatusJsonBean();
233 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
234 .getInstance(ISwitchManager.class, containerName, this);
235 Map<String, Property> nodeProperties = new HashMap<String, Property>();
236 Property desc = new Description(nodeName);
237 nodeProperties.put(desc.getName(), desc);
238 Property nodeTier = new Tier(Integer.parseInt(tier));
239 nodeProperties.put(nodeTier.getName(), nodeTier);
240 if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
241 Property mode = new ForwardingMode(Integer.parseInt(operationMode));
242 nodeProperties.put(mode.getName(), mode);
244 SwitchConfig cfg = new SwitchConfig(nodeId, nodeProperties);
245 Status result = switchManager.updateNodeConfig(cfg);
246 if (!result.isSuccess()) {
247 resultBean.setStatus(false);
248 resultBean.setMessage(result.getDescription());
250 resultBean.setStatus(true);
251 resultBean.setMessage("Updated node information successfully");
253 } catch (Exception e) {
254 resultBean.setStatus(false);
255 resultBean.setMessage("Error updating node information. "
261 @RequestMapping(value = "/staticRoutes", method = RequestMethod.GET)
263 public DevicesJsonBean getStaticRoutes(HttpServletRequest request,
264 @RequestParam(required = false) String container) {
265 Gson gson = new Gson();
266 String containerName = (container == null) ? GlobalConstants.DEFAULT
267 .toString() : container;
269 // Derive the privilege this user has on the current container
270 String userName = request.getUserPrincipal().getName();
271 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
273 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
274 .getInstance(IForwardingStaticRouting.class, containerName,
276 if (staticRouting == null) {
279 List<Map<String, String>> staticRoutes = new ArrayList<Map<String, String>>();
280 ConcurrentMap<String, StaticRouteConfig> routeConfigs = staticRouting
281 .getStaticRouteConfigs();
282 if (routeConfigs == null) {
285 if (privilege != Privilege.NONE) {
286 for (StaticRouteConfig conf : routeConfigs.values()) {
287 Map<String, String> staticRoute = new HashMap<String, String>();
288 staticRoute.put("name", conf.getName());
289 staticRoute.put("staticRoute", conf.getStaticRoute());
290 staticRoute.put("nextHopType", conf.getNextHopType());
291 staticRoute.put("nextHop", conf.getNextHop());
292 staticRoute.put("json", gson.toJson(conf));
293 staticRoutes.add(staticRoute);
296 DevicesJsonBean result = new DevicesJsonBean();
297 result.setPrivilege(privilege);
298 result.setColumnNames(StaticRouteConfig.getGuiFieldsNames());
299 result.setNodeData(staticRoutes);
303 @RequestMapping(value = "/staticRoute/add", method = RequestMethod.GET)
305 public StatusJsonBean addStaticRoute(
306 @RequestParam("routeName") String routeName,
307 @RequestParam("staticRoute") String staticRoute,
308 @RequestParam("nextHop") String nextHop,
309 HttpServletRequest request,
310 @RequestParam(required = false) String container) {
311 String containerName = (container == null) ? GlobalConstants.DEFAULT
312 .toString() : container;
314 // Authorization check
315 String userName = request.getUserPrincipal().getName();
317 .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
318 return unauthorizedMessage();
321 StatusJsonBean result = new StatusJsonBean();
323 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
324 .getInstance(IForwardingStaticRouting.class, containerName,
326 StaticRouteConfig config = new StaticRouteConfig();
327 config.setName(routeName);
328 config.setStaticRoute(staticRoute);
329 config.setNextHop(nextHop);
330 Status addStaticRouteResult = staticRouting.addStaticRoute(config);
331 if (addStaticRouteResult.isSuccess()) {
332 result.setStatus(true);
333 result.setMessage("Static Route saved successfully");
335 result.setStatus(false);
336 result.setMessage(addStaticRouteResult.getDescription());
338 } catch (Exception e) {
339 result.setStatus(false);
340 result.setMessage("Error - " + e.getMessage());
345 @RequestMapping(value = "/staticRoute/delete", method = RequestMethod.GET)
347 public StatusJsonBean deleteStaticRoute(
348 @RequestParam("routesToDelete") String routesToDelete,
349 HttpServletRequest request,
350 @RequestParam(required = false) String container) {
351 String containerName = (container == null) ? GlobalConstants.DEFAULT
352 .toString() : container;
354 // Authorization check
355 String userName = request.getUserPrincipal().getName();
356 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
357 return unauthorizedMessage();
360 StatusJsonBean resultBean = new StatusJsonBean();
362 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
363 .getInstance(IForwardingStaticRouting.class, containerName,
365 String[] routes = routesToDelete.split(",");
367 resultBean.setStatus(true);
369 .setMessage("Successfully deleted selected static routes");
370 for (String route : routes) {
371 result = staticRouting.removeStaticRoute(route);
372 if (!result.isSuccess()) {
373 resultBean.setStatus(false);
374 resultBean.setMessage(result.getDescription());
378 } catch (Exception e) {
379 resultBean.setStatus(false);
381 .setMessage("Error occurred while deleting static routes. "
387 @RequestMapping(value = "/subnets", method = RequestMethod.GET)
389 public DevicesJsonBean getSubnetGateways(HttpServletRequest request,
390 @RequestParam(required = false) String container) {
391 Gson gson = new Gson();
392 List<Map<String, String>> subnets = new ArrayList<Map<String, String>>();
393 String containerName = (container == null) ? GlobalConstants.DEFAULT
394 .toString() : container;
396 // Derive the privilege this user has on the current container
397 String userName = request.getUserPrincipal().getName();
398 Privilege privilege = DaylightWebUtil.getContainerPrivilege(
399 userName, containerName, this);
401 if (privilege != Privilege.NONE) {
402 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
403 .getInstance(ISwitchManager.class, containerName, this);
404 if (switchManager != null) {
405 for (SubnetConfig conf : switchManager.getSubnetsConfigList()) {
406 Map<String, String> subnet = new HashMap<String, String>();
407 subnet.put("name", conf.getName());
408 subnet.put("subnet", conf.getSubnet());
409 subnet.put("json", gson.toJson(conf));
414 DevicesJsonBean result = new DevicesJsonBean();
415 result.setPrivilege(privilege);
416 result.setColumnNames(SubnetConfig.getGuiFieldsNames());
417 result.setNodeData(subnets);
421 @RequestMapping(value = "/subnetGateway/add", method = RequestMethod.GET)
423 public StatusJsonBean addSubnetGateways(
424 @RequestParam("gatewayName") String gatewayName,
425 @RequestParam("gatewayIPAddress") String gatewayIPAddress,
426 HttpServletRequest request,
427 @RequestParam(required = false) String container) {
428 String containerName = (container == null) ? GlobalConstants.DEFAULT
429 .toString() : container;
431 // Authorization check
432 String userName = request.getUserPrincipal().getName();
433 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
434 return unauthorizedMessage();
437 StatusJsonBean resultBean = new StatusJsonBean();
439 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
440 .getInstance(ISwitchManager.class, containerName, this);
441 SubnetConfig cfgObject = new SubnetConfig(gatewayName,
442 gatewayIPAddress, new HashSet<String>());
443 Status result = switchManager.addSubnet(cfgObject);
444 if (result.isSuccess()) {
445 resultBean.setStatus(true);
446 resultBean.setMessage("Added gateway address successfully");
448 resultBean.setStatus(false);
449 resultBean.setMessage(result.getDescription());
451 } catch (Exception e) {
452 resultBean.setStatus(false);
453 resultBean.setMessage(e.getMessage());
458 @RequestMapping(value = "/subnetGateway/delete", method = RequestMethod.GET)
460 public StatusJsonBean deleteSubnetGateways(
461 @RequestParam("gatewaysToDelete") String gatewaysToDelete,
462 HttpServletRequest request,
463 @RequestParam(required = false) String container) {
464 String containerName = (container == null) ? GlobalConstants.DEFAULT
465 .toString() : container;
467 // Authorization check
468 String userName = request.getUserPrincipal().getName();
469 if (DaylightWebUtil.getContainerPrivilege(userName, container, this) != Privilege.WRITE) {
470 return unauthorizedMessage();
473 StatusJsonBean resultBean = new StatusJsonBean();
475 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
476 .getInstance(ISwitchManager.class, containerName, this);
477 String[] subnets = gatewaysToDelete.split(",");
478 resultBean.setStatus(true);
479 resultBean.setMessage("Added gateway address successfully");
480 for (String subnet : subnets) {
481 Status result = switchManager.removeSubnet(subnet);
482 if (!result.isSuccess()) {
483 resultBean.setStatus(false);
484 resultBean.setMessage(result.getDescription());
488 } catch (Exception e) {
489 resultBean.setStatus(false);
490 resultBean.setMessage(e.getMessage());
495 @RequestMapping(value = "/subnetGateway/ports/add", method = RequestMethod.GET)
497 public StatusJsonBean addSubnetGatewayPort(
498 @RequestParam("portsName") String portsName,
499 @RequestParam("ports") String ports,
500 @RequestParam("nodeId") String nodeId, HttpServletRequest request,
501 @RequestParam(required = false) String container) {
502 String containerName = (container == null) ? GlobalConstants.DEFAULT
503 .toString() : container;
505 // Authorization check
506 String userName = request.getUserPrincipal().getName();
507 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
508 return unauthorizedMessage();
511 StatusJsonBean resultBean = new StatusJsonBean();
513 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
514 .getInstance(ISwitchManager.class, containerName, this);
515 Status result = switchManager.addPortsToSubnet(portsName, nodeId
518 if (result.isSuccess()) {
519 resultBean.setStatus(true);
521 .setMessage("Added ports to subnet gateway address successfully");
523 resultBean.setStatus(false);
524 resultBean.setMessage(result.getDescription());
526 } catch (Exception e) {
527 resultBean.setStatus(false);
528 resultBean.setMessage(e.getMessage());
533 @RequestMapping(value = "/subnetGateway/ports/delete", method = RequestMethod.GET)
535 public StatusJsonBean deleteSubnetGatewayPort(
536 @RequestParam("gatewayName") String gatewayName,
537 @RequestParam("nodePort") String nodePort,
538 HttpServletRequest request,
539 @RequestParam(required = false) String container) {
540 String containerName = (container == null) ? GlobalConstants.DEFAULT
541 .toString() : container;
543 // Authorization check
544 String userName = request.getUserPrincipal().getName();
545 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
546 return unauthorizedMessage();
549 StatusJsonBean resultBean = new StatusJsonBean();
551 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
552 .getInstance(ISwitchManager.class, containerName, this);
553 Status result = switchManager.removePortsFromSubnet(gatewayName,
556 if (result.isSuccess()) {
557 resultBean.setStatus(true);
559 .setMessage("Deleted port from subnet gateway address successfully");
561 resultBean.setStatus(false);
562 resultBean.setMessage(result.getDescription());
564 } catch (Exception e) {
565 resultBean.setStatus(false);
566 resultBean.setMessage(e.getMessage());
571 @RequestMapping(value = "/spanPorts", method = RequestMethod.GET)
573 public DevicesJsonBean getSpanPorts(HttpServletRequest request,
574 @RequestParam(required = false) String container) {
575 Gson gson = new Gson();
576 List<Map<String, String>> spanConfigs = new ArrayList<Map<String, String>>();
577 String containerName = (container == null) ? GlobalConstants.DEFAULT
578 .toString() : container;
580 // Derive the privilege this user has on the current container
581 String userName = request.getUserPrincipal().getName();
582 Privilege privilege = DaylightWebUtil.getContainerPrivilege(
583 userName, containerName, this);
585 if (privilege != Privilege.NONE) {
586 List<String> spanConfigs_json = new ArrayList<String>();
587 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
588 .getInstance(ISwitchManager.class, containerName, this);
589 if (switchManager != null) {
590 for (SpanConfig conf : switchManager.getSpanConfigList()) {
591 spanConfigs_json.add(gson.toJson(conf));
594 ObjectMapper mapper = new ObjectMapper();
596 for (String config_json : spanConfigs_json) {
598 @SuppressWarnings("unchecked")
599 Map<String, String> config_data = mapper.readValue(config_json,
601 Map<String, String> config = new HashMap<String, String>();
602 for (String name : config_data.keySet()) {
603 config.put(name, config_data.get(name));
604 // Add switch name value (non-configuration field)
605 config.put("nodeName",
606 getNodeDesc(config_data.get("nodeId"), containerName));
608 config.put("json", config_json);
609 spanConfigs.add(config);
610 } catch (Exception e) {
611 // TODO: Handle the exception.
616 DevicesJsonBean result = new DevicesJsonBean();
617 result.setPrivilege(privilege);
618 result.setColumnNames(SpanConfig.getGuiFieldsNames());
619 result.setNodeData(spanConfigs);
623 @RequestMapping(value = "/nodeports")
625 public Map<String, Object> getNodePorts(HttpServletRequest request,
626 @RequestParam(required = false) String container) {
627 String containerName = (container == null) ? GlobalConstants.DEFAULT
628 .toString() : container;
630 // Derive the privilege this user has on the current container
631 String userName = request.getUserPrincipal().getName();
632 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
637 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
638 .getInstance(ISwitchManager.class, containerName, this);
639 if (switchManager == null) {
643 Map<String, Object> nodes = new HashMap<String, Object>();
644 Map<Short, String> port;
646 for (Switch node : switchManager.getNetworkDevices()) {
647 port = new HashMap<Short, String>(); // new port
648 Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
650 if (nodeConnectorSet != null) {
651 for (NodeConnector nodeConnector : nodeConnectorSet) {
652 String nodeConnectorName = ((Name) switchManager
653 .getNodeConnectorProp(nodeConnector,
654 Name.NamePropName)).getValue();
655 port.put((Short) nodeConnector.getID(), nodeConnectorName
656 + "(" + nodeConnector.getID() + ")");
660 nodes.put(node.getNode().toString(), port);
666 @RequestMapping(value = "/spanPorts/add", method = RequestMethod.GET)
668 public StatusJsonBean addSpanPort(
669 @RequestParam("jsonData") String jsonData,
670 HttpServletRequest request,
671 @RequestParam(required = false) String container) {
672 String containerName = (container == null) ? GlobalConstants.DEFAULT
673 .toString() : container;
675 // Authorization check
676 String userName = request.getUserPrincipal().getName();
677 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
678 return unauthorizedMessage();
681 StatusJsonBean resultBean = new StatusJsonBean();
683 Gson gson = new Gson();
684 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
685 .getInstance(ISwitchManager.class, containerName, this);
686 SpanConfig cfgObject = gson.fromJson(jsonData, SpanConfig.class);
687 Status result = switchManager.addSpanConfig(cfgObject);
688 if (result.isSuccess()) {
689 resultBean.setStatus(true);
690 resultBean.setMessage("SPAN Port added successfully");
692 resultBean.setStatus(false);
693 resultBean.setMessage(result.getDescription());
695 } catch (Exception e) {
696 resultBean.setStatus(false);
697 resultBean.setMessage("Error occurred while adding span port. "
703 @RequestMapping(value = "/spanPorts/delete", method = RequestMethod.GET)
705 public StatusJsonBean deleteSpanPorts(
706 @RequestParam("spanPortsToDelete") String spanPortsToDelete,
707 HttpServletRequest request,
708 @RequestParam(required = false) String container) {
709 String containerName = (container == null) ? GlobalConstants.DEFAULT
710 .toString() : container;
712 // Authorization check
713 String userName = request.getUserPrincipal().getName();
714 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
715 return unauthorizedMessage();
718 StatusJsonBean resultBean = new StatusJsonBean();
720 Gson gson = new Gson();
721 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
722 .getInstance(ISwitchManager.class, containerName, this);
723 String[] spans = spanPortsToDelete.split("###");
724 resultBean.setStatus(true);
725 resultBean.setMessage("SPAN Port(s) deleted successfully");
726 for (String span : spans) {
727 if (!span.isEmpty()) {
728 SpanConfig cfgObject = gson
729 .fromJson(span, SpanConfig.class);
730 Status result = switchManager.removeSpanConfig(cfgObject);
731 if (!result.isSuccess()) {
732 resultBean.setStatus(false);
733 resultBean.setMessage(result.getDescription());
738 } catch (Exception e) {
739 resultBean.setStatus(false);
740 resultBean.setMessage("Error occurred while deleting span port. "
746 private String getNodeDesc(String nodeId, String containerName) {
747 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
748 .getInstance(ISwitchManager.class, containerName, this);
749 String description = "";
750 if (switchManager != null) {
751 Description desc = (Description) switchManager.getNodeProp(Node.fromString(nodeId), Description.propertyName);
753 description = desc.getValue();
756 return (description.isEmpty() || description.equalsIgnoreCase("none")) ? nodeId
760 private StatusJsonBean unauthorizedMessage() {
761 StatusJsonBean message = new StatusJsonBean();
762 message.setStatus(false);
763 message.setMessage("Operation not authorized");
767 @RequestMapping(value = "login")
768 public String login(final HttpServletRequest request,
769 final HttpServletResponse response) {
770 // response.setHeader("X-Page-Location", "/login");
772 * IUserManager userManager = (IUserManager) ServiceHelper
773 * .getGlobalInstance(IUserManager.class, this); if (userManager ==
774 * null) { return "User Manager is not available"; }
776 * String username = request.getUserPrincipal().getName();
779 * model.addAttribute("username", username); model.addAttribute("role",
780 * userManager.getUserLevel(username).toNumber());
782 return "forward:" + "/";