blob: 8806bb36f33f4e2b2e50b86a0647c8b9db9b23e6 [file] [log] [blame]
Andreas Wundsam40e14f72013-05-06 14:49:08 -07001package org.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4import org.openflow.exceptions.OFParseError;
Andreas Wundsam40e14f72013-05-06 14:49:08 -07005import org.openflow.protocol.OFObject;
6import org.openflow.util.HexString;
7
8/**
9 * Wrapper around a 6 byte mac address.
10 *
11 * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
12 */
13
14public class MacAddress implements OFObject {
15 static final int MacAddrLen = 6;
16 private final long rawValue;
17
18 private MacAddress(final long rawValue) {
19 this.rawValue = rawValue;
20 }
21
22 public static MacAddress of(final byte[] address) {
23 long raw =
24 (address[0] & 0xFFL) << 40 | (address[1] & 0xFFL) << 32
25 | (address[2] & 0xFFL) << 24 | (address[3] & 0xFFL) << 16
26 | (address[4] & 0xFFL) << 8 | (address[5] & 0xFFL);
27 return MacAddress.of(raw);
28 }
29
30 public static MacAddress of(final long raw) {
31 return new MacAddress(raw);
32 }
33
34 public static MacAddress of(final String string) {
35 int index = 0;
36 int shift = 40;
37
38 long raw = 0;
39 if (string.length() != 6 * 2 + 5)
40 throw new IllegalArgumentException("Mac address not well formed: " + string);
41
42 while (shift >= 0) {
43 raw |=
44 ((long) (Character.digit(string.charAt(index++), 16) << 4 | Character
45 .digit(string.charAt(index++), 16))) << shift;
46
47 if (shift == 0)
48 break;
49 if (string.charAt(index++) != ':')
50 throw new IllegalArgumentException("Mac address not well formed: " + string);
51 shift -= 8;
52 }
53 return MacAddress.of(raw);
54 }
55
56 volatile byte[] bytesCache = null;
57
58 public byte[] getBytes() {
59 if (bytesCache == null) {
60 synchronized (this) {
61 if (bytesCache == null) {
62 bytesCache =
63 new byte[] { (byte) ((rawValue >> 40) & 0xFF),
64 (byte) ((rawValue >> 32) & 0xFF),
65 (byte) ((rawValue >> 24) & 0xFF),
66 (byte) ((rawValue >> 16) & 0xFF),
67 (byte) ((rawValue >> 8) & 0xFF),
68 (byte) ((rawValue >> 0) & 0xFF) };
69 }
70 }
71 }
72 return bytesCache;
73 }
74
75 @Override
76 public int getLength() {
77 return MacAddrLen;
78 }
79
80 public static MacAddress readFrom(final ChannelBuffer bb) throws OFParseError {
81 long raw = bb.readUnsignedInt() << 16 | bb.readUnsignedShort();
82 return MacAddress.of(raw);
83 }
84
85 @Override
Andreas Wundsam27303462013-07-16 12:52:35 -070086 public void writeTo(final ChannelBuffer bb) {
Andreas Wundsam40e14f72013-05-06 14:49:08 -070087 bb.writeInt((int) (rawValue >> 16));
88 bb.writeShort((int) rawValue & 0xFFFF);
89 }
90
91 @Override
92 public String toString() {
93 return HexString.toHexString(rawValue, 6);
94 }
95
96 @Override
97 public int hashCode() {
98 final int prime = 31;
99 int result = 1;
100 result = prime * result + (int) (rawValue ^ (rawValue >>> 32));
101 return result;
102 }
103
104 @Override
105 public boolean equals(final Object obj) {
106 if (this == obj)
107 return true;
108 if (obj == null)
109 return false;
110 if (getClass() != obj.getClass())
111 return false;
112 MacAddress other = (MacAddress) obj;
113 if (rawValue != other.rawValue)
114 return false;
115 return true;
116 }
117
118 public long getLong() {
119 return rawValue;
120 }
121
122}