java_gen: enable generation for all messages, lots of related fixes
diff --git a/java_gen/codegen.py b/java_gen/codegen.py
index 07b910a..f3619d4 100644
--- a/java_gen/codegen.py
+++ b/java_gen/codegen.py
@@ -108,13 +108,23 @@
         """ Create the OF classes with implementations for each of the interfaces and versions """
         for interface in self.java_model.interfaces:
             for java_class in interface.versioned_classes:
-                if not self.java_model.generate_class(java_class):
-                    continue
-                self.render_class(clazz=java_class,
-                        template='of_class.java', version=java_class.version, msg=java_class,
-                        impl_class=java_class.name)
+                if self.java_model.generate_class(java_class):
+                    if not java_class.is_virtual:
+                        self.render_class(clazz=java_class,
+                                template='of_class.java', version=java_class.version, msg=java_class,
+                                impl_class=java_class.name)
 
-                self.create_unit_test(java_class.unit_test)
+                        self.create_unit_test(java_class.unit_test)
+                    else:
+                        disc = java_class.discriminator
+                        if disc:
+                            self.render_class(clazz=java_class,
+                                template='of_virtual_class.java', version=java_class.version, msg=java_class,
+                                impl_class=java_class.name, model=self.java_model)
+                        else:
+                            print "Class %s virtual but no discriminator" % java_class.name
+                else:
+                    print "Class %s ignored by generate_class" % java_class.name
 
     def create_unit_test(self, unit_test):
         if unit_test.has_test_data:
diff --git a/java_gen/java_model.py b/java_gen/java_model.py
index a118fdf..4ac497f 100644
--- a/java_gen/java_model.py
+++ b/java_gen/java_model.py
@@ -48,8 +48,8 @@
 class JavaModel(object):
     enum_blacklist = set(("OFDefinitions",))
     enum_entry_blacklist = defaultdict(lambda: set(), OFFlowWildcards=set([ "NW_DST_BITS", "NW_SRC_BITS", "NW_SRC_SHIFT", "NW_DST_SHIFT" ]))
-    write_blacklist = defaultdict(lambda: set(), OFOxm=set(('typeLen',)), OFAction=set(('type',)), OFInstruction=set(('type',)))
-    virtual_interfaces = set(['OFOxm', 'OFAction', 'OFInstruction' ])
+    write_blacklist = defaultdict(lambda: set(), OFOxm=set(('typeLen',)), OFAction=set(('type',)), OFInstruction=set(('type',)), OFFlowMod=set(('command', )))
+    virtual_interfaces = set(['OFOxm', 'OFInstruction', 'OFFlowMod', 'OFBsnVport' ])
 
     @property
     @memoize
@@ -78,6 +78,11 @@
 
     @property
     @memoize
+    def all_classes(self):
+        return [clazz for interface in self.interfaces for clazz in interface.versioned_classes]
+
+    @property
+    @memoize
     def enums(self):
         name_version_enum_map = OrderedDefaultDict(lambda: OrderedDict())
 
@@ -109,12 +114,11 @@
                     members=self.interfaces)
 
     def generate_class(self, clazz):
-        if clazz.interface.is_virtual:
-            return False
-        if clazz.interface.name == "OFTableMod":
-            return False
         if clazz.interface.name.startswith("OFMatchV"):
             return True
+        elif clazz.name == "OFTableModVer10":
+            # tablemod ver 10 is a hack and has no oftype defined
+            return False
         if loxi_utils.class_is_message(clazz.interface.c_name):
             return True
         if loxi_utils.class_is_oxm(clazz.interface.c_name):
@@ -124,7 +128,7 @@
         if loxi_utils.class_is_instruction(clazz.interface.c_name):
             return True
         else:
-            return False
+            return True
 
 
 class OFFactory(namedtuple("OFFactory", ("package", "name", "members"))):
@@ -186,7 +190,7 @@
     def __init__(self, c_name, version_map):
         self.c_name = c_name
         self.version_map = version_map
