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;
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
23 import org.codehaus.jackson.map.ObjectMapper;
24 import org.opendaylight.controller.forwarding.staticrouting.IForwardingStaticRouting;
25 import org.opendaylight.controller.forwarding.staticrouting.StaticRouteConfig;
26 import org.opendaylight.controller.sal.authorization.Privilege;
27 import org.opendaylight.controller.sal.authorization.UserLevel;
28 import org.opendaylight.controller.sal.core.Config;
29 import org.opendaylight.controller.sal.core.Description;
30 import org.opendaylight.controller.sal.core.ForwardingMode;
31 import org.opendaylight.controller.sal.core.Name;
32 import org.opendaylight.controller.sal.core.Node;
33 import org.opendaylight.controller.sal.core.NodeConnector;
34 import org.opendaylight.controller.sal.core.Property;
35 import org.opendaylight.controller.sal.core.State;
36 import org.opendaylight.controller.sal.core.Tier;
37 import org.opendaylight.controller.sal.utils.GlobalConstants;
38 import org.opendaylight.controller.sal.utils.HexEncode;
39 import org.opendaylight.controller.sal.utils.ServiceHelper;
40 import org.opendaylight.controller.sal.utils.Status;
41 import org.opendaylight.controller.sal.utils.TierHelper;
42 import org.opendaylight.controller.switchmanager.ISwitchManager;
43 import org.opendaylight.controller.switchmanager.SpanConfig;
44 import org.opendaylight.controller.switchmanager.SubnetConfig;
45 import org.opendaylight.controller.switchmanager.Switch;
46 import org.opendaylight.controller.switchmanager.SwitchConfig;
47 import org.opendaylight.controller.web.DaylightWebUtil;
48 import org.opendaylight.controller.web.IDaylightWeb;
49 import org.springframework.stereotype.Controller;
50 import org.springframework.web.bind.annotation.RequestMapping;
51 import org.springframework.web.bind.annotation.RequestMethod;
52 import org.springframework.web.bind.annotation.RequestParam;
53 import org.springframework.web.bind.annotation.ResponseBody;
55 import com.google.gson.Gson;
59 public class Devices implements IDaylightWeb {
60 private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER;
61 private final String WEB_NAME = "Devices";
62 private final String WEB_ID = "devices";
63 private final short WEB_ORDER = 1;
66 ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null);
70 public String getWebName() {
75 public String getWebId() {
80 public short getWebOrder() {
85 public boolean isAuthorized(UserLevel userLevel) {
86 return userLevel.ordinal() <= AUTH_LEVEL.ordinal();
89 @RequestMapping(value = "/nodesLearnt", method = RequestMethod.GET)
91 public DevicesJsonBean getNodesLearnt(HttpServletRequest request,
92 @RequestParam(required = false) String container) {
93 Gson gson = new Gson();
94 String containerName = (container == null) ? GlobalConstants.DEFAULT
95 .toString() : container;
97 // Derive the privilege this user has on the current container
98 String userName = request.getUserPrincipal().getName();
99 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
101 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
103 List<Map<String, String>> nodeData = new ArrayList<Map<String, String>>();
104 if (switchManager != null && privilege != Privilege.NONE) {
105 for (Switch device : switchManager.getNetworkDevices()) {
106 HashMap<String, String> nodeDatum = new HashMap<String, String>();
107 Node node = device.getNode();
108 Tier tier = (Tier) switchManager.getNodeProp(node, Tier.TierPropName);
109 nodeDatum.put("containerName", containerName);
110 Description description = (Description) switchManager.getNodeProp(node, Description.propertyName);
111 String desc = (description == null) ? "" : description.getValue();
112 nodeDatum.put("nodeName", desc);
113 nodeDatum.put("nodeId", node.toString());
114 int tierNumber = (tier == null) ? TierHelper.unknownTierNumber : tier.getValue();
115 nodeDatum.put("tierName", TierHelper.getTierName(tierNumber) + " (Tier-" + tierNumber + ")");
116 nodeDatum.put("tier", tierNumber + "");
117 String modeStr = "0";
118 ForwardingMode mode = (ForwardingMode) switchManager.getNodeProp(node, ForwardingMode.name);
120 modeStr = String.valueOf(mode.getValue());
122 nodeDatum.put("mode", modeStr);
124 nodeDatum.put("json", gson.toJson(nodeDatum));
125 nodeDatum.put("mac", HexEncode.bytesToHexString(device.getDataLayerAddress()));
126 StringBuffer sb1 = new StringBuffer();
127 Set<NodeConnector> nodeConnectorSet = device.getNodeConnectors();
128 if (nodeConnectorSet != null && nodeConnectorSet.size() > 0) {
129 Map<Short, String> portList = new HashMap<Short, String>();
130 List<String> intfList = new ArrayList<String>();
131 for (NodeConnector nodeConnector : nodeConnectorSet) {
132 String nodeConnectorNumberToStr = nodeConnector.getID().toString();
133 Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector, Name.NamePropName));
134 Config portStatus = ((Config) switchManager
135 .getNodeConnectorProp(nodeConnector,
136 Config.ConfigPropName));
137 State portState = ((State) switchManager
138 .getNodeConnectorProp(nodeConnector,
139 State.StatePropName));
141 String nodeConnectorName = (ncName != null) ? ncName
143 nodeConnectorName += " (" + nodeConnector.getID() + ")";
145 if (portStatus != null) {
146 if (portStatus.getValue() == Config.ADMIN_UP) {
147 if (portState.getValue() == State.EDGE_UP) {
148 nodeConnectorName = "<span class='admin-up'>"
149 + nodeConnectorName + "</span>";
150 } else if (portState.getValue() == State.EDGE_DOWN) {
151 nodeConnectorName = "<span class='edge-down'>"
152 + nodeConnectorName + "</span>";
154 } else if (portStatus.getValue() == Config.ADMIN_DOWN) {
155 nodeConnectorName = "<span class='admin-down'>"
156 + nodeConnectorName + "</span>";
160 Class<?> idClass = nodeConnector.getID().getClass();
161 if (idClass.equals(Short.class)) {
163 Short.parseShort(nodeConnectorNumberToStr),
166 intfList.add(nodeConnectorName);
170 if (portList.size() > 0) {
171 Map<Short, String> sortedPortList = new TreeMap<Short, String>(
174 for (Entry<Short, String> e : sortedPortList.entrySet()) {
175 sb1.append(e.getValue());
178 } else if (intfList.size() > 0) {
179 for (String intf : intfList) {
185 nodeDatum.put("ports", sb1.toString());
186 nodeData.add(nodeDatum);
190 DevicesJsonBean result = new DevicesJsonBean();
191 result.setNodeData(nodeData);
192 result.setPrivilege(privilege);
193 List<String> columnNames = new ArrayList<String>();
194 columnNames.add("Node ID");
195 columnNames.add("Node Name");
196 columnNames.add("Tier");
197 columnNames.add("Mac Address");
198 columnNames.add("Ports");
199 columnNames.add("Port Status");
201 result.setColumnNames(columnNames);
205 @RequestMapping(value = "/tiers", method = RequestMethod.GET)
207 public List<String> getTiers() {
208 return TierHelper.getTiers();
211 @RequestMapping(value = "/nodesLearnt/update", method = RequestMethod.GET)
213 public StatusJsonBean updateLearntNode(
214 @RequestParam("nodeName") String nodeName,
215 @RequestParam("nodeId") String nodeId,
216 @RequestParam("tier") String tier,
217 @RequestParam("operationMode") String operationMode,
218 HttpServletRequest request,
219 @RequestParam(required = false) String container) {
220 String containerName = (container == null) ? GlobalConstants.DEFAULT
221 .toString() : container;
223 // Authorization check
224 String userName = request.getUserPrincipal().getName();
226 .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
227 return unauthorizedMessage();
230 StatusJsonBean resultBean = new StatusJsonBean();
232 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
233 .getInstance(ISwitchManager.class, containerName, this);
234 Map<String, Property> nodeProperties = new HashMap<String, Property>();
235 Property desc = new Description(nodeName);
236 nodeProperties.put(desc.getName(), desc);
237 Property nodeTier = new Tier(Integer.parseInt(tier));
238 nodeProperties.put(nodeTier.getName(), nodeTier);
239 if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
240 Property mode = new ForwardingMode(Integer.parseInt(operationMode));
241 nodeProperties.put(mode.getName(), mode);
243 SwitchConfig cfg = new SwitchConfig(nodeId, nodeProperties);
244 Status result = switchManager.updateNodeConfig(cfg);
245 if (!result.isSuccess()) {
246 resultBean.setStatus(false);
247 resultBean.setMessage(result.getDescription());
249 resultBean.setStatus(true);
250 resultBean.setMessage("Updated node information successfully");
252 } catch (Exception e) {
253 resultBean.setStatus(false);
254 resultBean.setMessage("Error updating node information. "
260 @RequestMapping(value = "/staticRoutes", method = RequestMethod.GET)
262 public DevicesJsonBean getStaticRoutes(HttpServletRequest request,
263 @RequestParam(required = false) String container) {
264 Gson gson = new Gson();
265 String containerName = (container == null) ? GlobalConstants.DEFAULT
266 .toString() : container;
268 // Derive the privilege this user has on the current container
269 String userName = request.getUserPrincipal().getName();
270 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
272 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
273 .getInstance(IForwardingStaticRouting.class, containerName,
275 if (staticRouting == null) {
278 List<Map<String, String>> staticRoutes = new ArrayList<Map<String, String>>();
279 ConcurrentMap<String, StaticRouteConfig> routeConfigs = staticRouting
280 .getStaticRouteConfigs();
281 if (routeConfigs == null) {
284 if (privilege != Privilege.NONE) {
285 for (StaticRouteConfig conf : routeConfigs.values()) {
286 Map<String, String> staticRoute = new HashMap<String, String>();
287 staticRoute.put("name", conf.getName());
288 staticRoute.put("staticRoute", conf.getStaticRoute());
289 staticRoute.put("nextHopType", conf.getNextHopType());
290 staticRoute.put("nextHop", conf.getNextHop());
291 staticRoute.put("json", gson.toJson(conf));
292 staticRoutes.add(staticRoute);
295 DevicesJsonBean result = new DevicesJsonBean();
296 result.setPrivilege(privilege);
297 result.setColumnNames(StaticRouteConfig.getGuiFieldsNames());
298 result.setNodeData(staticRoutes);
302 @RequestMapping(value = "/staticRoute/add", method = RequestMethod.GET)
304 public StatusJsonBean addStaticRoute(
305 @RequestParam("routeName") String routeName,
306 @RequestParam("staticRoute") String staticRoute,
307 @RequestParam("nextHop") String nextHop,
308 HttpServletRequest request,
309 @RequestParam(required = false) String container) {
310 String containerName = (container == null) ? GlobalConstants.DEFAULT
311 .toString() : container;
313 // Authorization check
314 String userName = request.getUserPrincipal().getName();
316 .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
317 return unauthorizedMessage();
320 StatusJsonBean result = new StatusJsonBean();
322 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
323 .getInstance(IForwardingStaticRouting.class, containerName,
325 StaticRouteConfig config = new StaticRouteConfig();
326 config.setName(routeName);
327 config.setStaticRoute(staticRoute);
328 config.setNextHop(nextHop);
329 Status addStaticRouteResult = staticRouting.addStaticRoute(config);
330 if (addStaticRouteResult.isSuccess()) {
331 result.setStatus(true);
332 result.setMessage("Static Route saved successfully");
334 result.setStatus(false);
335 result.setMessage(addStaticRouteResult.getDescription());
337 } catch (Exception e) {
338 result.setStatus(false);
339 result.setMessage("Error - " + e.getMessage());
344 @RequestMapping(value = "/staticRoute/delete", method = RequestMethod.GET)
346 public StatusJsonBean deleteStaticRoute(
347 @RequestParam("routesToDelete") String routesToDelete,
348 HttpServletRequest request,
349 @RequestParam(required = false) String container) {
350 String containerName = (container == null) ? GlobalConstants.DEFAULT
351 .toString() : container;
353 // Authorization check
354 String userName = request.getUserPrincipal().getName();
355 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
356 return unauthorizedMessage();
359 StatusJsonBean resultBean = new StatusJsonBean();
361 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
362 .getInstance(IForwardingStaticRouting.class, containerName,
364 String[] routes = routesToDelete.split(",");
366 resultBean.setStatus(true);
368 .setMessage("Successfully deleted selected static routes");
369 for (String route : routes) {
370 result = staticRouting.removeStaticRoute(route);
371 if (!result.isSuccess()) {
372 resultBean.setStatus(false);
373 resultBean.setMessage(result.getDescription());
377 } catch (Exception e) {
378 resultBean.setStatus(false);
380 .setMessage("Error occurred while deleting static routes. "
386 @RequestMapping(value = "/subnets", method = RequestMethod.GET)
388 public DevicesJsonBean getSubnetGateways(HttpServletRequest request,
389 @RequestParam(required = false) String container) {
390 Gson gson = new Gson();
391 List<Map<String, String>> subnets = new ArrayList<Map<String, String>>();
392 String containerName = (container == null) ? GlobalConstants.DEFAULT
393 .toString() : container;
395 // Derive the privilege this user has on the current container
396 String userName = request.getUserPrincipal().getName();
397 Privilege privilege = DaylightWebUtil.getContainerPrivilege(
398 userName, containerName, this);
400 if (privilege != Privilege.NONE) {
401 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
402 .getInstance(ISwitchManager.class, containerName, this);
403 if (switchManager != null) {
404 for (SubnetConfig conf : switchManager.getSubnetsConfigList()) {
405 Map<String, String> subnet = new HashMap<String, String>();
406 subnet.put("name", conf.getName());
407 subnet.put("subnet", conf.getSubnet());
408 subnet.put("json", gson.toJson(conf));
413 DevicesJsonBean result = new DevicesJsonBean();
414 result.setPrivilege(privilege);
415 result.setColumnNames(SubnetConfig.getGuiFieldsNames());
416 result.setNodeData(subnets);
420 @RequestMapping(value = "/subnetGateway/add", method = RequestMethod.GET)
422 public StatusJsonBean addSubnetGateways(
423 @RequestParam("gatewayName") String gatewayName,
424 @RequestParam("gatewayIPAddress") String gatewayIPAddress,
425 HttpServletRequest request,
426 @RequestParam(required = false) String container) {
427 String containerName = (container == null) ? GlobalConstants.DEFAULT
428 .toString() : container;
430 // Authorization check
431 String userName = request.getUserPrincipal().getName();
432 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
433 return unauthorizedMessage();
436 StatusJsonBean resultBean = new StatusJsonBean();
438 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
439 .getInstance(ISwitchManager.class, containerName, this);
440 SubnetConfig cfgObject = new SubnetConfig(gatewayName,
441 gatewayIPAddress, new ArrayList<String>());
442 Status result = switchManager.addSubnet(cfgObject);
443 if (result.isSuccess()) {
444 resultBean.setStatus(true);
445 resultBean.setMessage("Added gateway address successfully");
447 resultBean.setStatus(false);
448 resultBean.setMessage(result.getDescription());
450 } catch (Exception e) {
451 resultBean.setStatus(false);
452 resultBean.setMessage(e.getMessage());
457 @RequestMapping(value = "/subnetGateway/delete", method = RequestMethod.GET)
459 public StatusJsonBean deleteSubnetGateways(
460 @RequestParam("gatewaysToDelete") String gatewaysToDelete,
461 HttpServletRequest request,
462 @RequestParam(required = false) String container) {
463 String containerName = (container == null) ? GlobalConstants.DEFAULT
464 .toString() : container;
466 // Authorization check
467 String userName = request.getUserPrincipal().getName();
468 if (DaylightWebUtil.getContainerPrivilege(userName, container, this) != Privilege.WRITE) {
469 return unauthorizedMessage();
472 StatusJsonBean resultBean = new StatusJsonBean();
474 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
475 .getInstance(ISwitchManager.class, containerName, this);
476 String[] subnets = gatewaysToDelete.split(",");
477 resultBean.setStatus(true);
478 resultBean.setMessage("Added gateway address successfully");
479 for (String subnet : subnets) {
480 Status result = switchManager.removeSubnet(subnet);
481 if (!result.isSuccess()) {
482 resultBean.setStatus(false);
483 resultBean.setMessage(result.getDescription());
487 } catch (Exception e) {
488 resultBean.setStatus(false);
489 resultBean.setMessage(e.getMessage());
494 @RequestMapping(value = "/subnetGateway/ports/add", method = RequestMethod.GET)
496 public StatusJsonBean addSubnetGatewayPort(
497 @RequestParam("portsName") String portsName,
498 @RequestParam("ports") String ports,
499 @RequestParam("nodeId") String nodeId, HttpServletRequest request,
500 @RequestParam(required = false) String container) {
501 String containerName = (container == null) ? GlobalConstants.DEFAULT
502 .toString() : container;
504 // Authorization check
505 String userName = request.getUserPrincipal().getName();
506 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
507 return unauthorizedMessage();
510 StatusJsonBean resultBean = new StatusJsonBean();
512 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
513 .getInstance(ISwitchManager.class, containerName, this);
514 Status result = switchManager.addPortsToSubnet(portsName, nodeId
517 if (result.isSuccess()) {
518 resultBean.setStatus(true);
520 .setMessage("Added ports to subnet gateway address successfully");
522 resultBean.setStatus(false);
523 resultBean.setMessage(result.getDescription());
525 } catch (Exception e) {
526 resultBean.setStatus(false);
527 resultBean.setMessage(e.getMessage());
532 @RequestMapping(value = "/subnetGateway/ports/delete", method = RequestMethod.GET)
534 public StatusJsonBean deleteSubnetGatewayPort(
535 @RequestParam("gatewayName") String gatewayName,
536 @RequestParam("nodePort") String nodePort,
537 HttpServletRequest request,
538 @RequestParam(required = false) String container) {
539 String containerName = (container == null) ? GlobalConstants.DEFAULT
540 .toString() : container;
542 // Authorization check
543 String userName = request.getUserPrincipal().getName();
544 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
545 return unauthorizedMessage();
548 StatusJsonBean resultBean = new StatusJsonBean();
550 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
551 .getInstance(ISwitchManager.class, containerName, this);
552 Status result = switchManager.removePortsFromSubnet(gatewayName,
555 if (result.isSuccess()) {
556 resultBean.setStatus(true);
558 .setMessage("Deleted port from subnet gateway address successfully");
560 resultBean.setStatus(false);
561 resultBean.setMessage(result.getDescription());
563 } catch (Exception e) {
564 resultBean.setStatus(false);
565 resultBean.setMessage(e.getMessage());
570 @RequestMapping(value = "/spanPorts", method = RequestMethod.GET)
572 public DevicesJsonBean getSpanPorts(HttpServletRequest request,
573 @RequestParam(required = false) String container) {
574 Gson gson = new Gson();
575 List<Map<String, String>> spanConfigs = new ArrayList<Map<String, String>>();
576 String containerName = (container == null) ? GlobalConstants.DEFAULT
577 .toString() : container;
579 // Derive the privilege this user has on the current container
580 String userName = request.getUserPrincipal().getName();
581 Privilege privilege = DaylightWebUtil.getContainerPrivilege(
582 userName, containerName, this);
584 if (privilege != Privilege.NONE) {
585 List<String> spanConfigs_json = new ArrayList<String>();
586 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
587 .getInstance(ISwitchManager.class, containerName, this);
588 if (switchManager != null) {
589 for (SpanConfig conf : switchManager.getSpanConfigList()) {
590 spanConfigs_json.add(gson.toJson(conf));
593 ObjectMapper mapper = new ObjectMapper();
595 for (String config_json : spanConfigs_json) {
597 @SuppressWarnings("unchecked")
598 Map<String, String> config_data = mapper.readValue(config_json,
600 Map<String, String> config = new HashMap<String, String>();
601 for (String name : config_data.keySet()) {
602 config.put(name, config_data.get(name));
603 // Add switch name value (non-configuration field)
604 config.put("nodeName",
605 getNodeDesc(config_data.get("nodeId"), containerName));
607 config.put("json", config_json);
608 spanConfigs.add(config);
609 } catch (Exception e) {
610 // TODO: Handle the exception.
615 DevicesJsonBean result = new DevicesJsonBean();
616 result.setPrivilege(privilege);
617 result.setColumnNames(SpanConfig.getGuiFieldsNames());
618 result.setNodeData(spanConfigs);
622 @RequestMapping(value = "/nodeports")
624 public Map<String, Object> getNodePorts(HttpServletRequest request,
625 @RequestParam(required = false) String container) {
626 String containerName = (container == null) ? GlobalConstants.DEFAULT
627 .toString() : container;
629 // Derive the privilege this user has on the current container
630 String userName = request.getUserPrincipal().getName();
631 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
636 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
637 .getInstance(ISwitchManager.class, containerName, this);
638 if (switchManager == null) {
642 Map<String, Object> nodes = new HashMap<String, Object>();
643 Map<Short, String> port;
645 for (Switch node : switchManager.getNetworkDevices()) {
646 port = new HashMap<Short, String>(); // new port
647 Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
649 if (nodeConnectorSet != null) {
650 for (NodeConnector nodeConnector : nodeConnectorSet) {
651 String nodeConnectorName = ((Name) switchManager
652 .getNodeConnectorProp(nodeConnector,
653 Name.NamePropName)).getValue();
654 port.put((Short) nodeConnector.getID(), nodeConnectorName
655 + "(" + nodeConnector.getID() + ")");
659 nodes.put(node.getNode().toString(), port);
665 @RequestMapping(value = "/spanPorts/add", method = RequestMethod.GET)
667 public StatusJsonBean addSpanPort(
668 @RequestParam("jsonData") String jsonData,
669 HttpServletRequest request,
670 @RequestParam(required = false) String container) {
671 String containerName = (container == null) ? GlobalConstants.DEFAULT
672 .toString() : container;
674 // Authorization check
675 String userName = request.getUserPrincipal().getName();
676 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
677 return unauthorizedMessage();
680 StatusJsonBean resultBean = new StatusJsonBean();
682 Gson gson = new Gson();
683 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
684 .getInstance(ISwitchManager.class, containerName, this);
685 SpanConfig cfgObject = gson.fromJson(jsonData, SpanConfig.class);
686 Status result = switchManager.addSpanConfig(cfgObject);
687 if (result.isSuccess()) {
688 resultBean.setStatus(true);
689 resultBean.setMessage("SPAN Port added successfully");
691 resultBean.setStatus(false);
692 resultBean.setMessage(result.getDescription());
694 } catch (Exception e) {
695 resultBean.setStatus(false);
696 resultBean.setMessage("Error occurred while adding span port. "
702 @RequestMapping(value = "/spanPorts/delete", method = RequestMethod.GET)
704 public StatusJsonBean deleteSpanPorts(
705 @RequestParam("spanPortsToDelete") String spanPortsToDelete,
706 HttpServletRequest request,
707 @RequestParam(required = false) String container) {
708 String containerName = (container == null) ? GlobalConstants.DEFAULT
709 .toString() : container;
711 // Authorization check
712 String userName = request.getUserPrincipal().getName();
713 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
714 return unauthorizedMessage();
717 StatusJsonBean resultBean = new StatusJsonBean();
719 Gson gson = new Gson();
720 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
721 .getInstance(ISwitchManager.class, containerName, this);
722 String[] spans = spanPortsToDelete.split("###");
723 resultBean.setStatus(true);
724 resultBean.setMessage("SPAN Port(s) deleted successfully");
725 for (String span : spans) {
726 if (!span.isEmpty()) {
727 SpanConfig cfgObject = gson
728 .fromJson(span, SpanConfig.class);
729 Status result = switchManager.removeSpanConfig(cfgObject);
730 if (!result.isSuccess()) {
731 resultBean.setStatus(false);
732 resultBean.setMessage(result.getDescription());
737 } catch (Exception e) {
738 resultBean.setStatus(false);
739 resultBean.setMessage("Error occurred while deleting span port. "
745 private String getNodeDesc(String nodeId, String containerName) {
746 ISwitchManager switchManager = (ISwitchManager) ServiceHelper
747 .getInstance(ISwitchManager.class, containerName, this);
748 String description = "";
749 if (switchManager != null) {
750 Description desc = (Description) switchManager.getNodeProp(Node.fromString(nodeId), Description.propertyName);
752 description = desc.getValue();
755 return (description.isEmpty() || description.equalsIgnoreCase("none")) ? nodeId
759 private StatusJsonBean unauthorizedMessage() {
760 StatusJsonBean message = new StatusJsonBean();
761 message.setStatus(false);
762 message.setMessage("Operation not authorized");
766 @RequestMapping(value = "login")
767 public String login(final HttpServletRequest request,
768 final HttpServletResponse response) {
769 // response.setHeader("X-Page-Location", "/login");
771 * IUserManager userManager = (IUserManager) ServiceHelper
772 * .getGlobalInstance(IUserManager.class, this); if (userManager ==
773 * null) { return "User Manager is not available"; }
775 * String username = request.getUserPrincipal().getName();
778 * model.addAttribute("username", username); model.addAttribute("role",
779 * userManager.getUserLevel(username).toNumber());
781 return "forward:" + "/";