Implement cluster wide topology notifications and let routing use it
[controller.git] / opendaylight / web / devices / src / main / java / org / opendaylight / controller / devices / web / Devices.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.devices.web;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18 import java.util.TreeMap;
19 import java.util.concurrent.ConcurrentMap;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
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;
55
56 import com.google.gson.Gson;
57
58 @Controller
59 @RequestMapping("/")
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;
65
66     public Devices() {
67         ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null);
68     }
69
70     @Override
71     public String getWebName() {
72         return WEB_NAME;
73     }
74
75     @Override
76     public String getWebId() {
77         return WEB_ID;
78     }
79
80     @Override
81     public short getWebOrder() {
82         return WEB_ORDER;
83     }
84
85     @Override
86     public boolean isAuthorized(UserLevel userLevel) {
87         return userLevel.ordinal() <= AUTH_LEVEL.ordinal();
88     }
89
90     @RequestMapping(value = "/nodesLearnt", method = RequestMethod.GET)
91     @ResponseBody
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;
97
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);
101
102         ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
103                 this);
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);
120                 if (mode != null) {
121                     modeStr = String.valueOf(mode.getValue());
122                 }
123                 nodeDatum.put("mode", modeStr);
124
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));
141
142                         String nodeConnectorName = (ncName != null) ? ncName
143                                 .getValue() : "";
144                         nodeConnectorName += " (" + nodeConnector.getID() + ")";
145
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>";
154                                 }
155                             } else if (portStatus.getValue() == Config.ADMIN_DOWN) {
156                                 nodeConnectorName = "<span class='admin-down'>"
157                                         + nodeConnectorName + "</span>";
158                             }
159                         }
160
161                         Class<?> idClass = nodeConnector.getID().getClass();
162                         if (idClass.equals(Short.class)) {
163                             portList.put(
164                                     Short.parseShort(nodeConnectorNumberToStr),
165                                     nodeConnectorName);
166                         } else {
167                             intfList.add(nodeConnectorName);
168                         }
169                     }
170
171                     if (portList.size() > 0) {
172                         Map<Short, String> sortedPortList = new TreeMap<Short, String>(
173                                 portList);
174
175                         for (Entry<Short, String> e : sortedPortList.entrySet()) {
176                             sb1.append(e.getValue());
177                             sb1.append("<br>");
178                         }
179                     } else if (intfList.size() > 0) {
180                         for (String intf : intfList) {
181                             sb1.append(intf);
182                             sb1.append("<br>");
183                         }
184                     }
185                 }
186                 nodeDatum.put("ports", sb1.toString());
187                 nodeData.add(nodeDatum);
188             }
189         }
190
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");
201
202         result.setColumnNames(columnNames);
203         return result;
204     }
205
206     @RequestMapping(value = "/tiers", method = RequestMethod.GET)
207     @ResponseBody
208     public List<String> getTiers() {
209         return TierHelper.getTiers();
210     }
211
212     @RequestMapping(value = "/nodesLearnt/update", method = RequestMethod.GET)
213     @ResponseBody
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;
223
224         // Authorization check
225         String userName = request.getUserPrincipal().getName();
226         if (DaylightWebUtil
227                 .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
228             return unauthorizedMessage();
229         }
230
231         StatusJsonBean resultBean = new StatusJsonBean();
232         try {
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);
243             }
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());
249             } else {
250                 resultBean.setStatus(true);
251                 resultBean.setMessage("Updated node information successfully");
252             }
253         } catch (Exception e) {
254             resultBean.setStatus(false);
255             resultBean.setMessage("Error updating node information. "
256                     + e.getMessage());
257         }
258         return resultBean;
259     }
260
261     @RequestMapping(value = "/staticRoutes", method = RequestMethod.GET)
262     @ResponseBody
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;
268
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);
272
273         IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
274                 .getInstance(IForwardingStaticRouting.class, containerName,
275                         this);
276         if (staticRouting == null) {
277             return null;
278         }
279         List<Map<String, String>> staticRoutes = new ArrayList<Map<String, String>>();
280         ConcurrentMap<String, StaticRouteConfig> routeConfigs = staticRouting
281                 .getStaticRouteConfigs();
282         if (routeConfigs == null) {
283             return null;
284         }
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);
294             }
295         }
296         DevicesJsonBean result = new DevicesJsonBean();
297         result.setPrivilege(privilege);
298         result.setColumnNames(StaticRouteConfig.getGuiFieldsNames());
299         result.setNodeData(staticRoutes);
300         return result;
301     }
302
303     @RequestMapping(value = "/staticRoute/add", method = RequestMethod.GET)
304     @ResponseBody
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;
313
314         // Authorization check
315         String userName = request.getUserPrincipal().getName();
316         if (DaylightWebUtil
317                 .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
318             return unauthorizedMessage();
319         }
320
321         StatusJsonBean result = new StatusJsonBean();
322         try {
323             IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
324                     .getInstance(IForwardingStaticRouting.class, containerName,
325                             this);
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");
334             } else {
335                 result.setStatus(false);
336                 result.setMessage(addStaticRouteResult.getDescription());
337             }
338         } catch (Exception e) {
339             result.setStatus(false);
340             result.setMessage("Error - " + e.getMessage());
341         }
342         return result;
343     }
344
345     @RequestMapping(value = "/staticRoute/delete", method = RequestMethod.GET)
346     @ResponseBody
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;
353
354         // Authorization check
355         String userName = request.getUserPrincipal().getName();
356         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
357             return unauthorizedMessage();
358         }
359
360         StatusJsonBean resultBean = new StatusJsonBean();
361         try {
362             IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
363                     .getInstance(IForwardingStaticRouting.class, containerName,
364                             this);
365             String[] routes = routesToDelete.split(",");
366             Status result;
367             resultBean.setStatus(true);
368             resultBean
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());
375                     break;
376                 }
377             }
378         } catch (Exception e) {
379             resultBean.setStatus(false);
380             resultBean
381                     .setMessage("Error occurred while deleting static routes. "
382                             + e.getMessage());
383         }
384         return resultBean;
385     }
386
387     @RequestMapping(value = "/subnets", method = RequestMethod.GET)
388     @ResponseBody
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;
395
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);
400
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));
410                     subnets.add(subnet);
411                 }
412             }
413         }
414         DevicesJsonBean result = new DevicesJsonBean();
415         result.setPrivilege(privilege);
416         result.setColumnNames(SubnetConfig.getGuiFieldsNames());
417         result.setNodeData(subnets);
418         return result;
419     }
420
421     @RequestMapping(value = "/subnetGateway/add", method = RequestMethod.GET)
422     @ResponseBody
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;
430
431         // Authorization check
432         String userName = request.getUserPrincipal().getName();
433         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
434             return unauthorizedMessage();
435         }
436
437         StatusJsonBean resultBean = new StatusJsonBean();
438         try {
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");
447             } else {
448                 resultBean.setStatus(false);
449                 resultBean.setMessage(result.getDescription());
450             }
451         } catch (Exception e) {
452             resultBean.setStatus(false);
453             resultBean.setMessage(e.getMessage());
454         }
455         return resultBean;
456     }
457
458     @RequestMapping(value = "/subnetGateway/delete", method = RequestMethod.GET)
459     @ResponseBody
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;
466
467         // Authorization check
468         String userName = request.getUserPrincipal().getName();
469         if (DaylightWebUtil.getContainerPrivilege(userName, container, this) != Privilege.WRITE) {
470             return unauthorizedMessage();
471         }
472
473         StatusJsonBean resultBean = new StatusJsonBean();
474         try {
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());
485                     break;
486                 }
487             }
488         } catch (Exception e) {
489             resultBean.setStatus(false);
490             resultBean.setMessage(e.getMessage());
491         }
492         return resultBean;
493     }
494
495     @RequestMapping(value = "/subnetGateway/ports/add", method = RequestMethod.GET)
496     @ResponseBody
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;
504
505         // Authorization check
506         String userName = request.getUserPrincipal().getName();
507         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
508             return unauthorizedMessage();
509         }
510
511         StatusJsonBean resultBean = new StatusJsonBean();
512         try {
513             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
514                     .getInstance(ISwitchManager.class, containerName, this);
515             Status result = switchManager.addPortsToSubnet(portsName, nodeId
516                     + "/" + ports);
517
518             if (result.isSuccess()) {
519                 resultBean.setStatus(true);
520                 resultBean
521                         .setMessage("Added ports to subnet gateway address successfully");
522             } else {
523                 resultBean.setStatus(false);
524                 resultBean.setMessage(result.getDescription());
525             }
526         } catch (Exception e) {
527             resultBean.setStatus(false);
528             resultBean.setMessage(e.getMessage());
529         }
530         return resultBean;
531     }
532
533     @RequestMapping(value = "/subnetGateway/ports/delete", method = RequestMethod.GET)
534     @ResponseBody
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;
542
543         // Authorization check
544         String userName = request.getUserPrincipal().getName();
545         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
546             return unauthorizedMessage();
547         }
548
549         StatusJsonBean resultBean = new StatusJsonBean();
550         try {
551             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
552                     .getInstance(ISwitchManager.class, containerName, this);
553             Status result = switchManager.removePortsFromSubnet(gatewayName,
554                     nodePort);
555
556             if (result.isSuccess()) {
557                 resultBean.setStatus(true);
558                 resultBean
559                         .setMessage("Deleted port from subnet gateway address successfully");
560             } else {
561                 resultBean.setStatus(false);
562                 resultBean.setMessage(result.getDescription());
563             }
564         } catch (Exception e) {
565             resultBean.setStatus(false);
566             resultBean.setMessage(e.getMessage());
567         }
568         return resultBean;
569     }
570
571     @RequestMapping(value = "/spanPorts", method = RequestMethod.GET)
572     @ResponseBody
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;
579
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);
584
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));
592                 }
593             }
594             ObjectMapper mapper = new ObjectMapper();
595
596             for (String config_json : spanConfigs_json) {
597                 try {
598                     @SuppressWarnings("unchecked")
599                     Map<String, String> config_data = mapper.readValue(config_json,
600                             HashMap.class);
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));
607                     }
608                     config.put("json", config_json);
609                     spanConfigs.add(config);
610                 } catch (Exception e) {
611                     // TODO: Handle the exception.
612                 }
613             }
614         }
615
616         DevicesJsonBean result = new DevicesJsonBean();
617         result.setPrivilege(privilege);
618         result.setColumnNames(SpanConfig.getGuiFieldsNames());
619         result.setNodeData(spanConfigs);
620         return result;
621     }
622
623     @RequestMapping(value = "/nodeports")
624     @ResponseBody
625     public Map<String, Object> getNodePorts(HttpServletRequest request,
626             @RequestParam(required = false) String container) {
627         String containerName = (container == null) ? GlobalConstants.DEFAULT
628                 .toString() : container;
629
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) {
633             return null;
634         }
635
636
637         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
638                 .getInstance(ISwitchManager.class, containerName, this);
639         if (switchManager == null) {
640             return null;
641         }
642
643         Map<String, Object> nodes = new HashMap<String, Object>();
644         Map<Short, String> port;
645
646         for (Switch node : switchManager.getNetworkDevices()) {
647             port = new HashMap<Short, String>(); // new port
648             Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
649
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() + ")");
657                 }
658             }
659
660             nodes.put(node.getNode().toString(), port);
661         }
662
663         return nodes;
664     }
665
666     @RequestMapping(value = "/spanPorts/add", method = RequestMethod.GET)
667     @ResponseBody
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;
674
675         // Authorization check
676         String userName = request.getUserPrincipal().getName();
677         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
678             return unauthorizedMessage();
679         }
680
681         StatusJsonBean resultBean = new StatusJsonBean();
682         try {
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");
691             } else {
692                 resultBean.setStatus(false);
693                 resultBean.setMessage(result.getDescription());
694             }
695         } catch (Exception e) {
696             resultBean.setStatus(false);
697             resultBean.setMessage("Error occurred while adding span port. "
698                     + e.getMessage());
699         }
700         return resultBean;
701     }
702
703     @RequestMapping(value = "/spanPorts/delete", method = RequestMethod.GET)
704     @ResponseBody
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;
711
712         // Authorization check
713         String userName = request.getUserPrincipal().getName();
714         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
715             return unauthorizedMessage();
716         }
717
718         StatusJsonBean resultBean = new StatusJsonBean();
719         try {
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());
734                         break;
735                     }
736                 }
737             }
738         } catch (Exception e) {
739             resultBean.setStatus(false);
740             resultBean.setMessage("Error occurred while deleting span port. "
741                     + e.getMessage());
742         }
743         return resultBean;
744     }
745
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);
752             if(desc != null) {
753                 description = desc.getValue();
754             }
755         }
756         return (description.isEmpty() || description.equalsIgnoreCase("none")) ? nodeId
757                 : description;
758     }
759
760     private StatusJsonBean unauthorizedMessage() {
761         StatusJsonBean message = new StatusJsonBean();
762         message.setStatus(false);
763         message.setMessage("Operation not authorized");
764         return message;
765     }
766
767     @RequestMapping(value = "login")
768     public String login(final HttpServletRequest request,
769             final HttpServletResponse response) {
770         // response.setHeader("X-Page-Location", "/login");
771         /*
772          * IUserManager userManager = (IUserManager) ServiceHelper
773          * .getGlobalInstance(IUserManager.class, this); if (userManager ==
774          * null) { return "User Manager is not available"; }
775          *
776          * String username = request.getUserPrincipal().getName();
777          *
778          *
779          * model.addAttribute("username", username); model.addAttribute("role",
780          * userManager.getUserLevel(username).toNumber());
781          */
782         return "forward:" + "/";
783     }
784
785 }