use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class LinkDiscovery method processLldp.
private boolean processLldp(PacketContext packetContext, Ethernet eth) {
ONOSLLDP onoslldp = ONOSLLDP.parseLLDP(eth);
if (onoslldp != null) {
Type lt = eth.getEtherType() == Ethernet.TYPE_LLDP ? Type.DIRECT : Type.INDIRECT;
DeviceService deviceService = context.deviceService();
MacAddress srcChassisId = onoslldp.getChassisIdByMac();
String srcPortName = onoslldp.getPortNameString();
String srcPortDesc = onoslldp.getPortDescString();
log.debug("srcChassisId:{}, srcPortName:{}, srcPortDesc:{}", srcChassisId, srcPortName, srcPortDesc);
if (srcChassisId == null && srcPortDesc == null) {
log.warn("there are no valid port id");
return false;
}
Optional<Device> srcDevice = findSourceDeviceByChassisId(deviceService, srcChassisId);
if (!srcDevice.isPresent()) {
log.debug("source device not found. srcChassisId value: {}", srcChassisId);
return false;
}
Optional<Port> sourcePort = findSourcePortByName(srcPortName == null ? srcPortDesc : srcPortName, deviceService, srcDevice.get());
if (!sourcePort.isPresent()) {
log.debug("source port not found. sourcePort value: {}", sourcePort);
return false;
}
PortNumber srcPort = sourcePort.get().number();
PortNumber dstPort = packetContext.inPacket().receivedFrom().port();
DeviceId srcDeviceId = srcDevice.get().id();
DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId();
if (!sourcePort.get().isEnabled()) {
log.debug("Ports are disabled. Cannot create a link between {}/{} and {}/{}", srcDeviceId, sourcePort.get(), dstDeviceId, dstPort);
return false;
}
ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
DefaultAnnotations annotations = DefaultAnnotations.builder().set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()).set(AnnotationKeys.LAYER, ETHERNET).build();
LinkDescription ld = new DefaultLinkDescription(src, dst, lt, true, annotations);
try {
context.providerService().linkDetected(ld);
context.setTtl(LinkKey.linkKey(src, dst), onoslldp.getTtlBySeconds());
} catch (IllegalStateException e) {
log.debug("There is a exception during link creation: {}", e);
return true;
}
return true;
}
return false;
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class GnmiDeviceStateSubscriber method subscribeIfNeeded.
private void subscribeIfNeeded(DeviceId deviceId) {
Set<PortNumber> ports = deviceService.getPorts(deviceId).stream().map(Port::number).collect(Collectors.toSet());
if (Objects.equals(ports, deviceSubscribed.get(deviceId))) {
// Already subscribed for the same ports.
return;
}
// Subscribe for the new set of ports.
deviceSubscribed.put(deviceId, ports);
// Send subscription request.
final SubscriptionList subscriptionList = SubscriptionList.newBuilder().setMode(SubscriptionList.Mode.STREAM).setUpdatesOnly(true).addAllSubscription(ports.stream().map(port -> Subscription.newBuilder().setPath(interfaceStatePath(port.name())).setMode(SubscriptionMode.ON_CHANGE).build()).collect(Collectors.toList())).build();
gnmiController.get(deviceId).subscribe(SubscribeRequest.newBuilder().setSubscribe(subscriptionList).build());
log.info("Started gNMI subscription for {} ports on {}", ports.size(), deviceId);
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class BasicInterpreterImpl method mapInboundPacket.
@Override
public InboundPacket mapInboundPacket(PiPacketOperation packetIn, DeviceId deviceId) throws PiInterpreterException {
// Assuming that the packet is ethernet, which is fine since basic.p4
// can deparse only ethernet packets.
Ethernet ethPkt;
try {
ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0, packetIn.data().size());
} catch (DeserializationException dex) {
throw new PiInterpreterException(dex.getMessage());
}
// Returns the ingress port packet metadata.
Optional<PiPacketMetadata> packetMetadata = packetIn.metadatas().stream().filter(m -> m.id().equals(INGRESS_PORT)).findFirst();
if (packetMetadata.isPresent()) {
ImmutableByteSequence portByteSequence = packetMetadata.get().value();
short s = portByteSequence.asReadOnlyBuffer().getShort();
ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
} else {
throw new PiInterpreterException(format("Missing metadata '%s' in packet-in received from '%s': %s", INGRESS_PORT, deviceId, packetIn));
}
}
use of org.onosproject.net.Port in project onos by opennetworkinglab.
the class BasicInterpreterImpl method mapOutboundPacket.
@Override
public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet) throws PiInterpreterException {
TrafficTreatment treatment = packet.treatment();
// basic.p4 supports only OUTPUT instructions.
List<OutputInstruction> outInstructions = treatment.allInstructions().stream().filter(i -> i.type().equals(OUTPUT)).map(i -> (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 (OutputInstruction outInst : outInstructions) {
if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
throw new PiInterpreterException(format("Output on logical port '%s' not supported", outInst.port()));
} else if (outInst.port().equals(FLOOD)) {
// Since basic.p4 does not support flooding, we 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()));
}
} else {
builder.add(createPiPacketOperation(packet.data(), outInst.port().toLong()));
}
}
return builder.build();
}
use of org.onosproject.net.Port in project fabric-tna by stratum.
the class FabricInterpreterTest method testMapInboundPacketWithPortTranslation.
@Test
public void testMapInboundPacketWithPortTranslation() throws ImmutableByteSequence.ByteSequenceTrimException, PiPipelineInterpreter.PiInterpreterException {
PortNumber inputPort = PortNumber.portNumber(1, "ONE");
ConnectPoint receiveFrom = new ConnectPoint(DEVICE_ID, inputPort);
Port port = createNiceMock(Port.class);
expect(port.number()).andReturn(inputPort).anyTimes();
replay(port);
expect(deviceService.getPort(receiveFrom)).andReturn(port).anyTimes();
replay(deviceService);
PiPacketMetadata pktInMetadata = PiPacketMetadata.builder().withId(P4InfoConstants.INGRESS_PORT).withValue(ImmutableByteSequence.copyFrom(inputPort.toLong()).fit(P4InfoConstants.INGRESS_PORT_BITWIDTH)).build();
Ethernet packet = new Ethernet();
packet.setDestinationMACAddress(SRC_MAC);
packet.setSourceMACAddress(DST_MAC);
packet.setEtherType((short) 0xBA00);
packet.setPayload(new Data());
PiPacketOperation pktInOp = PiPacketOperation.builder().withMetadata(pktInMetadata).withData(ImmutableByteSequence.copyFrom(packet.serialize())).withType(PiPacketOperationType.PACKET_IN).build();
InboundPacket result = interpreter.mapInboundPacket(pktInOp, DEVICE_ID);
InboundPacket expectedInboundPacket = new DefaultInboundPacket(receiveFrom, packet, ByteBuffer.wrap(packet.serialize()));
assertEquals(result.receivedFrom(), expectedInboundPacket.receivedFrom());
assertEquals(result.receivedFrom().port().name(), expectedInboundPacket.receivedFrom().port().name());
assertEquals(result.parsed(), expectedInboundPacket.parsed());
assertEquals(result.cookie(), expectedInboundPacket.cookie());
assertEquals(result.unparsed(), expectedInboundPacket.unparsed());
}
Aggregations