Fix few commands handling PortNumber as args.

- should now be able to handle named ports

Change-Id: Ic913f2ac8e1cfd7a0fa2a7631bd5c207b9747eee
diff --git a/cli/src/main/java/org/onosproject/cli/net/DevicePortStateCommand.java b/cli/src/main/java/org/onosproject/cli/net/DevicePortStateCommand.java
index 490d514..7cd177f 100644
--- a/cli/src/main/java/org/onosproject/cli/net/DevicePortStateCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/DevicePortStateCommand.java
@@ -37,7 +37,7 @@
 
     @Argument(index = 1, name = "portNumber", description = "Port Number",
             required = true, multiValued = false)
-    Integer portNumber = null;
+    String portNumber = null;
 
     @Argument(index = 2, name = "portState",
             description = "Desired State. Either \"enable\" or \"disable\".",
@@ -53,7 +53,7 @@
             print(" %s", "Device does not exist");
             return;
         }
-        PortNumber pnum = PortNumber.portNumber(portNumber);
+        PortNumber pnum = PortNumber.fromString(portNumber);
         Port p = deviceService.getPort(dev.id(), pnum);
         if (p == null) {
             print(" %s", "Port does not exist");
diff --git a/cli/src/main/java/org/onosproject/cli/net/DevicePortStatsCommand.java b/cli/src/main/java/org/onosproject/cli/net/DevicePortStatsCommand.java
index 2aa631f..eb07071 100644
--- a/cli/src/main/java/org/onosproject/cli/net/DevicePortStatsCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/DevicePortStatsCommand.java
@@ -18,6 +18,7 @@
 import static org.onosproject.cli.net.DevicesListCommand.getSortedDevices;
 import static org.onosproject.net.DeviceId.deviceId;
 
+import java.util.Comparator;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 
@@ -28,6 +29,7 @@
 import org.onosproject.cli.AbstractShellCommand;
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
 import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.device.PortStatistics;
 
@@ -60,7 +62,9 @@
 
     @Argument(index = 1, name = "portNumber", description = "Port Number",
             required = false, multiValued = false)
-    Integer portNumber = null;
+    String portNumberStr = null;
+
+    PortNumber portNumber = null;
 
     private static final String FORMAT =
             "   port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s, pktRxDrp=%s, pktTxDrp=%s, Dur=%s";
@@ -69,6 +73,10 @@
     protected void execute() {
         DeviceService deviceService = get(DeviceService.class);
 
+        if (portNumberStr != null) {
+            portNumber = PortNumber.fromString(portNumberStr);
+        }
+
         if (uri == null) {
             for (Device d : getSortedDevices(deviceService)) {
                 if (delta) {
@@ -106,17 +114,22 @@
     private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) {
         print("deviceId=%s", deviceId);
         for (PortStatistics stat : sortByPort(portStats)) {
-            if (portNumber != null && stat.port() != portNumber) {
+            if (isIrrelevant(stat)) {
                 continue;
             }
             if (nonzero && stat.isZero()) {
                 continue;
             }
-            print(FORMAT, stat.port(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(),
+            print(FORMAT, stat.portNumber(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(),
                     stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec());
         }
     }
 
+    private boolean isIrrelevant(PortStatistics stat) {
+        // TODO revisit logical port (e.g., ALL) handling
+        return portNumber != null && !portNumber.equals(stat.portNumber());
+    }
+
     /**
      * Prints Port delta statistics.
      *
@@ -128,7 +141,7 @@
                 + " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s";
         print("deviceId=%s", deviceId);
         for (PortStatistics stat : sortByPort(portStats)) {
-            if (portNumber != null && stat.port() != portNumber) {
+            if (isIrrelevant(stat)) {
                 continue;
             }
             if (nonzero && stat.isZero()) {
@@ -138,7 +151,7 @@
                     (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
             float rateRx = stat.bytesReceived() * 8 / duration;
             float rateTx = stat.bytesSent() * 8 / duration;
-            print(formatDelta, stat.port(),
+            print(formatDelta, stat.portNumber(),
                     stat.packetsReceived(),
                     stat.packetsSent(),
                     stat.bytesReceived(),
@@ -167,7 +180,7 @@
         print("|---------------------------------------------------------------------------------------------------|");
 
         for (PortStatistics stat : sortByPort(portStats)) {
-            if (portNumber != null && stat.port() != portNumber) {
+            if (isIrrelevant(stat)) {
                 continue;
             }
             if (nonzero && stat.isZero()) {
@@ -177,7 +190,7 @@
                     (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
             float rateRx = duration > 0 ? stat.bytesReceived() * 8 / duration : 0;
             float rateTx = duration > 0 ? stat.bytesSent() * 8 / duration : 0;
-            print(formatDeltaTable, stat.port(),
+            print(formatDeltaTable, stat.portNumber(),
                   humanReadable(stat.packetsReceived()),
                   humanReadable(stat.bytesReceived()),
                   humanReadableBps(rateRx),
@@ -225,8 +238,8 @@
 
     private static List<PortStatistics> sortByPort(Iterable<PortStatistics> portStats) {
         List<PortStatistics> portStatsList = Lists.newArrayList(portStats);
-        portStatsList.sort((PortStatistics o1, PortStatistics o2) ->
-                o1.port() - o2.port());
+
+        portStatsList.sort(Comparator.comparing(ps -> ps.portNumber().toLong()));
         return portStatsList;
     }
 }