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