Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame^] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import org.jboss.netty.buffer.ChannelBuffer; |
| 4 | |
| 5 | public class OFPortMap implements OFValueType<OFPortMap> { |
| 6 | |
| 7 | static final int LENGTH = 16; |
| 8 | |
| 9 | private final long raw1; |
| 10 | private final long raw2; |
| 11 | |
| 12 | public static final OFPortMap NONE = of(0, 0); |
| 13 | |
| 14 | private OFPortMap(long raw1, long raw2) { |
| 15 | this.raw1 = raw1; |
| 16 | this.raw2 = raw2; |
| 17 | |
| 18 | } |
| 19 | |
| 20 | public static OFPortMap of(long raw1, long raw2) { |
| 21 | if (raw1 == 0 && raw2 == 0) |
| 22 | return NONE; |
| 23 | return new OFPortMap(raw1, raw2); |
| 24 | } |
| 25 | |
| 26 | @Override |
| 27 | public int getLength() { |
| 28 | return LENGTH; |
| 29 | } |
| 30 | |
| 31 | @Override |
| 32 | public OFPortMap applyMask(OFPortMap mask) { |
| 33 | return of(this.raw1 & mask.raw1, this.raw2 & mask.raw2); |
| 34 | } |
| 35 | |
| 36 | public void write16Bytes(ChannelBuffer cb) { |
| 37 | cb.writeLong(raw1); |
| 38 | cb.writeLong(raw2); |
| 39 | } |
| 40 | |
| 41 | public static OFPortMap read16Bytes(ChannelBuffer cb) { |
| 42 | long raw1 = cb.readLong(); |
| 43 | long raw2 = cb.readLong(); |
| 44 | return of(raw1, raw2); |
| 45 | } |
| 46 | |
| 47 | private static boolean isBitOn(long raw1, long raw2, int bit) { |
| 48 | if (bit < 0 || bit >= 128) |
| 49 | throw new IndexOutOfBoundsException("Port number is out of bounds"); |
| 50 | long word; |
| 51 | if (bit < 64) { |
| 52 | word = raw1; |
| 53 | } else { |
| 54 | word = raw2; |
| 55 | bit -= 64; |
| 56 | } |
| 57 | return (word & ((long)1 << bit)) != 0; |
| 58 | } |
| 59 | |
| 60 | public boolean isOn(OFPort port) { |
| 61 | return isBitOn(raw1, raw2, port.getPortNumber()); |
| 62 | } |
| 63 | |
| 64 | public static OFPortMap ofPorts(OFPort... ports) { |
| 65 | Builder builder = new Builder(); |
| 66 | for (OFPort port: ports) { |
| 67 | builder.set(port); |
| 68 | } |
| 69 | return builder.build(); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public String toString() { |
| 74 | return (String.format("%64s", Long.toBinaryString(raw2)) + String.format("%64s", Long.toBinaryString(raw1))).replaceAll(" ", "0"); |
| 75 | } |
| 76 | |
| 77 | public static class Builder { |
| 78 | private long raw1, raw2; |
| 79 | |
| 80 | public Builder() { |
| 81 | |
| 82 | } |
| 83 | |
| 84 | public boolean isOn(OFPort port) { |
| 85 | return isBitOn(raw1, raw2, port.getPortNumber()); |
| 86 | } |
| 87 | |
| 88 | public Builder set(OFPort port) { |
| 89 | int bit = port.getPortNumber(); |
| 90 | if (bit < 0 || bit >= 128) |
| 91 | throw new IndexOutOfBoundsException("Port number is out of bounds"); |
| 92 | if (bit < 64) { |
| 93 | raw1 |= ((long)1 << bit); |
| 94 | } else { |
| 95 | raw2 |= ((long)1 << (bit - 64)); |
| 96 | } |
| 97 | return this; |
| 98 | } |
| 99 | |
| 100 | public Builder unset(OFPort port) { |
| 101 | int bit = port.getPortNumber(); |
| 102 | if (bit < 0 || bit >= 128) |
| 103 | throw new IndexOutOfBoundsException("Port number is out of bounds"); |
| 104 | if (bit < 64) { |
| 105 | raw1 &= ~((long)1 << bit); |
| 106 | } else { |
| 107 | raw2 &= ~((long)1 << (bit - 64)); |
| 108 | } |
| 109 | return this; |
| 110 | } |
| 111 | |
| 112 | public OFPortMap build() { |
| 113 | return OFPortMap.of(raw1, raw2); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } |