java_gen: small improvements to gen_table/tlv extensions

 - move bsntlv classes to dedicated package/factory
 - change type of of_bsn_tlv_vlan_vid to VlanVid
 - change type of tableId to new typesafe constant GenTableId
diff --git a/java_gen/java_model.py b/java_gen/java_model.py
index c4e4ea5..6f86706 100644
--- a/java_gen/java_model.py
+++ b/java_gen/java_model.py
@@ -209,7 +209,7 @@
 
         factories = OrderedDict()
 
-        sub_factory_classes = ("OFAction", "OFInstruction", "OFMeterBand", "OFOxm", "OFQueueProp", "OFErrorMsg", "OFActionId", "OFInstructionId")
+        sub_factory_classes = ("OFAction", "OFInstruction", "OFMeterBand", "OFOxm", "OFQueueProp", "OFErrorMsg", "OFActionId", "OFInstructionId", "OFBsnTlv")
         for base_class in sub_factory_classes:
             package = base_class[2:].lower()
             remove_prefix = base_class[2].lower() + base_class[3:]
@@ -521,7 +521,7 @@
         elif loxi_utils.class_is_table_feature_prop(self.c_name):
             return ("", "OFTableFeatureProp", None)
         elif loxi_utils.class_is_bsn_tlv(self.c_name):
-            return ("", "OFBsnTlv", None)
+            return ("bsntlv", "OFBsnTlv", None)
         else:
             return ("", None, None)
 
diff --git a/java_gen/java_type.py b/java_gen/java_type.py
index ac1e672..87cd6e8 100644
--- a/java_gen/java_type.py
+++ b/java_gen/java_type.py
@@ -484,6 +484,10 @@
         .op(read='OFChecksum128.read16Bytes(bb)',
             write='$name.write16Bytes(bb)',
             default='OFChecksum128.ZERO')
+gen_table_id = JType("GenTableId") \
+        .op(read='GenTableId.read2Bytes(bb)',
+            write='$name.write2Bytes(bb)',
+           )
 
 generic_t = JType("T")
 
@@ -624,6 +628,9 @@
         'of_group_delete' : { 'command' : group_mod_cmd },
 
         'of_bucket' : { 'watch_group': of_group },
+
+        'of_bsn_tlv_vlan_vid' : { 'value' : vlan_vid },
+        'of_bsn_gentable_entry_add' : { 'table_id' : gen_table_id },
 }
 
 
@@ -682,6 +689,8 @@
         return datapath_id
     elif field_name == 'actions' and obj_name == 'of_features_reply':
         return action_type_set
+    elif field_name == "table_id" and re.match(r'of_bsn_gentable.*', obj_name):
+        return gen_table_id
     elif c_type in default_mtype_to_jtype_convert_map:
         return default_mtype_to_jtype_convert_map[c_type]
     elif re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type):
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/GenTableId.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/GenTableId.java
new file mode 100644
index 0000000..cfa7cdf
--- /dev/null
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/GenTableId.java
@@ -0,0 +1,93 @@
+package org.projectfloodlight.openflow.types;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.projectfloodlight.openflow.exceptions.OFParseError;
+
+import com.google.common.hash.PrimitiveSink;
+import com.google.common.primitives.UnsignedInts;
+
+public class GenTableId implements OFValueType<GenTableId>, Comparable<GenTableId> {
+    final static int LENGTH = 2;
+
+    private static final int VALIDATION_MASK = 0xFFFF;
+
+    private static final int ALL_VAL = 0xFFFF;
+    private static final int NONE_VAL = 0x0000;
+    public static final GenTableId NONE = new GenTableId(NONE_VAL);
+
+    public static final GenTableId ALL = new GenTableId(ALL_VAL);
+    public static final GenTableId ZERO = NONE;
+
+    private final int id;
+
+    private GenTableId(int id) {
+        this.id = id;
+    }
+
+    public static GenTableId of(int id) {
+        switch(id) {
+            case NONE_VAL:
+                return NONE;
+            case ALL_VAL:
+                return ALL;
+            default:
+                if ((id & VALIDATION_MASK) != id)
+                    throw new IllegalArgumentException("Illegal Table id value: " + id);
+                return new GenTableId(id);
+        }
+    }
+
+    @Override
+    public String toString() {
+        return "0x" + Integer.toHexString(id);
+    }
+
+    public int getValue() {
+        return id;
+    }
+
+    @Override
+    public int getLength() {
+        return LENGTH;
+    }
+
+    public void write2Bytes(ChannelBuffer c) {
+        c.writeShort(this.id);
+    }
+
+    public static GenTableId read2Bytes(ChannelBuffer c) throws OFParseError {
+        return GenTableId.of(c.readUnsignedShort());
+    }
+
+    @Override
+    public GenTableId applyMask(GenTableId mask) {
+        return GenTableId.of(this.id & mask.id);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (!(obj instanceof GenTableId))
+            return false;
+        GenTableId other = (GenTableId)obj;
+        if (other.id != this.id)
+            return false;
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int prime = 13873;
+        return this.id * prime;
+    }
+
+    @Override
+    public int compareTo(GenTableId other) {
+        return UnsignedInts.compare(this.id, other.id);
+    }
+
+    @Override
+    public void putTo(PrimitiveSink sink) {
+        sink.putShort((byte) id);
+    }
+
+}
diff --git a/java_gen/templates/_imports.java b/java_gen/templates/_imports.java
index 498d2f9..0c95916 100644
--- a/java_gen/templates/_imports.java
+++ b/java_gen/templates/_imports.java
@@ -8,6 +8,7 @@
 import org.projectfloodlight.openflow.protocol.*;
 import org.projectfloodlight.openflow.protocol.action.*;
 import org.projectfloodlight.openflow.protocol.actionid.*;
