Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.util; |
| 2 | |
| 3 | import java.util.Arrays; |
| 4 | |
HIGUCHI Yuta | f086d8a | 2013-06-12 15:26:35 -0700 | [diff] [blame] | 5 | import net.onrc.onos.ofcontroller.util.serializers.MACAddressDeserializer; |
| 6 | import net.onrc.onos.ofcontroller.util.serializers.MACAddressSerializer; |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 7 | |
| 8 | import org.codehaus.jackson.annotate.JsonProperty; |
| 9 | import org.codehaus.jackson.map.annotate.JsonDeserialize; |
| 10 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 11 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 12 | /** |
| 13 | * The class representing MAC address. |
| 14 | * |
| 15 | * @author Sho Shimizu (sho.shimizu@gmail.com) |
| 16 | */ |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 17 | @JsonDeserialize(using=MACAddressDeserializer.class) |
| 18 | @JsonSerialize(using=MACAddressSerializer.class) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | public class MACAddress { |
| 20 | public static final int MAC_ADDRESS_LENGTH = 6; |
| 21 | private byte[] address = new byte[MAC_ADDRESS_LENGTH]; |
| 22 | |
| 23 | public MACAddress(byte[] address) { |
| 24 | this.address = Arrays.copyOf(address, MAC_ADDRESS_LENGTH); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Returns a MAC address instance representing the value of the specified {@code String}. |
| 29 | * @param address the String representation of the MAC Address to be parsed. |
| 30 | * @return a MAC Address instance representing the value of the specified {@code String}. |
| 31 | * @throws IllegalArgumentException if the string cannot be parsed as a MAC address. |
| 32 | */ |
| 33 | public static MACAddress valueOf(String address) { |
| 34 | String[] elements = address.split(":"); |
| 35 | if (elements.length != MAC_ADDRESS_LENGTH) { |
| 36 | throw new IllegalArgumentException( |
| 37 | "Specified MAC Address must contain 12 hex digits" + |
| 38 | " separated pairwise by :'s."); |
| 39 | } |
| 40 | |
| 41 | byte[] addressInBytes = new byte[MAC_ADDRESS_LENGTH]; |
| 42 | for (int i = 0; i < MAC_ADDRESS_LENGTH; i++) { |
| 43 | String element = elements[i]; |
| 44 | addressInBytes[i] = (byte)Integer.parseInt(element, 16); |
| 45 | } |
| 46 | |
| 47 | return new MACAddress(addressInBytes); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns a MAC address instance representing the specified {@code byte} array. |
| 52 | * @param address the byte array to be parsed. |
| 53 | * @return a MAC address instance representing the specified {@code byte} array. |
| 54 | * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address. |
| 55 | */ |
| 56 | public static MACAddress valueOf(byte[] address) { |
| 57 | if (address.length != MAC_ADDRESS_LENGTH) { |
| 58 | throw new IllegalArgumentException("the length is not " + MAC_ADDRESS_LENGTH); |
| 59 | } |
| 60 | |
| 61 | return new MACAddress(address); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns a MAC address instance representing the specified {@code long} value. |
| 66 | * The lower 48 bits of the long value are used to parse as a MAC address. |
| 67 | * @param address the long value to be parsed. The lower 48 bits are used for a MAC address. |
| 68 | * @return a MAC address instance representing the specified {@code long} value. |
| 69 | * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address. |
| 70 | */ |
| 71 | public static MACAddress valueOf(long address) { |
| 72 | byte[] addressInBytes = new byte[] { |
| 73 | (byte)((address >> 40) & 0xff), |
| 74 | (byte)((address >> 32) & 0xff), |
| 75 | (byte)((address >> 24) & 0xff), |
| 76 | (byte)((address >> 16) & 0xff), |
| 77 | (byte)((address >> 8 ) & 0xff), |
| 78 | (byte)((address >> 0) & 0xff) |
| 79 | }; |
| 80 | |
| 81 | return new MACAddress(addressInBytes); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the length of the {@code MACAddress}. |
| 86 | * @return the length of the {@code MACAddress}. |
| 87 | */ |
| 88 | public int length() { |
| 89 | return address.length; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns the value of the {@code MACAddress} as a {@code byte} array. |
| 94 | * @return the numeric value represented by this object after conversion to type {@code byte} array. |
| 95 | */ |
| 96 | public byte[] toBytes() { |
| 97 | return Arrays.copyOf(address, address.length); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns the value of the {@code MACAddress} as a {@code long}. |
| 102 | * @return the numeric value represented by this object after conversion to type {@code long}. |
| 103 | */ |
| 104 | public long toLong() { |
| 105 | long mac = 0; |
| 106 | for (int i = 0; i < 6; i++) { |
| 107 | long t = (address[i] & 0xffL) << ((5 - i) * 8); |
| 108 | mac |= t; |
| 109 | } |
| 110 | return mac; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns {@code true} if the MAC address is the broadcast address. |
| 115 | * @return {@code true} if the MAC address is the broadcast address. |
| 116 | */ |
| 117 | public boolean isBroadcast() { |
| 118 | for (byte b : address) { |
| 119 | if (b != -1) // checks if equal to 0xff |
| 120 | return false; |
| 121 | } |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns {@code true} if the MAC address is the multicast address. |
| 127 | * @return {@code true} if the MAC address is the multicast address. |
| 128 | */ |
| 129 | public boolean isMulticast() { |
| 130 | if (isBroadcast()) { |
| 131 | return false; |
| 132 | } |
| 133 | return (address[0] & 0x01) != 0; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public boolean equals(Object o) { |
| 138 | if (o == this) { |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | if (!(o instanceof MACAddress)) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | MACAddress other = (MACAddress)o; |
| 147 | return Arrays.equals(this.address, other.address); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public int hashCode() { |
| 152 | return Arrays.hashCode(this.address); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public String toString() { |
| 157 | StringBuilder builder = new StringBuilder(); |
| 158 | for (byte b: address) { |
| 159 | if (builder.length() > 0) { |
| 160 | builder.append(":"); |
| 161 | } |
| 162 | builder.append(String.format("%02X", b & 0xFF)); |
| 163 | } |
| 164 | return builder.toString(); |
| 165 | } |
| 166 | } |