Update OF header lenght 40/51640/5
authormiroslav.macko <miroslav.macko@pantheon.tech>
Thu, 9 Feb 2017 16:01:47 +0000 (17:01 +0100)
committermiroslav.macko <miroslav.macko@pantheon.tech>
Wed, 15 Feb 2017 16:13:33 +0000 (17:13 +0100)
- Set OF header length base on the writer index

Change-Id: I48358e22a92ce400df8917a7e23a0a085b2b0a95
Signed-off-by: miroslav.macko <miroslav.macko@pantheon.tech>
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactory.java
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowRemovedMessageFactory.java
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GroupModInputMessageFactory.java
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/PortModInputMessageFactory.java
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactoryTest.java
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowRemovedMessageFactoryTest.java
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GroupModInputMessageFactoryTest.java
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/PortModInputMessageFactoryTest.java
openflowjava-util/src/main/java/org/opendaylight/openflowjava/util/ByteBufUtils.java
openflowjava-util/src/test/java/org/opendaylight/openflowjava/util/ByteBufUtilsTest.java

index 62fab01423f16ba25ea58d26642e754210be0c39..336b3d0347f192f5aac155145c4bd230a21908af 100644 (file)
@@ -38,6 +38,7 @@ public class FlowModInputMessageFactory implements OFSerializer<FlowMod>, Serial
 
     @Override
     public void serialize(final FlowMod message, final ByteBuf outBuffer) {
 
     @Override
     public void serialize(final FlowMod message, final ByteBuf outBuffer) {
+        int index = outBuffer.writerIndex();
         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
         outBuffer.writeLong(message.getCookie().longValue());
         outBuffer.writeLong(message.getCookieMask().longValue());
         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
         outBuffer.writeLong(message.getCookie().longValue());
         outBuffer.writeLong(message.getCookieMask().longValue());
@@ -54,7 +55,7 @@ public class FlowModInputMessageFactory implements OFSerializer<FlowMod>, Serial
         registry.<Match, OFSerializer<Match>>getSerializer(new MessageTypeKey<>(message.getVersion(), Match.class))
             .serialize(message.getMatch(), outBuffer);
         ListSerializer.serializeList(message.getInstruction(), INSTRUCTION_KEY_MAKER, registry, outBuffer);
         registry.<Match, OFSerializer<Match>>getSerializer(new MessageTypeKey<>(message.getVersion(), Match.class))
             .serialize(message.getMatch(), outBuffer);
         ListSerializer.serializeList(message.getInstruction(), INSTRUCTION_KEY_MAKER, registry, outBuffer);
-        ByteBufUtils.updateOFHeaderLength(outBuffer);
+        ByteBufUtils.updateOFHeaderLength(outBuffer, index);
     }
 
     @Override
     }
 
     @Override
index 7e7c48480c2fda8fd63a68681f9322d5a44cb20b..11c2948bbd3ae2ba7af3ea6c2dfbda7d57598183 100644 (file)
@@ -33,6 +33,7 @@ public class FlowRemovedMessageFactory implements OFSerializer<FlowRemovedMessag
 
     @Override
     public void serialize(FlowRemovedMessage message, ByteBuf outBuffer) {
 
     @Override
     public void serialize(FlowRemovedMessage message, ByteBuf outBuffer) {
+        int index = outBuffer.writerIndex();
         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
         outBuffer.writeLong(message.getCookie().longValue());
         outBuffer.writeShort(message.getPriority());
         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
         outBuffer.writeLong(message.getCookie().longValue());
         outBuffer.writeShort(message.getPriority());
@@ -47,7 +48,7 @@ public class FlowRemovedMessageFactory implements OFSerializer<FlowRemovedMessag
         OFSerializer<Match> matchSerializer = registry
                 .<Match, OFSerializer<Match>> getSerializer(new MessageTypeKey<>(message.getVersion(), Match.class));
         matchSerializer.serialize(message.getMatch(), outBuffer);
         OFSerializer<Match> matchSerializer = registry
                 .<Match, OFSerializer<Match>> getSerializer(new MessageTypeKey<>(message.getVersion(), Match.class));
         matchSerializer.serialize(message.getMatch(), outBuffer);
-        ByteBufUtils.updateOFHeaderLength(outBuffer);
+        ByteBufUtils.updateOFHeaderLength(outBuffer, index);
     }
 
 }
     }
 
 }
