blob: 13d83d181cb74acc7a00db398a92591719b08cf9 [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 Hart18ad9502013-12-15 18:28:00 -080011import org.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 */
Pavlin Radoslavovede97582013-03-08 18:57:28 -080018@JsonDeserialize(using=MACAddressDeserializer.class)
19@JsonSerialize(using=MACAddressSerializer.class)
pingping-lin017a8922013-12-11 11:15:33 +080020public class MACAddress implements Serializable{
21 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() {
29 this.address = new byte[] { 0, 0, 0, 0, 0, 0};
30 }
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}.
43 * @param address the String representation of the MAC Address to be parsed.
44 * @return a MAC Address instance representing the value of the specified {@code String}.
45 * @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
46 */
47 public static MACAddress valueOf(String address) {
48 String[] elements = address.split(":");
49 if (elements.length != MAC_ADDRESS_LENGTH) {
50 throw new IllegalArgumentException(
51 "Specified MAC Address must contain 12 hex digits" +
52 " separated pairwise by :'s.");
53 }
54
55 byte[] addressInBytes = new byte[MAC_ADDRESS_LENGTH];
56 for (int i = 0; i < MAC_ADDRESS_LENGTH; i++) {
57 String element = elements[i];
58 addressInBytes[i] = (byte)Integer.parseInt(element, 16);
59 }
60
61 return new MACAddress(addressInBytes);
62 }
63
64 /**
65 * Returns a MAC address instance representing the specified {@code byte} array.
66 * @param address the byte array to be parsed.
67 * @return a MAC address instance representing the specified {@code byte} array.
68 * @throws IllegalArgumentException if the byte array cannot be parsed as a MAC address.
69 */
70 public static MACAddress valueOf(byte[] address) {
71 if (address.length != MAC_ADDRESS_LENGTH) {
72 throw new IllegalArgumentException("the length is not " + MAC_ADDRESS_LENGTH);
73 }
74
75 return new MACAddress(address);
76 }
77
78 /**
79 * Returns a MAC address instance representing the specified {@code long} value.
80 * The lower 48 bits of the long value are used to parse as a MAC address.
81 * @param address the long value to be parsed. The lower 48 bits are used for a MAC address.
82 * @return a MAC address instance representing the specified {@code long} value.
83 * @throws IllegalArgumentException if the long value cannot be parsed as a MAC address.
84 */
85 public static MACAddress valueOf(long address) {
86 byte[] addressInBytes = new byte[] {
87 (byte)((address >> 40) & 0xff),
88 (byte)((address >> 32) & 0xff),
89 (byte)((address >> 24) & 0xff),
90 (byte)((address >> 16) & 0xff),
91 (byte)((address >> 8 ) & 0xff),
92 (byte)((address >> 0) & 0xff)
93 };
94
95 return new MACAddress(addressInBytes);
96 }
97
98 /**
99 * Returns the length of the {@code MACAddress}.
100 * @return the length of the {@code MACAddress}.
101 */
102 public int length() {
103 return address.length;
104 }
105
106 /**
107 * Returns the value of the {@code MACAddress} as a {@code byte} array.
108 * @return the numeric value represented by this object after conversion to type {@code byte} array.
109 */
110 public byte[] toBytes() {
111 return Arrays.copyOf(address, address.length);
112 }
113
114 /**
115 * Returns the value of the {@code MACAddress} as a {@code long}.
116 * @return the numeric value represented by this object after conversion to type {@code long}.
117 */
118 public long toLong() {
119 long mac = 0;
120 for (int i = 0; i < 6; i++) {
121 long t = (address[i] & 0xffL) << ((5 - i) * 8);
122 mac |= t;
123 }
124 return mac;
125 }
126
127 /**
128 * Returns {@code true} if the MAC address is the broadcast address.
129 * @return {@code true} if the MAC address is the broadcast address.
130 */
131 public boolean isBroadcast() {
132 for (byte b : address) {
133 if (b != -1) // checks if equal to 0xff
134 return false;
135 }
136 return true;
137 }
138
139 /**
140 * Returns {@code true} if the MAC address is the multicast address.
141 * @return {@code true} if the MAC address is the multicast address.
142 */
143 public boolean isMulticast() {
144 if (isBroadcast()) {
145 return false;
146 }
147 return (address[0] & 0x01) != 0;
148 }
149
150 @Override
151 public boolean equals(Object o) {
152 if (o == this) {
153 return true;
154 }
155
156 if (!(o instanceof MACAddress)) {
157 return false;
158 }
159
160 MACAddress other = (MACAddress)o;
161 return Arrays.equals(this.address, other.address);
162 }
163
164 @Override
165 public int hashCode() {
166 return Arrays.hashCode(this.address);
167 }
168
169 @Override
170 public String toString() {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800171 return HexString.toHexString(address);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800172 }
173}