Merge into master from pull request #99:
Rename and a few documentation/clarity fixes for OFPortBitMap (https://github.com/floodlight/loxigen/pull/99)
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFPortBitMap.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFPortBitMap.java
new file mode 100644
index 0000000..49c8c4e
--- /dev/null
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFPortBitMap.java
@@ -0,0 +1,126 @@
+package org.projectfloodlight.openflow.types;
+
+
+/** User-facing object representing a bitmap of ports that can be matched on.
+ * This is implemented by the custom BSN OXM type of_oxm_bsn_in_ports_182.
+ *
+ * You can call set() on the builder for all the Ports you want to match on
+ * and unset to exclude the port.
+ *
+ * <b>Implementation note:</b> to comply with the matching semantics of OXM (which is a logical "AND" not "OR")
+ * the underlying match uses a data format which is very unintuitive. The value is always
+ * 0, and the mask has the bits set for the ports that should <b>NOT</b> be included in the
+ * range.
+ *
+ * For the curious: We transformed the bitmap (a logical OR) problem into a logical
+ * AND NOT problem.
+ *
+ * We logically mean: Inport is 1 OR 3
+ * We technically say: Inport IS NOT 2 AND IS NOT 4 AND IS NOT 5 AND IS NOT ....
+ * The second term cannot be represented in OXM, the second can.
+ *
+ * That said, all that craziness is hidden from the user of this object.
+ * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
+ */
+public class OFPortBitMap extends Masked<OFBitMask128> {
+
+ private OFPortBitMap(OFBitMask128 mask) {
+ super(OFBitMask128.NONE, mask);
+ }
+
+ /** @return whether or not the given port is logically included in the
+ * match, i.e., whether a packet from in-port <emph>port</emph> be matched by
+ * this OXM.
+ */
+ public boolean isOn(OFPort port) {
+ // see the implementation note above about the logical inversion of the mask
+ return !(this.mask.isOn(port.getPortNumber()));
+ }
+
+ public static OFPortBitMap ofPorts(OFPort... ports) {
+ Builder builder = new Builder();
+ for (OFPort port: ports) {
+ builder.set(port);
+ }
+ return builder.build();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (!(obj instanceof OFPortBitMap))
+ return false;
+ OFPortBitMap other = (OFPortBitMap)obj;
+ return (other.value.equals(this.value) && other.mask.equals(this.mask));
+ }
+
+ @Override
+ public int hashCode() {
+ return 619 * mask.hashCode() + 257 * value.hashCode();
+ }
+
+ public static class Builder {
+ private long raw1 = -1, raw2 = -1;
+
+ public Builder() {
+
+ }
+
+ /** @return whether or not the given port is logically included in the
+ * match, i.e., whether a packet from in-port <emph>port</emph> be matched by
+ * this OXM.
+ */
+ public boolean isOn(OFPort port) {
+ // see the implementation note above about the logical inversion of the mask
+ return !(OFBitMask128.isBitOn(raw1, raw2, port.getPortNumber()));
+ }
+
+ /** remove this port from the match, i.e., packets from this in-port
+ * will NOT be matched.
+ */
+ public Builder unset(OFPort port) {
+ // see the implementation note above about the logical inversion of the mask
+ int bit = port.getPortNumber();
+ if (bit < 0 || bit > 127)
+ throw new IndexOutOfBoundsException("Port number is out of bounds");
+ else if (bit == 127)
+ // the highest order bit in the bitmask is reserved. The switch will
+ // set that bit for all ports >= 127. The reason is that we don't want
+ // the OFPortMap to match all ports out of its range (i.e., a packet
+ // coming in on port 181 would match *any* OFPortMap).
+ throw new IndexOutOfBoundsException("The highest order bit in the bitmask is reserved.");
+ else if (bit < 64) {
+ raw2 |= ((long)1 << bit);
+ } else {
+ raw1 |= ((long)1 << (bit - 64));
+ }
+ return this;
+ }
+
+ /** add this port from the match, i.e., packets from this in-port
+ * will NOT be matched.
+ */
+ public Builder set(OFPort port) {
+ // see the implementation note above about the logical inversion of the mask
+ int bit = port.getPortNumber();
+ if (bit < 0 || bit > 127)
+ throw new IndexOutOfBoundsException("Port number is out of bounds");
+ else if (bit == 127)
+ // the highest order bit in the bitmask is reserved. The switch will
+ // set that bit for all ports >= 127. The reason is that we don't want
+ // the OFPortMap to match all ports out of its range (i.e., a packet
+ // coming in on port 181 would match *any* OFPortMap).
+ throw new IndexOutOfBoundsException("The highest order bit in the bitmask is reserved.");
+ else if (bit < 64) {
+ raw2 &= ~((long)1 << bit);
+ } else {
+ raw1 &= ~((long)1 << (bit - 64));
+ }
+ return this;
+ }
+
+ public OFPortBitMap build() {
+ return new OFPortBitMap(OFBitMask128.of(raw1, raw2));
+ }
+ }
+
+}
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFPortMap.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFPortMap.java
deleted file mode 100644
index 8ec056e..0000000
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFPortMap.java
+++ /dev/null
@@ -1,75 +0,0 @@
-package org.projectfloodlight.openflow.types;
-
-
-public class OFPortMap extends Masked<OFBitMask128> {
-
- private OFPortMap(OFBitMask128 mask) {
- super(OFBitMask128.NONE, mask);
- }
-
- public boolean isOn(OFPort port) {
- return !(this.mask.isOn(port.getPortNumber()));
- }
-
- public static OFPortMap ofPorts(OFPort... ports) {
- Builder builder = new Builder();
- for (OFPort port: ports) {
- builder.set(port);
- }
- return builder.build();
- }
-
- @Override
- public boolean equals(Object obj) {
- if (!(obj instanceof OFPortMap))
- return false;
- OFPortMap other = (OFPortMap)obj;
- return (other.value.equals(this.value) && other.mask.equals(this.mask));
- }
-
- @Override
- public int hashCode() {
- return 619 * mask.hashCode() + 257 * value.hashCode();
- }
-
- public static class Builder {
- private long raw1 = -1, raw2 = -1;
-
- public Builder() {
-
- }
-
- public boolean isOn(OFPort port) {
- return !(OFBitMask128.isBitOn(raw1, raw2, port.getPortNumber()));
- }
-
- public Builder unset(OFPort port) {
- int bit = port.getPortNumber();
- if (bit < 0 || bit >= 127) // MAX PORT IS 127
- throw new IndexOutOfBoundsException("Port number is out of bounds");
- if (bit < 64) {
- raw2 |= ((long)1 << bit);
- } else {
- raw1 |= ((long)1 << (bit - 64));
- }
- return this;
- }
-
- public Builder set(OFPort port) {
- int bit = port.getPortNumber();
- if (bit < 0 || bit >= 127)
- throw new IndexOutOfBoundsException("Port number is out of bounds");
- if (bit < 64) {
- raw2 &= ~((long)1 << bit);
- } else {
- raw1 &= ~((long)1 << (bit - 64));
- }
- return this;
- }
-
- public OFPortMap build() {
- return new OFPortMap(OFBitMask128.of(raw1, raw2));
- }
- }
-
-}
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/OFPortMapTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/OFPortBitMapTest.java
similarity index 60%
rename from java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/OFPortMapTest.java
rename to java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/OFPortBitMapTest.java
index 7a75248..7f5ab5d 100644
--- a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/OFPortMapTest.java
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/OFPortBitMapTest.java
@@ -5,48 +5,48 @@
import org.junit.Test;
-public class OFPortMapTest extends TestCase {
+public class OFPortBitMapTest extends TestCase {
@Test
- public void testOFPortMap() {
- Boolean[] on = new Boolean[128];
- for (int i = 0; i < 128; i++) {
+ public void testOFPortBitMap() {
+ Boolean[] on = new Boolean[127];
+ for (int i = 0; i < 127; i++) {
on[i] = false;
}
- OFPortMap.Builder builder = new OFPortMap.Builder();
+ OFPortBitMap.Builder builder = new OFPortBitMap.Builder();
- for (int i = 0; i < 128; i += 3) {
+ for (int i = 0; i < 127; i += 3) {
OFPort p = OFPort.of(i);
builder.set(p);
on[p.getPortNumber()] = true;
}
// Test that all ports that were added are actually on, and all other ports are off
- OFPortMap portmap = builder.build();
+ OFPortBitMap portmap = builder.build();
//System.out.println(portmap);
- Boolean[] actual = new Boolean[128];
- for (int i = 0; i < 128; i++) {
+ Boolean[] actual = new Boolean[127];
+ for (int i = 0; i < 127; i++) {
actual[i] = false;
}
- for (int i = 0; i < 128; i++) {
+ for (int i = 0; i < 127; i++) {
actual[i] = portmap.isOn(OFPort.of(i));
}
assertArrayEquals(on, actual);
// Turn some ports off
- for (int i = 0; i < 128; i += 7) {
+ for (int i = 0; i < 127; i += 7) {
on[i] = false;
builder.unset(OFPort.of(i));
}
// Test again
portmap = builder.build();
- actual = new Boolean[128];
- for (int i = 0; i < 128; i++) {
+ actual = new Boolean[127];
+ for (int i = 0; i < 127; i++) {
actual[i] = false;
}
- for (int i = 0; i < 128; i++) {
+ for (int i = 0; i < 127; i++) {
actual[i] = portmap.isOn(OFPort.of(i));
}
assertArrayEquals(on, actual);