blob: 8a6d918da5674bc48a0db93869092b2967f2bd55 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
alshabibc4901cd2014-09-05 16:50:40 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
alshabibc4901cd2014-09-05 16:50:40 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
alshabibc4901cd2014-09-05 16:50:40 -070016package org.onlab.packet;
17
Yuta HIGUCHIbfc2e922017-06-07 21:46:05 -070018import com.google.common.collect.ImmutableSet;
alshabibc4901cd2014-09-05 16:50:40 -070019import java.util.Arrays;
Charles Chan928ff8b2017-05-04 12:22:54 -070020import java.util.Set;
Dominika Molenda52cc3a92017-06-19 17:08:35 +020021import java.util.regex.Matcher;
22import java.util.regex.Pattern;
alshabibc4901cd2014-09-05 16:50:40 -070023
24/**
25 * The class representing MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070026 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070027public class MacAddress {
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070028
Dominika Molenda52cc3a92017-06-19 17:08:35 +020029 private static final Pattern MAC_PATTERN = Pattern.compile("^([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})$");
Charles Chanba138712016-04-06 22:19:13 -070030 /**
Charles Chan928ff8b2017-05-04 12:22:54 -070031 * First MAC address in ONOS OUI range.
32 */
33 public static final MacAddress ONOS = valueOf("a4:23:05:00:00:00");
34 /**
Charles Chan79cddb12017-05-12 18:07:16 -070035 * Dummy MAC address. We use the first MAC address in ONOS OUI range as the dummy MAC address.
36 */
37 public static final MacAddress NONE = ONOS;
38 /**
jaegonkim47b1b4a2017-12-04 14:08:26 +090039 * ONOS LLDP MAC address with slow protocol destination address.
Charles Chan928ff8b2017-05-04 12:22:54 -070040 */
jaegonkim47b1b4a2017-12-04 14:08:26 +090041 public static final MacAddress ONOS_LLDP = valueOf("01:80:C2:00:00:0E");
Charles Chanba138712016-04-06 22:19:13 -070042 /**
43 * All-zero MAC address.
44 */
tom545708e2014-10-09 17:10:02 -070045 public static final MacAddress ZERO = valueOf("00:00:00:00:00:00");
Charles Chanba138712016-04-06 22:19:13 -070046 /**
47 * Broadcast MAC address.
48 */
tom545708e2014-10-09 17:10:02 -070049 public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff");
Charles Chanba138712016-04-06 22:19:13 -070050 /**
51 * IPv4 multicast MAC address.
52 */
53 public static final MacAddress IPV4_MULTICAST = valueOf("01:00:5e:00:00:00");
54 /**
55 * IPv4 multicast MAC mask.
56 */
57 public static final MacAddress IPV4_MULTICAST_MASK = valueOf("ff:ff:ff:80:00:00");
Charles Chan928ff8b2017-05-04 12:22:54 -070058 /**
Julia Ferguson65428c32017-08-10 18:15:24 +000059 * IPv6 multicast MAC address.
60 */
61 public static final MacAddress IPV6_MULTICAST = valueOf("33:33:00:00:00:00");
62 /**
63 * IPv6 multicast MAC mask.
64 */
65 public static final MacAddress IPV6_MULTICAST_MASK = valueOf("FF:FF:00:00:00:00");
66 /**
Charles Chan928ff8b2017-05-04 12:22:54 -070067 * A set of LLDP MAC addresses.
68 */
Yuta HIGUCHIbfc2e922017-06-07 21:46:05 -070069 public static final Set<MacAddress> LLDP = ImmutableSet.of(
Charles Chan928ff8b2017-05-04 12:22:54 -070070 MacAddress.valueOf("01:80:c2:00:00:00"),
71 MacAddress.valueOf("01:80:c2:00:00:03"),
72 MacAddress.valueOf("01:80:c2:00:00:0e"));
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070073
alshabibc4901cd2014-09-05 16:50:40 -070074 public static final int MAC_ADDRESS_LENGTH = 6;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070075 private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -070076
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070077 public MacAddress(final byte[] address) {
78 this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070079 }
80
81 /**
82 * Returns a MAC address instance representing the value of the specified
83 * {@code String}.
84 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070085 * @param address the String representation of the MAC Address to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070086 * @return a MAC Address instance representing the value of the specified
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070087 * {@code String}.
88 * @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070089 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070090 public static MacAddress valueOf(final String address) {
Dominika Molenda52cc3a92017-06-19 17:08:35 +020091 if (!isValid(address)) {
alshabibc4901cd2014-09-05 16:50:40 -070092 throw new IllegalArgumentException(
93 "Specified MAC Address must contain 12 hex digits"
94 + " separated pairwise by :'s.");
95 }
Dominika Molenda52cc3a92017-06-19 17:08:35 +020096 final String[] elements = address.split(":");
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070097 final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
98 for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
alshabibc4901cd2014-09-05 16:50:40 -070099 final String element = elements[i];
100 addressInBytes[i] = (byte) Integer.parseInt(element, 16);
101 }
102
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700103 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700104 }
105
106 /**
107 * Returns a MAC address instance representing the specified {@code byte}
108 * array.
109 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700110 * @param address the byte array to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -0700111 * @return a MAC address instance representing the specified {@code byte}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700112 * array.
113 * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700114 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700115 public static MacAddress valueOf(final byte[] address) {
116 if (address.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -0700117 throw new IllegalArgumentException("the length is not "
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700118 + MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -0700119 }
120
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700121 return new MacAddress(address);
alshabibc4901cd2014-09-05 16:50:40 -0700122 }
123
124 /**
125 * Returns a MAC address instance representing the specified {@code long}
126 * value. The lower 48 bits of the long value are used to parse as a MAC
127 * address.
128 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700129 * @param address the long value to be parsed. The lower 48 bits are used for a
130 * MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700131 * @return a MAC address instance representing the specified {@code long}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700132 * value.
133 * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700134 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700135 public static MacAddress valueOf(final long address) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700136 final byte[] addressInBytes = new byte[]{
alshabibc4901cd2014-09-05 16:50:40 -0700137 (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff),
138 (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700139 (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff)};
alshabibc4901cd2014-09-05 16:50:40 -0700140
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700141 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700142 }
143
144 /**
145 * Returns the length of the {@code MACAddress}.
146 *
147 * @return the length of the {@code MACAddress}.
148 */
149 public int length() {
150 return this.address.length;
151 }
152
153 /**
154 * Returns the value of the {@code MACAddress} as a {@code byte} array.
155 *
156 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700157 * type {@code byte} array.
alshabibc4901cd2014-09-05 16:50:40 -0700158 */
159 public byte[] toBytes() {
160 return Arrays.copyOf(this.address, this.address.length);
161 }
162
163 /**
164 * Returns the value of the {@code MACAddress} as a {@code long}.
165 *
166 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700167 * type {@code long}.
alshabibc4901cd2014-09-05 16:50:40 -0700168 */
169 public long toLong() {
170 long mac = 0;
171 for (int i = 0; i < 6; i++) {
172 final long t = (this.address[i] & 0xffL) << (5 - i) * 8;
173 mac |= t;
174 }
175 return mac;
176 }
177
178 /**
179 * Returns {@code true} if the MAC address is the broadcast address.
180 *
181 * @return {@code true} if the MAC address is the broadcast address.
182 */
183 public boolean isBroadcast() {
184 for (final byte b : this.address) {
185 if (b != -1) {
186 return false;
187 }
188 }
189 return true;
190 }
191
192 /**
193 * Returns {@code true} if the MAC address is the multicast address.
194 *
195 * @return {@code true} if the MAC address is the multicast address.
196 */
197 public boolean isMulticast() {
198 if (this.isBroadcast()) {
199 return false;
200 }
201 return (this.address[0] & 0x01) != 0;
202 }
203
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700204 /**
205 * Returns true if this MAC address is link local.
206 *
207 * @return true if link local
Charles Chan928ff8b2017-05-04 12:22:54 -0700208 * @deprecated in Kingfisher release. Link local is not a correct description for
209 * this MAC address. Replaced with {@link #isLldp()}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700210 */
Charles Chan928ff8b2017-05-04 12:22:54 -0700211 @Deprecated
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700212 public boolean isLinkLocal() {
Charles Chan928ff8b2017-05-04 12:22:54 -0700213 return isLldp();
214 }
215
216 /**
217 * Returns true if this MAC address is used by link layer discovery protocol.
218 *
219 * @return true if this MAC is LLDP MAC.
220 */
221 public boolean isLldp() {
222 return LLDP.contains(this);
223 }
224
225 /**
226 * Returns true if the Organizationally Unique Identifier (OUI) of this MAC
227 * address matches ONOS OUI.
228 *
229 * @return true if the OUI of this MAC address matches ONOS OUI.
230 */
231 public boolean isOnos() {
232 return Arrays.equals(this.oui(), ONOS.oui());
233 }
234
235 /**
236 * Returns the Organizationally Unique Identifier (OUI) of this MAC address.
237 *
238 * @return the OUI of this MAC address.
239 */
240 public byte[] oui() {
241 return Arrays.copyOfRange(this.address, 0, 3);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700242 }
243
alshabibc4901cd2014-09-05 16:50:40 -0700244 @Override
245 public boolean equals(final Object o) {
246 if (o == this) {
247 return true;
248 }
249
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700250 if (!(o instanceof MacAddress)) {
alshabibc4901cd2014-09-05 16:50:40 -0700251 return false;
252 }
253
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700254 final MacAddress other = (MacAddress) o;
alshabibc4901cd2014-09-05 16:50:40 -0700255 return Arrays.equals(this.address, other.address);
256 }
257
258 @Override
259 public int hashCode() {
Brian O'Connor8576c2a2014-11-19 16:49:26 -0800260 return Long.hashCode(toLong());
alshabibc4901cd2014-09-05 16:50:40 -0700261 }
262
263 @Override
264 public String toString() {
Yuta HIGUCHIed1ef3a2017-05-15 18:10:01 -0700265 final StringBuilder builder = new StringBuilder(2 * 6 + 5);
alshabibc4901cd2014-09-05 16:50:40 -0700266 for (final byte b : this.address) {
267 if (builder.length() > 0) {
Yuta HIGUCHIed1ef3a2017-05-15 18:10:01 -0700268 builder.append(':');
alshabibc4901cd2014-09-05 16:50:40 -0700269 }
270 builder.append(String.format("%02X", b & 0xFF));
271 }
272 return builder.toString();
273 }
274
275 /**
276 * @return MAC address in string representation without colons (useful for
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700277 * radix tree storage)
alshabibc4901cd2014-09-05 16:50:40 -0700278 */
279 public String toStringNoColon() {
280 final StringBuilder builder = new StringBuilder();
281 for (final byte b : this.address) {
282 builder.append(String.format("%02X", b & 0xFF));
283 }
284 return builder.toString();
285 }
Dominika Molenda52cc3a92017-06-19 17:08:35 +0200286
287 private static boolean isValid(final String mac) {
288 Matcher matcher = MAC_PATTERN.matcher(mac);
289 return matcher.matches();
290 }
alshabibc4901cd2014-09-05 16:50:40 -0700291}