Charles Chan
Committed by Ray Milkey

Sort portstats result by port number

Change-Id: I1165ae5d05f1ea557e7841d76977ad69060f01c2
......@@ -18,8 +18,10 @@ package org.onosproject.cli.net;
import static org.onosproject.net.DeviceId.deviceId;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.google.common.collect.Lists;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.apache.karaf.shell.commands.Option;
......@@ -90,9 +92,8 @@ public class DevicePortStatsCommand extends DevicesListCommand {
* @param portStats
*/
private void printPortStats(DeviceId deviceId, Iterable<PortStatistics> portStats) {
print("deviceId=%s", deviceId);
for (PortStatistics stat : portStats) {
for (PortStatistics stat : sortByPort(portStats)) {
print(FORMAT, stat.port(), stat.packetsReceived(), stat.packetsSent(), stat.bytesReceived(),
stat.bytesSent(), stat.packetsRxDropped(), stat.packetsTxDropped(), stat.durationSec());
}
......@@ -107,7 +108,7 @@ public class DevicePortStatsCommand extends DevicesListCommand {
final String formatDelta = " port=%s, pktRx=%s, pktTx=%s, bytesRx=%s, bytesTx=%s,"
+ " rateRx=%s, rateTx=%s, pktRxDrp=%s, pktTxDrp=%s, interval=%s";
print("deviceId=%s", deviceId);
for (PortStatistics stat : portStats) {
for (PortStatistics stat : sortByPort(portStats)) {
float duration = ((float) stat.durationSec()) +
(((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
float rateRx = stat.bytesReceived() * 8 / duration;
......@@ -140,7 +141,7 @@ public class DevicePortStatsCommand extends DevicesListCommand {
print("| Port | Packets | Bytes | Rate bps | Drop | Packets | Bytes | Rate bps | Drop | Interval |");
print("|---------------------------------------------------------------------------------------------------|");
for (PortStatistics stat : portStats) {
for (PortStatistics stat : sortByPort(portStats)) {
float duration = ((float) stat.durationSec()) +
(((float) stat.durationNano()) / TimeUnit.SECONDS.toNanos(1));
float rateRx = stat.bytesReceived() * 8 / duration;
......@@ -189,4 +190,11 @@ public class DevicePortStatsCommand extends DevicesListCommand {
Character pre = ("KMGTPE").charAt(exp - 1);
return String.format("%.2f%s", bps / Math.pow(unit, exp), pre);
}
private static List<PortStatistics> sortByPort(Iterable<PortStatistics> portStats) {
List<PortStatistics> portStatsList = Lists.newArrayList(portStats);
portStatsList.sort((PortStatistics o1, PortStatistics o2) ->
o1.port() - o2.port());
return portStatsList;
}
}
......