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