-        self.name = java_type.name_c_to_caps_camel(c_name)
+        self.name = java_type.name_c_to_caps_camel(c_name) if c_name != "of_header" else "OFMessage"
         self.variable_name = self.name[2].lower() + self.name[3:]
         self.constant_name = c_name.upper().replace("OF_", "")
 
@@ -198,20 +202,39 @@
             self.parent_interface = None
 
     def class_info(self):
-        if re.match(r'OFFlow(Add|Modify(Strict)?|Delete(Strict)?)$', self.name):
+        if re.match(r'OF.+StatsRequest$', self.name):
+            return ("", "OFStatsRequest")
+        elif re.match(r'OF.+StatsReply$', self.name):
+            return ("", "OFStatsReply")
+        elif re.match(r'OFFlow(Add|Modify(Strict)?|Delete(Strict)?)$', self.name):
             return ("", "OFFlowMod")
+        elif loxi_utils.class_is_message(self.c_name) and re.match(r'OFBsn.+$', self.name):
+            return ("", "OFBsnHeader")
+        elif loxi_utils.class_is_message(self.c_name) and re.match(r'OFNicira.+$', self.name):
+            return ("", "OFNiciraHeader")
         elif re.match(r'OFMatch.*', self.name):
             return ("", "Match")
         elif loxi_utils.class_is_message(self.c_name):
             return ("", "OFMessage")
         elif loxi_utils.class_is_action(self.c_name):
-            return ("action", "OFAction")
+            if re.match(r'OFActionBsn.*', self.name):
+                return ("action", "OFActionBsn")
+            elif re.match(r'OFActionNicira.*', self.name):
+                return ("action", "OFActionNicira")
+            else:
+                return ("action", "OFAction")
+        elif re.match(r'OFBsnVport.+$', self.name):
+            return ("", "OFBsnVport")
         elif loxi_utils.class_is_oxm(self.c_name):
             return ("oxm", "OFOxm")
         elif loxi_utils.class_is_instruction(self.c_name):
             return ("instruction", "OFInstruction")
         elif loxi_utils.class_is_meter_band(self.c_name):
             return ("meterband", "OFMeterBand")
+        elif loxi_utils.class_is_queue_prop(self.c_name):
+            return ("queueprop", "OFQueueProp")
+        elif loxi_utils.class_is_hello_elem(self.c_name):
+            return ("", "OFHelloElem")
         else:
             return ("", None)
 
@@ -235,7 +258,7 @@
     @property
     @memoize
     def is_virtual(self):
-        return self.name in model.virtual_interfaces
+        return self.name in model.virtual_interfaces or all(ir_class.virtual for ir_class in self.version_map.values())
 
     @property
     def is_universal(self):
@@ -255,9 +278,6 @@
     @property
     @memoize
     def versioned_classes(self):
-        if self.is_virtual:
-            return []
-        else:
             return [ self.versioned_class(version) for version in self.all_versions ]
 
 #######################################################################
@@ -448,6 +468,10 @@
         return isinstance(self.member, OFFieldLengthMember)
 
     @property
+    def is_discriminator(self):
+        return isinstance(self.member, OFDiscriminatorMember)
+
+    @property
     def is_length_value(self):
         return isinstance(self.member, OFLengthMember)
 
@@ -471,10 +495,18 @@
             return self.msg.version.int_version
         elif self.name == "length" or self.name == "len":
             return self.msg.length
-        elif self.java_type.public_type in ("int", "short", "byte") and self.member.value > 100:
-            return "0x%x" % self.member.value
         else:
-            return self.member.value
+            return self.java_type.format_value(self.member.value)
+
+    @property
+    def priv_value(self):
+        if self.name == "version":
+            return self.msg.version.int_version
+        elif self.name == "length" or self.name == "len":
+            return self.msg.length
+        else:
+            return self.java_type.format_value(self.member.value, pub_type=False)
+
 
     @property
     def is_writeable(self):
