blob: 8ec056ed7038320ed6c9e6d29f637b1e7dcb0ff5 [file] [log] [blame]
Yotam Harchola11f38b2013-09-26 15:38:17 -07001package org.projectfloodlight.openflow.types;
2
Yotam Harchola11f38b2013-09-26 15:38:17 -07003
Yotam Harchol2c535582013-10-01 15:50:20 -07004public class OFPortMap extends Masked<OFBitMask128> {
Yotam Harchola11f38b2013-09-26 15:38:17 -07005
Yotam Harchol2c535582013-10-01 15:50:20 -07006 private OFPortMap(OFBitMask128 mask) {
7 super(OFBitMask128.NONE, mask);
Yotam Harchola11f38b2013-09-26 15:38:17 -07008 }
9
10 public boolean isOn(OFPort port) {
Yotam Harchol2c535582013-10-01 15:50:20 -070011 return !(this.mask.isOn(port.getPortNumber()));
Yotam Harchola11f38b2013-09-26 15:38:17 -070012 }
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 Harchol595c6442013-09-27 16:29:08 -070023 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 Harchola11f38b2013-09-26 15:38:17 -070033 }
34
35 public static class Builder {
Yotam Harchol2c535582013-10-01 15:50:20 -070036 private long raw1 = -1, raw2 = -1;
Yotam Harchola11f38b2013-09-26 15:38:17 -070037
38 public Builder() {
39
40 }
41
42 public boolean isOn(OFPort port) {
Yotam Harchol2c535582013-10-01 15:50:20 -070043 return !(OFBitMask128.isBitOn(raw1, raw2, port.getPortNumber()));
Yotam Harchola11f38b2013-09-26 15:38:17 -070044 }
45
Yotam Harchol2c535582013-10-01 15:50:20 -070046 public Builder unset(OFPort port) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070047 int bit = port.getPortNumber();
Yotam Harchol2c535582013-10-01 15:50:20 -070048 if (bit < 0 || bit >= 127) // MAX PORT IS 127
Yotam Harchola11f38b2013-09-26 15:38:17 -070049 throw new IndexOutOfBoundsException("Port number is out of bounds");
50 if (bit < 64) {
Yotam Harchol595c6442013-09-27 16:29:08 -070051 raw2 |= ((long)1 << bit);
Yotam Harchola11f38b2013-09-26 15:38:17 -070052 } else {
Yotam Harchol595c6442013-09-27 16:29:08 -070053 raw1 |= ((long)1 << (bit - 64));
Yotam Harchola11f38b2013-09-26 15:38:17 -070054 }
55 return this;
56 }
57
Yotam Harchol2c535582013-10-01 15:50:20 -070058 public Builder set(OFPort port) {
Yotam Harchola11f38b2013-09-26 15:38:17 -070059 int bit = port.getPortNumber();
Yotam Harchol2c535582013-10-01 15:50:20 -070060 if (bit < 0 || bit >= 127)
Yotam Harchola11f38b2013-09-26 15:38:17 -070061 throw new IndexOutOfBoundsException("Port number is out of bounds");
62 if (bit < 64) {
Yotam Harchol595c6442013-09-27 16:29:08 -070063 raw2 &= ~((long)1 << bit);
Yotam Harchola11f38b2013-09-26 15:38:17 -070064 } else {
Yotam Harchol595c6442013-09-27 16:29:08 -070065 raw1 &= ~((long)1 << (bit - 64));
Yotam Harchola11f38b2013-09-26 15:38:17 -070066 }
67 return this;
68 }
69
70 public OFPortMap build() {
Yotam Harchol2c535582013-10-01 15:50:20 -070071 return new OFPortMap(OFBitMask128.of(raw1, raw2));
Yotam Harchola11f38b2013-09-26 15:38:17 -070072 }
73 }
74
75}