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