pyloxi: generate OXM classes
diff --git a/py_gen/codegen.py b/py_gen/codegen.py
index b8e014f..19bdbaf 100644
--- a/py_gen/codegen.py
+++ b/py_gen/codegen.py
@@ -68,6 +68,13 @@
             type_values['subtype'] = type_maps.extension_action_to_subtype(cls, version)
     elif utils.class_is_queue_prop(cls):
         type_values['type'] = util.constant_for_value(version, "ofp_queue_properties", util.primary_wire_type(cls, version))
+    elif utils.class_is_oxm(cls):
+        oxm_class = 0x8000
+        oxm_type = util.primary_wire_type(cls, version)
+        oxm_masked = cls.find('masked') != -1 and 1 or 0
+        oxm_len = of_g.base_length[(cls, version)]
+        type_values['type_len'] = '%#x' % (oxm_class << 16 | oxm_type << 8 | \
+                                           oxm_masked << 8 | oxm_len)
 
     return type_values
 
@@ -75,7 +82,7 @@
 def build_ofclasses(version):
     blacklist = ["of_action", "of_action_header", "of_header", "of_queue_prop",
                  "of_queue_prop_header", "of_experimenter", "of_action_experimenter",
-                 "of_oxm"]
+                 "of_oxm", "of_oxm_header", "of_oxm_experimenter_header"]
     ofclasses = []
     for cls in of_g.standard_class_order:
         if version not in of_g.unified[cls] or cls in blacklist:
@@ -85,6 +92,8 @@
         # Name for the generated Python class
         if utils.class_is_action(cls):
             pyname = cls[10:]
+        elif utils.class_is_oxm(cls):
+            pyname = cls[7:]
         else:
             pyname = cls[3:]
 
@@ -131,17 +140,23 @@
     return ofclasses
 
 def generate_init(out, name, version):
-    util.render_template(out, 'init.py')
+    util.render_template(out, 'init.py', version=version)
 
 def generate_action(out, name, version):
     ofclasses = [x for x in build_ofclasses(version)
                  if utils.class_is_action(x.name)]
     util.render_template(out, 'action.py', ofclasses=ofclasses, version=version)
 
+def generate_oxm(out, name, version):
+    ofclasses = [x for x in build_ofclasses(version)
+                 if utils.class_is_oxm(x.name)]
+    util.render_template(out, 'oxm.py', ofclasses=ofclasses, version=version)
+
 def generate_common(out, name, version):
     ofclasses = [x for x in build_ofclasses(version)
                  if not utils.class_is_message(x.name)
                     and not utils.class_is_action(x.name)
+                    and not utils.class_is_oxm(x.name)
                     and not utils.class_is_list(x.name)]
     util.render_template(out, 'common.py', ofclasses=ofclasses, version=version)
 
diff --git a/py_gen/templates/init.py b/py_gen/templates/init.py
index abc8d70..4a14228 100644
--- a/py_gen/templates/init.py
+++ b/py_gen/templates/init.py
@@ -30,6 +30,9 @@
 :: include('_autogen.py')
 
 import action, common, const, message
+:: if version >= 4:
+import oxm
+:: #endif
 from const import *
 from common import *
 from loxi import ProtocolError
