Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.sdnip; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 2 | |
Jonathan Hart | 850dddc | 2014-08-28 09:54:10 -0700 | [diff] [blame] | 3 | import java.math.BigInteger; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 4 | import java.net.InetAddress; |
| 5 | import java.net.UnknownHostException; |
Jonathan Hart | 850dddc | 2014-08-28 09:54:10 -0700 | [diff] [blame] | 6 | import java.nio.ByteBuffer; |
Jonathan Hart | 32e1822 | 2013-08-07 22:05:42 +1200 | [diff] [blame] | 7 | import java.util.Arrays; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 8 | |
Jonathan Hart | abf1022 | 2013-08-13 10:19:34 +1200 | [diff] [blame] | 9 | import com.google.common.net.InetAddresses; |
| 10 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 11 | /** |
| 12 | * Represents an IP prefix. |
| 13 | * <p/> |
| 14 | * It is made up of an IP address and a number of significant bits in the |
| 15 | * prefix (i.e. the size of the network part of the address). |
| 16 | * E.g. {@code 192.168.0.0/16}. |
| 17 | * <p/> |
| 18 | * Currently only IPv4 is supported, so a prefix length can be up to 32 bits. |
| 19 | */ |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 20 | public class Prefix { |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 21 | /** |
| 22 | * The length of addresses this class can represent prefixes of, in bytes. |
| 23 | */ |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 24 | public static final int ADDRESS_LENGTH_BYTES = 4; |
| 25 | |
| 26 | /** |
| 27 | * The length of addresses this class can represent prefixes of, in bits. |
| 28 | */ |
| 29 | public static final int MAX_PREFIX_LENGTH = Byte.SIZE * ADDRESS_LENGTH_BYTES; |
| 30 | |
| 31 | public static final int MIN_PREFIX_LENGTH = 0; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 32 | |
Jonathan Hart | 850dddc | 2014-08-28 09:54:10 -0700 | [diff] [blame] | 33 | private static final int BINARY_RADIX = 2; |
| 34 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | private final int prefixLength; |
| 36 | private final byte[] address; |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 37 | private final String binaryString; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 38 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 39 | // For verifying the arguments and pretty printing |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 40 | private final InetAddress inetAddress; |
Jonathan Hart | 32e1822 | 2013-08-07 22:05:42 +1200 | [diff] [blame] | 41 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Class constructor, taking an byte array representing and IP address and |
| 44 | * a prefix length. |
| 45 | * <p/> |
| 46 | * The valid values for addr and prefixLength are bounded by |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 47 | * {@link #ADDRESS_LENGTH_BYTES}. |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 48 | * <p/> |
| 49 | * A valid addr array satisfies |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 50 | * {@code addr.length == }{@value #ADDRESS_LENGTH_BYTES}. |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 51 | * <p/> |
| 52 | * A valid prefixLength satisfies |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 53 | * {@code (prefixLength >= 0 && prefixLength <=} {@link Byte#SIZE} |
| 54 | * {@code * }{@value #ADDRESS_LENGTH_BYTES}{@code )}. |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 55 | * |
| 56 | * @param addr a byte array representing the address |
| 57 | * @param prefixLength length of the prefix of the specified address |
| 58 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 59 | public Prefix(byte[] addr, int prefixLength) { |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 60 | if (addr == null || addr.length != ADDRESS_LENGTH_BYTES || |
| 61 | prefixLength < MIN_PREFIX_LENGTH || |
| 62 | prefixLength > MAX_PREFIX_LENGTH) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 63 | throw new IllegalArgumentException(); |
| 64 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 65 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | address = canonicalizeAddress(addr, prefixLength); |
| 67 | this.prefixLength = prefixLength; |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 68 | binaryString = createBinaryString(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 69 | |
| 70 | try { |
| 71 | inetAddress = InetAddress.getByAddress(address); |
| 72 | } catch (UnknownHostException e) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 73 | throw new IllegalArgumentException("Couldn't parse IP address", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Class constructor, taking an address in String format and a prefix |
| 79 | * length. The address must be in dot-notation form (e.g. {@code 0.0.0.0}). |
| 80 | * |
| 81 | * @param strAddress a String representing the address |
| 82 | * @param prefixLength length of the prefix of the specified address |
| 83 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | public Prefix(String strAddress, int prefixLength) { |
| 85 | byte[] addr = null; |
| 86 | addr = InetAddresses.forString(strAddress).getAddress(); |
| 87 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 88 | if (addr == null || addr.length != ADDRESS_LENGTH_BYTES || |
| 89 | prefixLength < MIN_PREFIX_LENGTH || |
| 90 | prefixLength > MAX_PREFIX_LENGTH) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | throw new IllegalArgumentException(); |
| 92 | } |
| 93 | |
| 94 | address = canonicalizeAddress(addr, prefixLength); |
| 95 | this.prefixLength = prefixLength; |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 96 | binaryString = createBinaryString(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 97 | |
| 98 | try { |
| 99 | inetAddress = InetAddress.getByAddress(address); |
| 100 | } catch (UnknownHostException e) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 101 | throw new IllegalArgumentException("Couldn't parse IP address", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 105 | /** |
| 106 | * This method takes a byte array passed in by the user and ensures it |
| 107 | * conforms to the format we want. The byte array can contain anything, |
| 108 | * but only some of the bits are significant. The bits after |
| 109 | * prefixLengthValue are not significant, and this method will zero them |
| 110 | * out in order to ensure there is a canonical representation for each |
| 111 | * prefix. This simplifies the equals and hashcode methods, because once we |
| 112 | * have a canonical byte array representation we can simply compare byte |
| 113 | * arrays to test equality. |
| 114 | * |
| 115 | * @param addressValue the byte array to canonicalize |
| 116 | * @param prefixLengthValue The length of the prefix. Bits up to and including |
| 117 | * prefixLength are significant, bits following prefixLength are not |
| 118 | * significant and will be zeroed out. |
| 119 | * @return the canonicalized representation of the prefix |
| 120 | */ |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 121 | private byte[] canonicalizeAddress(byte[] addressValue, |
| 122 | int prefixLengthValue) { |
| 123 | byte[] result = new byte[addressValue.length]; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 124 | |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 125 | if (prefixLengthValue == 0) { |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 126 | for (int i = 0; i < ADDRESS_LENGTH_BYTES; i++) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 127 | result[i] = 0; |
| 128 | } |
| 129 | |
| 130 | return result; |
| 131 | } |
| 132 | |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 133 | result = Arrays.copyOf(addressValue, addressValue.length); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 134 | |
| 135 | //Set all bytes after the end of the prefix to 0 |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 136 | int lastByteIndex = (prefixLengthValue - 1) / Byte.SIZE; |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 137 | for (int i = lastByteIndex; i < ADDRESS_LENGTH_BYTES; i++) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 138 | result[i] = 0; |
| 139 | } |
| 140 | |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 141 | byte lastByte = addressValue[lastByteIndex]; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 142 | byte mask = 0; |
| 143 | byte msb = (byte) 0x80; |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 144 | int lastBit = (prefixLengthValue - 1) % Byte.SIZE; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 145 | for (int i = 0; i < Byte.SIZE; i++) { |
| 146 | if (i <= lastBit) { |
| 147 | mask |= (msb >> i); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | result[lastByteIndex] = (byte) (lastByte & mask); |
| 152 | |
| 153 | return result; |
| 154 | } |
| 155 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 156 | /** |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 157 | * Creates the binary string representation of the prefix. |
| 158 | * The strings length is equal to prefixLength. |
| 159 | * |
| 160 | * @return the binary string representation |
| 161 | */ |
| 162 | private String createBinaryString() { |
| 163 | if (prefixLength == 0) { |
| 164 | return ""; |
| 165 | } |
| 166 | |
| 167 | StringBuilder result = new StringBuilder(prefixLength); |
| 168 | for (int i = 0; i < address.length; i++) { |
| 169 | byte b = address[i]; |
| 170 | for (int j = 0; j < Byte.SIZE; j++) { |
| 171 | byte mask = (byte) (0x80 >>> j); |
| 172 | result.append(((b & mask) == 0) ? "0" : "1"); |
| 173 | if (i * Byte.SIZE + j == prefixLength - 1) { |
| 174 | return result.toString(); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | return result.toString(); |
| 179 | } |
| 180 | |
| 181 | /** |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 182 | * Gets the length of the prefix of the address. |
| 183 | * |
| 184 | * @return the prefix length |
| 185 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 186 | public int getPrefixLength() { |
| 187 | return prefixLength; |
| 188 | } |
| 189 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Gets the address. |
| 192 | * |
| 193 | * @return the address as a byte array |
| 194 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 195 | public byte[] getAddress() { |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame] | 196 | return Arrays.copyOf(address, address.length); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 197 | } |
| 198 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 199 | /** |
| 200 | * Gets the InetAddress. |
| 201 | * |
| 202 | * @return the inetAddress |
| 203 | */ |
| 204 | public InetAddress getInetAddress() { |
| 205 | return inetAddress; |
| 206 | } |
| 207 | |
| 208 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 209 | @Override |
| 210 | public boolean equals(Object other) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 211 | if (!(other instanceof Prefix)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 212 | return false; |
| 213 | } |
| 214 | |
| 215 | Prefix otherPrefix = (Prefix) other; |
| 216 | |
| 217 | return (Arrays.equals(address, otherPrefix.address)) && |
| 218 | (prefixLength == otherPrefix.prefixLength); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public int hashCode() { |
| 223 | int hash = 17; |
| 224 | hash = 31 * hash + prefixLength; |
| 225 | hash = 31 * hash + Arrays.hashCode(address); |
| 226 | return hash; |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public String toString() { |
| 231 | return inetAddress.getHostAddress() + "/" + prefixLength; |
| 232 | } |
| 233 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 234 | /** |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 235 | * Gets a binary string representation of the prefix. |
| 236 | * |
| 237 | * @return a binary string representation |
| 238 | */ |
| 239 | public String toBinaryString() { |
| 240 | return binaryString; |
| 241 | } |
| 242 | |
| 243 | /** |
Jonathan Hart | 850dddc | 2014-08-28 09:54:10 -0700 | [diff] [blame] | 244 | * Creates a Prefix object from a binary string. |
| 245 | * <p/> |
| 246 | * This is the reverse operation of {@link Prefix#toBinaryString()}. It |
| 247 | * takes a binary string (a String containing only '0' and '1' |
| 248 | * characters) and parses it to create a corresponding Prefix object. |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 249 | * |
Jonathan Hart | 850dddc | 2014-08-28 09:54:10 -0700 | [diff] [blame] | 250 | * @param binaryString the binary string to convert to Prefix |
| 251 | * @return a Prefix object representing the same prefix as the input string |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 252 | */ |
Jonathan Hart | 850dddc | 2014-08-28 09:54:10 -0700 | [diff] [blame] | 253 | public static Prefix fromBinaryString(String binaryString) { |
| 254 | if (binaryString.length() > MAX_PREFIX_LENGTH) { |
| 255 | throw new IllegalArgumentException( |
| 256 | "Binary string length must not be greater than " |
| 257 | + MAX_PREFIX_LENGTH); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 258 | } |
Jonathan Hart | 850dddc | 2014-08-28 09:54:10 -0700 | [diff] [blame] | 259 | |
| 260 | for (int i = 0; i < binaryString.length(); i++) { |
| 261 | char character = binaryString.charAt(i); |
| 262 | if (character != '0' && character != '1') { |
| 263 | throw new IllegalArgumentException( |
| 264 | "Binary string may only contain the characters " |
| 265 | + "\'0\' or \'1\'"); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // Pad the binary string out to be MAX_PREFIX_LENGTH bits long |
| 270 | StringBuilder paddedBinaryString = new StringBuilder(MAX_PREFIX_LENGTH); |
| 271 | paddedBinaryString.append(binaryString); |
| 272 | for (int i = binaryString.length(); i < MAX_PREFIX_LENGTH; i++) { |
| 273 | paddedBinaryString.append('0'); |
| 274 | } |
| 275 | |
| 276 | // BigInteger will parse the binary string to an integer using radix 2 |
| 277 | BigInteger bigInteger = |
| 278 | new BigInteger(paddedBinaryString.toString(), BINARY_RADIX); |
| 279 | |
| 280 | // Convert the integer to a byte array |
| 281 | ByteBuffer bb = ByteBuffer.allocate(ADDRESS_LENGTH_BYTES); |
| 282 | bb.putInt(bigInteger.intValue()); |
| 283 | |
| 284 | return new Prefix(bb.array(), binaryString.length()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 285 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 286 | } |