index 95a3b7db47ee620f33718d0f8960e666ef66c0ed..db8142a54f47113f883c5fde737f0e3ea9e43759 100644 (file)
@@ -34,13 +34,14 @@ public class GroupModInputMessageFactory implements OFSerializer<GroupMod>, Seri
 
     @Override
     public void serialize(GroupMod message, ByteBuf outBuffer) {
 
     @Override
     public void serialize(GroupMod message, ByteBuf outBuffer) {
+        int index = outBuffer.writerIndex();
         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
         outBuffer.writeShort(message.getCommand().getIntValue());
         outBuffer.writeByte(message.getType().getIntValue());
         outBuffer.writeZero(PADDING_IN_GROUP_MOD_MESSAGE);
         outBuffer.writeInt(message.getGroupId().getValue().intValue());
         serializerBuckets(message.getBucketsList(), outBuffer);
         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
         outBuffer.writeShort(message.getCommand().getIntValue());
         outBuffer.writeByte(message.getType().getIntValue());
         outBuffer.writeZero(PADDING_IN_GROUP_MOD_MESSAGE);
         outBuffer.writeInt(message.getGroupId().getValue().intValue());
         serializerBuckets(message.getBucketsList(), outBuffer);
-        ByteBufUtils.updateOFHeaderLength(outBuffer);
+        ByteBufUtils.updateOFHeaderLength(outBuffer, index);
     }
 
     private void serializerBuckets(List<BucketsList> buckets, ByteBuf outBuffer) {
     }
 
     private void serializerBuckets(List<BucketsList> buckets, ByteBuf outBuffer) {
index ded3af6d259b4a794f4cb96bfc8385dc7a75e3e6..03c39887f8e786d5365d37b30848183bd0384613 100644 (file)
@@ -33,6 +33,7 @@ public class PortModInputMessageFactory implements OFSerializer<PortMod> {
 
     @Override
     public void serialize(final PortMod message, final ByteBuf outBuffer) {
 
     @Override
     public void serialize(final PortMod message, final ByteBuf outBuffer) {
+        int index = outBuffer.writerIndex();
         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
         outBuffer.writeInt(message.getPortNo().getValue().intValue());
         outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_01);
         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
         outBuffer.writeInt(message.getPortNo().getValue().intValue());
         outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_01);
@@ -42,7 +43,7 @@ public class PortModInputMessageFactory implements OFSerializer<PortMod> {
         outBuffer.writeInt(createPortConfigBitmask(message.getMask()));
         outBuffer.writeInt(createPortFeaturesBitmask(message.getAdvertise()));
         outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_03);
         outBuffer.writeInt(createPortConfigBitmask(message.getMask()));
         outBuffer.writeInt(createPortFeaturesBitmask(message.getAdvertise()));
         outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_03);
-        ByteBufUtils.updateOFHeaderLength(outBuffer);
+        ByteBufUtils.updateOFHeaderLength(outBuffer, index);
     }
 
     /**
     }
 
     /**
index edf21a2c6f1cce1b05feb5103891fa4e27b58960..a3a72a27d4c885f84de77519e0833d9a5ed34605 100644 (file)
@@ -154,8 +154,19 @@ public class FlowModInputMessageFactoryTest {
         FlowModInput message = builder.build();
 
         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
         FlowModInput message = builder.build();
 
         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
+
+        // simulate parent message
+        out.writeInt(1);
+        out.writeZero(2);
+        out.writeShort(3);
+
         flowModFactory.serialize(message, out);
 
         flowModFactory.serialize(message, out);
 
+        // read parent message
+        out.readInt();
+        out.skipBytes(2);
+        out.readShort();
+
         BufferHelper.checkHeaderV13(out,(byte) 14, 128);
         cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
         out.readBytes(cookie);
         BufferHelper.checkHeaderV13(out,(byte) 14, 128);
         cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
         out.readBytes(cookie);
index 616b289bbf8c56e0b5010106e9f0d383f83381d5..0ba90a23d946ea3411a41f1546d24119e6202559 100644 (file)
@@ -94,8 +94,19 @@ public class FlowRemovedMessageFactoryTest {
         builder.setMatch(matchBuilder.build());
         FlowRemovedMessage message = builder.build();
         ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
         builder.setMatch(matchBuilder.build());
         FlowRemovedMessage message = builder.build();
         ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
+
+        // simulate parent message
+        serializedBuffer.writeInt(1);
+        serializedBuffer.writeZero(2);
+        serializedBuffer.writeShort(3);
+
         factory.serialize(message, serializedBuffer);
 
         factory.serialize(message, serializedBuffer);
 
+        // read parent message
+        serializedBuffer.readInt();
+        serializedBuffer.skipBytes(2);
+        serializedBuffer.readShort();
+
         BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 72);
         Assert.assertEquals("Wrong cookie", message.getCookie().longValue(), serializedBuffer.readLong());
         Assert.assertEquals("Wrong priority", message.getPriority().intValue(), serializedBuffer.readShort());
         BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 72);
         Assert.assertEquals("Wrong cookie", message.getCookie().longValue(), serializedBuffer.readLong());
         Assert.assertEquals("Wrong priority", message.getPriority().intValue(), serializedBuffer.readShort());
index 37396655a372426a6cdb3d42583c7cdddf0b5c34..6e75130f9ebe358eaf3b6f56aa7f86940add4e54 100644 (file)
@@ -69,8 +69,19 @@ public class GroupModInputMessageFactoryTest {
         GroupModInput message = builder.build();
 
         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
         GroupModInput message = builder.build();
 
         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
+
+        // simulate parent message
+        out.writeInt(1);
+        out.writeZero(2);
+        out.writeShort(3);
+
         groupModFactory.serialize(message, out);
 
         groupModFactory.serialize(message, out);
 
+        // read parent message
+        out.readInt();
+        out.skipBytes(2);
+        out.readShort();
+
         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 32);
         Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readUnsignedShort());
         Assert.assertEquals("Wrong type", message.getType().getIntValue(), out.readUnsignedByte());
         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 32);
         Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readUnsignedShort());
         Assert.assertEquals("Wrong type", message.getType().getIntValue(), out.readUnsignedByte());
index 3aebd727e5fb37d6416eea1e25cc987bdd4aeab9..f9319019170e0be2c76f9cb3c64e534420952324 100644 (file)
@@ -70,8 +70,19 @@ public class PortModInputMessageFactoryTest {
         PortModInput message = builder.build();
 
         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
         PortModInput message = builder.build();
 
         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
+
+        // simulate parent message
+        out.writeInt(1);
+        out.writeZero(2);
+        out.writeShort(3);
+
         portModFactory.serialize(message, out);
 
         portModFactory.serialize(message, out);
 
+        // read parent message
+        out.readInt();
+        out.skipBytes(2);
+        out.readShort();
+
         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, MESSAGE_LENGTH);
         Assert.assertEquals("Wrong PortNo", message.getPortNo().getValue().longValue(), out.readUnsignedInt());
         out.skipBytes(PADDING_IN_PORT_MOD_MESSAGE_01);
         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, MESSAGE_LENGTH);
         Assert.assertEquals("Wrong PortNo", message.getPortNo().getValue().longValue(), out.readUnsignedInt());
         out.skipBytes(PADDING_IN_PORT_MOD_MESSAGE_01);
index 6be2ee39ea80736c287e0b7bba9fa4dec1a81673..5d6869cfb961e072dab713fdd05d7f971f7d12d3 100644 (file)
@@ -136,6 +136,15 @@ public abstract class ByteBufUtils {
         out.setShort(EncodeConstants.OFHEADER_LENGTH_INDEX, out.readableBytes());
     }
 
         out.setShort(EncodeConstants.OFHEADER_LENGTH_INDEX, out.readableBytes());
     }
 
+    /**
+     * Write length OF header
+     * @param out writing buffer
+     * @param index writing index
+     */
+    public static void updateOFHeaderLength(final ByteBuf out, int index) {
+        out.setShort(index + EncodeConstants.OFHEADER_LENGTH_INDEX, out.writerIndex() - index);
+    }
+
     /**
      * Fills the bitmask from boolean map where key is bit position
      * @param booleanMap bit to boolean mapping
     /**
      * Fills the bitmask from boolean map where key is bit position
      * @param booleanMap bit to boolean mapping
index 8196e69ba095f5f035f1a557e48ef7de0d6dd333..b7f0f593caed035f5481f86576537c4c7979c42a 100644 (file)
@@ -448,4 +448,22 @@ public class ByteBufUtilsTest {
         Assert.assertTrue(bytes.length == shorts.size()*2);
         Assert.assertArrayEquals(EXPECTEDVALUES1AND255, bytes);
     }
         Assert.assertTrue(bytes.length == shorts.size()*2);
         Assert.assertArrayEquals(EXPECTEDVALUES1AND255, bytes);
     }
+
+    @Test
+    public void testUpdateHeader() throws IOException {
+        ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
+        buffer.writeInt(1);
+        int start = buffer.writerIndex();
+        buffer.writeShort(4);
+        buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
+        buffer.writeLong(8);
+        int end = buffer.writerIndex();
+
+        ByteBufUtils.updateOFHeaderLength(buffer, start);
+        Assert.assertEquals(buffer.readInt(), 1);
+        Assert.assertEquals(buffer.readShort(), 4);
+        Assert.assertEquals(buffer.readShort(), 12);
+        Assert.assertEquals(buffer.readLong(), 8l);
+        Assert.assertEquals(buffer.getShort(start + EncodeConstants.OFHEADER_LENGTH_INDEX), end - start);
+    }
 }
 }