diff --git a/py_gen/templates/oxm.py b/py_gen/templates/oxm.py
new file mode 100644
index 0000000..c210874
--- /dev/null
+++ b/py_gen/templates/oxm.py
@@ -0,0 +1,93 @@
+:: # 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.
+::
+:: import itertools
+:: import of_g
+:: include('_copyright.py')
+
+:: include('_autogen.py')
+
+import struct
+import const
+import util
+import loxi
+
+class OXM(object):
+    type_len = None # override in subclass
+    pass
+
+:: for ofclass in ofclasses:
+:: nonskip_members = [m for m in ofclass.members if not m.skip]
+class ${ofclass.pyname}(OXM):
+:: for m in ofclass.type_members:
+    ${m.name} = ${m.value}
+:: #endfor
+
+    def __init__(self, ${', '.join(["%s=None" % m.name for m in nonskip_members])}):
+:: for m in nonskip_members:
+        if ${m.name} != None:
+            self.${m.name} = ${m.name}
+        else:
+            self.${m.name} = ${m.oftype.gen_init_expr()}
+:: #endfor
+
+    def pack(self):
+        packed = []
+:: include("_pack.py", ofclass=ofclass)
+        return ''.join(packed)
+
+    @staticmethod
+    def unpack(buf):
+        obj = ${ofclass.pyname}()
+:: include("_unpack.py", ofclass=ofclass)
+        return obj
+
+    def __eq__(self, other):
+        if type(self) != type(other): return False
+:: for m in nonskip_members:
+        if self.${m.name} != other.${m.name}: return False
+:: #endfor
+        return True
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
+
+    def show(self):
+        import loxi.pp
+        return loxi.pp.pp(self)
+
+    def pretty_print(self, q):
+:: include('_pretty_print.py', ofclass=ofclass)
+
+:: #endfor
+
+parsers = {
+:: key = lambda x: int(x.type_members[0].value, 16)
+:: for ofclass in sorted(ofclasses, key=key):
+    ${key(ofclass)} : ${ofclass.pyname}.unpack,
+:: #endfor
+}
diff --git a/py_gen/tests/of13.py b/py_gen/tests/of13.py
index 512d4aa..7d146a4 100644
--- a/py_gen/tests/of13.py
+++ b/py_gen/tests/of13.py
@@ -53,6 +53,30 @@
         self.assertTrue(hasattr(loxi.of13, "const"))
         self.assertTrue(hasattr(loxi.of13, "message"))
 
+class TestOXM(unittest.TestCase):
+    def test_oxm_in_phy_port_pack(self):
+        import loxi.of13 as ofp
+        obj = ofp.oxm.in_phy_port(value=42)
+        expected = ''.join([
+            '\x80\x00', # class
+            '\x02', # type/masked
+            '\x08', # length
+            '\x00\x00\x00\x2a' # value
+        ])
+        self.assertEquals(expected, obj.pack())
+
+    def test_oxm_in_phy_port_masked_pack(self):
+        import loxi.of13 as ofp
+        obj = ofp.oxm.in_phy_port_masked(value=42, value_mask=0xaabbccdd)
+        expected = ''.join([
+            '\x80\x00', # class
+            '\x03', # type/masked
+            '\x0c', # length
+            '\x00\x00\x00\x2a', # value
+            '\xaa\xbb\xcc\xdd' # mask
+        ])
+        self.assertEquals(expected, obj.pack())
+
 class TestAllOF13(unittest.TestCase):
     """
     Round-trips every class through serialization/deserialization.
@@ -61,7 +85,7 @@
     """
 
     def setUp(self):
-        mods = [ofp.action,ofp.message,ofp.common]
+        mods = [ofp.action,ofp.message,ofp.common,ofp.oxm]
         self.klasses = [klass for mod in mods
                               for klass in mod.__dict__.values()
                               if hasattr(klass, 'show')]
@@ -111,79 +135,6 @@
             ofp.common.meter_band_dscp_remark,
             ofp.common.meter_band_experimenter,
             ofp.common.meter_band_header,
