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