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 | |
Andreas Wundsam | 14ae8c6 | 2013-11-05 17:15:35 -0800 | [diff] [blame] | 17 | public static final OFBitMask128 NO_MASK = ALL; |
| 18 | public static final OFBitMask128 FULL_MASK = NONE; |
| 19 | |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 20 | private OFBitMask128(long raw1, long raw2) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 21 | this.raw1 = raw1; |
| 22 | this.raw2 = raw2; |
| 23 | } |
| 24 | |
Ronald Li | 7fb7eb2 | 2013-11-25 16:11:06 -0800 | [diff] [blame] | 25 | public static OFBitMask128 of(long raw1, long raw2) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 26 | if (raw1 == -1 && raw2 == -1) |
| 27 | return ALL; |
| 28 | if (raw1 == 0 && raw2 == 0) |
| 29 | return NONE; |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 30 | return new OFBitMask128(raw1, raw2); |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public int getLength() { |
| 35 | return LENGTH; |
| 36 | } |
| 37 | |
| 38 | @Override |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 39 | public OFBitMask128 applyMask(OFBitMask128 mask) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 40 | return of(this.raw1 & mask.raw1, this.raw2 & mask.raw2); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public boolean equals(Object obj) { |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 45 | if (!(obj instanceof OFBitMask128)) |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 46 | return false; |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 47 | OFBitMask128 other = (OFBitMask128)obj; |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 48 | 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 Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 58 | throw new IndexOutOfBoundsException(); |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 59 | 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 Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 69 | public void write16Bytes(ChannelBuffer cb) { |
| 70 | cb.writeLong(raw1); |
| 71 | cb.writeLong(raw2); |
| 72 | } |
| 73 | |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 74 | public static OFBitMask128 read16Bytes(ChannelBuffer cb) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 75 | long raw1 = cb.readLong(); |
| 76 | long raw2 = cb.readLong(); |
| 77 | return of(raw1, raw2); |
| 78 | } |
| 79 | |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 80 | public boolean isOn(int bit) { |
| 81 | return isBitOn(raw1, raw2, bit); |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 82 | } |
| 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 Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame] | 89 | @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 Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 97 | @Override |
| 98 | public void putTo(PrimitiveSink sink) { |
| 99 | sink.putLong(raw1); |
| 100 | sink.putLong(raw2); |
| 101 | } |
| 102 | |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 103 | } |