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