Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 3 | |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame^] | 4 | public class OFPortMap extends Masked<OFBitMask128> { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 5 | |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame^] | 6 | private OFPortMap(OFBitMask128 mask) { |
| 7 | super(OFBitMask128.NONE, mask); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 8 | } |
| 9 | |
| 10 | public boolean isOn(OFPort port) { |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame^] | 11 | return !(this.mask.isOn(port.getPortNumber())); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 12 | } |
| 13 | |
| 14 | public static OFPortMap ofPorts(OFPort... ports) { |
| 15 | Builder builder = new Builder(); |
| 16 | for (OFPort port: ports) { |
| 17 | builder.set(port); |
| 18 | } |
| 19 | return builder.build(); |
| 20 | } |
| 21 | |
| 22 | @Override |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 23 | public boolean equals(Object obj) { |
| 24 | if (!(obj instanceof OFPortMap)) |
| 25 | return false; |
| 26 | OFPortMap other = (OFPortMap)obj; |
| 27 | return (other.value.equals(this.value) && other.mask.equals(this.mask)); |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public int hashCode() { |
| 32 | return 619 * mask.hashCode() + 257 * value.hashCode(); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | public static class Builder { |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame^] | 36 | private long raw1 = -1, raw2 = -1; |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 37 | |
| 38 | public Builder() { |
| 39 | |
| 40 | } |
| 41 | |
| 42 | public boolean isOn(OFPort port) { |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame^] | 43 | return !(OFBitMask128.isBitOn(raw1, raw2, port.getPortNumber())); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame^] | 46 | public Builder unset(OFPort port) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 47 | int bit = port.getPortNumber(); |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame^] | 48 | if (bit < 0 || bit >= 127) // MAX PORT IS 127 |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 49 | throw new IndexOutOfBoundsException("Port number is out of bounds"); |
| 50 | if (bit < 64) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 51 | raw2 |= ((long)1 << bit); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 52 | } else { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 53 | raw1 |= ((long)1 << (bit - 64)); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 54 | } |
| 55 | return this; |
| 56 | } |
| 57 | |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame^] | 58 | public Builder set(OFPort port) { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 59 | int bit = port.getPortNumber(); |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame^] | 60 | if (bit < 0 || bit >= 127) |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 61 | throw new IndexOutOfBoundsException("Port number is out of bounds"); |
| 62 | if (bit < 64) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 63 | raw2 &= ~((long)1 << bit); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 64 | } else { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 65 | raw1 &= ~((long)1 << (bit - 64)); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 66 | } |
| 67 | return this; |
| 68 | } |
| 69 | |
| 70 | public OFPortMap build() { |
Yotam Harchol | 2c53558 | 2013-10-01 15:50:20 -0700 | [diff] [blame^] | 71 | return new OFPortMap(OFBitMask128.of(raw1, raw2)); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | } |