Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Andreas Wundsam | 5f71b41 | 2014-02-18 12:56:35 -0800 | [diff] [blame] | 3 | import java.util.Arrays; |
| 4 | |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 5 | import javax.annotation.Nonnull; |
| 6 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 7 | import org.jboss.netty.buffer.ChannelBuffer; |
| 8 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 9 | import org.projectfloodlight.openflow.util.HexString; |
| 10 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 11 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 12 | import com.google.common.primitives.Longs; |
| 13 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 14 | /** |
| 15 | * Wrapper around a 6 byte mac address. |
| 16 | * |
| 17 | * @author Andreas Wundsam <andreas.wundsam@bigswitch.com> |
| 18 | */ |
| 19 | |
| 20 | public class MacAddress implements OFValueType<MacAddress> { |
| 21 | static final int MacAddrLen = 6; |
| 22 | private final long rawValue; |
| 23 | |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 24 | private final static long NONE_VAL = 0x0L; |
| 25 | public static final MacAddress NONE = new MacAddress(NONE_VAL); |
| 26 | |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 27 | private final static long BROADCAST_VAL = 0x0000FFFFFFFFFFFFL; |
| 28 | public static final MacAddress BROADCAST = new MacAddress(BROADCAST_VAL); |
| 29 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 30 | public static final MacAddress NO_MASK = MacAddress.of(0xFFFFFFFFFFFFFFFFl); |
| 31 | public static final MacAddress FULL_MASK = MacAddress.of(0x0); |
| 32 | |
Shudong Zhou | a4fb4c6 | 2014-02-10 16:57:45 -0800 | [diff] [blame] | 33 | private static final long LLDP_MAC_ADDRESS_MASK = 0xfffffffffff0L; |
| 34 | private static final long LLDP_MAC_ADDRESS_VALUE = 0x0180c2000000L; |
Shudong Zhou | e5a3680 | 2014-02-09 14:54:14 -0800 | [diff] [blame] | 35 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 36 | private MacAddress(final long rawValue) { |
| 37 | this.rawValue = rawValue; |
| 38 | } |
| 39 | |
| 40 | public static MacAddress of(final byte[] address) { |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 41 | if (address.length != MacAddrLen) |
| 42 | throw new IllegalArgumentException( |
| 43 | "Mac address byte array must be exactly 6 bytes long; length = " + address.length); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 44 | long raw = |
| 45 | (address[0] & 0xFFL) << 40 | (address[1] & 0xFFL) << 32 |
| 46 | | (address[2] & 0xFFL) << 24 | (address[3] & 0xFFL) << 16 |
| 47 | | (address[4] & 0xFFL) << 8 | (address[5] & 0xFFL); |
| 48 | return MacAddress.of(raw); |
| 49 | } |
| 50 | |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 51 | public static MacAddress of(long raw) { |
| 52 | raw &= BROADCAST_VAL; |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 53 | if(raw == NONE_VAL) |
| 54 | return NONE; |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 55 | if (raw == BROADCAST_VAL) |
| 56 | return BROADCAST; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 57 | return new MacAddress(raw); |
| 58 | } |
| 59 | |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 60 | /** Parse a mac adress from the canonical string representation as |
| 61 | * 6 hex bytes separated by colons (01:02:03:04:05:06). |
| 62 | * |
| 63 | * @param macString - a mac address in canonical string representation |
| 64 | * @return the parsed MacAddress |
| 65 | * @throws IllegalArgumentException if macString is not a valid mac adddress |
| 66 | */ |
| 67 | @Nonnull |
| 68 | public static MacAddress of(@Nonnull final String macString) throws IllegalArgumentException { |
| 69 | if (macString == null) { |
| 70 | throw new NullPointerException("macString must not be null"); |
| 71 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 72 | int index = 0; |
| 73 | int shift = 40; |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 74 | final String FORMAT_ERROR = "Mac address is not well-formed. " + |
| 75 | "It must consist of 6 hex digit pairs separated by colons: "; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 76 | |
| 77 | long raw = 0; |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 78 | if (macString.length() != 6 * 2 + 5) |
| 79 | throw new IllegalArgumentException(FORMAT_ERROR + macString); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 80 | |
| 81 | while (shift >= 0) { |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 82 | int digit1 = Character.digit(macString.charAt(index++), 16); |
| 83 | int digit2 = Character.digit(macString.charAt(index++), 16); |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 84 | if ((digit1 < 0) || (digit2 < 0)) |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 85 | throw new IllegalArgumentException(FORMAT_ERROR + macString); |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 86 | raw |= ((long) (digit1 << 4 | digit2)) << shift; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 87 | |
| 88 | if (shift == 0) |
| 89 | break; |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 90 | if (macString.charAt(index++) != ':') |
| 91 | throw new IllegalArgumentException(FORMAT_ERROR + macString); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 92 | shift -= 8; |
| 93 | } |
| 94 | return MacAddress.of(raw); |
| 95 | } |
| 96 | |
Andreas Wundsam | 4e2469e | 2014-02-17 15:32:43 -0800 | [diff] [blame] | 97 | private volatile byte[] bytesCache = null; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 98 | |
| 99 | public byte[] getBytes() { |
| 100 | if (bytesCache == null) { |
| 101 | synchronized (this) { |
| 102 | if (bytesCache == null) { |
| 103 | bytesCache = |
| 104 | new byte[] { (byte) ((rawValue >> 40) & 0xFF), |
| 105 | (byte) ((rawValue >> 32) & 0xFF), |
| 106 | (byte) ((rawValue >> 24) & 0xFF), |
| 107 | (byte) ((rawValue >> 16) & 0xFF), |
| 108 | (byte) ((rawValue >> 8) & 0xFF), |
| 109 | (byte) ((rawValue >> 0) & 0xFF) }; |
| 110 | } |
| 111 | } |
| 112 | } |
Andreas Wundsam | 5f71b41 | 2014-02-18 12:56:35 -0800 | [diff] [blame] | 113 | return Arrays.copyOf(bytesCache, bytesCache.length); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Rob Vaterlaus | 83f40ab | 2013-10-14 15:44:46 -0700 | [diff] [blame] | 116 | /** |
| 117 | * Returns {@code true} if the MAC address is the broadcast address. |
| 118 | * @return {@code true} if the MAC address is the broadcast address. |
| 119 | */ |
| 120 | public boolean isBroadcast() { |
| 121 | return this == BROADCAST; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns {@code true} if the MAC address is a multicast address. |
| 126 | * @return {@code true} if the MAC address is a multicast address. |
| 127 | */ |
| 128 | public boolean isMulticast() { |
| 129 | if (isBroadcast()) { |
| 130 | return false; |
| 131 | } |
| 132 | return (rawValue & (0x01L << 40)) != 0; |
| 133 | } |
| 134 | |
Shudong Zhou | e5a3680 | 2014-02-09 14:54:14 -0800 | [diff] [blame] | 135 | /** |
Shudong Zhou | a4fb4c6 | 2014-02-10 16:57:45 -0800 | [diff] [blame] | 136 | * Returns {@code true} if the MAC address is an LLDP mac address. |
| 137 | * @return {@code true} if the MAC address is an LLDP mac address. |
Shudong Zhou | e5a3680 | 2014-02-09 14:54:14 -0800 | [diff] [blame] | 138 | */ |
Shudong Zhou | a4fb4c6 | 2014-02-10 16:57:45 -0800 | [diff] [blame] | 139 | public boolean isLLDPAddress() { |
| 140 | return (rawValue & LLDP_MAC_ADDRESS_MASK) == LLDP_MAC_ADDRESS_VALUE; |
Shudong Zhou | e5a3680 | 2014-02-09 14:54:14 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 143 | @Override |
| 144 | public int getLength() { |
| 145 | return MacAddrLen; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public String toString() { |
| 150 | return HexString.toHexString(rawValue, 6); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public int hashCode() { |
| 155 | final int prime = 31; |
| 156 | int result = 1; |
| 157 | result = prime * result + (int) (rawValue ^ (rawValue >>> 32)); |
| 158 | return result; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public boolean equals(final Object obj) { |
| 163 | if (this == obj) |
| 164 | return true; |
| 165 | if (obj == null) |
| 166 | return false; |
| 167 | if (getClass() != obj.getClass()) |
| 168 | return false; |
| 169 | MacAddress other = (MacAddress) obj; |
| 170 | if (rawValue != other.rawValue) |
| 171 | return false; |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | public long getLong() { |
| 176 | return rawValue; |
| 177 | } |
| 178 | |
| 179 | public void write6Bytes(ChannelBuffer c) { |
| 180 | c.writeInt((int) (this.rawValue >> 16)); |
| 181 | c.writeShort((int) this.rawValue & 0xFFFF); |
| 182 | } |
| 183 | |
| 184 | public static MacAddress read6Bytes(ChannelBuffer c) throws OFParseError { |
| 185 | long raw = c.readUnsignedInt() << 16 | c.readUnsignedShort(); |
| 186 | return MacAddress.of(raw); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public MacAddress applyMask(MacAddress mask) { |
| 191 | return MacAddress.of(this.rawValue & mask.rawValue); |
| 192 | } |
| 193 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 194 | @Override |
| 195 | public int compareTo(MacAddress o) { |
| 196 | return Longs.compare(rawValue, o.rawValue); |
| 197 | } |
| 198 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 199 | @Override |
| 200 | public void putTo(PrimitiveSink sink) { |
| 201 | sink.putInt((int) (this.rawValue >> 16)); |
| 202 | sink.putShort((short) (this.rawValue & 0xFFFF)); |
| 203 | } |
| 204 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 205 | |
| 206 | |
| 207 | } |