Merge "Revert "Checkstyle enforcer""
[controller.git] / opendaylight / web / troubleshoot / src / main / java / org / opendaylight / controller / troubleshoot / web / Troubleshoot.java
index e20c35abf1fe0a49c02d0a29f12d967d4c98d1cd..f145c568b58b18bf62efdda84eadbadefb5934b7 100644 (file)
@@ -16,6 +16,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Set;
 
+import javax.servlet.http.HttpServletRequest;
+
 import org.opendaylight.controller.sal.action.Action;
 import org.opendaylight.controller.sal.action.Output;
 import org.opendaylight.controller.sal.action.SetVlanId;
@@ -32,11 +34,12 @@ import org.opendaylight.controller.sal.utils.EtherTypes;
 import org.opendaylight.controller.sal.utils.GlobalConstants;
 import org.opendaylight.controller.sal.utils.HexEncode;
 import org.opendaylight.controller.sal.utils.IPProtocols;
+import org.opendaylight.controller.sal.utils.NetUtils;
 import org.opendaylight.controller.sal.utils.ServiceHelper;
 import org.opendaylight.controller.statisticsmanager.IStatisticsManager;
 import org.opendaylight.controller.switchmanager.ISwitchManager;
-import org.opendaylight.controller.switchmanager.SwitchConfig;
-import org.opendaylight.controller.web.IOneWeb;
+import org.opendaylight.controller.web.DaylightWebUtil;
+import org.opendaylight.controller.web.IDaylightWeb;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -45,15 +48,14 @@ import org.springframework.web.bind.annotation.ResponseBody;
 
 @Controller
 @RequestMapping("/")
