blob: 2827a7216c68526e251c577c2c957db9b914402e [file] [log] [blame]
Yotam Harchol595c6442013-09-27 16:29:08 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4
Yotam Harchol2c535582013-10-01 15:50:20 -07005public class OFBitMask128 implements OFValueType<OFBitMask128> {
Yotam Harchol595c6442013-09-27 16:29:08 -07006
7 static final int LENGTH = 16;
8
9 private final long raw1; // MSBs (ports 64-127)
10 private final long raw2; // LSBs (ports 0-63)
11
Yotam Harchol2c535582013-10-01 15:50:20 -070012 public static final OFBitMask128 ALL = new OFBitMask128(-1, -1);
13 public static final OFBitMask128 NONE = new OFBitMask128(0, 0);
Yotam Harchol595c6442013-09-27 16:29:08 -070014
Yotam Harchol2c535582013-10-01 15:50:20 -070015 private OFBitMask128(long raw1, long raw2) {
Yotam Harchol595c6442013-09-27 16:29:08 -070016 this.raw1 = raw1;
17 this.raw2 = raw2;
18 }
19
Yotam Harchol2c535582013-10-01 15:50:20 -070020 static OFBitMask128 of(long raw1, long raw2) {
Yotam Harchol595c6442013-09-27 16:29:08 -070021 if (raw1 == -1 && raw2 == -1)
22 return ALL;
23 if (raw1 == 0 && raw2 == 0)
24 return NONE;
Yotam Harchol2c535582013-10-01 15:50:20 -070025 return new OFBitMask128(raw1, raw2);
Yotam Harchol595c6442013-09-27 16:29:08 -070026 }
27
28 @Override
29 public int getLength() {
30 return LENGTH;
31 }
32
33 @Override
Yotam Harchol2c535582013-10-01 15:50:20 -070034 public OFBitMask128 applyMask(OFBitMask128 mask) {
Yotam Harchol595c6442013-09-27 16:29:08 -070035 return of(this.raw1 & mask.raw1, this.raw2 & mask.raw2);
36 }
37
38 @Override
39 public boolean equals(Object obj) {
Yotam Harchol2c535582013-10-01 15:50:20 -070040 if (!(obj instanceof OFBitMask128))
Yotam Harchol595c6442013-09-27 16:29:08 -070041 return false;
Yotam Harchol2c535582013-10-01 15:50:20 -070042 OFBitMask128 other = (OFBitMask128)obj;
Yotam Harchol595c6442013-09-27 16:29:08 -070043 return (other.raw1 == this.raw1 && other.raw2 == this.raw2);
44 }
45
46 @Override
47 public int hashCode() {
48 return (int)(31 * raw1 + raw2);
49 }
50
51 protected static boolean isBitOn(long raw1, long raw2, int bit) {
52 if (bit < 0 || bit >= 128)
Yotam Harchol2c535582013-10-01 15:50:20 -070053 throw new IndexOutOfBoundsException();
Yotam Harchol595c6442013-09-27 16:29:08 -070054 long word;
55 if (bit < 64) {
56 word = raw2; // ports 0-63
57 } else {
58 word = raw1; // ports 64-127
59 bit -= 64;
60 }
61 return (word & ((long)1 << bit)) != 0;
62 }
63
Yotam Harchol595c6442013-09-27 16:29:08 -070064 public void write16Bytes(ChannelBuffer cb) {
65 cb.writeLong(raw1);
66 cb.writeLong(raw2);
67 }
68
Yotam Harchol2c535582013-10-01 15:50:20 -070069 public static OFBitMask128 read16Bytes(ChannelBuffer cb) {
Yotam Harchol595c6442013-09-27 16:29:08 -070070 long raw1 = cb.readLong();
71 long raw2 = cb.readLong();
72 return of(raw1, raw2);
73 }
74
Yotam Harchol2c535582013-10-01 15:50:20 -070075 public boolean isOn(int bit) {
76 return isBitOn(raw1, raw2, bit);
Yotam Harchol595c6442013-09-27 16:29:08 -070077 }
78
79 @Override
80 public String toString() {
81 return (String.format("%64s", Long.toBinaryString(raw2)) + String.format("%64s", Long.toBinaryString(raw1))).replaceAll(" ", "0");
82 }
83
Yotam Harchol2c535582013-10-01 15:50:20 -070084 @Override
85 public int compareTo(OFBitMask128 o) {
86 long c = this.raw1 - o.raw1;
87 if (c != 0)
88 return Long.signum(c);
89 return Long.signum(this.raw2 - o.raw2);
90 }
91
Yotam Harchol595c6442013-09-27 16:29:08 -070092}