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