From: Daniel Bartos Date: Tue, 10 Dec 2013 07:15:47 +0000 (+0000) Subject: Merge "OF1.3 matchSerializer (Ipv6 address) fix + test" X-Git-Tag: jenkins-openflowjava-bulk-release-prepare-only-1~32 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=3d6680258cc25e4e8ec0476c947b2a6f3b1ce96d;hp=55670826f95d73afa4292f8cf729778e38d07aeb;p=openflowjava.git Merge "OF1.3 matchSerializer (Ipv6 address) fix + test" --- diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/MatchSerializer.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/MatchSerializer.java index bde832c0..557ff7bc 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/MatchSerializer.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/MatchSerializer.java @@ -441,22 +441,28 @@ public class MatchSerializer { private static void writeIpv6AddressRelatedEntry(MatchEntries entry, ByteBuf out, int value) { int fieldValue = value << 1; - String[] addressGroups = entry.getAugmentation(Ipv6AddressMatchEntry.class).getIpv6Address().getValue().split(":"); - String[] address = parseIpv6Address(addressGroups); + String textAddress = entry.getAugmentation(Ipv6AddressMatchEntry.class).getIpv6Address().getValue(); + String[] address; + if (textAddress.equals("::")) { + address = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS]; + Arrays.fill(address, "0"); + } else { + address = parseIpv6Address(textAddress.split(":")); + } if (entry.isHasMask()) { fieldValue = fieldValue | 1; out.writeByte(fieldValue); byte[] mask = entry.getAugmentation(MaskMatchEntry.class).getMask(); out.writeByte(EncodeConstants.SIZE_OF_IPV6_ADDRESS_IN_BYTES + mask.length); for (int i = 0; i < address.length; i++) { - out.writeShort(Integer.parseInt(addressGroups[i], 16)); + out.writeShort(Integer.parseInt(address[i], 16)); } out.writeBytes(mask); } else { out.writeByte(fieldValue); out.writeByte(EncodeConstants.SIZE_OF_IPV6_ADDRESS_IN_BYTES); - for (int i = 0; i < addressGroups.length; i++) { - out.writeShort(Integer.parseInt(addressGroups[i], 16)); + for (int i = 0; i < address.length; i++) { + out.writeShort(Integer.parseInt(address[i], 16)); } } } @@ -475,15 +481,16 @@ public class MatchSerializer { break; case 1: int zerosToBePushed = EncodeConstants.GROUPS_IN_IPV6_ADDRESS - addressGroups.length + 1; - int pushed = 0; + int index = 0; for (int i = 0; i < addressGroups.length; i++) { if (addressGroups[i].equals("")) { for (int j = 0; j < zerosToBePushed; j++) { - ready[i+j] = "0"; - pushed++; + ready[index] = "0"; + index++; } } else { - ready[i + pushed] = addressGroups[i]; + ready[index] = addressGroups[i]; + index++; } } break; @@ -491,12 +498,8 @@ public class MatchSerializer { Arrays.fill(ready, "0"); ready[ready.length - 1] = addressGroups[addressGroups.length - 1]; break; - case 3: - Arrays.fill(ready, "0"); - break; - default: - break; + throw new IllegalStateException("Incorrect ipv6 address"); } return ready; } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/MatchSerializerTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/MatchSerializerTest.java index 1fbed39c..67d148fb 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/MatchSerializerTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/MatchSerializerTest.java @@ -10,9 +10,17 @@ import java.util.List; import org.junit.Assert; import org.junit.Test; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntry; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntryBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntry; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntryBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv4Src; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Dst; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6NdTarget; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Src; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Nxm0Class; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Nxm1Class; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.MatchEntries; @@ -58,5 +66,142 @@ public class MatchSerializerTest { Assert.assertEquals("Wrong ip address (third number)", 3, out.readUnsignedByte()); Assert.assertEquals("Wrong ip address (fourth number)", 4, out.readUnsignedByte()); } + + /** + * Test for correct serialization of Ipv6Address match entry + */ + @Test + public void test2() { + MatchBuilder builder = new MatchBuilder(); + builder.setType(OxmMatchType.class); + List entries = new ArrayList<>(); + // ipv6 match entry with correct Ipv6 address + MatchEntriesBuilder entriesBuilder = new MatchEntriesBuilder(); + entriesBuilder.setOxmClass(OpenflowBasicClass.class); + entriesBuilder.setOxmMatchField(Ipv6Src.class); + entriesBuilder.setHasMask(false); + Ipv6AddressMatchEntryBuilder addressBuilder = new Ipv6AddressMatchEntryBuilder(); + addressBuilder.setIpv6Address(new Ipv6Address("1:2:3:4:5:6:7:8")); + entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build()); + entries.add(entriesBuilder.build()); + // ipv6 match entry with abbreviated Ipv6 address + entriesBuilder = new MatchEntriesBuilder(); + entriesBuilder.setOxmClass(OpenflowBasicClass.class); + entriesBuilder.setOxmMatchField(Ipv6NdTarget.class); + entriesBuilder.setHasMask(false); + addressBuilder = new Ipv6AddressMatchEntryBuilder(); + addressBuilder.setIpv6Address(new Ipv6Address("1:2::6:7:8")); + entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build()); + entries.add(entriesBuilder.build()); + // ipv6 match entry with abbreviated Ipv6 address + entriesBuilder = new MatchEntriesBuilder(); + entriesBuilder.setOxmClass(Nxm1Class.class); + entriesBuilder.setOxmMatchField(Ipv6Dst.class); + entriesBuilder.setHasMask(false); + addressBuilder = new Ipv6AddressMatchEntryBuilder(); + addressBuilder.setIpv6Address(new Ipv6Address("1::8")); + entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build()); + entries.add(entriesBuilder.build()); + // ipv6 match entry with abbreviated Ipv6 address + entriesBuilder = new MatchEntriesBuilder(); + entriesBuilder.setOxmClass(Nxm1Class.class); + entriesBuilder.setOxmMatchField(Ipv6Dst.class); + entriesBuilder.setHasMask(false); + addressBuilder = new Ipv6AddressMatchEntryBuilder(); + addressBuilder.setIpv6Address(new Ipv6Address("::1")); + entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build()); + entries.add(entriesBuilder.build()); + // ipv6 match entry with abbreviated Ipv6 address + entriesBuilder = new MatchEntriesBuilder(); + entriesBuilder.setOxmClass(Nxm0Class.class); + entriesBuilder.setOxmMatchField(Ipv6Dst.class); + entriesBuilder.setHasMask(false); + addressBuilder = new Ipv6AddressMatchEntryBuilder(); + addressBuilder.setIpv6Address(new Ipv6Address("::")); + entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build()); + entries.add(entriesBuilder.build()); + // ipv6 match entry with incorrect Ipv6 address (longer) + entriesBuilder = new MatchEntriesBuilder(); + entriesBuilder.setOxmClass(OpenflowBasicClass.class); + entriesBuilder.setOxmMatchField(Ipv6Dst.class); + entriesBuilder.setHasMask(false); + addressBuilder = new Ipv6AddressMatchEntryBuilder(); + addressBuilder.setIpv6Address(new Ipv6Address("1:2:3:4:5:6:7:8:9")); + entriesBuilder.addAugmentation(Ipv6AddressMatchEntry.class, addressBuilder.build()); + entries.add(entriesBuilder.build()); + builder.setMatchEntries(entries); + Match match = builder.build(); + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + MatchSerializer.encodeMatch(match, out); + + Assert.assertEquals("Wrong type", 1, out.readUnsignedShort()); + out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES); + Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort()); + Assert.assertEquals("Wrong field and mask", 52, out.readUnsignedByte()); + Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte()); + Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 2, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 3, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 4, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 5, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 6, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 7, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort()); + Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort()); + Assert.assertEquals("Wrong field and mask", 62, out.readUnsignedByte()); + Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte()); + Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 2, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 6, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 7, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort()); + Assert.assertEquals("Wrong class", 0x0001, out.readUnsignedShort()); + Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte()); + Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte()); + Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort()); + Assert.assertEquals("Wrong class", 0x0001, out.readUnsignedShort()); + Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte()); + Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort()); + Assert.assertEquals("Wrong class", 0x0000, out.readUnsignedShort()); + Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte()); + Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 0, out.readUnsignedShort()); + Assert.assertEquals("Wrong class", 0x8000, out.readUnsignedShort()); + Assert.assertEquals("Wrong field and mask", 54, out.readUnsignedByte()); + Assert.assertEquals("Wrong entry length", 16, out.readUnsignedByte()); + Assert.assertEquals("Wrong ipv6 address", 1, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 2, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 3, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 4, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 5, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 6, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 7, out.readUnsignedShort()); + Assert.assertEquals("Wrong ipv6 address", 8, out.readUnsignedShort()); + } }