Fixed port duration 0 on P4Runtime devices running basic.4 and fabric.p4
Change-Id: I3e2e81be6753f25a6b8c6b602804686fc1c05677
diff --git a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/PortStatisticsDiscoveryImpl.java b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/PortStatisticsDiscoveryImpl.java
index 31c7266..ec8d5cf 100644
--- a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/PortStatisticsDiscoveryImpl.java
+++ b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/PortStatisticsDiscoveryImpl.java
@@ -18,7 +18,9 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
+import org.apache.commons.lang3.tuple.Pair;
import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
import org.onosproject.net.device.DefaultPortStatistics;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.device.PortStatistics;
@@ -50,6 +52,9 @@
*/
public class PortStatisticsDiscoveryImpl extends AbstractHandlerBehaviour implements PortStatisticsDiscovery {
+ private static final Map<Pair<DeviceId, PortNumber>, Long> PORT_START_TIMES =
+ Maps.newConcurrentMap();
+
protected final Logger log = LoggerFactory.getLogger(getClass());
/**
@@ -93,10 +98,12 @@
Map<Long, DefaultPortStatistics.Builder> portStatBuilders = Maps.newHashMap();
deviceService.getPorts(deviceId)
- .forEach(p -> portStatBuilders.put(p.number().toLong(),
- DefaultPortStatistics.builder()
- .setPort(p.number())
- .setDeviceId(deviceId)));
+ .forEach(p -> portStatBuilders.put(
+ p.number().toLong(),
+ DefaultPortStatistics.builder()
+ .setPort(p.number())
+ .setDeviceId(deviceId)
+ .setDurationSec(getDuration(p.number()))));
Set<PiCounterCellId> counterCellIds = Sets.newHashSet();
portStatBuilders.keySet().forEach(p -> {
@@ -134,6 +141,7 @@
} else {
log.warn("Unrecognized counter ID {}, skipping", counterData);
}
+
});
return portStatBuilders
@@ -142,4 +150,13 @@
.map(DefaultPortStatistics.Builder::build)
.collect(Collectors.toList());
}
+
+ private long getDuration(PortNumber port) {
+ // FIXME: This is a workaround since we cannot determine the port
+ // duration from a P4 counter. We'll be fixed by gNMI.
+ final long now = System.currentTimeMillis() / 1000;
+ final Long startTime = PORT_START_TIMES.putIfAbsent(
+ Pair.of(data().deviceId(), port), now);
+ return startTime == null ? now : now - startTime;
+ }
}