use of org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.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);
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.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();
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.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);
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.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();
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.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();
}
Aggregations