-            ofp.common.oxm_arp_op,
-            ofp.common.oxm_arp_op_masked,
-            ofp.common.oxm_arp_sha,
-            ofp.common.oxm_arp_sha_masked,
-            ofp.common.oxm_arp_spa,
-            ofp.common.oxm_arp_spa_masked,
-            ofp.common.oxm_arp_tha,
-            ofp.common.oxm_arp_tha_masked,
-            ofp.common.oxm_arp_tpa,
-            ofp.common.oxm_arp_tpa_masked,
-            ofp.common.oxm_eth_dst,
-            ofp.common.oxm_eth_dst_masked,
-            ofp.common.oxm_eth_src,
-            ofp.common.oxm_eth_src_masked,
-            ofp.common.oxm_eth_type,
-            ofp.common.oxm_eth_type_masked,
-            ofp.common.oxm_header,
-            ofp.common.oxm_icmpv4_code,
-            ofp.common.oxm_icmpv4_code_masked,
-            ofp.common.oxm_icmpv4_type,
-            ofp.common.oxm_icmpv4_type_masked,
-            ofp.common.oxm_icmpv6_code,
-            ofp.common.oxm_icmpv6_code_masked,
-            ofp.common.oxm_icmpv6_type,
-            ofp.common.oxm_icmpv6_type_masked,
-            ofp.common.oxm_in_phy_port,
-            ofp.common.oxm_in_phy_port_masked,
-            ofp.common.oxm_in_port,
-            ofp.common.oxm_in_port_masked,
-            ofp.common.oxm_ip_dscp,
-            ofp.common.oxm_ip_dscp_masked,
-            ofp.common.oxm_ip_ecn,
-            ofp.common.oxm_ip_ecn_masked,
-            ofp.common.oxm_ip_proto,
-            ofp.common.oxm_ip_proto_masked,
-            ofp.common.oxm_ipv4_dst,
-            ofp.common.oxm_ipv4_dst_masked,
-            ofp.common.oxm_ipv4_src,
-            ofp.common.oxm_ipv4_src_masked,
-            ofp.common.oxm_ipv6_dst,
-            ofp.common.oxm_ipv6_dst_masked,
-            ofp.common.oxm_ipv6_flabel,
-            ofp.common.oxm_ipv6_flabel_masked,
-            ofp.common.oxm_ipv6_nd_sll,
-            ofp.common.oxm_ipv6_nd_sll_masked,
-            ofp.common.oxm_ipv6_nd_target,
-            ofp.common.oxm_ipv6_nd_target_masked,
-            ofp.common.oxm_ipv6_nd_tll,
-            ofp.common.oxm_ipv6_nd_tll_masked,
-            ofp.common.oxm_ipv6_src,
-            ofp.common.oxm_ipv6_src_masked,
-            ofp.common.oxm_metadata,
-            ofp.common.oxm_metadata_masked,
-            ofp.common.oxm_mpls_label,
-            ofp.common.oxm_mpls_label_masked,
-            ofp.common.oxm_mpls_tc,
-            ofp.common.oxm_mpls_tc_masked,
-            ofp.common.oxm_sctp_dst,
-            ofp.common.oxm_sctp_dst_masked,
-            ofp.common.oxm_sctp_src,
-            ofp.common.oxm_sctp_src_masked,
-            ofp.common.oxm_tcp_dst,
-            ofp.common.oxm_tcp_dst_masked,
-            ofp.common.oxm_tcp_src,
-            ofp.common.oxm_tcp_src_masked,
-            ofp.common.oxm_udp_dst,
-            ofp.common.oxm_udp_dst_masked,
-            ofp.common.oxm_udp_src,
-            ofp.common.oxm_udp_src_masked,
-            ofp.common.oxm_vlan_pcp,
-            ofp.common.oxm_vlan_pcp_masked,
-            ofp.common.oxm_vlan_vid,
-            ofp.common.oxm_vlan_vid_masked,
             ofp.common.table_feature_prop,
             ofp.common.table_feature_prop_apply_actions,
             ofp.common.table_feature_prop_apply_actions_miss,
@@ -274,79 +225,6 @@
             ofp.common.meter_band_dscp_remark,
             ofp.common.meter_band_experimenter,
             ofp.common.meter_band_header,
