blob: 87be7c28e82d9dde6e306d3c0ca0074cd016eea9 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4import org.projectfloodlight.openflow.exceptions.OFParseError;
5import org.projectfloodlight.openflow.util.HexString;
6
Andreas Wundsam85c961f2013-09-29 21:22:12 -07007import com.google.common.primitives.Longs;
8
Yotam Harcholf3f11152013-09-05 16:47:16 -07009/**
10 * Wrapper around a 6 byte mac address.
11 *
12 * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
13 */
14
15public class MacAddress implements OFValueType<MacAddress> {
16 static final int MacAddrLen = 6;
17 private final long rawValue;
18
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070019 private final static long NONE_VAL = 0x0L;
20 public static final MacAddress NONE = new MacAddress(NONE_VAL);
21
Yotam Harcholf3f11152013-09-05 16:47:16 -070022 public static final MacAddress NO_MASK = MacAddress.of(0xFFFFFFFFFFFFFFFFl);
23 public static final MacAddress FULL_MASK = MacAddress.of(0x0);
24
25 private MacAddress(final long rawValue) {
26 this.rawValue = rawValue;
27 }
28
29 public static MacAddress of(final byte[] address) {
30 long raw =
31 (address[0] & 0xFFL) << 40 | (address[1] & 0xFFL) << 32
32 | (address[2] & 0xFFL) << 24 | (address[3] & 0xFFL) << 16
33 | (address[4] & 0xFFL) << 8 | (address[5] & 0xFFL);
34 return MacAddress.of(raw);
35 }
36
37 public static MacAddress of(final long raw) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070038 if(raw == NONE_VAL)
39 return NONE;
40
Yotam Harcholf3f11152013-09-05 16:47:16 -070041 return new MacAddress(raw);
42 }
43
44 public static MacAddress of(final String string) {
45 int index = 0;
46 int shift = 40;
47
48 long raw = 0;
49 if (string.length() != 6 * 2 + 5)
50 throw new IllegalArgumentException("Mac address not well formed: " + string);
51
52 while (shift >= 0) {
53 raw |=
54 ((long) (Character.digit(string.charAt(index++), 16) << 4 | Character
55 .digit(string.charAt(index++), 16))) << shift;
56
57 if (shift == 0)
58 break;
59 if (string.charAt(index++) != ':')
60 throw new IllegalArgumentException("Mac address not well formed: " + string);
61 shift -= 8;
62 }
63 return MacAddress.of(raw);
64 }
65
66 volatile byte[] bytesCache = null;
67
68 public byte[] getBytes() {
69 if (bytesCache == null) {
70 synchronized (this) {
71 if (bytesCache == null) {
72 bytesCache =
73 new byte[] { (byte) ((rawValue >> 40) & 0xFF),
74 (byte) ((rawValue >> 32) & 0xFF),
75 (byte) ((rawValue >> 24) & 0xFF),
76 (byte) ((rawValue >> 16) & 0xFF),
77 (byte) ((rawValue >> 8) & 0xFF),
78 (byte) ((rawValue >> 0) & 0xFF) };
79 }
80 }
81 }
82 return bytesCache;
83 }
84
85 @Override
86 public int getLength() {
87 return MacAddrLen;
88 }
89
90 @Override
91 public String toString() {
92 return HexString.toHexString(rawValue, 6);
93 }
94
95 @Override
96 public int hashCode() {
97 final int prime = 31;
98 int result = 1;
99 result = prime * result + (int) (rawValue ^ (rawValue >>> 32));
100 return result;
101 }
102
103 @Override
104 public boolean equals(final Object obj) {
105 if (this == obj)
106 return true;
107 if (obj == null)
108 return false;
109 if (getClass() != obj.getClass())
110 return false;
111 MacAddress other = (MacAddress) obj;
112 if (rawValue != other.rawValue)
113 return false;
114 return true;
115 }
116
117 public long getLong() {
118 return rawValue;
119 }
120
121 public void write6Bytes(ChannelBuffer c) {
122 c.writeInt((int) (this.rawValue >> 16));
123 c.writeShort((int) this.rawValue & 0xFFFF);
124 }
125
126 public static MacAddress read6Bytes(ChannelBuffer c) throws OFParseError {
127 long raw = c.readUnsignedInt() << 16 | c.readUnsignedShort();
128 return MacAddress.of(raw);
129 }
130
131 @Override
132 public MacAddress applyMask(MacAddress mask) {
133 return MacAddress.of(this.rawValue & mask.rawValue);
134 }
135
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700136 @Override
137 public int compareTo(MacAddress o) {
138 return Longs.compare(rawValue, o.rawValue);
139 }
140
Yotam Harcholf3f11152013-09-05 16:47:16 -0700141
142
143}