Search in sources :

Example 11 with PacketInMessage

use of org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage in project openflowplugin by opendaylight.

the class PacketInMessageFactoryTest method testSerialize.

@Test
public void testSerialize() throws Exception {
    PacketInMessageBuilder builder = new PacketInMessageBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
    builder.setBufferId(256L);
    builder.setTotalLen(10);
    builder.setReason(PacketInReason.forValue(0));
    builder.setTableId(new TableId(1L));
    byte[] cookie = new byte[] { (byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01 };
    builder.setCookie(new BigInteger(1, cookie));
    MatchBuilder matchBuilder = new MatchBuilder();
    matchBuilder.setType(OxmMatchType.class);
    final List<MatchEntry> entries = new ArrayList<>();
    MatchEntryBuilder entriesBuilder = new MatchEntryBuilder();
    entriesBuilder.setOxmClass(OpenflowBasicClass.class);
    entriesBuilder.setOxmMatchField(InPhyPort.class);
    entriesBuilder.setHasMask(false);
    InPhyPortCaseBuilder inPhyPortCaseBuilder = new InPhyPortCaseBuilder();
    InPhyPortBuilder inPhyPortBuilder = new InPhyPortBuilder();
    inPhyPortBuilder.setPortNumber(new PortNumber(42L));
    inPhyPortCaseBuilder.setInPhyPort(inPhyPortBuilder.build());
    entriesBuilder.setMatchEntryValue(inPhyPortCaseBuilder.build());
    entries.add(entriesBuilder.build());
    entriesBuilder.setOxmClass(OpenflowBasicClass.class);
    entriesBuilder.setOxmMatchField(IpEcn.class);
    entriesBuilder.setHasMask(false);
    IpEcnCaseBuilder ipEcnCaseBuilder = new IpEcnCaseBuilder();
    IpEcnBuilder ipEcnBuilder = new IpEcnBuilder();
    ipEcnBuilder.setEcn((short) 4);
    ipEcnCaseBuilder.setIpEcn(ipEcnBuilder.build());
    entriesBuilder.setMatchEntryValue(ipEcnCaseBuilder.build());
    entries.add(entriesBuilder.build());
    matchBuilder.setMatchEntry(entries);
    builder.setMatch(matchBuilder.build());
    byte[] data = ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14");
    builder.setData(data);
    PacketInMessage message = builder.build();
    ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    factory.serialize(message, serializedBuffer);
    BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 66);
    Assert.assertEquals("Wrong BufferId", message.getBufferId().longValue(), serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong actions length", message.getTotalLen().intValue(), serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong reason", message.getReason().getIntValue(), serializedBuffer.readUnsignedByte());
    Assert.assertEquals("Wrong tableId", message.getTableId().getValue().intValue(), serializedBuffer.readUnsignedByte());
    cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
    serializedBuffer.readBytes(cookie);
    Assert.assertEquals("Wrong cookie", message.getCookie(), new BigInteger(1, cookie));
    Assert.assertEquals("Wrong match type", 1, serializedBuffer.readUnsignedShort());
    serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
    Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort());
    short fieldAndMask = serializedBuffer.readUnsignedByte();
    Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
    Assert.assertEquals("Wrong oxm field", 1, fieldAndMask >> 1);
    serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
    Assert.assertEquals("Wrong oxm value", 42, serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort());
    fieldAndMask = serializedBuffer.readUnsignedByte();
    Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
    Assert.assertEquals("Wrong oxm field", 9, fieldAndMask >> 1);
    serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
    Assert.assertEquals("Wrong oxm value", 4, serializedBuffer.readUnsignedByte());
    serializedBuffer.skipBytes(7);
    serializedBuffer.skipBytes(PADDING);
    byte[] readData = new byte[serializedBuffer.readableBytes()];
    serializedBuffer.readBytes(readData);
    Assert.assertArrayEquals("Wrong data", message.getData(), readData);
}
Also used : TableId(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId) MatchEntry(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry) IpEcnBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.ecn._case.IpEcnBuilder) ArrayList(java.util.ArrayList) PacketInMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage) ByteBuf(io.netty.buffer.ByteBuf) InPhyPortBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.phy.port._case.InPhyPortBuilder) PacketInMessageBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder) MatchEntryBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder) InPhyPortCaseBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPhyPortCaseBuilder) BigInteger(java.math.BigInteger) PortNumber(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber) IpEcnCaseBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.IpEcnCaseBuilder) MatchBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder) Test(org.junit.Test)

