Pavlin Radoslavov

Completed the IPv6 implementation for IpPrefix, and added

a new set of IpPrefix unit tests.

Also, fix few nits in IpAddress and IpAddressTest
...@@ -30,7 +30,6 @@ import static com.google.common.base.Preconditions.checkState; ...@@ -30,7 +30,6 @@ import static com.google.common.base.Preconditions.checkState;
30 30
31 /** 31 /**
32 * A class representing an IP address. 32 * A class representing an IP address.
33 - * TODO: Add support for IPv6 as well.
34 */ 33 */
35 public final class IpAddress implements Comparable<IpAddress> { 34 public final class IpAddress implements Comparable<IpAddress> {
36 // IP Versions 35 // IP Versions
......
...@@ -17,8 +17,6 @@ package org.onlab.packet; ...@@ -17,8 +17,6 @@ package org.onlab.packet;
17 17
18 import java.util.Objects; 18 import java.util.Objects;
19 19
20 -// TODO: Add support for IPv6 as well.
21 -
22 /** 20 /**
23 * A class representing an IP prefix. A prefix consists of an IP address and 21 * A class representing an IP prefix. A prefix consists of an IP address and
24 * a subnet mask. 22 * a subnet mask.
...@@ -40,26 +38,39 @@ public final class IpPrefix { ...@@ -40,26 +38,39 @@ public final class IpPrefix {
40 * 38 *
41 * @param address the IP address 39 * @param address the IP address
42 * @param prefixLength the prefix length 40 * @param prefixLength the prefix length
41 + * @throws IllegalArgumentException if the prefix length value is invalid
43 */ 42 */
44 private IpPrefix(IpAddress address, int prefixLength) { 43 private IpPrefix(IpAddress address, int prefixLength) {
45 - checkPrefixLength(prefixLength); 44 + checkPrefixLength(address.version(), prefixLength);
46 this.address = IpAddress.makeMaskedAddress(address, prefixLength); 45 this.address = IpAddress.makeMaskedAddress(address, prefixLength);
47 this.prefixLength = (short) prefixLength; 46 this.prefixLength = (short) prefixLength;
48 } 47 }
49 48
50 /** 49 /**
51 - * Checks whether the prefix length is valid. 50 + * Returns the IP version of the prefix.
52 * 51 *
53 - * @param prefixLength the prefix length value to check 52 + * @return the IP version of the prefix
54 - * @throws IllegalArgumentException if the prefix length value is invalid
55 */ 53 */
56 - private static void checkPrefixLength(int prefixLength) { 54 + public IpAddress.Version version() {
57 - if ((prefixLength < 0) || (prefixLength > MAX_INET_MASK_LENGTH)) { 55 + return address.version();
58 - String msg = "Invalid prefix length " + prefixLength + ". " + 56 + }
59 - "The value must be in the interval [0, " + 57 +
60 - MAX_INET_MASK_LENGTH + "]"; 58 + /**
61 - throw new IllegalArgumentException(msg); 59 + * Returns the IP address value of the prefix.
60 + *
61 + * @return the IP address value of the prefix
62 + */
63 + public IpAddress address() {
64 + return address;
62 } 65 }
66 +
67 + /**
68 + * Returns the IP address prefix length.
69 + *
70 + * @return the IP address prefix length
71 + */
72 + public int prefixLength() {
73 + return prefixLength;
63 } 74 }
64 75
65 /** 76 /**
...@@ -68,6 +79,7 @@ public final class IpPrefix { ...@@ -68,6 +79,7 @@ public final class IpPrefix {
68 * @param address an integer representing the IPv4 address 79 * @param address an integer representing the IPv4 address
69 * @param prefixLength the prefix length 80 * @param prefixLength the prefix length
70 * @return an IP prefix 81 * @return an IP prefix
82 + * @throws IllegalArgumentException if the prefix length value is invalid
71 */ 83 */
72 public static IpPrefix valueOf(int address, int prefixLength) { 84 public static IpPrefix valueOf(int address, int prefixLength) {
73 return new IpPrefix(IpAddress.valueOf(address), prefixLength); 85 return new IpPrefix(IpAddress.valueOf(address), prefixLength);
...@@ -80,11 +92,11 @@ public final class IpPrefix { ...@@ -80,11 +92,11 @@ public final class IpPrefix {
80 * @param address the IP address value stored in network byte order 92 * @param address the IP address value stored in network byte order
81 * @param prefixLength the prefix length 93 * @param prefixLength the prefix length
82 * @return an IP prefix 94 * @return an IP prefix
95 + * @throws IllegalArgumentException if the prefix length value is invalid
83 */ 96 */
84 public static IpPrefix valueOf(IpAddress.Version version, byte[] address, 97 public static IpPrefix valueOf(IpAddress.Version version, byte[] address,
85 int prefixLength) { 98 int prefixLength) {
86 - return new IpPrefix(IpAddress.valueOf(version, address), 99 + return new IpPrefix(IpAddress.valueOf(version, address), prefixLength);
87 - prefixLength);
88 } 100 }
89 101
90 /** 102 /**
...@@ -93,6 +105,7 @@ public final class IpPrefix { ...@@ -93,6 +105,7 @@ public final class IpPrefix {
93 * @param address the IP address 105 * @param address the IP address
94 * @param prefixLength the prefix length 106 * @param prefixLength the prefix length
95 * @return an IP prefix 107 * @return an IP prefix
108 + * @throws IllegalArgumentException if the prefix length value is invalid
96 */ 109 */
97 public static IpPrefix valueOf(IpAddress address, int prefixLength) { 110 public static IpPrefix valueOf(IpAddress address, int prefixLength) {
98 return new IpPrefix(address, prefixLength); 111 return new IpPrefix(address, prefixLength);
...@@ -104,6 +117,7 @@ public final class IpPrefix { ...@@ -104,6 +117,7 @@ public final class IpPrefix {
104 * 117 *
105 * @param address an IP prefix in string form, e.g. "10.1.0.0/16" 118 * @param address an IP prefix in string form, e.g. "10.1.0.0/16"
106 * @return an IP prefix 119 * @return an IP prefix
120 + * @throws IllegalArgumentException if the arguments are invalid
107 */ 121 */
108 public static IpPrefix valueOf(String address) { 122 public static IpPrefix valueOf(String address) {
109 final String[] parts = address.split("/"); 123 final String[] parts = address.split("/");
...@@ -119,33 +133,6 @@ public final class IpPrefix { ...@@ -119,33 +133,6 @@ public final class IpPrefix {
119 } 133 }
120 134
121 /** 135 /**
122 - * Returns the IP version of the prefix.
123 - *
124 - * @return the IP version of the prefix
125 - */
126 - public IpAddress.Version version() {
127 - return address.version();
128 - }
129 -
130 - /**
131 - * Returns the IP address value of the prefix.
132 - *
133 - * @return the IP address value of the prefix
134 - */
135 - public IpAddress address() {
136 - return address;
137 - }
138 -
139 - /**
140 - * Returns the IP address prefix length.
141 - *
142 - * @return the IP address prefix length
143 - */
144 - public int prefixLength() {
145 - return prefixLength;
146 - }
147 -
148 - /**
149 * Determines whether a given IP prefix is contained within this prefix. 136 * Determines whether a given IP prefix is contained within this prefix.
150 * 137 *
151 * @param other the IP prefix to test 138 * @param other the IP prefix to test
...@@ -217,4 +204,35 @@ public final class IpPrefix { ...@@ -217,4 +204,35 @@ public final class IpPrefix {
217 builder.append(String.format("%d", prefixLength)); 204 builder.append(String.format("%d", prefixLength));
218 return builder.toString(); 205 return builder.toString();
219 } 206 }
207 +
208 + /**
209 + * Checks whether the prefix length is valid.
210 + *
211 + * @param version the IP address version
212 + * @param prefixLength the prefix length value to check
213 + * @throws IllegalArgumentException if the prefix length value is invalid
214 + */
215 + private static void checkPrefixLength(IpAddress.Version version,
216 + int prefixLength) {
217 + int maxPrefixLen = 0;
218 +
219 + switch (version) {
220 + case INET:
221 + maxPrefixLen = MAX_INET_MASK_LENGTH;
222 + break;
223 + case INET6:
224 + maxPrefixLen = MAX_INET6_MASK_LENGTH;
225 + break;
226 + default:
227 + String msg = "Invalid IP version " + version;
228 + throw new IllegalArgumentException(msg);
229 + }
230 +
231 + if ((prefixLength < 0) || (prefixLength > maxPrefixLen)) {
232 + String msg = "Invalid prefix length " + prefixLength + ". " +
233 + "The value must be in the interval [0, " +
234 + maxPrefixLen + "]";
235 + throw new IllegalArgumentException(msg);
236 + }
237 + }
220 } 238 }
......
...@@ -135,7 +135,7 @@ public class IpAddressTest { ...@@ -135,7 +135,7 @@ public class IpAddressTest {
135 * Tests returning an IPv4 address asn an integer. 135 * Tests returning an IPv4 address asn an integer.
136 */ 136 */
137 @Test 137 @Test
138 - public void testToint() { 138 + public void testToInt() {
139 IpAddress ipAddress; 139 IpAddress ipAddress;
140 140
141 ipAddress = IpAddress.valueOf("1.2.3.4"); 141 ipAddress = IpAddress.valueOf("1.2.3.4");
...@@ -149,10 +149,10 @@ public class IpAddressTest { ...@@ -149,10 +149,10 @@ public class IpAddressTest {
149 } 149 }
150 150
151 /** 151 /**
152 - * Tests valueOf() converter for an integer value. 152 + * Tests valueOf() converter for IPv4 integer value.
153 */ 153 */
154 @Test 154 @Test
155 - public void testValueOfForInteger() { 155 + public void testValueOfForIntegerIPv4() {
156 IpAddress ipAddress; 156 IpAddress ipAddress;
157 157
158 ipAddress = IpAddress.valueOf(0x01020304); 158 ipAddress = IpAddress.valueOf(0x01020304);
......