blob: 5cec23310232737cd0467663b2992bd4764e4c2b [file] [log] [blame]
Yotam Harchol595c6442013-09-27 16:29:08 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4
Andreas Wundsam22ba3af2013-10-04 16:00:30 -07005import com.google.common.hash.PrimitiveSink;
6
Yotam Harchol2c535582013-10-01 15:50:20 -07007public class OFBitMask128 implements OFValueType<OFBitMask128> {
Yotam Harchol595c6442013-09-27 16:29:08 -07008
9 static final int LENGTH = 16;
10
11 private final long raw1; // MSBs (ports 64-127)
12 private final long raw2; // LSBs (ports 0-63)
13
Yotam Harchol2c535582013-10-01 15:50:20 -070014 public static final OFBitMask128 ALL = new OFBitMask128(-1, -1);
15 public static final OFBitMask128 NONE = new OFBitMask128(0, 0);
Yotam Harchol595c6442013-09-27 16:29:08 -070016
Yotam Harchol2c535582013-10-01 15:50:20 -070017 private OFBitMask128(long raw1, long raw2) {
Yotam Harchol595c6442013-09-27 16:29:08 -070018 this.raw1 = raw1;
19 this.raw2 = raw2;
20 }
21
Yotam Harchol2c535582013-10-01 15:50:20 -070022 static OFBitMask128 of(long raw1, long raw2) {
Yotam Harchol595c6442013-09-27 16:29:08 -070023 if (raw1 == -1 && raw2 == -1)
24 return ALL;
25 if (raw1 == 0 && raw2 == 0)
26 return NONE;
Yotam Harchol2c535582013-10-01 15:50:20 -070027 return new OFBitMask128(raw1, raw2);
Yotam Harchol595c6442013-09-27 16:29:08 -070028 }
29
30 @Override
31 public int getLength() {
32 return LENGTH;
33 }
34
35 @Override
Yotam Harchol2c535582013-10-01 15:50:20 -070036 public OFBitMask128 applyMask(OFBitMask128 mask) {
Yotam Harchol595c6442013-09-27 16:29:08 -070037 return of(this.raw1 & mask.raw1, this.raw2 & mask.raw2);
38 }
39
40 @Override
41 public boolean equals(Object obj) {
Yotam Harchol2c535582013-10-01 15:50:20 -070042 if (!(obj instanceof OFBitMask128))
Yotam Harchol595c6442013-09-27 16:29:08 -070043 return false;
Yotam Harchol2c535582013-10-01 15:50:20 -070044 OFBitMask128 other = (OFBitMask128)obj;
Yotam Harchol595c6442013-09-27 16:29:08 -070045 return (other.raw1 == this.raw1 && other.raw2 == this.raw2);
46 }
47
48 @Override
49 public int hashCode() {
50 return (int)(31 * raw1 + raw2);
51 }
52
53 protected static boolean isBitOn(long raw1, long raw2, int bit) {
54 if (bit < 0 || bit >= 128)
Yotam Harchol2c535582013-10-01 15:50:20 -070055 throw new IndexOutOfBoundsException();
Yotam Harchol595c6442013-09-27 16:29:08 -070056 long word;
57 if (bit < 64) {
58 word = raw2; // ports 0-63
59 } else {
60 word = raw1; // ports 64-127
61 bit -= 64;
62 }
63 return (word & ((long)1 << bit)) != 0;
64 }
65
Yotam Harchol595c6442013-09-27 16:29:08 -070066 public void write16Bytes(ChannelBuffer cb) {
67 cb.writeLong(raw1);
68 cb.writeLong(raw2);
69 }
70
Yotam Harchol2c535582013-10-01 15:50:20 -070071 public static OFBitMask128 read16Bytes(ChannelBuffer cb) {
Yotam Harchol595c6442013-09-27 16:29:08 -070072 long raw1 = cb.readLong();
73 long raw2 = cb.readLong();
74 return of(raw1, raw2);
75 }
76
Yotam Harchol2c535582013-10-01 15:50:20 -070077 public boolean isOn(int bit) {
78 return isBitOn(raw1, raw2, bit);
Yotam Harchol595c6442013-09-27 16:29:08 -070079 }
80
81 @Override
82 public String toString() {
83 return (String.format("%64s", Long.toBinaryString(raw2)) + String.format("%64s", Long.toBinaryString(raw1))).replaceAll(" ", "0");
84 }
85
Yotam Harchol2c535582013-10-01 15:50:20 -070086 @Override
87 public int compareTo(OFBitMask128 o) {
88 long c = this.raw1 - o.raw1;
89 if (c != 0)
90 return Long.signum(c);
91 return Long.signum(this.raw2 - o.raw2);
92 }
93
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070094 @Override
95 public void putTo(PrimitiveSink sink) {
96 sink.putLong(raw1);
97 sink.putLong(raw2);
98 }
99
Yotam Harchol595c6442013-09-27 16:29:08 -0700100}