use of org.onosproject.p4runtime.api.P4RuntimeClient in project onos by opennetworkinglab.
the class PortStatisticsDiscoveryImpl method discoverPortStatistics.
@Override
public Collection<PortStatistics> discoverPortStatistics() {
DeviceService deviceService = this.handler().get(DeviceService.class);
DeviceId deviceId = this.data().deviceId();
PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class);
if (!piPipeconfService.ofDevice(deviceId).isPresent() || !piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) {
log.warn("Unable to get the pipeconf of {}, aborting operation", deviceId);
return Collections.emptyList();
}
PiPipeconf pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get();
P4RuntimeController controller = handler().get(P4RuntimeController.class);
P4RuntimeClient client = controller.get(deviceId);
if (client == null) {
log.warn("Unable to find client for {}, aborting operation", deviceId);
return Collections.emptyList();
}
Map<Long, DefaultPortStatistics.Builder> portStatBuilders = Maps.newHashMap();
deviceService.getPorts(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 -> {
// Counter cell/index = port number.
counterCellIds.add(PiCounterCellId.ofIndirect(ingressCounterId(), p));
counterCellIds.add(PiCounterCellId.ofIndirect(egressCounterId(), p));
});
Set<PiCounterCellHandle> counterCellHandles = counterCellIds.stream().map(id -> PiCounterCellHandle.of(deviceId, id)).collect(Collectors.toSet());
// Query the device.
Collection<PiCounterCell> counterEntryResponse = client.read(DEFAULT_P4_DEVICE_ID, pipeconf).handles(counterCellHandles).submitSync().all(PiCounterCell.class);
counterEntryResponse.forEach(counterCell -> {
if (counterCell.cellId().counterType() != INDIRECT) {
log.warn("Invalid counter data type {}, skipping", counterCell.cellId().counterType());
return;
}
PiCounterCellId indCellId = counterCell.cellId();
if (!portStatBuilders.containsKey(indCellId.index())) {
log.warn("Unrecognized counter index {}, skipping", counterCell);
return;
}
DefaultPortStatistics.Builder statsBuilder = portStatBuilders.get(indCellId.index());
if (counterCell.cellId().counterId().equals(ingressCounterId())) {
statsBuilder.setPacketsReceived(counterCell.data().packets());
statsBuilder.setBytesReceived(counterCell.data().bytes());
} else if (counterCell.cellId().counterId().equals(egressCounterId())) {
statsBuilder.setPacketsSent(counterCell.data().packets());
statsBuilder.setBytesSent(counterCell.data().bytes());
} else {
log.warn("Unrecognized counter ID {}, skipping", counterCell);
}
});
return portStatBuilders.values().stream().map(DefaultPortStatistics.Builder::build).collect(Collectors.toList());
}
use of org.onosproject.p4runtime.api.P4RuntimeClient in project onos by opennetworkinglab.
the class PortStatisticsDiscoveryImpl method discoverPortStatistics.
@Override
public Collection<PortStatistics> discoverPortStatistics() {
DeviceService deviceService = this.handler().get(DeviceService.class);
DeviceId deviceId = this.data().deviceId();
// Get a client for this device.
P4RuntimeController controller = handler().get(P4RuntimeController.class);
P4RuntimeClient client = controller.get(deviceId);
if (client == null) {
log.warn("Unable to find client for {}, aborting operation", deviceId);
return Collections.emptyList();
}
// Get the pipeconf of this device.
PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class);
if (!piPipeconfService.ofDevice(deviceId).isPresent() || !piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) {
log.warn("Unable to get the pipeconf of {}, aborting operation", deviceId);
return Collections.emptyList();
}
PiPipeconf pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get();
// Prepare PortStatistics objects to return, one per port of this device.
Map<Long, DefaultPortStatistics.Builder> portStatBuilders = Maps.newHashMap();
deviceService.getPorts(deviceId).forEach(p -> portStatBuilders.put(p.number().toLong(), DefaultPortStatistics.builder().setPort(p.number()).setDeviceId(deviceId)));
// Generate the counter cell IDs.
Set<PiCounterCellId> counterCellIds = Sets.newHashSet();
portStatBuilders.keySet().forEach(p -> {
// Counter cell/index = port number.
counterCellIds.add(PiCounterCellId.ofIndirect(INGRESS_COUNTER_ID, p));
counterCellIds.add(PiCounterCellId.ofIndirect(EGRESS_COUNTER_ID, p));
});
Set<PiCounterCellHandle> counterCellHandles = counterCellIds.stream().map(id -> PiCounterCellHandle.of(deviceId, id)).collect(Collectors.toSet());
// Query the device.
Collection<PiCounterCell> counterEntryResponse = client.read(DEFAULT_P4_DEVICE_ID, pipeconf).handles(counterCellHandles).submitSync().all(PiCounterCell.class);
// Process response.
counterEntryResponse.forEach(counterCell -> {
if (counterCell.cellId().counterType() != INDIRECT) {
log.warn("Invalid counter data type {}, skipping", counterCell.cellId().counterType());
return;
}
if (!portStatBuilders.containsKey(counterCell.cellId().index())) {
log.warn("Unrecognized counter index {}, skipping", counterCell);
return;
}
DefaultPortStatistics.Builder statsBuilder = portStatBuilders.get(counterCell.cellId().index());
if (counterCell.cellId().counterId().equals(INGRESS_COUNTER_ID)) {
statsBuilder.setPacketsReceived(counterCell.data().packets());
statsBuilder.setBytesReceived(counterCell.data().bytes());
} else if (counterCell.cellId().counterId().equals(EGRESS_COUNTER_ID)) {
statsBuilder.setPacketsSent(counterCell.data().packets());
statsBuilder.setBytesSent(counterCell.data().bytes());
} else {
log.warn("Unrecognized counter ID {}, skipping", counterCell);
}
});
return portStatBuilders.values().stream().map(DefaultPortStatistics.Builder::build).collect(Collectors.toList());
}
Aggregations