blob: 814660bde367f0bb5d4c897fc10aeddb90721ff4 [file] [log] [blame]
alshabibc4901cd2014-09-05 16:50:40 -07001/*******************************************************************************
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * 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
7 *
8 * 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.
15 ******************************************************************************/
16package org.onlab.packet;
17
18import java.util.Arrays;
19
20/**
21 * The class representing MAC address.
22 *
alshabibc4901cd2014-09-05 16:50:40 -070023 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070024public class MacAddress {
Jonathan Hart8f6f1ea2014-10-03 16:05:19 -070025 public static final byte[] ZERO_MAC_ADDRESS =
26 MacAddress.valueOf("00:00:00:00:00:00").getAddress();
27
28 public static final byte[] BROADCAST_MAC =
29 MacAddress.valueOf("ff:ff:ff:ff:ff:ff").getAddress();
30
alshabibc4901cd2014-09-05 16:50:40 -070031 public static final int MAC_ADDRESS_LENGTH = 6;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070032 private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -070033
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070034 public MacAddress(final byte[] address) {
35 this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070036 }
37
38 /**
39 * Returns a MAC address instance representing the value of the specified
40 * {@code String}.
41 *
42 * @param address
43 * the String representation of the MAC Address to be parsed.
44 * @return a MAC Address instance representing the value of the specified
45 * {@code String}.
46 * @throws IllegalArgumentException
47 * if the string cannot be parsed as a MAC address.
48 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070049 public static MacAddress valueOf(final String address) {
alshabibc4901cd2014-09-05 16:50:40 -070050 final String[] elements = address.split(":");
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070051 if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070052 throw new IllegalArgumentException(
53 "Specified MAC Address must contain 12 hex digits"
54 + " separated pairwise by :'s.");
55 }
56
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070057 final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
58 for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
alshabibc4901cd2014-09-05 16:50:40 -070059 final String element = elements[i];
60 addressInBytes[i] = (byte) Integer.parseInt(element, 16);
61 }
62
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070063 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -070064 }
65
66 /**
67 * Returns a MAC address instance representing the specified {@code byte}
68 * array.
69 *
70 * @param address
71 * the byte array to be parsed.
72 * @return a MAC address instance representing the specified {@code byte}
73 * array.
74 * @throws IllegalArgumentException
75 * if the byte array cannot be parsed as a MAC address.
76 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070077 public static MacAddress valueOf(final byte[] address) {
78 if (address.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070079 throw new IllegalArgumentException("the length is not "
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070080 + MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070081 }
82
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070083 return new MacAddress(address);
alshabibc4901cd2014-09-05 16:50:40 -070084 }
85
86 /**
87 * Returns a MAC address instance representing the specified {@code long}
88 * value. The lower 48 bits of the long value are used to parse as a MAC
89 * address.
90 *
91 * @param address
92 * the long value to be parsed. The lower 48 bits are used for a
93 * MAC address.
94 * @return a MAC address instance representing the specified {@code long}
95 * value.
96 * @throws IllegalArgumentException
97 * if the long value cannot be parsed as a MAC address.
98 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070099 public static MacAddress valueOf(final long address) {
alshabibc4901cd2014-09-05 16:50:40 -0700100 final byte[] addressInBytes = new byte[] {
101 (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff),
102 (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff),
103 (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff) };
104
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700105 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700106 }
107
108 /**
109 * Returns the length of the {@code MACAddress}.
110 *
111 * @return the length of the {@code MACAddress}.
112 */
113 public int length() {
114 return this.address.length;
115 }
116
117 /**
118 * Returns the value of the {@code MACAddress} as a {@code byte} array.
119 *
120 * @return the numeric value represented by this object after conversion to
121 * type {@code byte} array.
122 */
123 public byte[] toBytes() {
124 return Arrays.copyOf(this.address, this.address.length);
125 }
126
127 /**
128 * Returns the value of the {@code MACAddress} as a {@code long}.
129 *
130 * @return the numeric value represented by this object after conversion to
131 * type {@code long}.
132 */
133 public long toLong() {
134 long mac = 0;
135 for (int i = 0; i < 6; i++) {
136 final long t = (this.address[i] & 0xffL) << (5 - i) * 8;
137 mac |= t;
138 }
139 return mac;
140 }
141
142 /**
143 * Returns {@code true} if the MAC address is the broadcast address.
144 *
145 * @return {@code true} if the MAC address is the broadcast address.
146 */
147 public boolean isBroadcast() {
148 for (final byte b : this.address) {
149 if (b != -1) {
150 return false;
151 }
152 }
153 return true;
154 }
155
156 /**
157 * Returns {@code true} if the MAC address is the multicast address.
158 *
159 * @return {@code true} if the MAC address is the multicast address.
160 */
161 public boolean isMulticast() {
162 if (this.isBroadcast()) {
163 return false;
164 }
165 return (this.address[0] & 0x01) != 0;
166 }
167
168 @Override
169 public boolean equals(final Object o) {
170 if (o == this) {
171 return true;
172 }
173
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700174 if (!(o instanceof MacAddress)) {
alshabibc4901cd2014-09-05 16:50:40 -0700175 return false;
176 }
177
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700178 final MacAddress other = (MacAddress) o;
alshabibc4901cd2014-09-05 16:50:40 -0700179 return Arrays.equals(this.address, other.address);
180 }
181
182 @Override
183 public int hashCode() {
184 return Arrays.hashCode(this.address);
185 }
186
187 @Override
188 public String toString() {
189 final StringBuilder builder = new StringBuilder();
190 for (final byte b : this.address) {
191 if (builder.length() > 0) {
192 builder.append(":");
193 }
194 builder.append(String.format("%02X", b & 0xFF));
195 }
196 return builder.toString();
197 }
198
199 /**
200 * @return MAC address in string representation without colons (useful for
201 * radix tree storage)
202 */
203 public String toStringNoColon() {
204 final StringBuilder builder = new StringBuilder();
205 for (final byte b : this.address) {
206 builder.append(String.format("%02X", b & 0xFF));
207 }
208 return builder.toString();
209 }
210
211 public byte[] getAddress() {
212 return this.address;
213 }
214}