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