-public class Troubleshoot implements IOneWeb {
+public class Troubleshoot implements IDaylightWeb {
     private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER;
     private final String WEB_NAME = "Troubleshoot";
     private final String WEB_ID = "troubleshoot";
     private final short WEB_ORDER = 4;
-    private final String containerName = GlobalConstants.DEFAULT.toString();
 
     public Troubleshoot() {
-        ServiceHelper.registerGlobalService(IOneWeb.class, this, null);
+        ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null);
     }
 
     @Override
@@ -78,22 +80,19 @@ public class Troubleshoot implements IOneWeb {
 
     @RequestMapping(value = "/existingNodes", method = RequestMethod.GET)
     @ResponseBody
-    public TroubleshootingJsonBean getExistingNodes() {
+    public TroubleshootingJsonBean getExistingNodes(HttpServletRequest request, @RequestParam(required = false) String container) {
+        String containerName = DaylightWebUtil.getAuthorizedContainer(request, container, this);
         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
                 .getInstance(ISwitchManager.class, containerName, this);
         List<HashMap<String, String>> lines = new ArrayList<HashMap<String, String>>();
         Set<Node> nodeSet = null;
         if (switchManager != null) {
             nodeSet = switchManager.getNodes();
-        } else {
-            // TODO: Change to use logger instead.
-            System.out.println("SwitchManager reference is NULL");
         }
         if (nodeSet != null) {
             for (Node node : nodeSet) {
                 HashMap<String, String> device = new HashMap<String, String>();
-                SwitchConfig switchConfig = switchManager.getSwitchConfig(node.getNodeIDString());
-                device.put("nodeName", switchConfig == null ? "" : switchConfig.getNodeName());
+                device.put("nodeName", switchManager.getNodeDescription(node));
                 device.put("nodeId", node.toString());
                 lines.add(device);
             }
@@ -101,7 +100,7 @@ public class Troubleshoot implements IOneWeb {
         TroubleshootingJsonBean result = new TroubleshootingJsonBean();
 
         List<String> guiFieldNames = new ArrayList<String>();
-        guiFieldNames.add("Node Names");
+        guiFieldNames.add("Node");
         guiFieldNames.add("Node ID");
         guiFieldNames.add("Statistics");
 
@@ -112,22 +111,19 @@ public class Troubleshoot implements IOneWeb {
 
     @RequestMapping(value = "/uptime", method = RequestMethod.GET)
     @ResponseBody
-    public TroubleshootingJsonBean getUptime() {
+    public TroubleshootingJsonBean getUptime(HttpServletRequest request, @RequestParam(required = false) String container) {
+        String containerName = DaylightWebUtil.getAuthorizedContainer(request, container, this);
         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
                 .getInstance(ISwitchManager.class, containerName, this);
         List<HashMap<String, String>> lines = new ArrayList<HashMap<String, String>>();
         Set<Node> nodeSet = null;
         if (switchManager != null) {
             nodeSet = switchManager.getNodes();
-        } else {
-            // TODO: Change to use logger instead.
-            System.out.println("SwitchManager reference is NULL");
         }
         if (nodeSet != null) {
             for (Node node : nodeSet) {
                 HashMap<String, String> device = new HashMap<String, String>();
-                SwitchConfig switchConfig = switchManager.getSwitchConfig(node.getNodeIDString());
-                device.put("nodeName", switchConfig == null ? "" : switchConfig.getNodeName());
+                device.put("nodeName", switchManager.getNodeDescription(node));
                 device.put("nodeId", node.toString());
                 TimeStamp timeStamp = (TimeStamp) switchManager.getNodeProp(
                         node, TimeStamp.TimeStampPropName);
@@ -140,7 +136,7 @@ public class Troubleshoot implements IOneWeb {
         TroubleshootingJsonBean result = new TroubleshootingJsonBean();
 
         List<String> guiFieldNames = new ArrayList<String>();
-        guiFieldNames.add("Node Names");
+        guiFieldNames.add("Node");
         guiFieldNames.add("Node ID");
         guiFieldNames.add("Connected");
 
@@ -152,15 +148,17 @@ public class Troubleshoot implements IOneWeb {
     @RequestMapping(value = "/flowStats", method = RequestMethod.GET)
     @ResponseBody
     public TroubleshootingJsonBean getFlowStats(
-            @RequestParam("nodeId") String nodeId) {
+            @RequestParam("nodeId") String nodeId,
+            HttpServletRequest request, @RequestParam(required = false) String container) {
         Node node = Node.fromString(nodeId);
         List<HashMap<String, String>> cells = new ArrayList<HashMap<String, String>>();
+        String containerName = DaylightWebUtil.getAuthorizedContainer(request, container, this);
         IStatisticsManager statisticsManager = (IStatisticsManager) ServiceHelper
                 .getInstance(IStatisticsManager.class, containerName, this);
 
         List<FlowOnNode> statistics = statisticsManager.getFlows(node);
         for (FlowOnNode stats : statistics) {
-            cells.add(this.convertFlowStatistics(node, stats));
+            cells.add(this.convertFlowStatistics(node, stats, containerName));
         }
         List<String> columnNames = new ArrayList<String>();
         columnNames.addAll(Arrays.asList(new String[] { "Node", "In Port",
@@ -177,9 +175,11 @@ public class Troubleshoot implements IOneWeb {
     @RequestMapping(value = "/portStats", method = RequestMethod.GET)
     @ResponseBody
     public TroubleshootingJsonBean getPortStats(
-            @RequestParam("nodeId") String nodeId) {
+            @RequestParam("nodeId") String nodeId,
+            HttpServletRequest request, @RequestParam(required = false) String container) {
         Node node = Node.fromString(nodeId);
         List<HashMap<String, String>> cells = new ArrayList<HashMap<String, String>>();
+        String containerName = DaylightWebUtil.getAuthorizedContainer(request, container, this);
         IStatisticsManager statisticsManager = (IStatisticsManager) ServiceHelper
                 .getInstance(IStatisticsManager.class, containerName, this);
         List<NodeConnectorStatistics> statistics = statisticsManager
@@ -224,14 +224,22 @@ public class Troubleshoot implements IOneWeb {
     }
 
     private HashMap<String, String> convertFlowStatistics(Node node,
-            FlowOnNode flowOnNode) {
+            FlowOnNode flowOnNode,
+            String containerName) {
         HashMap<String, String> row = new HashMap<String, String>();
         Flow flow = flowOnNode.getFlow();
         Match match = flow.getMatch();
-        row.put("nodeName", getNodeName(node));
+        ISwitchManager switchManager = (ISwitchManager) ServiceHelper
+                .getInstance(ISwitchManager.class, containerName, this);
+        String desc = (switchManager == null)? 
+                       "" : switchManager.getNodeDescription(node);
+        desc = (desc.isEmpty() || desc.equalsIgnoreCase("none"))? 
+                       node.toString(): desc;
+        row.put("nodeName", desc);
         if (match.isPresent(MatchType.IN_PORT)) {
             row.put(MatchType.IN_PORT.id(), ((NodeConnector) flow.getMatch()
-                    .getField(MatchType.IN_PORT).getValue()).getID().toString());
+                    .getField(MatchType.IN_PORT).getValue())
+                    .getNodeConnectorIdAsString());
         } else {
             row.put(MatchType.IN_PORT.id(), "*");
         }
@@ -291,26 +299,16 @@ public class Troubleshoot implements IOneWeb {
         if (match.isPresent(MatchType.TP_SRC)) {
             Short tpSrc = (Short) (flow.getMatch().getField(MatchType.TP_SRC)
                     .getValue());
-            if (tpSrc < 0) {
-                row.put(MatchType.TP_SRC.id(),
-                        ((Integer) (tpSrc.intValue() & 0x7FFF | 0x8000))
-                                .toString());
-            } else {
-                row.put(MatchType.TP_SRC.id(), tpSrc.toString());
-            }
+            row.put(MatchType.TP_SRC.id(),
+                       String.valueOf(NetUtils.getUnsignedShort(tpSrc)));
         } else {
             row.put(MatchType.TP_SRC.id(), "*");
         }
         if (match.isPresent(MatchType.TP_DST)) {
             Short tpDst = (Short) (flow.getMatch().getField(MatchType.TP_DST)
                     .getValue());
-            if (tpDst < 0) {
-                row.put(MatchType.TP_DST.id(),
-                        ((Integer) (tpDst.intValue() & 0x7FFF | 0x8000))
-                                .toString());
-            } else {
-                row.put(MatchType.TP_DST.id(), tpDst.toString());
-            }
+            row.put(MatchType.TP_DST.id(),
+                       String.valueOf(NetUtils.getUnsignedShort(tpDst)));
         } else {
             row.put(MatchType.TP_DST.id(), "*");
         }
@@ -350,17 +348,4 @@ public class Troubleshoot implements IOneWeb {
         return row;
     }
 
-    private String getNodeName(Node node) {
-        String nodeName = "";
-        ISwitchManager switchManager = (ISwitchManager) ServiceHelper
-                .getInstance(ISwitchManager.class, containerName, this);
-        if (switchManager != null) {
-            SwitchConfig config = switchManager.getSwitchConfig(node
-                    .getNodeIDString());
-            if (config != null) {
-                nodeName = config.getNodeName();
-            }
-        }
-        return nodeName;
-    }
 }