Example 12 with PacketInMessage

use of org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage in project openflowplugin by opendaylight.

the class PacketInMessageDeserializer method deserialize.

@Override
public PacketInMessage deserialize(final ByteBuf message) {
    final PacketInMessageBuilder packetInMessageBuilder = new PacketInMessageBuilder().setVersion((short) EncodeConstants.OF13_VERSION_ID).setXid(message.readUnsignedInt());
    // We are ignoring buffer id and total len as it is not specified in OpenFlowPlugin models
    message.readUnsignedInt();
    message.readUnsignedShort();
    packetInMessageBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(PacketInReason.forValue(message.readUnsignedByte()))).setTableId(new TableId(message.readUnsignedByte()));
    final byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
    message.readBytes(cookie);
    packetInMessageBuilder.setFlowCookie(new FlowCookie(new BigInteger(1, cookie)));
    final OFDeserializer<Match> matchDeserializer = Preconditions.checkNotNull(registry).getDeserializer(MATCH_KEY);
    packetInMessageBuilder.setMatch(MatchUtil.transformMatch(matchDeserializer.deserialize(message), org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.in.message.Match.class));
    message.skipBytes(PADDING_IN_PACKET_IN_HEADER);
    final byte[] data = new byte[message.readableBytes()];
    message.readBytes(data);
    return packetInMessageBuilder.setPayload(data).build();
}
Also used : TableId(org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId) Match(org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match) PacketInMessageBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketInMessageBuilder) FlowCookie(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie) BigInteger(java.math.BigInteger)

Example 13 with PacketInMessage

use of org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage in project openflowplugin by opendaylight.

the class PacketInMessageDeserializerTest method deserialize.

@Test
public void deserialize() throws Exception {
    buffer.writeByte(TYPE);
    buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
    buffer.writeInt(XID);
    // Buffer id - irrelevant
    buffer.writeInt(EncodeConstants.EMPTY_VALUE);
    // Total len - irrelevant
    buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
    buffer.writeByte(REASON);
    buffer.writeByte(TABLE_ID);
    buffer.writeLong(FLOW_COOKIE);
    // Match header
    int matchStartIndex = buffer.writerIndex();
    buffer.writeShort(OXM_MATCH_TYPE_CODE);
    int matchLengthIndex = buffer.writerIndex();
    buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
    // MplsLabel match
    buffer.writeShort(OxmMatchConstants.OPENFLOW_BASIC_CLASS);
    buffer.writeByte(OxmMatchConstants.MPLS_LABEL << 1);
    buffer.writeByte(EncodeConstants.SIZE_OF_INT_IN_BYTES);
    buffer.writeInt(MPLS_LABEL);
    // Match footer
    int matchLength = buffer.writerIndex() - matchStartIndex;
    buffer.setShort(matchLengthIndex, matchLength);
    int paddingRemainder = matchLength % EncodeConstants.PADDING;
    if (paddingRemainder != 0) {
        buffer.writeZero(EncodeConstants.PADDING - paddingRemainder);
    }
    buffer.writeZero(PADDING_IN_PACKET_IN_HEADER);
    buffer.writeBytes(PAYLOAD);
    final PacketInMessage message = (PacketInMessage) getFactory().deserialize(buffer, EncodeConstants.OF13_VERSION_ID);
    assertEquals(XID, message.getXid().intValue());
    assertEquals(PacketInUtil.getMdSalPacketInReason(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason.forValue(REASON)), message.getPacketInReason());
    assertEquals(TABLE_ID, message.getTableId().getValue().shortValue());
    assertEquals(FLOW_COOKIE, message.getFlowCookie().getValue().longValue());
    assertEquals(MPLS_LABEL, message.getMatch().getProtocolMatchFields().getMplsLabel().intValue());
    assertArrayEquals(PAYLOAD, message.getPayload());
    assertEquals(buffer.readableBytes(), 0);
}
Also used : PacketInMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketInMessage) Test(org.junit.Test) AbstractDeserializerTest(org.opendaylight.openflowplugin.impl.protocol.deserialization.AbstractDeserializerTest)

