blob: 7f0e9f562a9e46a9846c07b52d2112246a109a36 [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
Jonathan Hart23701d12014-04-03 10:45:48 -07006import net.onrc.onos.core.util.serializers.MACAddressDeserializer;
7import net.onrc.onos.core.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;
Jonathan Hartc78b8f62014-08-07 22:31:09 -070011import org.projectfloodlight.openflow.util.HexString;
Pavlin Radoslavovede97582013-03-08 18:57:28 -080012
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080013/**
14 * The class representing MAC address.
15 *
16 * @author Sho Shimizu (sho.shimizu@gmail.com)
17 */
Ray Milkey269ffb92014-04-03 14:43:30 -070018@JsonDeserialize(using = MACAddressDeserializer.class)
19@JsonSerialize(using = MACAddressSerializer.class)
20public class MACAddress implements Serializable {
pingping-lin017a8922013-12-11 11:15:33 +080021 private static final long serialVersionUID = 10000L;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080022 public static final int MAC_ADDRESS_LENGTH = 6;
23 private byte[] address = new byte[MAC_ADDRESS_LENGTH];
24
Pavlin Radoslavova680d492013-10-24 14:05:53 -070025 /**
26 * Default constructor.
27 */
28 public MACAddress() {
Ray Milkey269ffb92014-04-03 14:43:30 -070029 this.address = new byte[]{0, 0, 0, 0, 0, 0};
Pavlin Radoslavova680d492013-10-24 14:05:53 -070030 }
31
32 /**
33 * Constructor for a given address stored in a byte array.
34 *
35 * @param address the address stored in a byte array.
36 */
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080037 public MACAddress(byte[] address) {
38 this.address = Arrays.copyOf(address, MAC_ADDRESS_LENGTH);
39 }
40
41 /**
42 * Returns a MAC address instance representing the value of the specified {@code String}.
Ray Milkey269ffb92014-04-03 14:43:30 -070043 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080044 * @param address the String representation of the MAC Address to be parsed.
45 * @return a MAC Address instance representing the value of the specified {@code String}.
46 * @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
47 */
48 public static MACAddress valueOf(String address) {
49 String[] elements = address.split(":");
50 if (elements.length != MAC_ADDRESS_LENGTH) {
51 throw new IllegalArgumentException(
52 "Specified MAC Address must contain 12 hex digits" +
Ray Milkey269ffb92014-04-03 14:43:30 -070053 " separated pairwise by :'s.");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080054 }
55
56 byte[] addressInBytes = new byte[MAC_ADDRESS_LENGTH];
57 for (int i = 0; i < MAC_ADDRESS_LENGTH; i++) {
58 String element = elements[i];
Ray Milkey269ffb92014-04-03 14:43:30 -070059 addressInBytes[i] = (byte) Integer.parseInt(element, 16);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080060 }
61
62 return new MACAddress(addressInBytes);
63 }
64
65 /**
66 * Returns a MAC address instance representing the specified {@code byte} array.
Ray Milkey269ffb92014-04-03 14:43:30 -070067 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080068 * @param address the byte array to be parsed.
69 * @return a MAC address instance representing the specified {@code byte} array.
70 * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address.
71 */
72 public static MACAddress valueOf(byte[] address) {
73 if (address.length != MAC_ADDRESS_LENGTH) {
74 throw new IllegalArgumentException("the length is not " + MAC_ADDRESS_LENGTH);
75 }
76
77 return new MACAddress(address);
78 }
79
80 /**
81 * Returns a MAC address instance representing the specified {@code long} value.
82 * The lower 48 bits of the long value are used to parse as a MAC address.
Ray Milkey269ffb92014-04-03 14:43:30 -070083 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080084 * @param address the long value to be parsed. The lower 48 bits are used for a MAC address.
85 * @return a MAC address instance representing the specified {@code long} value.
86 * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address.
87 */
88 public static MACAddress valueOf(long address) {
Ray Milkey269ffb92014-04-03 14:43:30 -070089 byte[] addressInBytes = new byte[]{
90 (byte) ((address >> 40) & 0xff),
91 (byte) ((address >> 32) & 0xff),
92 (byte) ((address >> 24) & 0xff),
93 (byte) ((address >> 16) & 0xff),
94 (byte) ((address >> 8) & 0xff),
95 (byte) ((address >> 0) & 0xff)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080096 };
97
98 return new MACAddress(addressInBytes);
99 }
100
101 /**
102 * Returns the length of the {@code MACAddress}.
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800104 * @return the length of the {@code MACAddress}.
105 */
106 public int length() {
107 return address.length;
108 }
109
110 /**
111 * Returns the value of the {@code MACAddress} as a {@code byte} array.
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800113 * @return the numeric value represented by this object after conversion to type {@code byte} array.
114 */
115 public byte[] toBytes() {
116 return Arrays.copyOf(address, address.length);
117 }
118
119 /**
120 * Returns the value of the {@code MACAddress} as a {@code long}.
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800122 * @return the numeric value represented by this object after conversion to type {@code long}.
123 */
124 public long toLong() {
125 long mac = 0;
126 for (int i = 0; i < 6; i++) {
127 long t = (address[i] & 0xffL) << ((5 - i) * 8);
128 mac |= t;
129 }
130 return mac;
131 }
132
133 /**
134 * Returns {@code true} if the MAC address is the broadcast address.
Ray Milkey269ffb92014-04-03 14:43:30 -0700135 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800136 * @return {@code true} if the MAC address is the broadcast address.
137 */
138 public boolean isBroadcast() {
139 for (byte b : address) {
140 if (b != -1) // checks if equal to 0xff
141 return false;
142 }
143 return true;
144 }
145
146 /**
147 * Returns {@code true} if the MAC address is the multicast address.
Ray Milkey269ffb92014-04-03 14:43:30 -0700148 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800149 * @return {@code true} if the MAC address is the multicast address.
150 */
151 public boolean isMulticast() {
152 if (isBroadcast()) {
153 return false;
154 }
155 return (address[0] & 0x01) != 0;
156 }
157
158 @Override
159 public boolean equals(Object o) {
160 if (o == this) {
161 return true;
162 }
163
164 if (!(o instanceof MACAddress)) {
165 return false;
166 }
167
Ray Milkey269ffb92014-04-03 14:43:30 -0700168 MACAddress other = (MACAddress) o;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800169 return Arrays.equals(this.address, other.address);
170 }
171
172 @Override
173 public int hashCode() {
174 return Arrays.hashCode(this.address);
175 }
176
177 @Override
178 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700179 return HexString.toHexString(address);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800180 }
181}