+import org.projectfloodlight.openflow.protocol.bsntlv.*;
 import org.projectfloodlight.openflow.protocol.errormsg.*;
 import org.projectfloodlight.openflow.protocol.meterband.*;
 import org.projectfloodlight.openflow.protocol.instruction.*;
diff --git a/loxi_utils/loxi_utils.py b/loxi_utils/loxi_utils.py
index 5d725a5..865891f 100644
--- a/loxi_utils/loxi_utils.py
+++ b/loxi_utils/loxi_utils.py
@@ -157,6 +157,9 @@
     """
     return (cls.find("of_list_") == 0)
 
+def class_is(cls, cand_name):
+    return _unified_by_name(cls).is_instanceof(cand_name)
+
 def type_is_of_object(m_type):
     """
     Return True if m_type is an OF object type
diff --git a/test_data/of13/bsn_gentable_clear_request.data b/test_data/of13/bsn_gentable_clear_request.data
index 911ee27..847af3c 100644
--- a/test_data/of13/bsn_gentable_clear_request.data
+++ b/test_data/of13/bsn_gentable_clear_request.data
@@ -18,7 +18,7 @@
 builder.setXid(0x12345678)
     .setChecksum(OFChecksum128.of(0xFEDCBA9876543210L, 0xFFEECCBBAA990000L))
     .setChecksumMask(OFChecksum128.of(0xFFFFFFFFFFFFFFFFL, 0xFFFFFFFFFFFF0000L))
-    .setTableId(20)
+    .setTableId(GenTableId.of(20))
 -- c
 obj = of_bsn_gentable_clear_request_new(OF_VERSION_1_3);
 of_bsn_gentable_clear_request_xid_set(obj, 0x12345678);
diff --git a/test_data/of13/bsn_gentable_desc_stats_reply.data b/test_data/of13/bsn_gentable_desc_stats_reply.data
index 64346c8..180a8a1 100644
--- a/test_data/of13/bsn_gentable_desc_stats_reply.data
+++ b/test_data/of13/bsn_gentable_desc_stats_reply.data
@@ -43,13 +43,13 @@
     .setEntries(
         ImmutableList.<OFBsnGentableDescStatsEntry>of(
             factory.buildBsnGentableDescStatsEntry()
-                .setTableId(0)
+                .setTableId(GenTableId.of(0))
                 .setName("table 0")
                 .setBucketsSize(32)
                 .setMaxEntries(64)
                 .build(),
             factory.buildBsnGentableDescStatsEntry()
-                .setTableId(1)
+                .setTableId(GenTableId.of(1))
                 .setName("table 1.........................")
                 .setBucketsSize(64)
                 .setMaxEntries(128)
diff --git a/test_data/of13/bsn_gentable_entry_add.data b/test_data/of13/bsn_gentable_entry_add.data
index d8db0f8..02c2bb4 100644
--- a/test_data/of13/bsn_gentable_entry_add.data
+++ b/test_data/of13/bsn_gentable_entry_add.data
@@ -39,17 +39,17 @@
 -- java
 builder.setXid(0x12345678)
     .setChecksum(OFChecksum128.of(0xFEDCBA9876543210L, 0xFFEECCBBAA998877L))
-    .setTableId(20)
+    .setTableId(GenTableId.of(20))
     .setKey(
         ImmutableList.<OFBsnTlv>of(
-            factory.bsnTlvPort(OFPort.of(5)),
-            factory.bsnTlvMac(MacAddress.of("01:23:45:67:89:ab"))
+            factory.bsnTlvs().port(OFPort.of(5)),
+            factory.bsnTlvs().mac(MacAddress.of("01:23:45:67:89:ab"))
         )
     )
     .setValue(
         ImmutableList.<OFBsnTlv>of(
-            factory.bsnTlvPort(OFPort.of(6)),
-            factory.bsnTlvMac(MacAddress.of("ff:ee:dd:cc:bb:aa"))
+            factory.bsnTlvs().port(OFPort.of(6)),
+            factory.bsnTlvs().mac(MacAddress.of("ff:ee:dd:cc:bb:aa"))
         )
     )
 -- c
diff --git a/test_data/of13/bsn_gentable_entry_delete.data b/test_data/of13/bsn_gentable_entry_delete.data
index 0f17bfd..58c6c03 100644
--- a/test_data/of13/bsn_gentable_entry_delete.data
+++ b/test_data/of13/bsn_gentable_entry_delete.data
@@ -23,11 +23,11 @@
     ])
 -- java
 builder.setXid(0x12345678)