Example 14 with PacketInMessage

use of org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage in project openflowplugin by opendaylight.

the class PacketReceivedTranslator method translate.

@Override
public PacketReceived translate(final PacketInMessage input, final DeviceInfo deviceInfo, final Object connectionDistinguisher) {
    PacketReceivedBuilder packetReceivedBuilder = new PacketReceivedBuilder();
    BigInteger datapathId = deviceInfo.getDatapathId();
    // TODO: connection cookie from connection distinguisher
    packetReceivedBuilder.setPayload(input.getData());
    // get the Cookie if it exists
    if (input.getCookie() != null) {
        packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
    }
    // Try to create the NodeConnectorRef
    BigInteger dataPathId = deviceInfo.getDatapathId();
    NodeConnectorRef nodeConnectorRef = NodeConnectorRefToPortTranslator.toNodeConnectorRef(input, dataPathId);
    // If we was able to create NodeConnectorRef, use it
    if (nodeConnectorRef != null) {
        packetReceivedBuilder.setIngress(nodeConnectorRef);
    }
    packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
    if (input.getTableId() != null) {
        packetReceivedBuilder.setTableId(new TableId(input.getTableId().getValue().shortValue()));
    }
    if (input.getMatch() != null) {
        org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match packetInMatch = getPacketInMatch(input, datapathId);
        packetReceivedBuilder.setMatch(packetInMatch);
    }
    return packetReceivedBuilder.build();
}
Also used : TableId(org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId) FlowCookie(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie) NodeConnectorRef(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef) BigInteger(java.math.BigInteger) PacketReceivedBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder)

Example 15 with PacketInMessage

use of org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage in project openflowplugin by opendaylight.

the class PacketReceivedTranslatorTest method createPacketInMessage.

private static PacketInMessage createPacketInMessage(final byte[] data, final long port) {
    final PacketInReason reason = PacketInReason.OFPRACTION;
    MatchEntryBuilder matchEntryBuilder = assembleMatchEntryBld(port);
    MatchBuilder packetInMatchBld = new MatchBuilder().setMatchEntry(Lists.newArrayList(matchEntryBuilder.build()));
    return new PacketInMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_0).setData(data).setReason(reason).setMatch(packetInMatchBld.build()).setVersion(OFConstants.OFP_VERSION_1_3).setCookie(BigInteger.ZERO).setTableId(new TableId(42L)).build();
}
Also used : TableId(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId) PacketInMessageBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder) MatchEntryBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder) PacketInReason(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason) MatchBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder)

Aggregations

Test (org.junit.Test)11 PacketInMessage (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage)10 PacketInMessageBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder)7 ByteBuf (io.netty.buffer.ByteBuf)5 BigInteger (java.math.BigInteger)5 PacketReceived (org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived)5 TableId (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId)4 FlowCookie (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 NotificationPublishService (org.opendaylight.controller.md.sal.binding.api.NotificationPublishService)2 NodeConnectorRef (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef)2 MatchEntryBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder)2 MatchBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder)2 TableId (org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 DeviceInfo (org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo)1 MessageTranslator (org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator)1