-            ofp.common.oxm_arp_op,
-            ofp.common.oxm_arp_op_masked,
-            ofp.common.oxm_arp_sha,
-            ofp.common.oxm_arp_sha_masked,
-            ofp.common.oxm_arp_spa,
-            ofp.common.oxm_arp_spa_masked,
-            ofp.common.oxm_arp_tha,
-            ofp.common.oxm_arp_tha_masked,
-            ofp.common.oxm_arp_tpa,
-            ofp.common.oxm_arp_tpa_masked,
-            ofp.common.oxm_eth_dst,
-            ofp.common.oxm_eth_dst_masked,
-            ofp.common.oxm_eth_src,
-            ofp.common.oxm_eth_src_masked,
-            ofp.common.oxm_eth_type,
-            ofp.common.oxm_eth_type_masked,
-            ofp.common.oxm_header,
-            ofp.common.oxm_icmpv4_code,
-            ofp.common.oxm_icmpv4_code_masked,
-            ofp.common.oxm_icmpv4_type,
-            ofp.common.oxm_icmpv4_type_masked,
-            ofp.common.oxm_icmpv6_code,
-            ofp.common.oxm_icmpv6_code_masked,
-            ofp.common.oxm_icmpv6_type,
-            ofp.common.oxm_icmpv6_type_masked,
-            ofp.common.oxm_in_phy_port,
-            ofp.common.oxm_in_phy_port_masked,
-            ofp.common.oxm_in_port,
-            ofp.common.oxm_in_port_masked,
-            ofp.common.oxm_ip_dscp,
-            ofp.common.oxm_ip_dscp_masked,
-            ofp.common.oxm_ip_ecn,
-            ofp.common.oxm_ip_ecn_masked,
-            ofp.common.oxm_ip_proto,
-            ofp.common.oxm_ip_proto_masked,
-            ofp.common.oxm_ipv4_dst,
-            ofp.common.oxm_ipv4_dst_masked,
-            ofp.common.oxm_ipv4_src,
-            ofp.common.oxm_ipv4_src_masked,
-            ofp.common.oxm_ipv6_dst,
-            ofp.common.oxm_ipv6_dst_masked,
-            ofp.common.oxm_ipv6_flabel,
-            ofp.common.oxm_ipv6_flabel_masked,
-            ofp.common.oxm_ipv6_nd_sll,
-            ofp.common.oxm_ipv6_nd_sll_masked,
-            ofp.common.oxm_ipv6_nd_target,
-            ofp.common.oxm_ipv6_nd_target_masked,
-            ofp.common.oxm_ipv6_nd_tll,
-            ofp.common.oxm_ipv6_nd_tll_masked,
-            ofp.common.oxm_ipv6_src,
-            ofp.common.oxm_ipv6_src_masked,
-            ofp.common.oxm_metadata,
-            ofp.common.oxm_metadata_masked,
-            ofp.common.oxm_mpls_label,
-            ofp.common.oxm_mpls_label_masked,
-            ofp.common.oxm_mpls_tc,
-            ofp.common.oxm_mpls_tc_masked,
-            ofp.common.oxm_sctp_dst,
-            ofp.common.oxm_sctp_dst_masked,
-            ofp.common.oxm_sctp_src,
-            ofp.common.oxm_sctp_src_masked,
-            ofp.common.oxm_tcp_dst,
-            ofp.common.oxm_tcp_dst_masked,
-            ofp.common.oxm_tcp_src,
-            ofp.common.oxm_tcp_src_masked,
-            ofp.common.oxm_udp_dst,
-            ofp.common.oxm_udp_dst_masked,
-            ofp.common.oxm_udp_src,
-            ofp.common.oxm_udp_src_masked,
-            ofp.common.oxm_vlan_pcp,
-            ofp.common.oxm_vlan_pcp_masked,
-            ofp.common.oxm_vlan_vid,
-            ofp.common.oxm_vlan_vid_masked,
             ofp.common.table_feature_prop,
             ofp.common.table_feature_prop_apply_actions,
             ofp.common.table_feature_prop_apply_actions_miss,