Stats CLI support
diff --git a/src/main/java/net/floodlightcontroller/core/web/OFPortStatsEntryMod.java b/src/main/java/net/floodlightcontroller/core/web/OFPortStatsEntryMod.java
new file mode 100644
index 0000000..222f279
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/core/web/OFPortStatsEntryMod.java
@@ -0,0 +1,23 @@
+package net.floodlightcontroller.core.web;
+
+import net.floodlightcontroller.core.web.serializers.OFPortStatsEntrySerializer;
+
+import org.codehaus.jackson.map.annotate.JsonSerialize;
+import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
+
+@JsonSerialize(using = OFPortStatsEntrySerializer.class)
+public class OFPortStatsEntryMod {
+	private OFPortStatsEntry portStatsEntry = null;
+
+	public OFPortStatsEntryMod() {
+	}
+
+	public OFPortStatsEntryMod(OFPortStatsEntry portStatsEntry) {
+		this.portStatsEntry = portStatsEntry;
+	}
+
+	public OFPortStatsEntry getPortStatsEntry() {
+		return portStatsEntry;
+	}
+}
+
diff --git a/src/main/java/net/floodlightcontroller/core/web/SwitchResourceBase.java b/src/main/java/net/floodlightcontroller/core/web/SwitchResourceBase.java
index 0c2188a..38b0d1e 100644
--- a/src/main/java/net/floodlightcontroller/core/web/SwitchResourceBase.java
+++ b/src/main/java/net/floodlightcontroller/core/web/SwitchResourceBase.java
@@ -17,6 +17,7 @@
 
 package net.floodlightcontroller.core.web;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
@@ -28,6 +29,9 @@
 import org.projectfloodlight.openflow.protocol.OFFeaturesReply;
 import org.projectfloodlight.openflow.protocol.OFMatchV3;
 import org.projectfloodlight.openflow.protocol.OFOxmList;
+import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
+import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
+import org.projectfloodlight.openflow.protocol.OFPortStatsRequest;
 import org.projectfloodlight.openflow.protocol.OFStatsReply;
 import org.projectfloodlight.openflow.protocol.OFStatsRequest;
 import org.projectfloodlight.openflow.protocol.OFStatsType;
@@ -97,13 +101,6 @@
                 specificReq.setTableId((byte) 0xff);
                 req.setStatistics(Collections.singletonList((OFStatistics) specificReq));
                 requestLength += specificReq.getLength();
