blob: 33764bb614303547082c267a15da818b9185b552 [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 {
alshabibc4901cd2014-09-05 16:50:40 -070025 public static final int MAC_ADDRESS_LENGTH = 6;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070026 private byte[] address = new byte[MacAddress.MAC_ADDRESS_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -070027
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070028 public MacAddress(final byte[] address) {
29 this.address = Arrays.copyOf(address, MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070030 }
31
32 /**
33 * Returns a MAC address instance representing the value of the specified
34 * {@code String}.
35 *
36 * @param address
37 * the String representation of the MAC Address to be parsed.
38 * @return a MAC Address instance representing the value of the specified
39 * {@code String}.
40 * @throws IllegalArgumentException
41 * if the string cannot be parsed as a MAC address.
42 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070043 public static MacAddress valueOf(final String address) {
alshabibc4901cd2014-09-05 16:50:40 -070044 final String[] elements = address.split(":");
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070045 if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070046 throw new IllegalArgumentException(
47 "Specified MAC Address must contain 12 hex digits"
48 + " separated pairwise by :'s.");
49 }
50
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070051 final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
52 for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
alshabibc4901cd2014-09-05 16:50:40 -070053 final String element = elements[i];
54 addressInBytes[i] = (byte) Integer.parseInt(element, 16);
55 }
56
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070057 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -070058 }
59
60 /**
61 * Returns a MAC address instance representing the specified {@code byte}
62 * array.
63 *
64 * @param address
65 * the byte array to be parsed.
66 * @return a MAC address instance representing the specified {@code byte}
67 * array.
68 * @throws IllegalArgumentException
69 * if the byte array cannot be parsed as a MAC address.
70 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070071 public static MacAddress valueOf(final byte[] address) {
72 if (address.length != MacAddress.MAC_ADDRESS_LENGTH) {
alshabibc4901cd2014-09-05 16:50:40 -070073 throw new IllegalArgumentException("the length is not "
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070074 + MacAddress.MAC_ADDRESS_LENGTH);
alshabibc4901cd2014-09-05 16:50:40 -070075 }
76
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070077 return new MacAddress(address);
alshabibc4901cd2014-09-05 16:50:40 -070078 }
79
80 /**
81 * Returns a MAC address instance representing the specified {@code long}
82 * value. The lower 48 bits of the long value are used to parse as a MAC
83 * address.
84 *
85 * @param address
86 * the long value to be parsed. The lower 48 bits are used for a
87 * MAC address.
88 * @return a MAC address instance representing the specified {@code long}
89 * value.
90 * @throws IllegalArgumentException
91 * if the long value cannot be parsed as a MAC address.
92 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070093 public static MacAddress valueOf(final long address) {
alshabibc4901cd2014-09-05 16:50:40 -070094 final byte[] addressInBytes = new byte[] {
95 (byte) (address >> 40 & 0xff), (byte) (address >> 32 & 0xff),
96 (byte) (address >> 24 & 0xff), (byte) (address >> 16 & 0xff),
97 (byte) (address >> 8 & 0xff), (byte) (address >> 0 & 0xff) };
98
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070099 return new MacAddress(addressInBytes);
alshabibc4901cd2014-09-05 16:50:40 -0700100 }
101
102 /**
103 * Returns the length of the {@code MACAddress}.
104 *
105 * @return the length of the {@code MACAddress}.
106 */
107 public int length() {
108 return this.address.length;
109 }
110
111 /**
112 * Returns the value of the {@code MACAddress} as a {@code byte} array.
113 *
114 * @return the numeric value represented by this object after conversion to
115 * type {@code byte} array.
116 */
117 public byte[] toBytes() {
118 return Arrays.copyOf(this.address, this.address.length);
119 }
120
121 /**
122 * Returns the value of the {@code MACAddress} as a {@code long}.
123 *
124 * @return the numeric value represented by this object after conversion to
125 * type {@code long}.
126 */
127 public long toLong() {
128 long mac = 0;
129 for (int i = 0; i < 6; i++) {
130 final long t = (this.address[i] & 0xffL) << (5 - i) * 8;
131 mac |= t;
132 }
133 return mac;
134 }
135
136 /**
137 * Returns {@code true} if the MAC address is the broadcast address.
138 *
139 * @return {@code true} if the MAC address is the broadcast address.
140 */
141 public boolean isBroadcast() {
142 for (final byte b : this.address) {
143 if (b != -1) {
144 return false;
145 }
146 }
147 return true;
148 }
149
150 /**
151 * Returns {@code true} if the MAC address is the multicast address.
152 *
153 * @return {@code true} if the MAC address is the multicast address.
154 */
155 public boolean isMulticast() {
156 if (this.isBroadcast()) {
157 return false;
158 }
159 return (this.address[0] & 0x01) != 0;
160 }
161
162 @Override
163 public boolean equals(final Object o) {
164 if (o == this) {
165 return true;
166 }
167
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700168 if (!(o instanceof MacAddress)) {
alshabibc4901cd2014-09-05 16:50:40 -0700169 return false;
170 }
171
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700172 final MacAddress other = (MacAddress) o;
alshabibc4901cd2014-09-05 16:50:40 -0700173 return Arrays.equals(this.address, other.address);
174 }
175
176 @Override
177 public int hashCode() {
178 return Arrays.hashCode(this.address);
179 }
180
181 @Override
182 public String toString() {
183 final StringBuilder builder = new StringBuilder();
184 for (final byte b : this.address) {
185 if (builder.length() > 0) {
186 builder.append(":");
187 }
188 builder.append(String.format("%02X", b & 0xFF));
189 }
190 return builder.toString();
191 }
192
193 /**
194 * @return MAC address in string representation without colons (useful for
195 * radix tree storage)
196 */
197 public String toStringNoColon() {
198 final StringBuilder builder = new StringBuilder();
199 for (final byte b : this.address) {
200 builder.append(String.format("%02X", b & 0xFF));
201 }
202 return builder.toString();
203 }
204
205 public byte[] getAddress() {
206 return this.address;
207 }
208}