diff --git a/java_gen/pre-written/src/main/java/org/openflow/protocol/OFFlowMod.java b/java_gen/pre-written/src/main/java/org/openflow/protocol/OFFlowMod.java
deleted file mode 100644
index 06d8049..0000000
--- a/java_gen/pre-written/src/main/java/org/openflow/protocol/OFFlowMod.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package org.openflow.protocol;
-
-public interface OFFlowMod extends OFMessage {
-
-    public interface Builder extends OFMessage.Builder {
-
-    }
-}
diff --git a/java_gen/pre-written/src/main/java/org/openflow/protocol/OFMessage.java b/java_gen/pre-written/src/main/java/org/openflow/protocol/OFMessage.java
deleted file mode 100644
index 32319fa..0000000
--- a/java_gen/pre-written/src/main/java/org/openflow/protocol/OFMessage.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package org.openflow.protocol;
-
-public interface OFMessage extends OFObject {
-    int getXid();
-
-    OFType getType();
-
-    OFVersion getVersion();
-
-    interface Builder {
-
-    }
-}
diff --git a/java_gen/pre-written/src/main/java/org/openflow/protocol/OFObject.java b/java_gen/pre-written/src/main/java/org/openflow/protocol/OFObject.java
index 140182b..aae6178 100644
--- a/java_gen/pre-written/src/main/java/org/openflow/protocol/OFObject.java
+++ b/java_gen/pre-written/src/main/java/org/openflow/protocol/OFObject.java
@@ -1,17 +1,8 @@
 package org.openflow.protocol;
 
-import org.jboss.netty.buffer.ChannelBuffer;
 
 /**
  * Base interface of all OpenFlow objects (e.g., messages, actions, stats, etc.)
- *
- * All objects have a length and can be read and written from a buffer. When
- * writing, the length field is dynamically updated, so it need not be managed
- * manually. However, you can override the auto calculated length with
- * overrideLength() call, if, for example, you want to intentionally create
- * malformed packets, for example, for negative testing.
  */
-
-public interface OFObject {
-    void writeTo(ChannelBuffer bb);
+public interface OFObject extends Writeable {
 }
diff --git a/java_gen/pre-written/src/main/java/org/openflow/protocol/Wildcards.java b/java_gen/pre-written/src/main/java/org/openflow/protocol/Wildcards.java
index e1993f5..8c03b93 100644
--- a/java_gen/pre-written/src/main/java/org/openflow/protocol/Wildcards.java
+++ b/java_gen/pre-written/src/main/java/org/openflow/protocol/Wildcards.java
@@ -288,7 +288,7 @@
      * @return
      */
     public static Wildcards of(int paramFlags) {
-        int flags = sanitizeInt(paramFlags);
+        int flags = paramFlags; //sanitizeInt(paramFlags);
         switch(flags) {
             case 0x0000:
                 return EXACT;
diff --git a/java_gen/templates/_imports.java b/java_gen/templates/_imports.java
index bac7065..5b86571 100644
--- a/java_gen/templates/_imports.java
+++ b/java_gen/templates/_imports.java
@@ -7,6 +7,7 @@
 import org.openflow.protocol.instruction.*;
 import org.openflow.protocol.match.*;
 import org.openflow.protocol.oxm.*;
+import org.openflow.protocol.queueprop.*;
 import org.openflow.types.*;
 import org.openflow.types.*;
 import org.openflow.util.*;
diff --git a/java_gen/templates/of_class.java b/java_gen/templates/of_class.java
index 05018d3..0781dcf 100644
--- a/java_gen/templates/of_class.java
+++ b/java_gen/templates/of_class.java
@@ -129,26 +129,27 @@
     static class Reader implements OFMessageReader<${msg.interface.name}> {
         @Override
         public ${msg.interface.name} readFrom(ChannelBuffer bb) throws OFParseError {
+            int start = bb.readerIndex();
 //:: fields_with_length_member = {}
 //:: for prop in msg.members:
 //:: if prop.is_data:
-            ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version,
+            ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True,
                     length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)};
 //:: elif prop.is_pad:
             // pad: ${prop.length} bytes
             bb.skipBytes(${prop.length});
 //:: elif prop.is_fixed_value:
             // fixed value property ${prop.name} == ${prop.value}
-            ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version)};
-            if(${prop.name} != ${prop.value})
+            ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
+            if(${prop.name} != ${prop.priv_value})
                 throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
 //:: elif prop.is_length_value:
-            ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version)};
+            ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
             if(${prop.name} < MINIMUM_LENGTH)
                 throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name});
 //:: elif prop.is_field_length_value:
 //::        fields_with_length_member[prop.member.field_name] = prop.name
