blob: 93f5a2dc2c00027b2ee285973ca3953ef7fb2bb8 [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
Andreas Wundsam14ae8c62013-11-05 17:15:35 -080017 public static final OFBitMask128 NO_MASK = ALL;
18 public static final OFBitMask128 FULL_MASK = NONE;
19
Yotam Harchol2c535582013-10-01 15:50:20 -070020 private OFBitMask128(long raw1, long raw2) {
Yotam Harchol595c6442013-09-27 16:29:08 -070021 this.raw1 = raw1;
22 this.raw2 = raw2;
23 }
24
Ronald Li7fb7eb22013-11-25 16:11:06 -080025 public static OFBitMask128 of(long raw1, long raw2) {
Yotam Harchol595c6442013-09-27 16:29:08 -070026 if (raw1 == -1 && raw2 == -1)
27 return ALL;
28 if (raw1 == 0 && raw2 == 0)
29 return NONE;
Yotam Harchol2c535582013-10-01 15:50:20 -070030 return new OFBitMask128(raw1, raw2);
Yotam Harchol595c6442013-09-27 16:29:08 -070031 }
32
33 @Override
34 public int getLength() {
35 return LENGTH;
36 }
37
38 @Override
Yotam Harchol2c535582013-10-01 15:50:20 -070039 public OFBitMask128 applyMask(OFBitMask128 mask) {
Yotam Harchol595c6442013-09-27 16:29:08 -070040 return of(this.raw1 & mask.raw1, this.raw2 & mask.raw2);
41 }
42
43 @Override
44 public boolean equals(Object obj) {
Yotam Harchol2c535582013-10-01 15:50:20 -070045 if (!(obj instanceof OFBitMask128))
Yotam Harchol595c6442013-09-27 16:29:08 -070046 return false;
Yotam Harchol2c535582013-10-01 15:50:20 -070047 OFBitMask128 other = (OFBitMask128)obj;
Yotam Harchol595c6442013-09-27 16:29:08 -070048 return (other.raw1 == this.raw1 && other.raw2 == this.raw2);
49 }
50
51 @Override
52 public int hashCode() {
53 return (int)(31 * raw1 + raw2);
54 }
55
56 protected static boolean isBitOn(long raw1, long raw2, int bit) {
57 if (bit < 0 || bit >= 128)
Yotam Harchol2c535582013-10-01 15:50:20 -070058 throw new IndexOutOfBoundsException();
Yotam Harchol595c6442013-09-27 16:29:08 -070059 long word;
60 if (bit < 64) {
61 word = raw2; // ports 0-63
62 } else {
63 word = raw1; // ports 64-127
64 bit -= 64;
65 }
66 return (word & ((long)1 << bit)) != 0;
67 }
68
Yotam Harchol595c6442013-09-27 16:29:08 -070069 public void write16Bytes(ChannelBuffer cb) {
70 cb.writeLong(raw1);
71 cb.writeLong(raw2);
72 }
73
Yotam Harchol2c535582013-10-01 15:50:20 -070074 public static OFBitMask128 read16Bytes(ChannelBuffer cb) {
Yotam Harchol595c6442013-09-27 16:29:08 -070075 long raw1 = cb.readLong();
76 long raw2 = cb.readLong();
77 return of(raw1, raw2);
78 }
79
Yotam Harchol2c535582013-10-01 15:50:20 -070080 public boolean isOn(int bit) {
81 return isBitOn(raw1, raw2, bit);
Yotam Harchol595c6442013-09-27 16:29:08 -070082 }
83
84 @Override
85 public String toString() {
86 return (String.format("%64s", Long.toBinaryString(raw2)) + String.format("%64s", Long.toBinaryString(raw1))).replaceAll(" ", "0");
87 }
88
Yotam Harchol2c535582013-10-01 15:50:20 -070089 @Override
90 public int compareTo(OFBitMask128 o) {
91 long c = this.raw1 - o.raw1;
92 if (c != 0)
93 return Long.signum(c);
94 return Long.signum(this.raw2 - o.raw2);
95 }
96
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070097 @Override
98 public void putTo(PrimitiveSink sink) {
99 sink.putLong(raw1);
100 sink.putLong(raw2);
101 }
102
Yotam Harchol595c6442013-09-27 16:29:08 -0700103}