blob: a040b69492ee09901acba84d21660c0a082e9d7b [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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
Charles Chan928ff8b2017-05-04 12:22:54 -070018import com.google.common.collect.Sets;
19
alshabibc4901cd2014-09-05 16:50:40 -070020import java.util.Arrays;
Charles Chan928ff8b2017-05-04 12:22:54 -070021import java.util.Set;
alshabibc4901cd2014-09-05 16:50:40 -070022
23/**
24 * The class representing MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070025 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070026public class MacAddress {
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070027
Charles Chanba138712016-04-06 22:19:13 -070028 /**
29 * Dummy MAC address.
Charles Chan928ff8b2017-05-04 12:22:54 -070030 * We use the first MAC address in ONOS OUI range as a dummy MAC address.
Charles Chanba138712016-04-06 22:19:13 -070031 */
Charles Chan928ff8b2017-05-04 12:22:54 -070032 public static final MacAddress NONE = MacAddress.ONOS;
33 /**
34 * First MAC address in ONOS OUI range.
35 */
36 public static final MacAddress ONOS = valueOf("a4:23:05:00:00:00");
37 /**
38 * ONOS LLDP MAC address with multicast bit set.
39 */
40 public static final MacAddress ONOS_LLDP = valueOf("a5:23:05:00:00:01");
Charles Chanba138712016-04-06 22:19:13 -070041 /**
42 * All-zero MAC address.
43 */
tom545708e2014-10-09 17:10:02 -070044 public static final MacAddress ZERO = valueOf("00:00:00:00:00:00");
Charles Chanba138712016-04-06 22:19:13 -070045 /**
46 * Broadcast MAC address.
47 */
tom545708e2014-10-09 17:10:02 -070048 public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff");
Charles Chanba138712016-04-06 22:19:13 -070049 /**
50 * IPv4 multicast MAC address.
51 */
52 public static final MacAddress IPV4_MULTICAST = valueOf("01:00:5e:00:00:00");
53 /**
54 * IPv4 multicast MAC mask.
55 */
56 public static final MacAddress IPV4_MULTICAST_MASK = valueOf("ff:ff:ff:80:00:00");
Charles Chan928ff8b2017-05-04 12:22:54 -070057 /**
58 * A set of LLDP MAC addresses.
59 */
60 public static final Set<MacAddress> LLDP = Sets.newHashSet(
61 MacAddress.valueOf("01:80:c2:00:00:00"),
62 MacAddress.valueOf("01:80:c2:00:00:03"),
63 MacAddress.valueOf("01:80:c2:00:00:0e"));
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070064
alshabibc4901cd2014-09-05 16:50:40 -070065 public static final int MAC_ADDRESS_LENGTH = 6;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070066 private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -070067
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070068 public MacAddress(final byte[] address) {
69 this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070070 }
71
72 /**
73 * Returns a MAC address instance representing the value of the specified
74 * {@code String}.
75 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070076 * @param address the String representation of the MAC Address to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070077 * @return a MAC Address instance representing the value of the specified
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070078 * {@code String}.
79 * @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070080 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070081 public static MacAddress valueOf(final String address) {
alshabibc4901cd2014-09-05 16:50:40 -070082 final String[] elements = address.split(":");
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070083 if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070084 throw new IllegalArgumentException(
85 "Specified MAC Address must contain 12 hex digits"
86 + " separated pairwise by :'s.");
87 }
88
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070089 final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
90 for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
alshabibc4901cd2014-09-05 16:50:40 -070091 final String element = elements[i];
92 addressInBytes[i] = (byte) Integer.parseInt(element, 16);
93 }
94
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070095 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -070096 }
97
98 /**
99 * Returns a MAC address instance representing the specified {@code byte}
100 * array.
101 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700102 * @param address the byte array to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -0700103 * @return a MAC address instance representing the specified {@code byte}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700104 * array.
105 * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700106 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700107 public static MacAddress valueOf(final byte[] address) {
108 if (address.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -0700109 throw new IllegalArgumentException("the length is not "
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700110 + MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -0700111 }
112
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700113 return new MacAddress(address);
alshabibc4901cd2014-09-05 16:50:40 -0700114 }
115
116 /**
117 * Returns a MAC address instance representing the specified {@code long}
118 * value. The lower 48 bits of the long value are used to parse as a MAC
119 * address.
120 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700121 * @param address the long value to be parsed. The lower 48 bits are used for a
122 * MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700123 * @return a MAC address instance representing the specified {@code long}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700124 * value.
125 * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700126 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700127 public static MacAddress valueOf(final long address) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700128 final byte[] addressInBytes = new byte[]{
alshabibc4901cd2014-09-05 16:50:40 -0700129 (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff),
130 (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700131 (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff)};
alshabibc4901cd2014-09-05 16:50:40 -0700132
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700133 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700134 }
135
136 /**
137 * Returns the length of the {@code MACAddress}.
138 *
139 * @return the length of the {@code MACAddress}.
140 */
141 public int length() {
142 return this.address.length;
143 }
144
145 /**
146 * Returns the value of the {@code MACAddress} as a {@code byte} array.
147 *
148 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700149 * type {@code byte} array.
alshabibc4901cd2014-09-05 16:50:40 -0700150 */
151 public byte[] toBytes() {
152 return Arrays.copyOf(this.address, this.address.length);
153 }
154
155 /**
156 * Returns the value of the {@code MACAddress} as a {@code long}.
157 *
158 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700159 * type {@code long}.
alshabibc4901cd2014-09-05 16:50:40 -0700160 */
161 public long toLong() {
162 long mac = 0;
163 for (int i = 0; i < 6; i++) {
164 final long t = (this.address[i] & 0xffL) << (5 - i) * 8;
165 mac |= t;
166 }
167 return mac;
168 }
169
170 /**
171 * Returns {@code true} if the MAC address is the broadcast address.
172 *
173 * @return {@code true} if the MAC address is the broadcast address.
174 */
175 public boolean isBroadcast() {
176 for (final byte b : this.address) {
177 if (b != -1) {
178 return false;
179 }
180 }
181 return true;
182 }
183
184 /**
185 * Returns {@code true} if the MAC address is the multicast address.
186 *
187 * @return {@code true} if the MAC address is the multicast address.
188 */
189 public boolean isMulticast() {
190 if (this.isBroadcast()) {
191 return false;
192 }
193 return (this.address[0] & 0x01) != 0;
194 }
195
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700196 /**
197 * Returns true if this MAC address is link local.
198 *
199 * @return true if link local
Charles Chan928ff8b2017-05-04 12:22:54 -0700200 * @deprecated in Kingfisher release. Link local is not a correct description for
201 * this MAC address. Replaced with {@link #isLldp()}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700202 */
Charles Chan928ff8b2017-05-04 12:22:54 -0700203 @Deprecated
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700204 public boolean isLinkLocal() {
Charles Chan928ff8b2017-05-04 12:22:54 -0700205 return isLldp();
206 }
207
208 /**
209 * Returns true if this MAC address is used by link layer discovery protocol.
210 *
211 * @return true if this MAC is LLDP MAC.
212 */
213 public boolean isLldp() {
214 return LLDP.contains(this);
215 }
216
217 /**
218 * Returns true if the Organizationally Unique Identifier (OUI) of this MAC
219 * address matches ONOS OUI.
220 *
221 * @return true if the OUI of this MAC address matches ONOS OUI.
222 */
223 public boolean isOnos() {
224 return Arrays.equals(this.oui(), ONOS.oui());
225 }
226
227 /**
228 * Returns the Organizationally Unique Identifier (OUI) of this MAC address.
229 *
230 * @return the OUI of this MAC address.
231 */
232 public byte[] oui() {
233 return Arrays.copyOfRange(this.address, 0, 3);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700234 }
235
alshabibc4901cd2014-09-05 16:50:40 -0700236 @Override
237 public boolean equals(final Object o) {
238 if (o == this) {
239 return true;
240 }
241
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700242 if (!(o instanceof MacAddress)) {
alshabibc4901cd2014-09-05 16:50:40 -0700243 return false;
244 }
245
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700246 final MacAddress other = (MacAddress) o;
alshabibc4901cd2014-09-05 16:50:40 -0700247 return Arrays.equals(this.address, other.address);
248 }
249
250 @Override
251 public int hashCode() {
Brian O'Connor8576c2a2014-11-19 16:49:26 -0800252 return Long.hashCode(toLong());
alshabibc4901cd2014-09-05 16:50:40 -0700253 }
254
255 @Override
256 public String toString() {
257 final StringBuilder builder = new StringBuilder();
258 for (final byte b : this.address) {
259 if (builder.length() > 0) {
260 builder.append(":");
261 }
262 builder.append(String.format("%02X", b & 0xFF));
263 }
264 return builder.toString();
265 }
266
267 /**
268 * @return MAC address in string representation without colons (useful for
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700269 * radix tree storage)
alshabibc4901cd2014-09-05 16:50:40 -0700270 */
271 public String toStringNoColon() {
272 final StringBuilder builder = new StringBuilder();
273 for (final byte b : this.address) {
274 builder.append(String.format("%02X", b & 0xFF));
275 }
276 return builder.toString();
277 }
alshabibc4901cd2014-09-05 16:50:40 -0700278}