/** * Returns a collection of port statistics for given ports using the given BMv2 device agent. * * @param deviceAgent a device agent * @param ports a collection of ports * @return a collection of port statistics */ static Collection<PortStatistics> getPortStatistics(Bmv2DeviceAgent deviceAgent, Collection<Port> ports) { List<PortStatistics> ps = Lists.newArrayList(); for (Port port : ports) { int portNumber = (int) port.number().toLong(); try { Pair<Long, Long> egressCounter = deviceAgent.readCounter(EGRESS_COUNTER, portNumber); Pair<Long, Long> ingressCounter = deviceAgent.readCounter(INGRESS_COUNTER, portNumber); ps.add(DefaultPortStatistics.builder() .setPort(portNumber) .setBytesSent(egressCounter.getLeft()) .setPacketsSent(egressCounter.getRight()) .setBytesReceived(ingressCounter.getLeft()) .setPacketsReceived(ingressCounter.getRight()) .build()); } catch (Bmv2RuntimeException e) { log.info("Unable to read port statistics from {}: {}", port, e.explain()); } } return ps; }
private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId, List<OFPortStatsEntry> entries) { HashSet<PortStatistics> stats = Sets.newHashSet(); for (OFPortStatsEntry entry : entries) { try { if (entry == null || entry.getPortNo() == null || entry.getPortNo().getPortNumber() < 0) { continue; } DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder(); DefaultPortStatistics stat = builder.setDeviceId(deviceId) .setPort(entry.getPortNo().getPortNumber()) .setPacketsReceived(entry.getRxPackets().getValue()) .setPacketsSent(entry.getTxPackets().getValue()) .setBytesReceived(entry.getRxBytes().getValue()) .setBytesSent(entry.getTxBytes().getValue()) .setPacketsRxDropped(entry.getRxDropped().getValue()) .setPacketsTxDropped(entry.getTxDropped().getValue()) .setPacketsRxErrors(entry.getRxErrors().getValue()) .setPacketsTxErrors(entry.getTxErrors().getValue()) .setDurationSec(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec()) .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec()) .build(); stats.add(stat); } catch (Exception e) { LOG.warn("Unable to process port stats", e); } } return Collections.unmodifiableSet(stats); }