use of org.onosproject.net.device.DeviceService in project onos by opennetworkinglab.
the class DevicesWebResource method getDevicePorts.
/**
* Gets ports of infrastructure device.
* Returns details of the specified infrastructure device.
*
* @onos.rsModel DeviceGetPorts
* @param id device identifier
* @return 200 OK with a collection of ports of the given device
*/
@GET
@Path("{id}/ports")
@Produces(MediaType.APPLICATION_JSON)
public Response getDevicePorts(@PathParam("id") String id) {
DeviceService service = get(DeviceService.class);
Device device = nullIsNotFound(service.getDevice(deviceId(id)), DEVICE_NOT_FOUND);
List<Port> ports = checkNotNull(service.getPorts(deviceId(id)), "Ports could not be retrieved");
ObjectNode result = codec(Device.class).encode(device, this);
result.set("ports", codec(Port.class).encode(ports, this));
return ok(result).build();
}
use of org.onosproject.net.device.DeviceService in project onos by opennetworkinglab.
the class FabricInterpreter method mapOutboundPacket.
@Override
public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet) throws PiInterpreterException {
DeviceId deviceId = packet.sendThrough();
TrafficTreatment treatment = packet.treatment();
// fabric.p4 supports only OUTPUT instructions.
List<Instructions.OutputInstruction> outInstructions = treatment.allInstructions().stream().filter(i -> i.type().equals(OUTPUT)).map(i -> (Instructions.OutputInstruction) i).collect(toList());
if (treatment.allInstructions().size() != outInstructions.size()) {
// There are other instructions that are not of type OUTPUT.
throw new PiInterpreterException("Treatment not supported: " + treatment);
}
ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
for (Instructions.OutputInstruction outInst : outInstructions) {
if (outInst.port().equals(TABLE)) {
// Logical port. Forward using the switch tables like a regular packet.
builder.add(createPiPacketOperation(packet.data(), -1, true));
} else if (outInst.port().equals(FLOOD)) {
// Logical port. Create a packet operation for each switch port.
final DeviceService deviceService = handler().get(DeviceService.class);
for (Port port : deviceService.getPorts(packet.sendThrough())) {
builder.add(createPiPacketOperation(packet.data(), port.number().toLong(), false));
}
} else if (outInst.port().isLogical()) {
throw new PiInterpreterException(format("Output on logical port '%s' not supported", outInst.port()));
} else {
// Send as-is to given port bypassing all switch tables.
builder.add(createPiPacketOperation(packet.data(), outInst.port().toLong(), false));
}
}
return builder.build();
}
use of org.onosproject.net.device.DeviceService 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.DeviceService in project onos by opennetworkinglab.
the class TrafficMonitorBase method createTrafficLinks.
protected void createTrafficLinks(Highlights highlights, TrafficLinkMap linkMap, Set<Intent> intents, LinkHighlight.Flavor flavor, boolean showTraffic) {
for (Intent intent : intents) {
List<Intent> installables = services.intent().getInstallableIntents(intent.key());
Iterable<Link> links = null;
if (installables != null) {
for (Intent installable : installables) {
if (installable instanceof PathIntent) {
links = ((PathIntent) installable).path().links();
} else if (installable instanceof FlowRuleIntent) {
Collection<Link> l = new ArrayList<>();
l.addAll(linkResources(installable));
// Add cross connect links
if (intent instanceof OpticalConnectivityIntent) {
OpticalConnectivityIntent ocIntent = (OpticalConnectivityIntent) intent;
LinkService linkService = services.link();
DeviceService deviceService = services.device();
l.addAll(linkService.getDeviceIngressLinks(ocIntent.getSrc().deviceId()).stream().filter(i -> deviceService.getDevice(i.src().deviceId()).type() == Device.Type.SWITCH).collect(Collectors.toList()));
l.addAll(linkService.getDeviceEgressLinks(ocIntent.getDst().deviceId()).stream().filter(e -> deviceService.getDevice(e.dst().deviceId()).type() == Device.Type.SWITCH).collect(Collectors.toList()));
}
links = l;
} else if (installable instanceof FlowObjectiveIntent) {
links = linkResources(installable);
} else if (installable instanceof LinkCollectionIntent) {
links = ((LinkCollectionIntent) installable).links();
} else if (installable instanceof OpticalPathIntent) {
links = ((OpticalPathIntent) installable).path().links();
}
if (links == null) {
links = Lists.newArrayList();
}
links = addEdgeLinksIfNeeded(intent, Lists.newArrayList(links));
boolean isOptical = intent instanceof OpticalConnectivityIntent;
processLinks(linkMap, links, flavor, isOptical, showTraffic);
updateHighlights(highlights, links);
}
}
}
}
use of org.onosproject.net.device.DeviceService 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();
}
Aggregations