Charles Chan
Committed by Ray Milkey

Sort portstats result by port number

Change-Id: I1165ae5d05f1ea557e7841d76977ad69060f01c2
...@@ -18,8 +18,10 @@ package org.onosproject.cli.net; ...@@ -18,8 +18,10 @@ package org.onosproject.cli.net;
18 18
19 import static org.onosproject.net.DeviceId.deviceId; 19 import static org.onosproject.net.DeviceId.deviceId;
20 20
21 +import java.util.List;
21 import java.util.concurrent.TimeUnit; 22 import java.util.concurrent.TimeUnit;
22 23
24 +import com.google.common.collect.Lists;
23 import org.apache.karaf.shell.commands.Argument; 25 import org.apache.karaf.shell.commands.Argument;
24 import org.apache.karaf.shell.commands.Command; 26 import org.apache.karaf.shell.commands.Command;
25 import org.apache.karaf.shell.commands.Option; 27 import org.apache.karaf.shell.commands.Option;
...@@ -90,9 +92,8 @@ public class DevicePortStatsCommand extends DevicesListCommand { ...@@ -90,9 +92,8 @@ public class DevicePortStatsCommand extends DevicesListCommand {
90 * @param portStats 92 * @param portStats
91 */ 93 */
92 private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) { 94 private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) {
93 -
94 print("deviceId=%s", deviceId); 95 print("deviceId=%s", deviceId);
95 - for (PortStatistics stat : portStats) { 96 + for (PortStatistics stat : sortByPort(portStats)) {
96 print(FORMAT, stat.port(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(), 97 print(FORMAT, stat.port(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(),
97 stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec()); 98 stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec());
98 } 99 }
...@@ -107,7 +108,7 @@ public class DevicePortStatsCommand extends DevicesListCommand { ...@@ -107,7 +108,7 @@ public class DevicePortStatsCommand extends DevicesListCommand {
107 final String formatDelta = " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s," 108 final String formatDelta = " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s,"
108 + " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s"; 109 + " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s";
109 print("deviceId=%s", deviceId); 110 print("deviceId=%s", deviceId);
110 - for (PortStatistics stat : portStats) { 111 + for (PortStatistics stat : sortByPort(portStats)) {
111 float duration = ((float) stat.durationSec()) + 112 float duration = ((float) stat.durationSec()) +
112 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1)); 113 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
113 float rateRx = stat.bytesReceived() * 8 / duration; 114 float rateRx = stat.bytesReceived() * 8 / duration;
...@@ -140,7 +141,7 @@ public class DevicePortStatsCommand extends DevicesListCommand { ...@@ -140,7 +141,7 @@ public class DevicePortStatsCommand extends DevicesListCommand {
140 print("| Port | Packets | Bytes | Rate bps | Drop | Packets | Bytes | Rate bps | Drop | Interval |"); 141 print("| Port | Packets | Bytes | Rate bps | Drop | Packets | Bytes | Rate bps | Drop | Interval |");
141 print("|---------------------------------------------------------------------------------------------------|"); 142 print("|---------------------------------------------------------------------------------------------------|");
142 143
143 - for (PortStatistics stat : portStats) { 144 + for (PortStatistics stat : sortByPort(portStats)) {
144 float duration = ((float) stat.durationSec()) + 145 float duration = ((float) stat.durationSec()) +
145 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1)); 146 (((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
146 float rateRx = stat.bytesReceived() * 8 / duration; 147 float rateRx = stat.bytesReceived() * 8 / duration;
...@@ -189,4 +190,11 @@ public class DevicePortStatsCommand extends DevicesListCommand { ...@@ -189,4 +190,11 @@ public class DevicePortStatsCommand extends DevicesListCommand {
189 Character pre = ("KMGTPE").charAt(exp - 1); 190 Character pre = ("KMGTPE").charAt(exp - 1);
190 return String.format("%.2f%s", bps / Math.pow(unit, exp), pre); 191 return String.format("%.2f%s", bps / Math.pow(unit, exp), pre);
191 } 192 }
193 +
194 + private static List<PortStatistics> sortByPort(Iterable<PortStatistics> portStats) {
195 + List<PortStatistics> portStatsList = Lists.newArrayList(portStats);
196 + portStatsList.sort((PortStatistics o1, PortStatistics o2) ->
197 + o1.port() - o2.port());
198 + return portStatsList;
199 + }
192 } 200 }
......