-            int ${prop.name} = ${prop.java_type.read_op(version)};
+            int ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
 //:: else:
     // fixme: todo ${prop.name}
 //:: #endif
@@ -173,32 +174,33 @@
     static class Writer implements OFMessageWriter<${impl_class}> {
         @Override
         public void write(ChannelBuffer bb, ${impl_class} message) {
-//:: if not msg.is_fixed_length:
             int startIndex = bb.writerIndex();
-//:: #end
-
 //:: fields_with_length_member = {}
 //:: for prop in msg.members:
 //:: if prop.c_name in fields_with_length_member:
             int ${prop.name}StartIndex = bb.writerIndex();
 //:: #endif
 //:: if prop.is_data:
-            ${prop.java_type.write_op(version, "message." + prop.name)};
+            ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)};
 //:: elif prop.is_pad:
             // pad: ${prop.length} bytes
             bb.writeZero(${prop.length});
 //:: elif prop.is_fixed_value:
             // fixed value property ${prop.name} = ${prop.value}
-            ${prop.java_type.write_op(version, prop.value)};
+            ${prop.java_type.write_op(version, prop.value, pub_type=False)};
 //:: elif prop.is_length_value:
             // ${prop.name} is length of variable message, will be updated at the end
+//:: if not msg.is_fixed_length:
+            int lengthIndex = bb.writerIndex();
+//:: #end
             ${prop.java_type.write_op(version, 0)};
+
 //:: elif prop.is_field_length_value:
 //::        fields_with_length_member[prop.member.field_name] = prop.name
             // ${prop.name} is length indicator for ${prop.member.field_name}, will be
             // udpated when ${prop.member.field_name} has been written
             int ${prop.name}Index = bb.writerIndex();
-            ${prop.java_type.write_op(version, 0)};
+            ${prop.java_type.write_op(version, 0, pub_type=False)};
 //:: else:
             // FIXME: todo write ${prop.name}
 //:: #endif
@@ -213,7 +215,7 @@
 //:: if not msg.is_fixed_length:
             // update length field
             int length = bb.writerIndex() - startIndex;
-            bb.setShort(startIndex + 2, length);
+            bb.setShort(lengthIndex, length);
             //:: if msg.align:
             // align message to ${msg.align} bytes
             bb.writeZero( ((length + ${msg.align-1})/${msg.align} * ${msg.align}) - length);
diff --git a/java_gen/templates/of_interface.java b/java_gen/templates/of_interface.java
index 186b787..4dec92f 100644
--- a/java_gen/templates/of_interface.java
+++ b/java_gen/templates/of_interface.java
@@ -51,6 +51,5 @@
         Builder set${prop.title_name}(${prop.java_type.public_type} ${prop.name})${ "" if prop.is_universal else " throws UnsupportedOperationException"};
 //:: #endif
 //:: #endfor
-
     }
 }
