Added IPv4Address.ofCidrMaskLength() and the corresponding unit tests
Reference:
IPv4AddressWithMask.of(String)
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java
index d398a85..8f3cce3 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/IPv4Address.java
@@ -158,6 +158,21 @@
return IPv4Address.of(raw);
}
+ public static IPv4Address ofCidrMaskLength(final int cidrMaskLength) {
+ Preconditions.checkArgument(
+ cidrMaskLength >= 0 && cidrMaskLength <= 32,
+ "Invalid IPv4 CIDR mask length: %s", cidrMaskLength);
+
+ if (cidrMaskLength == 32) {
+ return IPv4Address.NO_MASK;
+ } else if (cidrMaskLength == 0) {
+ return IPv4Address.FULL_MASK;
+ } else {
+ int mask = (-1) << (32 - cidrMaskLength);
+ return IPv4Address.of(mask);
+ }
+ }
+
public int getInt() {
return rawValue;
}
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java
index a57b42a..9f3025c 100644
--- a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/IPv4AddressTest.java
@@ -224,6 +224,51 @@
}
@Test
+ public void testOfCidrMaskLength() {
+ for (int i = 0; i <= 32; i++) {
+ assertEquals(IPv4Address.ofCidrMaskLength(i).asCidrMaskLength(), i);
+ }
+
+ assertEquals(IPv4Address.ofCidrMaskLength(0).getInt(), 0x0000_0000);
+
+ assertEquals(IPv4Address.ofCidrMaskLength(1).getInt(), 0x8000_0000);
+ assertEquals(IPv4Address.ofCidrMaskLength(2).getInt(), 0xC000_0000);
+ assertEquals(IPv4Address.ofCidrMaskLength(3).getInt(), 0xE000_0000);
+ assertEquals(IPv4Address.ofCidrMaskLength(4).getInt(), 0xF000_0000);
+ assertEquals(IPv4Address.ofCidrMaskLength(5).getInt(), 0xF800_0000);
+ assertEquals(IPv4Address.ofCidrMaskLength(6).getInt(), 0xFC00_0000);
+ assertEquals(IPv4Address.ofCidrMaskLength(7).getInt(), 0xFE00_0000);
+ assertEquals(IPv4Address.ofCidrMaskLength(8).getInt(), 0xFF00_0000);
+
+ assertEquals(IPv4Address.ofCidrMaskLength(9).getInt(), 0xFF80_0000);
+ assertEquals(IPv4Address.ofCidrMaskLength(10).getInt(), 0xFFC0_0000);
+ assertEquals(IPv4Address.ofCidrMaskLength(11).getInt(), 0xFFE0_0000);
+ assertEquals(IPv4Address.ofCidrMaskLength(12).getInt(), 0xFFF0_0000);
+ assertEquals(IPv4Address.ofCidrMaskLength(13).getInt(), 0xFFF8_0000);
+ assertEquals(IPv4Address.ofCidrMaskLength(14).getInt(), 0xFFFC_0000);
+ assertEquals(IPv4Address.ofCidrMaskLength(15).getInt(), 0xFFFE_0000);
+ assertEquals(IPv4Address.ofCidrMaskLength(16).getInt(), 0xFFFF_0000);
+
+ assertEquals(IPv4Address.ofCidrMaskLength(17).getInt(), 0xFFFF_8000);
+ assertEquals(IPv4Address.ofCidrMaskLength(18).getInt(), 0xFFFF_C000);
+ assertEquals(IPv4Address.ofCidrMaskLength(19).getInt(), 0xFFFF_E000);
+ assertEquals(IPv4Address.ofCidrMaskLength(20).getInt(), 0xFFFF_F000);
+ assertEquals(IPv4Address.ofCidrMaskLength(21).getInt(), 0xFFFF_F800);
+ assertEquals(IPv4Address.ofCidrMaskLength(22).getInt(), 0xFFFF_FC00);
+ assertEquals(IPv4Address.ofCidrMaskLength(23).getInt(), 0xFFFF_FE00);
+ assertEquals(IPv4Address.ofCidrMaskLength(24).getInt(), 0xFFFF_FF00);
+
+ assertEquals(IPv4Address.ofCidrMaskLength(25).getInt(), 0xFFFF_FF80);
+ assertEquals(IPv4Address.ofCidrMaskLength(26).getInt(), 0xFFFF_FFC0);
+ assertEquals(IPv4Address.ofCidrMaskLength(27).getInt(), 0xFFFF_FFE0);
+ assertEquals(IPv4Address.ofCidrMaskLength(28).getInt(), 0xFFFF_FFF0);
+ assertEquals(IPv4Address.ofCidrMaskLength(29).getInt(), 0xFFFF_FFF8);
+ assertEquals(IPv4Address.ofCidrMaskLength(30).getInt(), 0xFFFF_FFFC);
+ assertEquals(IPv4Address.ofCidrMaskLength(31).getInt(), 0xFFFF_FFFE);
+ assertEquals(IPv4Address.ofCidrMaskLength(32).getInt(), 0xFFFF_FFFF);
+ }
+
+ @Test
public void testReadFrom() throws OFParseError {
for(int i=0; i < testAddresses.length; i++ ) {
IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i]));
@@ -367,5 +412,17 @@
} catch (IllegalArgumentException e) {
assertNotNull(e.getMessage());
}
+ try {
+ IPv4Address.ofCidrMaskLength(-1);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertNotNull(e.getMessage());
+ }
+ try {
+ IPv4Address.ofCidrMaskLength(33);
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertNotNull(e.getMessage());
+ }
}
}