blob: 4da6fe54d9bf6a927f8359adf9b1411139de30c2 [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 /**
Charles Chan928ff8b2017-05-04 12:22:54 -070029 * First MAC address in ONOS OUI range.
30 */
31 public static final MacAddress ONOS = valueOf("a4:23:05:00:00:00");
32 /**
Charles Chan79cddb12017-05-12 18:07:16 -070033 * Dummy MAC address. We use the first MAC address in ONOS OUI range as the dummy MAC address.
34 */
35 public static final MacAddress NONE = ONOS;
36 /**
Charles Chan928ff8b2017-05-04 12:22:54 -070037 * ONOS LLDP MAC address with multicast bit set.
38 */
39 public static final MacAddress ONOS_LLDP = valueOf("a5:23:05:00:00:01");
Charles Chanba138712016-04-06 22:19:13 -070040 /**
41 * All-zero MAC address.
42 */
tom545708e2014-10-09 17:10:02 -070043 public static final MacAddress ZERO = valueOf("00:00:00:00:00:00");
Charles Chanba138712016-04-06 22:19:13 -070044 /**
45 * Broadcast MAC address.
46 */
tom545708e2014-10-09 17:10:02 -070047 public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff");
Charles Chanba138712016-04-06 22:19:13 -070048 /**
49 * IPv4 multicast MAC address.
50 */
51 public static final MacAddress IPV4_MULTICAST = valueOf("01:00:5e:00:00:00");
52 /**
53 * IPv4 multicast MAC mask.
54 */
55 public static final MacAddress IPV4_MULTICAST_MASK = valueOf("ff:ff:ff:80:00:00");
Charles Chan928ff8b2017-05-04 12:22:54 -070056 /**
57 * A set of LLDP MAC addresses.
58 */
59 public static final Set<MacAddress> LLDP = Sets.newHashSet(
60 MacAddress.valueOf("01:80:c2:00:00:00"),
61 MacAddress.valueOf("01:80:c2:00:00:03"),
62 MacAddress.valueOf("01:80:c2:00:00:0e"));
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070063
alshabibc4901cd2014-09-05 16:50:40 -070064 public static final int MAC_ADDRESS_LENGTH = 6;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070065 private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -070066
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070067 public MacAddress(final byte[] address) {
68 this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070069 }
70
71 /**
72 * Returns a MAC address instance representing the value of the specified
73 * {@code String}.
74 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070075 * @param address the String representation of the MAC Address to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070076 * @return a MAC Address instance representing the value of the specified
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070077 * {@code String}.
78 * @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070079 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070080 public static MacAddress valueOf(final String address) {
alshabibc4901cd2014-09-05 16:50:40 -070081 final String[] elements = address.split(":");
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070082 if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070083 throw new IllegalArgumentException(
84 "Specified MAC Address must contain 12 hex digits"
85 + " separated pairwise by :'s.");
86 }
87
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070088 final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
89 for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
alshabibc4901cd2014-09-05 16:50:40 -070090 final String element = elements[i];
91 addressInBytes[i] = (byte) Integer.parseInt(element, 16);
92 }
93
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070094 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -070095 }
96
97 /**
98 * Returns a MAC address instance representing the specified {@code byte}
99 * array.
100 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700101 * @param address the byte array to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -0700102 * @return a MAC address instance representing the specified {@code byte}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700103 * array.
104 * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700105 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700106 public static MacAddress valueOf(final byte[] address) {
107 if (address.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -0700108 throw new IllegalArgumentException("the length is not "
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700109 + MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -0700110 }
111
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700112 return new MacAddress(address);
alshabibc4901cd2014-09-05 16:50:40 -0700113 }
114
115 /**
116 * Returns a MAC address instance representing the specified {@code long}
117 * value. The lower 48 bits of the long value are used to parse as a MAC
118 * address.
119 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700120 * @param address the long value to be parsed. The lower 48 bits are used for a
121 * MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700122 * @return a MAC address instance representing the specified {@code long}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700123 * value.
124 * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700125 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700126 public static MacAddress valueOf(final long address) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700127 final byte[] addressInBytes = new byte[]{
alshabibc4901cd2014-09-05 16:50:40 -0700128 (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff),
129 (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700130 (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff)};
alshabibc4901cd2014-09-05 16:50:40 -0700131
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700132 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700133 }
134
135 /**
136 * Returns the length of the {@code MACAddress}.
137 *
138 * @return the length of the {@code MACAddress}.
139 */
140 public int length() {
141 return this.address.length;
142 }
143
144 /**
145 * Returns the value of the {@code MACAddress} as a {@code byte} array.
146 *
147 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700148 * type {@code byte} array.
alshabibc4901cd2014-09-05 16:50:40 -0700149 */
150 public byte[] toBytes() {
151 return Arrays.copyOf(this.address, this.address.length);
152 }
153
154 /**
155 * Returns the value of the {@code MACAddress} as a {@code long}.
156 *
157 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700158 * type {@code long}.
alshabibc4901cd2014-09-05 16:50:40 -0700159 */
160 public long toLong() {
161 long mac = 0;
162 for (int i = 0; i < 6; i++) {
163 final long t = (this.address[i] & 0xffL) << (5 - i) * 8;
164 mac |= t;
165 }
166 return mac;
167 }
168
169 /**
170 * Returns {@code true} if the MAC address is the broadcast address.
171 *
172 * @return {@code true} if the MAC address is the broadcast address.
173 */
174 public boolean isBroadcast() {
175 for (final byte b : this.address) {
176 if (b != -1) {
177 return false;
178 }
179 }
180 return true;
181 }
182
183 /**
184 * Returns {@code true} if the MAC address is the multicast address.
185 *
186 * @return {@code true} if the MAC address is the multicast address.
187 */
188 public boolean isMulticast() {
189 if (this.isBroadcast()) {
190 return false;
191 }
192 return (this.address[0] & 0x01) != 0;
193 }
194
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700195 /**
196 * Returns true if this MAC address is link local.
197 *
198 * @return true if link local
Charles Chan928ff8b2017-05-04 12:22:54 -0700199 * @deprecated in Kingfisher release. Link local is not a correct description for
200 * this MAC address. Replaced with {@link #isLldp()}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700201 */
Charles Chan928ff8b2017-05-04 12:22:54 -0700202 @Deprecated
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700203 public boolean isLinkLocal() {
Charles Chan928ff8b2017-05-04 12:22:54 -0700204 return isLldp();
205 }
206
207 /**
208 * Returns true if this MAC address is used by link layer discovery protocol.
209 *
210 * @return true if this MAC is LLDP MAC.
211 */
212 public boolean isLldp() {
213 return LLDP.contains(this);
214 }
215
216 /**
217 * Returns true if the Organizationally Unique Identifier (OUI) of this MAC
218 * address matches ONOS OUI.
219 *
220 * @return true if the OUI of this MAC address matches ONOS OUI.
221 */
222 public boolean isOnos() {
223 return Arrays.equals(this.oui(), ONOS.oui());
224 }
225
226 /**
227 * Returns the Organizationally Unique Identifier (OUI) of this MAC address.
228 *
229 * @return the OUI of this MAC address.
230 */
231 public byte[] oui() {
232 return Arrays.copyOfRange(this.address, 0, 3);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700233 }
234
alshabibc4901cd2014-09-05 16:50:40 -0700235 @Override
236 public boolean equals(final Object o) {
237 if (o == this) {
238 return true;
239 }
240
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700241 if (!(o instanceof MacAddress)) {
alshabibc4901cd2014-09-05 16:50:40 -0700242 return false;
243 }
244
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700245 final MacAddress other = (MacAddress) o;
alshabibc4901cd2014-09-05 16:50:40 -0700246 return Arrays.equals(this.address, other.address);
247 }
248
249 @Override
250 public int hashCode() {
Brian O'Connor8576c2a2014-11-19 16:49:26 -0800251 return Long.hashCode(toLong());
alshabibc4901cd2014-09-05 16:50:40 -0700252 }
253
254 @Override
255 public String toString() {
256 final StringBuilder builder = new StringBuilder();
257 for (final byte b : this.address) {
258 if (builder.length() > 0) {
259 builder.append(":");
260 }
261 builder.append(String.format("%02X", b & 0xFF));
262 }
263 return builder.toString();
264 }
265
266 /**
267 * @return MAC address in string representation without colons (useful for
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700268 * radix tree storage)
alshabibc4901cd2014-09-05 16:50:40 -0700269 */
270 public String toStringNoColon() {
271 final StringBuilder builder = new StringBuilder();
272 for (final byte b : this.address) {
273 builder.append(String.format("%02X", b & 0xFF));
274 }
275 return builder.toString();
276 }
alshabibc4901cd2014-09-05 16:50:40 -0700277}