diff --git a/java_gen/templates/of_virtual_class.java b/java_gen/templates/of_virtual_class.java
new file mode 100644
index 0000000..f1ac849
--- /dev/null
+++ b/java_gen/templates/of_virtual_class.java
@@ -0,0 +1,102 @@
+//:: # Copyright 2013, Big Switch Networks, Inc.
+//:: #
+//:: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
+//:: # the following special exception:
+//:: #
+//:: # LOXI Exception
+//:: #
+//:: # As a special exception to the terms of the EPL, you may distribute libraries
+//:: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
+//:: # that copyright and licensing notices generated by LoxiGen are not altered or removed
+//:: # from the LoxiGen Libraries and the notice provided below is (i) included in
+//:: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any
+//:: # documentation for the LoxiGen Libraries, if distributed in binary form.
+//:: #
+//:: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
+//:: #
+//:: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
+//:: # a copy of the EPL at:
+//:: #
+//:: # http::: #www.eclipse.org/legal/epl-v10.html
+//:: #
+//:: # Unless required by applicable law or agreed to in writing, software
+//:: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+//:: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+//:: # EPL for the specific language governing permissions and limitations
+//:: # under the EPL.
+//::
+//:: from loxi_ir import *
+//:: import os
+//:: import itertools
+//:: import of_g
+//:: include('_copyright.java')
+
+//:: include('_autogen.java')
+
+package ${msg.package};
+
+//:: include("_imports.java", msg=msg)
+
+abstract class ${msg.name} implements ${msg.interface.name} {
+    // version: ${version}
+    private final static byte WIRE_VERSION = ${version.int_version};
+//:: if msg.is_fixed_length:
+    private final static int LENGTH = ${msg.length};
+//:: else:
+    private final static int MINIMUM_LENGTH = ${msg.min_length};
+//:: #endif
+
+
+    public final static ${msg.name}.Reader READER = new Reader();
+
+    static class Reader implements OFMessageReader<${msg.interface.name}> {
+        @Override
+        public ${msg.interface.name} readFrom(ChannelBuffer bb) throws OFParseError {
+            int start = bb.readerIndex();
+//:: fields_with_length_member = {}
+//::    for prop in msg.members:
+//::       if prop.is_data:
+            ${prop.java_type.skip_op(version,
+                    length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)};
+//:: elif prop.is_pad:
+            // pad: ${prop.length} bytes
+            bb.skipBytes(${prop.length});
+//:: elif prop.is_fixed_value:
+            // fixed value property ${prop.name} == ${prop.value}
+            ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
+            if(${prop.name} != ${prop.value})
+                throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
+//:: elif prop.is_length_value:
+            ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)};
+            if(${prop.name} < MINIMUM_LENGTH)
+                throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name});
+//:: elif prop.is_field_length_value:
+//::        fields_with_length_member[prop.member.field_name] = prop.name
+            int ${prop.name} = ${prop.java_type.read_op(version)};
+//:: elif prop.is_discriminator:
+            ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
+            bb.readerIndex(start);
+            switch(${prop.name}) {
+//::     for sub in msg.subclasses:
+//::           if not model.generate_class(sub):
+               // skip ${sub.name} - excluded from generation
+//::           else:
+//::           m = sub.get_member(prop.name)
+//::           if not m.is_fixed_value:
+//::                  raise Exception("subtype %s of %s does not have fixed value for discriminator %s" %
+//::                           (sub.name, msg.name, prop.name))
+//::           #endif
+               case ${m.priv_value}:
+                   // discriminator value ${m.enum_value}=${m.value} for class ${sub.name}
+                   return ${sub.name}.READER.readFrom(bb);
+//:: #endif    # generate_class
+//:: #endfor
+               default:
+                   throw new OFParseError("Unknown value for discriminator ${prop.name} of class ${msg.name}: " + ${prop.name});
+            }
+//::        break
+//:: #endif
+//:: #endfor
+        }
+    }
+}
diff --git a/java_gen/templates/unit_test.java b/java_gen/templates/unit_test.java
index 1462465..45472f2 100644
--- a/java_gen/templates/unit_test.java
+++ b/java_gen/templates/unit_test.java
@@ -77,7 +77,7 @@
         // FIXME should invoke the overall reader once implemented
         ${var_type} ${var_name}Read = ${msg.name}.READER.readFrom(input);
 
-        assertEquals(${var_name}Read, ${var_name}Built);
+        assertEquals(${var_name}Built, ${var_name}Read);
    }
    //:: else:
    // FIXME: No java stanza in test_data for this class. Add for more comprehensive unit testing