blob: 05e0f7bcb3f5545704b3d9873fb97634e3b7bb44 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
alshabibc4901cd2014-09-05 16:50:40 -07009 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
Thomas Vachuska24c849c2014-10-27 09:53:05 -070012 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
alshabibc4901cd2014-09-05 16:50:40 -070019package org.onlab.packet;
20
21import java.util.Arrays;
22
23/**
24 * The class representing MAC address.
25 *
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
tom545708e2014-10-09 17:10:02 -070029 public static final MacAddress ZERO = valueOf("00:00:00:00:00:00");
30 public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff");
31
32 public static final byte[] ZERO_MAC_ADDRESS = ZERO.getAddress();
33 public static final byte[] BROADCAST_MAC = BROADCAST.getAddress();
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070034
alshabibc4901cd2014-09-05 16:50:40 -070035 public static final int MAC_ADDRESS_LENGTH = 6;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070036 private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -070037
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070038 public MacAddress(final byte[] address) {
39 this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070040 }
41
42 /**
43 * Returns a MAC address instance representing the value of the specified
44 * {@code String}.
45 *
46 * @param address
47 * the String representation of the MAC Address to be parsed.
48 * @return a MAC Address instance representing the value of the specified
49 * {@code String}.
50 * @throws IllegalArgumentException
51 * if the string cannot be parsed as a MAC address.
52 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070053 public static MacAddress valueOf(final String address) {
alshabibc4901cd2014-09-05 16:50:40 -070054 final String[] elements = address.split(":");
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070055 if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070056 throw new IllegalArgumentException(
57 "Specified MAC Address must contain 12 hex digits"
58 + " separated pairwise by :'s.");
59 }
60
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070061 final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
62 for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
alshabibc4901cd2014-09-05 16:50:40 -070063 final String element = elements[i];
64 addressInBytes[i] = (byte) Integer.parseInt(element, 16);
65 }
66
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070067 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -070068 }
69
70 /**
71 * Returns a MAC address instance representing the specified {@code byte}
72 * array.
73 *
74 * @param address
75 * the byte array to be parsed.
76 * @return a MAC address instance representing the specified {@code byte}
77 * array.
78 * @throws IllegalArgumentException
79 * if the byte array cannot be parsed as a MAC address.
80 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070081 public static MacAddress valueOf(final byte[] address) {
82 if (address.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070083 throw new IllegalArgumentException("the length is not "
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070084 + MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070085 }
86
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070087 return new MacAddress(address);
alshabibc4901cd2014-09-05 16:50:40 -070088 }
89
90 /**
91 * Returns a MAC address instance representing the specified {@code long}
92 * value. The lower 48 bits of the long value are used to parse as a MAC
93 * address.
94 *
95 * @param address
96 * the long value to be parsed. The lower 48 bits are used for a
97 * MAC address.
98 * @return a MAC address instance representing the specified {@code long}
99 * value.
100 * @throws IllegalArgumentException
101 * if the long value cannot be parsed as a MAC address.
102 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700103 public static MacAddress valueOf(final long address) {
alshabibc4901cd2014-09-05 16:50:40 -0700104 final byte[] addressInBytes = new byte[] {
105 (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff),
106 (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff),
107 (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff) };
108
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700109 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700110 }
111
112 /**
113 * Returns the length of the {@code MACAddress}.
114 *
115 * @return the length of the {@code MACAddress}.
116 */
117 public int length() {
118 return this.address.length;
119 }
120
121 /**
122 * Returns the value of the {@code MACAddress} as a {@code byte} array.
123 *
124 * @return the numeric value represented by this object after conversion to
125 * type {@code byte} array.
126 */
127 public byte[] toBytes() {
128 return Arrays.copyOf(this.address, this.address.length);
129 }
130
131 /**
132 * Returns the value of the {@code MACAddress} as a {@code long}.
133 *
134 * @return the numeric value represented by this object after conversion to
135 * type {@code long}.
136 */
137 public long toLong() {
138 long mac = 0;
139 for (int i = 0; i < 6; i++) {
140 final long t = (this.address[i] & 0xffL) << (5 - i) * 8;
141 mac |= t;
142 }
143 return mac;
144 }
145
146 /**
147 * Returns {@code true} if the MAC address is the broadcast address.
148 *
149 * @return {@code true} if the MAC address is the broadcast address.
150 */
151 public boolean isBroadcast() {
152 for (final byte b : this.address) {
153 if (b != -1) {
154 return false;
155 }
156 }
157 return true;
158 }
159
160 /**
161 * Returns {@code true} if the MAC address is the multicast address.
162 *
163 * @return {@code true} if the MAC address is the multicast address.
164 */
165 public boolean isMulticast() {
166 if (this.isBroadcast()) {
167 return false;
168 }
169 return (this.address[0] & 0x01) != 0;
170 }
171
172 @Override
173 public boolean equals(final Object o) {
174 if (o == this) {
175 return true;
176 }
177
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700178 if (!(o instanceof MacAddress)) {
alshabibc4901cd2014-09-05 16:50:40 -0700179 return false;
180 }
181
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700182 final MacAddress other = (MacAddress) o;
alshabibc4901cd2014-09-05 16:50:40 -0700183 return Arrays.equals(this.address, other.address);
184 }
185
186 @Override
187 public int hashCode() {
188 return Arrays.hashCode(this.address);
189 }
190
191 @Override
192 public String toString() {
193 final StringBuilder builder = new StringBuilder();
194 for (final byte b : this.address) {
195 if (builder.length() > 0) {
196 builder.append(":");
197 }
198 builder.append(String.format("%02X", b & 0xFF));
199 }
200 return builder.toString();
201 }
202
203 /**
204 * @return MAC address in string representation without colons (useful for
205 * radix tree storage)
206 */
207 public String toStringNoColon() {
208 final StringBuilder builder = new StringBuilder();
209 for (final byte b : this.address) {
210 builder.append(String.format("%02X", b & 0xFF));
211 }
212 return builder.toString();
213 }
214
215 public byte[] getAddress() {
216 return this.address;
217 }
218}