Merge "Added default output directory to CodeGeneratorImpl in maven-sal-api-gen-plugi...
[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.Name;
30 import org.opendaylight.controller.sal.core.Node;
31 import org.opendaylight.controller.sal.core.NodeConnector;
32 import org.opendaylight.controller.sal.core.Tier;
33 import org.opendaylight.controller.sal.utils.GlobalConstants;
34 import org.opendaylight.controller.sal.utils.HexEncode;
35 import org.opendaylight.controller.sal.utils.ServiceHelper;
36 import org.opendaylight.controller.sal.utils.Status;
37 import org.opendaylight.controller.sal.utils.TierHelper;
38 import org.opendaylight.controller.switchmanager.ISwitchManager;
39 import org.opendaylight.controller.switchmanager.SpanConfig;
40 import org.opendaylight.controller.switchmanager.SubnetConfig;
41 import org.opendaylight.controller.switchmanager.Switch;
42 import org.opendaylight.controller.switchmanager.SwitchConfig;
43 import org.opendaylight.controller.web.DaylightWebUtil;
44 import org.opendaylight.controller.web.IDaylightWeb;
45 import org.springframework.stereotype.Controller;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.bind.annotation.RequestMethod;
48 import org.springframework.web.bind.annotation.RequestParam;
49 import org.springframework.web.bind.annotation.ResponseBody;
50
51 import com.google.gson.Gson;
52
53 @Controller
54 @RequestMapping("/")
55 public class Devices implements IDaylightWeb {
56     private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER;
57     private final String WEB_NAME = "Devices";
58     private final String WEB_ID = "devices";
59     private final short WEB_ORDER = 1;
60
61     public Devices() {
62         ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null);
63     }
64
65     @Override
66     public String getWebName() {
67         return WEB_NAME;
68     }
69
70     @Override
71     public String getWebId() {
72         return WEB_ID;
73     }
74
75     @Override
76     public short getWebOrder() {
77         return WEB_ORDER;
78     }
79
80     @Override
81     public boolean isAuthorized(UserLevel userLevel) {
82         return userLevel.ordinal() <= AUTH_LEVEL.ordinal();
83     }
84
85     @RequestMapping(value = "/nodesLearnt", method = RequestMethod.GET)
86     @ResponseBody
87     public DevicesJsonBean getNodesLearnt(HttpServletRequest request,
88             @RequestParam(required = false) String container) {
89         Gson gson = new Gson();
90         String containerName = (container == null) ? GlobalConstants.DEFAULT
91                 .toString() : container;
92
93         // Derive the privilege this user has on the current container
94         String userName = request.getUserPrincipal().getName();
95         Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
96
97         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
98                 .getInstance(ISwitchManager.class, containerName, this);
99         List<Map<String, String>> nodeData = new ArrayList<Map<String, String>>();
100         if (switchManager != null && privilege != Privilege.NONE) {
101             for (Switch device : switchManager.getNetworkDevices()) {
102                 HashMap<String, String> nodeDatum = new HashMap<String, String>();
103                 Node node = device.getNode();
104                 Tier tier = (Tier) switchManager.getNodeProp(node,
105                         Tier.TierPropName);
106
107                 nodeDatum.put("containerName", containerName);
108                 nodeDatum.put("nodeName",
109                         switchManager.getNodeDescription(node));
110                 nodeDatum.put("nodeId", node.toString());
111                 int tierNumber = (tier == null) ? TierHelper.unknownTierNumber
112                         : tier.getValue();
113                 nodeDatum.put("tierName", TierHelper.getTierName(tierNumber)
114                         + " (Tier-" + tierNumber + ")");
115                 nodeDatum.put("tier", tierNumber + "");
116                 SwitchConfig sc = switchManager.getSwitchConfig(device
117                         .getNode().toString());
118                 String modeStr = (sc != null) ? sc.getMode() : "0";
119                 nodeDatum.put("mode", modeStr);
120
121                 nodeDatum.put("json", gson.toJson(nodeDatum));
122                 nodeDatum.put("mac", HexEncode.bytesToHexString(device
123                         .getDataLayerAddress()));
124                 StringBuffer sb1 = new StringBuffer();
125                 Set<NodeConnector> nodeConnectorSet = device
126                         .getNodeConnectors();
127                 if (nodeConnectorSet != null && nodeConnectorSet.size() > 0) {
128                     Map<Short, String> portList = new HashMap<Short, String>();
129                     List<String> intfList = new ArrayList<String>();
130                     for (NodeConnector nodeConnector : nodeConnectorSet) {
131                         String nodeConnectorNumberToStr = nodeConnector.getID()
132                                 .toString();
133                         Name ncName = ((Name) switchManager
134                                 .getNodeConnectorProp(nodeConnector,
135                                         Name.NamePropName));
136                         Config portStatus = ((Config) switchManager
137                                 .getNodeConnectorProp(nodeConnector,
138                                         Config.ConfigPropName));
139
140                         String nodeConnectorName = (ncName != null) ? ncName
141                                 .getValue() : "";
142                         nodeConnectorName += " (" + nodeConnector.getID() + ")";
143
144                         if (portStatus != null) {
145                             if (portStatus.getValue() == Config.ADMIN_UP) {
146                                 nodeConnectorName = "<span style='color:green;'>"
147                                         + nodeConnectorName + "</span>";
148                             } else if (portStatus.getValue() == Config.ADMIN_DOWN) {
149                                 nodeConnectorName = "<span style='color:red;'>"
150                                         + nodeConnectorName + "</span>";
151                             }
152                         }
153
154                         Class<?> idClass = nodeConnector.getID().getClass();
155                         if (idClass.equals(Short.class)) {
156                             portList.put(
157                                     Short.parseShort(nodeConnectorNumberToStr),
158                                     nodeConnectorName);
159                         } else {
160                             intfList.add(nodeConnectorName);
161                         }
162                     }
163
164                     if (portList.size() > 0) {
165                         Map<Short, String> sortedPortList = new TreeMap<Short, String>(
166                                 portList);
167
168                         for (Entry<Short, String> e : sortedPortList.entrySet()) {
169                             sb1.append(e.getValue());
170                             sb1.append("<br>");
171                         }
172                     } else if (intfList.size() > 0) {
173                         for (String intf : intfList) {
174                             sb1.append(intf);
175                             sb1.append("<br>");
176                         }
177                     }
178                 }
179                 nodeDatum.put("ports", sb1.toString());
180                 nodeData.add(nodeDatum);
181             }
182         }
183
184         DevicesJsonBean result = new DevicesJsonBean();
185         result.setNodeData(nodeData);
186         result.setPrivilege(privilege);
187         List<String> columnNames = new ArrayList<String>();
188         columnNames.add("Node ID");
189         columnNames.add("Node Name");
190         columnNames.add("Tier");
191         columnNames.add("Mac Address");
192         columnNames.add("Ports");
193         columnNames.add("Port Status");
194
195         result.setColumnNames(columnNames);
196         return result;
197     }
198
199     @RequestMapping(value = "/tiers", method = RequestMethod.GET)
200     @ResponseBody
201     public List<String> getTiers() {
202         return TierHelper.getTiers();
203     }
204
205     @RequestMapping(value = "/nodesLearnt/update", method = RequestMethod.GET)
206     @ResponseBody
207     public StatusJsonBean updateLearntNode(
208             @RequestParam("nodeName") String nodeName,
209             @RequestParam("nodeId") String nodeId,
210             @RequestParam("tier") String tier,
211             @RequestParam("operationMode") String operationMode,
212             HttpServletRequest request,
213             @RequestParam(required = false) String container) {
214         String containerName = (container == null) ? GlobalConstants.DEFAULT
215                 .toString() : container;
216
217         // Authorization check
218         String userName = request.getUserPrincipal().getName();
219         if (DaylightWebUtil
220                 .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
221             return unauthorizedMessage();
222         }
223
224         StatusJsonBean resultBean = new StatusJsonBean();
225         try {
226             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
227                     .getInstance(ISwitchManager.class, containerName, this);
228             SwitchConfig cfg = new SwitchConfig(nodeId, nodeName, tier,
229                     operationMode);
230             switchManager.updateSwitchConfig(cfg);
231             resultBean.setStatus(true);
232             resultBean.setMessage("Updated node information successfully");
233         } catch (Exception e) {
234             resultBean.setStatus(false);
235             resultBean.setMessage("Error updating node information. "
236                     + e.getMessage());
237         }
238         return resultBean;
239     }
240
241     @RequestMapping(value = "/staticRoutes", method = RequestMethod.GET)
242     @ResponseBody
243     public DevicesJsonBean getStaticRoutes(HttpServletRequest request,
244             @RequestParam(required = false) String container) {
245         Gson gson = new Gson();
246         String containerName = (container == null) ? GlobalConstants.DEFAULT
247                 .toString() : container;
248
249         // Derive the privilege this user has on the current container
250         String userName = request.getUserPrincipal().getName();
251         Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
252
253         IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
254                 .getInstance(IForwardingStaticRouting.class, containerName,
255                         this);
256         if (staticRouting == null) {
257             return null;
258         }
259         List<Map<String, String>> staticRoutes = new ArrayList<Map<String, String>>();
260         ConcurrentMap<String, StaticRouteConfig> routeConfigs = staticRouting
261                 .getStaticRouteConfigs();
262         if (routeConfigs == null) {
263             return null;
264         }
265         if (privilege != Privilege.NONE) {
266             for (StaticRouteConfig conf : routeConfigs.values()) {
267                 Map<String, String> staticRoute = new HashMap<String, String>();
268                 staticRoute.put("name", conf.getName());
269                 staticRoute.put("staticRoute", conf.getStaticRoute());
270                 staticRoute.put("nextHopType", conf.getNextHopType());
271                 staticRoute.put("nextHop", conf.getNextHop());
272                 staticRoute.put("json", gson.toJson(conf));
273                 staticRoutes.add(staticRoute);
274             }
275         }
276         DevicesJsonBean result = new DevicesJsonBean();
277         result.setPrivilege(privilege);
278         result.setColumnNames(StaticRouteConfig.getGuiFieldsNames());
279         result.setNodeData(staticRoutes);
280         return result;
281     }
282
283     @RequestMapping(value = "/staticRoute/add", method = RequestMethod.GET)
284     @ResponseBody
285     public StatusJsonBean addStaticRoute(
286             @RequestParam("routeName") String routeName,
287             @RequestParam("staticRoute") String staticRoute,
288             @RequestParam("nextHop") String nextHop,
289             HttpServletRequest request,
290             @RequestParam(required = false) String container) {
291         String containerName = (container == null) ? GlobalConstants.DEFAULT
292                 .toString() : container;
293
294         // Authorization check
295         String userName = request.getUserPrincipal().getName();
296         if (DaylightWebUtil
297                 .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
298             return unauthorizedMessage();
299         }
300
301         StatusJsonBean result = new StatusJsonBean();
302         try {
303             IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
304                     .getInstance(IForwardingStaticRouting.class, containerName,
305                             this);
306             StaticRouteConfig config = new StaticRouteConfig();
307             config.setName(routeName);
308             config.setStaticRoute(staticRoute);
309             config.setNextHop(nextHop);
310             Status addStaticRouteResult = staticRouting.addStaticRoute(config);
311             if (addStaticRouteResult.isSuccess()) {
312                 result.setStatus(true);
313                 result.setMessage("Static Route saved successfully");
314             } else {
315                 result.setStatus(false);
316                 result.setMessage(addStaticRouteResult.getDescription());
317             }
318         } catch (Exception e) {
319             result.setStatus(false);
320             result.setMessage("Error - " + e.getMessage());
321         }
322         return result;
323     }
324
325     @RequestMapping(value = "/staticRoute/delete", method = RequestMethod.GET)
326     @ResponseBody
327     public StatusJsonBean deleteStaticRoute(
328             @RequestParam("routesToDelete") String routesToDelete,
329             HttpServletRequest request,
330             @RequestParam(required = false) String container) {
331         String containerName = (container == null) ? GlobalConstants.DEFAULT
332                 .toString() : container;
333
334         // Authorization check
335         String userName = request.getUserPrincipal().getName();
336         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
337             return unauthorizedMessage();
338         }
339
340         StatusJsonBean resultBean = new StatusJsonBean();
341         try {
342             IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
343                     .getInstance(IForwardingStaticRouting.class, containerName,
344                             this);
345             String[] routes = routesToDelete.split(",");
346             Status result;
347             resultBean.setStatus(true);
348             resultBean
349                     .setMessage("Successfully deleted selected static routes");
350             for (String route : routes) {
351                 result = staticRouting.removeStaticRoute(route);
352                 if (!result.isSuccess()) {
353                     resultBean.setStatus(false);
354                     resultBean.setMessage(result.getDescription());
355                     break;
356                 }
357             }
358         } catch (Exception e) {
359             resultBean.setStatus(false);
360             resultBean
361                     .setMessage("Error occurred while deleting static routes. "
362                             + e.getMessage());
363         }
364         return resultBean;
365     }
366
367     @RequestMapping(value = "/subnets", method = RequestMethod.GET)
368     @ResponseBody
369     public DevicesJsonBean getSubnetGateways(HttpServletRequest request,
370             @RequestParam(required = false) String container) {
371         Gson gson = new Gson();
372         List<Map<String, String>> subnets = new ArrayList<Map<String, String>>();
373         String containerName = (container == null) ? GlobalConstants.DEFAULT
374                 .toString() : container;
375
376         // Derive the privilege this user has on the current container
377         String userName = request.getUserPrincipal().getName();
378         Privilege privilege = DaylightWebUtil.getContainerPrivilege(
379                 userName, containerName, this);
380
381         if (privilege != Privilege.NONE) {
382             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
383                     .getInstance(ISwitchManager.class, containerName, this);
384             if (switchManager != null) {
385                 for (SubnetConfig conf : switchManager.getSubnetsConfigList()) {
386                     Map<String, String> subnet = new HashMap<String, String>();
387                     subnet.put("name", conf.getName());
388                     subnet.put("subnet", conf.getSubnet());
389                     subnet.put("json", gson.toJson(conf));
390                     subnets.add(subnet);
391                 }
392             }
393         }
394         DevicesJsonBean result = new DevicesJsonBean();
395         result.setPrivilege(privilege);
396         result.setColumnNames(SubnetConfig.getGuiFieldsNames());
397         result.setNodeData(subnets);
398         return result;
399     }
400
401     @RequestMapping(value = "/subnetGateway/add", method = RequestMethod.GET)
402     @ResponseBody
403     public StatusJsonBean addSubnetGateways(
404             @RequestParam("gatewayName") String gatewayName,
405             @RequestParam("gatewayIPAddress") String gatewayIPAddress,
406             HttpServletRequest request,
407             @RequestParam(required = false) String container) {
408         String containerName = (container == null) ? GlobalConstants.DEFAULT
409                 .toString() : container;
410
411         // Authorization check
412         String userName = request.getUserPrincipal().getName();
413         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
414             return unauthorizedMessage();
415         }
416
417         StatusJsonBean resultBean = new StatusJsonBean();
418         try {
419             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
420                     .getInstance(ISwitchManager.class, containerName, this);
421             SubnetConfig cfgObject = new SubnetConfig(gatewayName,
422                     gatewayIPAddress, new ArrayList<String>());
423             Status result = switchManager.addSubnet(cfgObject);
424             if (result.isSuccess()) {
425                 resultBean.setStatus(true);
426                 resultBean.setMessage("Added gateway address successfully");
427             } else {
428                 resultBean.setStatus(false);
429                 resultBean.setMessage(result.getDescription());
430             }
431         } catch (Exception e) {
432             resultBean.setStatus(false);
433             resultBean.setMessage(e.getMessage());
434         }
435         return resultBean;
436     }
437
438     @RequestMapping(value = "/subnetGateway/delete", method = RequestMethod.GET)
439     @ResponseBody
440     public StatusJsonBean deleteSubnetGateways(
441             @RequestParam("gatewaysToDelete") String gatewaysToDelete,
442             HttpServletRequest request,
443             @RequestParam(required = false) String container) {
444         String containerName = (container == null) ? GlobalConstants.DEFAULT
445                 .toString() : container;
446
447         // Authorization check
448         String userName = request.getUserPrincipal().getName();
449         if (DaylightWebUtil.getContainerPrivilege(userName, container, this) != Privilege.WRITE) {
450             return unauthorizedMessage();
451         }
452
453         StatusJsonBean resultBean = new StatusJsonBean();
454         try {
455             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
456                     .getInstance(ISwitchManager.class, containerName, this);
457             String[] subnets = gatewaysToDelete.split(",");
458             resultBean.setStatus(true);
459             resultBean.setMessage("Added gateway address successfully");
460             for (String subnet : subnets) {
461                 Status result = switchManager.removeSubnet(subnet);
462                 if (!result.isSuccess()) {
463                     resultBean.setStatus(false);
464                     resultBean.setMessage(result.getDescription());
465                     break;
466                 }
467             }
468         } catch (Exception e) {
469             resultBean.setStatus(false);
470             resultBean.setMessage(e.getMessage());
471         }
472         return resultBean;
473     }
474
475     @RequestMapping(value = "/subnetGateway/ports/add", method = RequestMethod.GET)
476     @ResponseBody
477     public StatusJsonBean addSubnetGatewayPort(
478             @RequestParam("portsName") String portsName,
479             @RequestParam("ports") String ports,
480             @RequestParam("nodeId") String nodeId, HttpServletRequest request,
481             @RequestParam(required = false) String container) {
482         String containerName = (container == null) ? GlobalConstants.DEFAULT
483                 .toString() : container;
484
485         // Authorization check
486         String userName = request.getUserPrincipal().getName();
487         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
488             return unauthorizedMessage();
489         }
490
491         StatusJsonBean resultBean = new StatusJsonBean();
492         try {
493             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
494                     .getInstance(ISwitchManager.class, containerName, this);
495             Status result = switchManager.addPortsToSubnet(portsName, nodeId
496                     + "/" + ports);
497
498             if (result.isSuccess()) {
499                 resultBean.setStatus(true);
500                 resultBean
501                         .setMessage("Added ports to subnet gateway address successfully");
502             } else {
503                 resultBean.setStatus(false);
504                 resultBean.setMessage(result.getDescription());
505             }
506         } catch (Exception e) {
507             resultBean.setStatus(false);
508             resultBean.setMessage(e.getMessage());
509         }
510         return resultBean;
511     }
512
513     @RequestMapping(value = "/subnetGateway/ports/delete", method = RequestMethod.GET)
514     @ResponseBody
515     public StatusJsonBean deleteSubnetGatewayPort(
516             @RequestParam("gatewayName") String gatewayName,
517             @RequestParam("nodePort") String nodePort,
518             HttpServletRequest request,
519             @RequestParam(required = false) String container) {
520         String containerName = (container == null) ? GlobalConstants.DEFAULT
521                 .toString() : container;
522
523         // Authorization check
524         String userName = request.getUserPrincipal().getName();
525         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
526             return unauthorizedMessage();
527         }
528
529         StatusJsonBean resultBean = new StatusJsonBean();
530         try {
531             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
532                     .getInstance(ISwitchManager.class, containerName, this);
533             Status result = switchManager.removePortsFromSubnet(gatewayName,
534                     nodePort);
535
536             if (result.isSuccess()) {
537                 resultBean.setStatus(true);
538                 resultBean
539                         .setMessage("Deleted port from subnet gateway address successfully");
540             } else {
541                 resultBean.setStatus(false);
542                 resultBean.setMessage(result.getDescription());
543             }
544         } catch (Exception e) {
545             resultBean.setStatus(false);
546             resultBean.setMessage(e.getMessage());
547         }
548         return resultBean;
549     }
550
551     @RequestMapping(value = "/spanPorts", method = RequestMethod.GET)
552     @ResponseBody
553     public DevicesJsonBean getSpanPorts(HttpServletRequest request,
554             @RequestParam(required = false) String container) {
555         Gson gson = new Gson();
556         List<Map<String, String>> spanConfigs = new ArrayList<Map<String, String>>();
557         String containerName = (container == null) ? GlobalConstants.DEFAULT
558                 .toString() : container;
559
560         // Derive the privilege this user has on the current container
561         String userName = request.getUserPrincipal().getName();
562         Privilege privilege = DaylightWebUtil.getContainerPrivilege(
563                 userName, containerName, this);
564
565         if (privilege != Privilege.NONE) {
566             List<String> spanConfigs_json = new ArrayList<String>();
567             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
568                     .getInstance(ISwitchManager.class, containerName, this);
569             if (switchManager != null) {
570                 for (SpanConfig conf : switchManager.getSpanConfigList()) {
571                     spanConfigs_json.add(gson.toJson(conf));
572                 }
573             }
574             ObjectMapper mapper = new ObjectMapper();
575
576             for (String config_json : spanConfigs_json) {
577                 try {
578                     @SuppressWarnings("unchecked")
579                     Map<String, String> config_data = mapper.readValue(config_json,
580                             HashMap.class);
581                     Map<String, String> config = new HashMap<String, String>();
582                     for (String name : config_data.keySet()) {
583                         config.put(name, config_data.get(name));
584                         // Add switch name value (non-configuration field)
585                         config.put("nodeName",
586                                 getNodeDesc(config_data.get("nodeId"), containerName));
587                     }
588                     config.put("json", config_json);
589                     spanConfigs.add(config);
590                 } catch (Exception e) {
591                     // TODO: Handle the exception.
592                 }
593             }
594         }
595
596         DevicesJsonBean result = new DevicesJsonBean();
597         result.setPrivilege(privilege);
598         result.setColumnNames(SpanConfig.getGuiFieldsNames());
599         result.setNodeData(spanConfigs);
600         return result;
601     }
602
603     @RequestMapping(value = "/nodeports")
604     @ResponseBody
605     public Map<String, Object> getNodePorts(HttpServletRequest request,
606             @RequestParam(required = false) String container) {
607         String containerName = (container == null) ? GlobalConstants.DEFAULT
608                 .toString() : container;
609
610         // Derive the privilege this user has on the current container
611         String userName = request.getUserPrincipal().getName();
612         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
613             return null;
614         }
615
616
617         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
618                 .getInstance(ISwitchManager.class, containerName, this);
619         if (switchManager == null) {
620             return null;
621         }
622
623         Map<String, Object> nodes = new HashMap<String, Object>();
624         Map<Short, String> port;
625
626         for (Switch node : switchManager.getNetworkDevices()) {
627             port = new HashMap<Short, String>(); // new port
628             Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
629
630             if (nodeConnectorSet != null) {
631                 for (NodeConnector nodeConnector : nodeConnectorSet) {
632                     String nodeConnectorName = ((Name) switchManager
633                             .getNodeConnectorProp(nodeConnector,
634                                     Name.NamePropName)).getValue();
635                     port.put((Short) nodeConnector.getID(), nodeConnectorName
636                             + "(" + nodeConnector.getID() + ")");
637                 }
638             }
639
640             nodes.put(node.getNode().toString(), port);
641         }
642
643         return nodes;
644     }
645
646     @RequestMapping(value = "/spanPorts/add", method = RequestMethod.GET)
647     @ResponseBody
648     public StatusJsonBean addSpanPort(
649             @RequestParam("jsonData") String jsonData,
650             HttpServletRequest request,
651             @RequestParam(required = false) String container) {
652         String containerName = (container == null) ? GlobalConstants.DEFAULT
653                 .toString() : container;
654
655         // Authorization check
656         String userName = request.getUserPrincipal().getName();
657         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
658             return unauthorizedMessage();
659         }
660
661         StatusJsonBean resultBean = new StatusJsonBean();
662         try {
663             Gson gson = new Gson();
664             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
665                     .getInstance(ISwitchManager.class, containerName, this);
666             SpanConfig cfgObject = gson.fromJson(jsonData, SpanConfig.class);
667             Status result = switchManager.addSpanConfig(cfgObject);
668             if (result.isSuccess()) {
669                 resultBean.setStatus(true);
670                 resultBean.setMessage("SPAN Port added successfully");
671             } else {
672                 resultBean.setStatus(false);
673                 resultBean.setMessage(result.getDescription());
674             }
675         } catch (Exception e) {
676             resultBean.setStatus(false);
677             resultBean.setMessage("Error occurred while adding span port. "
678                     + e.getMessage());
679         }
680         return resultBean;
681     }
682
683     @RequestMapping(value = "/spanPorts/delete", method = RequestMethod.GET)
684     @ResponseBody
685     public StatusJsonBean deleteSpanPorts(
686             @RequestParam("spanPortsToDelete") String spanPortsToDelete,
687             HttpServletRequest request,
688             @RequestParam(required = false) String container) {
689         String containerName = (container == null) ? GlobalConstants.DEFAULT
690                 .toString() : container;
691
692         // Authorization check
693         String userName = request.getUserPrincipal().getName();
694         if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
695             return unauthorizedMessage();
696         }
697
698         StatusJsonBean resultBean = new StatusJsonBean();
699         try {
700             Gson gson = new Gson();
701             ISwitchManager switchManager = (ISwitchManager) ServiceHelper
702                     .getInstance(ISwitchManager.class, containerName, this);
703             String[] spans = spanPortsToDelete.split("###");
704             resultBean.setStatus(true);
705             resultBean.setMessage("SPAN Port(s) deleted successfully");
706             for (String span : spans) {
707                 if (!span.isEmpty()) {
708                     SpanConfig cfgObject = gson
709                             .fromJson(span, SpanConfig.class);
710                     Status result = switchManager.removeSpanConfig(cfgObject);
711                     if (!result.isSuccess()) {
712                         resultBean.setStatus(false);
713                         resultBean.setMessage(result.getDescription());
714                         break;
715                     }
716                 }
717             }
718         } catch (Exception e) {
719             resultBean.setStatus(false);
720             resultBean.setMessage("Error occurred while deleting span port. "
721                     + e.getMessage());
722         }
723         return resultBean;
724     }
725
726     private String getNodeDesc(String nodeId, String containerName) {
727         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
728                 .getInstance(ISwitchManager.class, containerName, this);
729         String description = "";
730         if (switchManager != null) {
731             description = switchManager.getNodeDescription(Node
732                     .fromString(nodeId));
733         }
734         return (description.isEmpty() || description.equalsIgnoreCase("none")) ? nodeId
735                 : description;
736     }
737
738     private StatusJsonBean unauthorizedMessage() {
739         StatusJsonBean message = new StatusJsonBean();
740         message.setStatus(false);
741         message.setMessage("Operation not authorized");
742         return message;
743     }
744
745     @RequestMapping(value = "login")
746     public String login(final HttpServletRequest request,
747             final HttpServletResponse response) {
748         // response.setHeader("X-Page-Location", "/login");
749         /*
750          * IUserManager userManager = (IUserManager) ServiceHelper
751          * .getGlobalInstance(IUserManager.class, this); if (userManager ==
752          * null) { return "User Manager is not available"; }
753          *
754          * String username = request.getUserPrincipal().getName();
755          *
756          *
757          * model.addAttribute("username", username); model.addAttribute("role",
758          * userManager.getUserLevel(username).toNumber());
759          */
760         return "forward:" + "/";
761     }
762
763 }