-            } */else if (statType == OFStatsType.PORT) {
-            	log.debug("Switch Port Stats: req sent for all "
-            			+ "ports in switch {}", sw.getStringId());
-                req = sw.getFactory()
-        	 		.buildPortStatsRequest()
-        	 		.setPortNo(OFPort.ANY).setXid
-        	 		(sw.getNextTransactionId()).build();
             } /*else if (statType == OFStatisticsType.QUEUE) {
                 OFQueueStatisticsRequest specificReq = new OFQueueStatisticsRequest();
                 specificReq.setPortNumber(OFPort.OFPP_ALL.getValue());
@@ -127,7 +124,43 @@
         return values;
     }
 
-    protected List<OFStatsReply> getSwitchStatistics(String switchId, OFStatsType statType) {
+    protected List<OFPortStatsEntryMod> getSwitchPortStatistics(long switchId) {
+    	IFloodlightProviderService floodlightProvider =
+    				(IFloodlightProviderService) getContext().getAttributes().
+    				get(IFloodlightProviderService.class.getCanonicalName());
+
+    	IOFSwitch sw = floodlightProvider.getSwitches().get(switchId);
+    	Future<List<OFStatsReply>> future;
+    	List<OFStatsReply> values = null;
+    	List<OFPortStatsEntryMod> portStats = null;
+
+    	if (sw != null) {
+        	log.debug("Switch Port Stats: req sent for all "
+        			+ "ports in switch {}", sw.getStringId());
+       	    OFPortStatsRequest req = sw.getFactory()
+    	 		.buildPortStatsRequest()
+    	 		.setPortNo(OFPort.ANY).setXid
+    	 		(sw.getNextTransactionId()).build();
+
+            try {
+                future = sw.getStatistics(req);
+                values = future.get(10, TimeUnit.SECONDS);
+                portStats = new ArrayList<OFPortStatsEntryMod>();
+                for (OFPortStatsEntry entry : ((OFPortStatsReply)values.get(0)).getEntries()) {
+                    OFPortStatsEntryMod entryMod = new OFPortStatsEntryMod(entry);
+                    portStats.add(entryMod);
+                }
+            	log.debug("Switch Port Stats Entries from switch {} are {}",
+            			sw.getStringId(), portStats);
+            } catch (Exception e) {
+                log.error("Failure retrieving statistics from switch " + sw, e);
+            }
+    	}
+
+    	return portStats;
+    }
+
+    protected Object getSwitchStatistics(String switchId, OFStatsType statType) {
         return getSwitchStatistics(HexString.toLong(switchId), statType);
     }
 
diff --git a/src/main/java/net/floodlightcontroller/core/web/SwitchStatisticsResource.java b/src/main/java/net/floodlightcontroller/core/web/SwitchStatisticsResource.java
index e86bb61..8c794ab 100644
--- a/src/main/java/net/floodlightcontroller/core/web/SwitchStatisticsResource.java
+++ b/src/main/java/net/floodlightcontroller/core/web/SwitchStatisticsResource.java
@@ -18,9 +18,9 @@
 package net.floodlightcontroller.core.web;
 
 import java.util.HashMap;
-import java.util.Map;
 
 import org.projectfloodlight.openflow.protocol.OFStatsType;
+import org.projectfloodlight.openflow.util.HexString;
 import org.restlet.resource.Get;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -36,7 +36,7 @@
             LoggerFactory.getLogger(SwitchStatisticsResource.class);
 
     @Get("json")
-    public Map<String, Object> retrieve() {
+    public HashMap<String, Object> retrieve() {
         HashMap<String, Object> result = new HashMap<String, Object>();
         Object values = null;
 
@@ -44,7 +44,7 @@
         String statType = (String) getRequestAttributes().get("statType");
 
         if (statType.equals("port")) {
-            values = getSwitchStatistics(switchId, OFStatsType.PORT);
+            values = getSwitchPortStatistics(HexString.toLong(switchId));
         } else if (statType.equals("queue")) {
             values = getSwitchStatistics(switchId, OFStatsType.QUEUE);
         } else if (statType.equals("flow")) {
@@ -61,5 +61,6 @@
 
         result.put(switchId, values);
         return result;
+        //return toRepresentation(result, null);
     }
 }
diff --git a/src/main/java/net/floodlightcontroller/core/web/serializers/OFPortStatsEntrySerializer.java b/src/main/java/net/floodlightcontroller/core/web/serializers/OFPortStatsEntrySerializer.java
new file mode 100644
index 0000000..c9293f7
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/core/web/serializers/OFPortStatsEntrySerializer.java
@@ -0,0 +1,42 @@
+package net.floodlightcontroller.core.web.serializers;
+
+import java.io.IOException;
+
+import net.floodlightcontroller.core.web.OFPortStatsEntryMod;
+
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.map.SerializerProvider;
+import org.codehaus.jackson.map.ser.std.SerializerBase;
+import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
+
+public class OFPortStatsEntrySerializer extends SerializerBase<OFPortStatsEntryMod> {
+
+    protected OFPortStatsEntrySerializer() {
+        super(OFPortStatsEntryMod.class);
+    }
+
+    @Override
+    public void serialize(OFPortStatsEntryMod portStatModEntry, JsonGenerator jGen,
+    		SerializerProvider sp) throws IOException, JsonGenerationException {
+
+    	OFPortStatsEntry portStatEntry = portStatModEntry.getPortStatsEntry();
+        jGen.writeStartObject();
+        jGen.writeNumberField("portNumber", portStatEntry.getPortNo().getPortNumber());
+        jGen.writeNumberField("receivePackets", portStatEntry.getRxPackets().getValue());
+        jGen.writeNumberField("transmitPackets", portStatEntry.getTxPackets().getValue());
+        jGen.writeNumberField("receiveBytes", portStatEntry.getRxBytes().getValue());
+        jGen.writeNumberField("transmitBytes", portStatEntry.getTxBytes().getValue());
+        jGen.writeNumberField("receiveDropped", portStatEntry.getRxDropped().getValue());
+        jGen.writeNumberField("transmitDropped", portStatEntry.getTxDropped().getValue());
+        jGen.writeNumberField("receiveErrors", portStatEntry.getRxErrors().getValue());
+        jGen.writeNumberField("transmitErrors", portStatEntry.getTxErrors().getValue());
+        jGen.writeNumberField("receiveFrameErrors", portStatEntry.getRxFrameErr().getValue());
+        jGen.writeNumberField("receiveOverrunErrors", portStatEntry.getRxOverErr().getValue());
+        jGen.writeNumberField("receiveCRCErrors", portStatEntry.getRxCrcErr().getValue());
+        jGen.writeNumberField("collisions", portStatEntry.getCollisions().getValue());
+
+        jGen.writeEndObject();
+    }
+
+}