blob: b77d4ccf5eed8ded35693beb53ab6be0a8bef394 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001package net.floodlightcontroller.util;
2
3import java.util.Arrays;
4
HIGUCHI Yutaf086d8a2013-06-12 15:26:35 -07005import net.onrc.onos.ofcontroller.util.serializers.MACAddressDeserializer;
6import net.onrc.onos.ofcontroller.util.serializers.MACAddressSerializer;
Pavlin Radoslavovede97582013-03-08 18:57:28 -08007
Pavlin Radoslavovede97582013-03-08 18:57:28 -08008import org.codehaus.jackson.map.annotate.JsonDeserialize;
9import org.codehaus.jackson.map.annotate.JsonSerialize;
10
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080011/**
12 * The class representing MAC address.
13 *
14 * @author Sho Shimizu (sho.shimizu@gmail.com)
15 */
Pavlin Radoslavovede97582013-03-08 18:57:28 -080016@JsonDeserialize(using=MACAddressDeserializer.class)
17@JsonSerialize(using=MACAddressSerializer.class)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080018public class MACAddress {
19 public static final int MAC_ADDRESS_LENGTH = 6;
20 private byte[] address = new byte[MAC_ADDRESS_LENGTH];
21
Pavlin Radoslavova680d492013-10-24 14:05:53 -070022 /**
23 * Default constructor.
24 */
25 public MACAddress() {
26 this.address = new byte[] { 0, 0, 0, 0, 0, 0};
27 }
28
29 /**
30 * Constructor for a given address stored in a byte array.
31 *
32 * @param address the address stored in a byte array.
33 */
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080034 public MACAddress(byte[] address) {
35 this.address = Arrays.copyOf(address, MAC_ADDRESS_LENGTH);
36 }
37
38 /**
39 * Returns a MAC address instance representing the value of the specified {@code String}.
40 * @param address the String representation of the MAC Address to be parsed.
41 * @return a MAC Address instance representing the value of the specified {@code String}.
42 * @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
43 */
44 public static MACAddress valueOf(String address) {
45 String[] elements = address.split(":");
46 if (elements.length != MAC_ADDRESS_LENGTH) {
47 throw new IllegalArgumentException(
48 "Specified MAC Address must contain 12 hex digits" +
49 " separated pairwise by :'s.");
50 }
51
52 byte[] addressInBytes = new byte[MAC_ADDRESS_LENGTH];
53 for (int i = 0; i < MAC_ADDRESS_LENGTH; i++) {
54 String element = elements[i];
55 addressInBytes[i] = (byte)Integer.parseInt(element, 16);
56 }
57
58 return new MACAddress(addressInBytes);
59 }
60
61 /**
62 * Returns a MAC address instance representing the specified {@code byte} array.
63 * @param address the byte array to be parsed.
64 * @return a MAC address instance representing the specified {@code byte} array.
65 * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address.
66 */
67 public static MACAddress valueOf(byte[] address) {
68 if (address.length != MAC_ADDRESS_LENGTH) {
69 throw new IllegalArgumentException("the length is not " + MAC_ADDRESS_LENGTH);
70 }
71
72 return new MACAddress(address);
73 }
74
75 /**
76 * Returns a MAC address instance representing the specified {@code long} value.
77 * The lower 48 bits of the long value are used to parse as a MAC address.
78 * @param address the long value to be parsed. The lower 48 bits are used for a MAC address.
79 * @return a MAC address instance representing the specified {@code long} value.
80 * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address.
81 */
82 public static MACAddress valueOf(long address) {
83 byte[] addressInBytes = new byte[] {
84 (byte)((address >> 40) & 0xff),
85 (byte)((address >> 32) & 0xff),
86 (byte)((address >> 24) & 0xff),
87 (byte)((address >> 16) & 0xff),
88 (byte)((address >> 8 ) & 0xff),
89 (byte)((address >> 0) & 0xff)
90 };
91
92 return new MACAddress(addressInBytes);
93 }
94
95 /**
96 * Returns the length of the {@code MACAddress}.
97 * @return the length of the {@code MACAddress}.
98 */
99 public int length() {
100 return address.length;
101 }
102
103 /**
104 * Returns the value of the {@code MACAddress} as a {@code byte} array.
105 * @return the numeric value represented by this object after conversion to type {@code byte} array.
106 */
107 public byte[] toBytes() {
108 return Arrays.copyOf(address, address.length);
109 }
110
111 /**
112 * Returns the value of the {@code MACAddress} as a {@code long}.
113 * @return the numeric value represented by this object after conversion to type {@code long}.
114 */
115 public long toLong() {
116 long mac = 0;
117 for (int i = 0; i < 6; i++) {
118 long t = (address[i] & 0xffL) << ((5 - i) * 8);
119 mac |= t;
120 }
121 return mac;
122 }
123
124 /**
125 * Returns {@code true} if the MAC address is the broadcast address.
126 * @return {@code true} if the MAC address is the broadcast address.
127 */
128 public boolean isBroadcast() {
129 for (byte b : address) {
130 if (b != -1) // checks if equal to 0xff
131 return false;
132 }
133 return true;
134 }
135
136 /**
137 * Returns {@code true} if the MAC address is the multicast address.
138 * @return {@code true} if the MAC address is the multicast address.
139 */
140 public boolean isMulticast() {
141 if (isBroadcast()) {
142 return false;
143 }
144 return (address[0] & 0x01) != 0;
145 }
146
147 @Override
148 public boolean equals(Object o) {
149 if (o == this) {
150 return true;
151 }
152
153 if (!(o instanceof MACAddress)) {
154 return false;
155 }
156
157 MACAddress other = (MACAddress)o;
158 return Arrays.equals(this.address, other.address);
159 }
160
161 @Override
162 public int hashCode() {
163 return Arrays.hashCode(this.address);
164 }
165
166 @Override
167 public String toString() {
168 StringBuilder builder = new StringBuilder();
169 for (byte b: address) {
170 if (builder.length() > 0) {
171 builder.append(":");
172 }
173 builder.append(String.format("%02X", b & 0xFF));
174 }
175 return builder.toString();
176 }
177}