blob: 1027708236153fb8eafed9af25f6091e45b8c9f6 [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
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;
alshabibc4901cd2014-09-05 16:50:40 -070021
22/**
23 * The class representing MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070024 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070025public class MacAddress {
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070026
Charles Chanba138712016-04-06 22:19:13 -070027 /**
Charles Chan928ff8b2017-05-04 12:22:54 -070028 * First MAC address in ONOS OUI range.
29 */
30 public static final MacAddress ONOS = valueOf("a4:23:05:00:00:00");
31 /**
Charles Chan79cddb12017-05-12 18:07:16 -070032 * Dummy MAC address. We use the first MAC address in ONOS OUI range as the dummy MAC address.
33 */
34 public static final MacAddress NONE = ONOS;
35 /**
Charles Chan928ff8b2017-05-04 12:22:54 -070036 * ONOS LLDP MAC address with multicast bit set.
37 */
38 public static final MacAddress ONOS_LLDP = valueOf("a5:23:05:00:00:01");
Charles Chanba138712016-04-06 22:19:13 -070039 /**
40 * All-zero MAC address.
41 */
tom545708e2014-10-09 17:10:02 -070042 public static final MacAddress ZERO = valueOf("00:00:00:00:00:00");
Charles Chanba138712016-04-06 22:19:13 -070043 /**
44 * Broadcast MAC address.
45 */
tom545708e2014-10-09 17:10:02 -070046 public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff");
Charles Chanba138712016-04-06 22:19:13 -070047 /**
48 * IPv4 multicast MAC address.
49 */
50 public static final MacAddress IPV4_MULTICAST = valueOf("01:00:5e:00:00:00");
51 /**
52 * IPv4 multicast MAC mask.
53 */
54 public static final MacAddress IPV4_MULTICAST_MASK = valueOf("ff:ff:ff:80:00:00");
Charles Chan928ff8b2017-05-04 12:22:54 -070055 /**
56 * A set of LLDP MAC addresses.
57 */
Yuta HIGUCHIbfc2e922017-06-07 21:46:05 -070058 public static final Set<MacAddress> LLDP = ImmutableSet.of(
Charles Chan928ff8b2017-05-04 12:22:54 -070059 MacAddress.valueOf("01:80:c2:00:00:00"),
60 MacAddress.valueOf("01:80:c2:00:00:03"),
61 MacAddress.valueOf("01:80:c2:00:00:0e"));
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070062
alshabibc4901cd2014-09-05 16:50:40 -070063 public static final int MAC_ADDRESS_LENGTH = 6;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070064 private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -070065
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070066 public MacAddress(final byte[] address) {
67 this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070068 }
69
70 /**
71 * Returns a MAC address instance representing the value of the specified
72 * {@code String}.
73 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070074 * @param address the String representation of the MAC Address to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -070075 * @return a MAC Address instance representing the value of the specified
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070076 * {@code String}.
77 * @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -070078 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070079 public static MacAddress valueOf(final String address) {
alshabibc4901cd2014-09-05 16:50:40 -070080 final String[] elements = address.split(":");
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070081 if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070082 throw new IllegalArgumentException(
83 "Specified MAC Address must contain 12 hex digits"
84 + " separated pairwise by :'s.");
85 }
86
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070087 final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
88 for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
alshabibc4901cd2014-09-05 16:50:40 -070089 final String element = elements[i];
90 addressInBytes[i] = (byte) Integer.parseInt(element, 16);
91 }
92
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070093 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -070094 }
95
96 /**
97 * Returns a MAC address instance representing the specified {@code byte}
98 * array.
99 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700100 * @param address the byte array to be parsed.
alshabibc4901cd2014-09-05 16:50:40 -0700101 * @return a MAC address instance representing the specified {@code byte}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700102 * array.
103 * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700104 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700105 public static MacAddress valueOf(final byte[] address) {
106 if (address.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -0700107 throw new IllegalArgumentException("the length is not "
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700108 + MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -0700109 }
110
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700111 return new MacAddress(address);
alshabibc4901cd2014-09-05 16:50:40 -0700112 }
113
114 /**
115 * Returns a MAC address instance representing the specified {@code long}
116 * value. The lower 48 bits of the long value are used to parse as a MAC
117 * address.
118 *
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700119 * @param address the long value to be parsed. The lower 48 bits are used for a
120 * MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700121 * @return a MAC address instance representing the specified {@code long}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700122 * value.
123 * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address.
alshabibc4901cd2014-09-05 16:50:40 -0700124 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700125 public static MacAddress valueOf(final long address) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700126 final byte[] addressInBytes = new byte[]{
alshabibc4901cd2014-09-05 16:50:40 -0700127 (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff),
128 (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700129 (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff)};
alshabibc4901cd2014-09-05 16:50:40 -0700130
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700131 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700132 }
133
134 /**
135 * Returns the length of the {@code MACAddress}.
136 *
137 * @return the length of the {@code MACAddress}.
138 */
139 public int length() {
140 return this.address.length;
141 }
142
143 /**
144 * Returns the value of the {@code MACAddress} as a {@code byte} array.
145 *
146 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700147 * type {@code byte} array.
alshabibc4901cd2014-09-05 16:50:40 -0700148 */
149 public byte[] toBytes() {
150 return Arrays.copyOf(this.address, this.address.length);
151 }
152
153 /**
154 * Returns the value of the {@code MACAddress} as a {@code long}.
155 *
156 * @return the numeric value represented by this object after conversion to
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700157 * type {@code long}.
alshabibc4901cd2014-09-05 16:50:40 -0700158 */
159 public long toLong() {
160 long mac = 0;
161 for (int i = 0; i < 6; i++) {
162 final long t = (this.address[i] & 0xffL) << (5 - i) * 8;
163 mac |= t;
164 }
165 return mac;
166 }
167
168 /**
169 * Returns {@code true} if the MAC address is the broadcast address.
170 *
171 * @return {@code true} if the MAC address is the broadcast address.
172 */
173 public boolean isBroadcast() {
174 for (final byte b : this.address) {
175 if (b != -1) {
176 return false;
177 }
178 }
179 return true;
180 }
181
182 /**
183 * Returns {@code true} if the MAC address is the multicast address.
184 *
185 * @return {@code true} if the MAC address is the multicast address.
186 */
187 public boolean isMulticast() {
188 if (this.isBroadcast()) {
189 return false;
190 }
191 return (this.address[0] & 0x01) != 0;
192 }
193
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700194 /**
195 * Returns true if this MAC address is link local.
196 *
197 * @return true if link local
Charles Chan928ff8b2017-05-04 12:22:54 -0700198 * @deprecated in Kingfisher release. Link local is not a correct description for
199 * this MAC address. Replaced with {@link #isLldp()}
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700200 */
Charles Chan928ff8b2017-05-04 12:22:54 -0700201 @Deprecated
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700202 public boolean isLinkLocal() {
Charles Chan928ff8b2017-05-04 12:22:54 -0700203 return isLldp();
204 }
205
206 /**
207 * Returns true if this MAC address is used by link layer discovery protocol.
208 *
209 * @return true if this MAC is LLDP MAC.
210 */
211 public boolean isLldp() {
212 return LLDP.contains(this);
213 }
214
215 /**
216 * Returns true if the Organizationally Unique Identifier (OUI) of this MAC
217 * address matches ONOS OUI.
218 *
219 * @return true if the OUI of this MAC address matches ONOS OUI.
220 */
221 public boolean isOnos() {
222 return Arrays.equals(this.oui(), ONOS.oui());
223 }
224
225 /**
226 * Returns the Organizationally Unique Identifier (OUI) of this MAC address.
227 *
228 * @return the OUI of this MAC address.
229 */
230 public byte[] oui() {
231 return Arrays.copyOfRange(this.address, 0, 3);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700232 }
233
alshabibc4901cd2014-09-05 16:50:40 -0700234 @Override
235 public boolean equals(final Object o) {
236 if (o == this) {
237 return true;
238 }
239
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700240 if (!(o instanceof MacAddress)) {
alshabibc4901cd2014-09-05 16:50:40 -0700241 return false;
242 }
243
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700244 final MacAddress other = (MacAddress) o;
alshabibc4901cd2014-09-05 16:50:40 -0700245 return Arrays.equals(this.address, other.address);
246 }
247
248 @Override
249 public int hashCode() {
Brian O'Connor8576c2a2014-11-19 16:49:26 -0800250 return Long.hashCode(toLong());
alshabibc4901cd2014-09-05 16:50:40 -0700251 }
252
253 @Override
254 public String toString() {
Yuta HIGUCHIed1ef3a2017-05-15 18:10:01 -0700255 final StringBuilder builder = new StringBuilder(2 * 6 + 5);
alshabibc4901cd2014-09-05 16:50:40 -0700256 for (final byte b : this.address) {
257 if (builder.length() > 0) {
Yuta HIGUCHIed1ef3a2017-05-15 18:10:01 -0700258 builder.append(':');
alshabibc4901cd2014-09-05 16:50:40 -0700259 }
260 builder.append(String.format("%02X", b & 0xFF));
261 }
262 return builder.toString();
263 }
264
265 /**
266 * @return MAC address in string representation without colons (useful for
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700267 * radix tree storage)
alshabibc4901cd2014-09-05 16:50:40 -0700268 */
269 public String toStringNoColon() {
270 final StringBuilder builder = new StringBuilder();
271 for (final byte b : this.address) {
272 builder.append(String.format("%02X", b & 0xFF));
273 }
274 return builder.toString();
275 }
alshabibc4901cd2014-09-05 16:50:40 -0700276}