use of org.onosproject.net.device.PortStatistics in project onos by opennetworkinglab.
the class ServerDevicesDiscovery method getGlobalMonitoringStatistics.
/**
* Query a server to retrieve its global monitoring statistics.
*
* @param deviceId the device ID to be queried
* @return global monitoring statistics
*/
public MonitoringStatistics getGlobalMonitoringStatistics(DeviceId deviceId) {
// Monitoring statistics to return
MonitoringStatistics monStats = null;
RestServerSBDevice device = null;
try {
device = (RestServerSBDevice) getDevice(deviceId);
} catch (ClassCastException ccEx) {
log.error("Failed to retrieve global monitoring statistics from device {}", deviceId);
return monStats;
}
if ((device == null) || (!device.isActive())) {
return monStats;
}
// Hit the path that provides the server's global resources
InputStream response = null;
try {
response = getController().get(deviceId, URL_SRV_GLOBAL_STATS, JSON);
} catch (ProcessingException pEx) {
log.error("Failed to retrieve global monitoring statistics from device {}", deviceId);
raiseDeviceDisconnect(device);
return monStats;
}
// Load the JSON into objects
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> jsonMap = null;
ObjectNode objNode = null;
try {
jsonMap = mapper.readValue(response, Map.class);
JsonNode jsonNode = mapper.convertValue(jsonMap, JsonNode.class);
objNode = (ObjectNode) jsonNode;
} catch (IOException ioEx) {
log.error("Failed to retrieve global monitoring statistics from device {}", deviceId);
raiseDeviceDisconnect(device);
return monStats;
}
if (jsonMap == null) {
log.error("Failed to retrieve global monitoring statistics from device {}", deviceId);
raiseDeviceDisconnect(device);
return monStats;
}
// Get high-level CPU statistics
int busyCpus = objNode.path(PARAM_MON_BUSY_CPUS).asInt();
int freeCpus = objNode.path(PARAM_MON_FREE_CPUS).asInt();
// Get a list of CPU statistics per core
Collection<CpuStatistics> cpuStats = parseCpuStatistics(deviceId, objNode);
// Get main memory statistics
MemoryStatistics memStats = parseMemoryStatistics(deviceId, objNode);
// Get a list of port statistics
Collection<PortStatistics> nicStats = parseNicStatistics(deviceId, objNode);
// Get zero timing statistics
TimingStatistics timinsgStats = getZeroTimingStatistics();
// Construct a global monitoring statistics object out of smaller ones
monStats = DefaultMonitoringStatistics.builder().setDeviceId(deviceId).setTimingStatistics(timinsgStats).setCpuStatistics(cpuStats).setMemoryStatistics(memStats).setNicStatistics(nicStats).build();
// When a device reports monitoring data, it means it is alive
raiseDeviceReconnect(device);
log.debug("Global monitoring statistics: {}", monStats.toString());
return monStats;
}
use of org.onosproject.net.device.PortStatistics 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.net.device.PortStatistics in project onos by opennetworkinglab.
the class StatisticsWebResource method getPortStatistics.
/**
* Gets port statistics of all devices.
* @onos.rsModel StatisticsPorts
* @return 200 OK with JSON encoded array of port statistics
*/
@GET
@Path("ports")
@Produces(MediaType.APPLICATION_JSON)
public Response getPortStatistics() {
final DeviceService service = get(DeviceService.class);
final Iterable<Device> devices = service.getDevices();
final ObjectNode root = mapper().createObjectNode();
final ArrayNode rootArrayNode = root.putArray("statistics");
for (final Device device : devices) {
final ObjectNode deviceStatsNode = mapper().createObjectNode();
deviceStatsNode.put("device", device.id().toString());
final ArrayNode statisticsNode = deviceStatsNode.putArray("ports");
final Iterable<PortStatistics> portStatsEntries = service.getPortStatistics(device.id());
if (portStatsEntries != null) {
for (final PortStatistics entry : portStatsEntries) {
statisticsNode.add(codec(PortStatistics.class).encode(entry, this));
}
}
rootArrayNode.add(deviceStatsNode);
}
return ok(root).build();
}
use of org.onosproject.net.device.PortStatistics in project onos by opennetworkinglab.
the class StatisticsWebResource method getPortDeltaStatistics.
/**
* Gets port delta statistics of all devices.
* @onos.rsModel StatisticsPorts
* @return 200 OK with JSON encoded array of port delta statistics
*/
@GET
@Path("delta/ports")
@Produces(MediaType.APPLICATION_JSON)
public Response getPortDeltaStatistics() {
final DeviceService service = get(DeviceService.class);
final Iterable<Device> devices = service.getDevices();
final ObjectNode root = mapper().createObjectNode();
final ArrayNode rootArrayNode = root.putArray("statistics");
for (final Device device : devices) {
final ObjectNode deviceStatsNode = mapper().createObjectNode();
deviceStatsNode.put("device", device.id().toString());
final ArrayNode statisticsNode = deviceStatsNode.putArray("ports");
final Iterable<PortStatistics> portStatsEntries = service.getPortDeltaStatistics(device.id());
if (portStatsEntries != null) {
for (final PortStatistics entry : portStatsEntries) {
statisticsNode.add(codec(PortStatistics.class).encode(entry, this));
}
}
rootArrayNode.add(deviceStatsNode);
}
return ok(root).build();
}
use of org.onosproject.net.device.PortStatistics in project onos by opennetworkinglab.
the class StatisticsWebResource method getPortDeltaStatisticsByDeviceIdAndPort.
/**
* Gets port delta statistics of a specified device and port.
* @onos.rsModel StatisticsPorts
* @param deviceId device ID
* @param port port
* @return 200 OK with JSON encoded array of port delta statistics for the specified port
*/
@GET
@Path("delta/ports/{deviceId}/{port}")
@Produces(MediaType.APPLICATION_JSON)
public Response getPortDeltaStatisticsByDeviceIdAndPort(@PathParam("deviceId") String deviceId, @PathParam("port") String port) {
final DeviceService service = get(DeviceService.class);
final PortNumber portNumber = portNumber(port);
final PortStatistics portStatsEntry = service.getDeltaStatisticsForPort(DeviceId.deviceId(deviceId), portNumber);
final ObjectNode root = mapper().createObjectNode();
final ArrayNode rootArrayNode = root.putArray("statistics");
final ObjectNode deviceStatsNode = mapper().createObjectNode();
deviceStatsNode.put("device", deviceId);
final ArrayNode statisticsNode = deviceStatsNode.putArray("ports");
if (portStatsEntry != null) {
statisticsNode.add(codec(PortStatistics.class).encode(portStatsEntry, this));
}
rootArrayNode.add(deviceStatsNode);
return ok(root).build();
}
Aggregations