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