Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import org.jboss.netty.buffer.ChannelBuffer; |
| 4 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 5 | import com.google.common.hash.PrimitiveSink; |
| 6 | |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 7 | public class OFBitMask128 implements OFValueType<OFBitMask128> { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 8 | |
| 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 Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 14 | public static final OFBitMask128 ALL = new OFBitMask128(-1, -1); |
| 15 | public static final OFBitMask128 NONE = new OFBitMask128(0, 0); |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 16 | |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 17 | private OFBitMask128(long raw1, long raw2) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 18 | this.raw1 = raw1; |
| 19 | this.raw2 = raw2; |
| 20 | } |
| 21 | |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 22 | static OFBitMask128 of(long raw1, long raw2) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 23 | if (raw1 == -1 && raw2 == -1) |
| 24 | return ALL; |
| 25 | if (raw1 == 0 && raw2 == 0) |
| 26 | return NONE; |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 27 | return new OFBitMask128(raw1, raw2); |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public int getLength() { |
| 32 | return LENGTH; |
| 33 | } |
| 34 | |
| 35 | @Override |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 36 | public OFBitMask128 applyMask(OFBitMask128 mask) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 37 | return of(this.raw1 & mask.raw1, this.raw2 & mask.raw2); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public boolean equals(Object obj) { |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 42 | if (!(obj instanceof OFBitMask128)) |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 43 | return false; |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 44 | OFBitMask128 other = (OFBitMask128)obj; |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 45 | 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 Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 55 | throw new IndexOutOfBoundsException(); |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 56 | 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 Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 66 | public void write16Bytes(ChannelBuffer cb) { |
| 67 | cb.writeLong(raw1); |
| 68 | cb.writeLong(raw2); |
| 69 | } |
| 70 | |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 71 | public static OFBitMask128 read16Bytes(ChannelBuffer cb) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 72 | long raw1 = cb.readLong(); |
| 73 | long raw2 = cb.readLong(); |
| 74 | return of(raw1, raw2); |
| 75 | } |
| 76 | |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 77 | public boolean isOn(int bit) { |
| 78 | return isBitOn(raw1, raw2, bit); |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 79 | } |
| 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 Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 86 | @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 Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public void putTo(PrimitiveSink sink) { |
| 96 | sink.putLong(raw1); |
| 97 | sink.putLong(raw2); |
| 98 | } |
| 99 | |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 100 | } |