-    .setTableId(20)
+    .setTableId(GenTableId.of(20))
     .setKey(
         ImmutableList.<OFBsnTlv>of(
-            factory.bsnTlvPort(OFPort.of(5)),
-            factory.bsnTlvMac(MacAddress.of("01:23:45:67:89:ab"))
+            factory.bsnTlvs().port(OFPort.of(5)),
+            factory.bsnTlvs().mac(MacAddress.of("01:23:45:67:89:ab"))
         )
     )
 -- c
diff --git a/test_data/of13/bsn_gentable_entry_desc_stats_reply.data b/test_data/of13/bsn_gentable_entry_desc_stats_reply.data
index 40de7dd..4035f4c 100644
--- a/test_data/of13/bsn_gentable_entry_desc_stats_reply.data
+++ b/test_data/of13/bsn_gentable_entry_desc_stats_reply.data
@@ -57,19 +57,19 @@
             factory.buildBsnGentableEntryDescStatsEntry()
                 .setChecksum(OFChecksum128.of(0xFEDCBA9876543210L, 0xFFEECCBBAA998800L))
                 .setKey(ImmutableList.<OFBsnTlv>of(
-                    factory.bsnTlvPort(OFPort.of(5))
+                    factory.bsnTlvs().port(OFPort.of(5))
                 ))
                 .setValue(ImmutableList.<OFBsnTlv>of(
-                    factory.bsnTlvMac(MacAddress.of("ff:ee:dd:cc:bb:00"))
+                    factory.bsnTlvs().mac(MacAddress.of("ff:ee:dd:cc:bb:00"))
                 ))
                 .build(),
             factory.buildBsnGentableEntryDescStatsEntry()
                 .setChecksum(OFChecksum128.of(0xFEDCBA9876543210L, 0xFFEECCBBAA998801L))
                 .setKey(ImmutableList.<OFBsnTlv>of(
-                    factory.bsnTlvPort(OFPort.of(6))
+                    factory.bsnTlvs().port(OFPort.of(6))
                 ))
                 .setValue(ImmutableList.<OFBsnTlv>of(
-                    factory.bsnTlvMac(MacAddress.of("ff:ee:dd:cc:bb:01"))
+                    factory.bsnTlvs().mac(MacAddress.of("ff:ee:dd:cc:bb:01"))
                 ))
                 .build()
         )
diff --git a/test_data/of13/bsn_gentable_entry_stats_reply.data b/test_data/of13/bsn_gentable_entry_stats_reply.data
index cd4ab62..255de88 100644
--- a/test_data/of13/bsn_gentable_entry_stats_reply.data
+++ b/test_data/of13/bsn_gentable_entry_stats_reply.data
@@ -60,20 +60,20 @@
         ImmutableList.<OFBsnGentableEntryStatsEntry>of(
             factory.bsnGentableEntryStatsEntry(
                 ImmutableList.<OFBsnTlv>of(
-                    factory.bsnTlvPort(OFPort.of(5))
+                    factory.bsnTlvs().port(OFPort.of(5))
                 ),
                 ImmutableList.<OFBsnTlv>of(
-                    factory.bsnTlvRxPackets(U64.of(100)),
-                    factory.bsnTlvTxPackets(U64.of(101))
+                    factory.bsnTlvs().rxPackets(U64.of(100)),
+                    factory.bsnTlvs().txPackets(U64.of(101))
                 )
             ),
             factory.bsnGentableEntryStatsEntry(
                 ImmutableList.<OFBsnTlv>of(
-                    factory.bsnTlvPort(OFPort.of(6))
+                    factory.bsnTlvs().port(OFPort.of(6))
                 ),
                 ImmutableList.<OFBsnTlv>of(
-                    factory.bsnTlvRxPackets(U64.of(100)),
-                    factory.bsnTlvTxPackets(U64.of(101))
+                    factory.bsnTlvs().rxPackets(U64.of(100)),
+                    factory.bsnTlvs().txPackets(U64.of(101))
                 )
             )
         )