Merge branch 'master' of github.com:floodlight/loxigen into funnel

Conflicts:
	java_gen/java_type.py
	java_gen/templates/of_factory_interface.java
diff --git a/java_gen/codegen.py b/java_gen/codegen.py
index d94d78a..776b8c5 100644
--- a/java_gen/codegen.py
+++ b/java_gen/codegen.py
@@ -50,7 +50,6 @@
         shutil.rmtree(basedir)
     os.makedirs(basedir)
     copy_prewrite_tree(basedir)
-    java_model.adjust_ir()
     gen = JavaGenerator(basedir)
     gen.create_of_interfaces()
     gen.create_of_classes()
diff --git a/java_gen/java_model.py b/java_gen/java_model.py
index 96ba163..19ac5bc 100644
--- a/java_gen/java_model.py
+++ b/java_gen/java_model.py
@@ -46,97 +46,6 @@
 import java_gen.java_type as java_type
 from java_gen.java_type import erase_type_annotation
 
-# Key is modified name of error code; value is OFErrorType value string
-error_type_map = {}
-
-def adjust_ir():
-    """
-    For Java we change of_error_message to combine the 16-bit type and code
-    fields into a single 32-bit code field and we combine the per-error-type
-    code enums into a single ofp_error_code enum. This enables the generated
-    OFErrorMsg class to have a getCode method that can return all supported
-    error codes. Otherwise we'd need to do something like having per-error-type
-    subclasses of OFErrorMsg that had a getCode that returned the different
-    error codes for each error type, which would be less convenient for clients
-    and would also entail changing the LOXI OF input files and impacting other
-    language backends.
-    """
-    for version in of_g.target_version_list:
-        of_protocol = of_g.ir[version]
-        error_type = find(lambda e: e.name == "ofp_error_type", of_protocol.enums)
-        if error_type == None:
-            raise Exception("ofp_error_type enum not found; OF version: " + str(version))
-        error_code_entries = []
-        # For each error type value look up the corresponding error code enum.
-        # Add those values to the combined entries for the new ofp_error_code
-        # enum. The name of the new value is formed by concatenating the name
-        # of the error type value with the name of the old error code value.
-        for error_type_entry in error_type.entries:
-            # Strip off the OFPxx prefix
-            prefix_length = error_type_entry.name.find('_')
-            if prefix_length < 0:
-                raise Exception("OFPET prefix not found for ofp_error_type value " + error_type_entry.name + "; OF version: " + str(version))
-            error_type_entry_name = error_type_entry.name[prefix_length+1:]
-            if error_type_entry_name == "EXPERIMENTER":
-                # There isn't an error code enum defined for the experimenter error type
-                # FIXME: Need to add support for the message ofp_error_experimenter_msg format
-                continue
-            # The per-error-type code enums follow a naming conventions where
-            # the middle part of the enum name is the same as the name of the
-            # error type value (except lower-case).
-            error_code_enum_name = "ofp_" + error_type_entry_name.lower() + "_code"
-            # Look up the error code enum from the IR
-            error_code_enum = None
-            for i, enum in enumerate(of_protocol.enums):
-                if enum.name == error_code_enum_name:
-                    error_code_enum = enum
-                    # We don't want to generate code for the per-error-type
-                    # enum so remove it from the IR
-                    del of_protocol.enums[i]
-                    break
-            if error_code_enum == None:
-                raise Exception("Error code enum not found: " + error_code_enum_name + "; OF version: " + str(version))
-            for error_code_entry in error_code_enum.entries:
-                # Strip off the prefix from the entry name
-                prefix_length = error_code_entry.name.find('_')
-                if prefix_length < 0:
-                    raise Exception("Prefix not found for error code value " + error_code_entry.name + "; OF version: " + str(version))
-                error_code_entry_name = error_code_entry.name[prefix_length+1:]
-                # Combine the entry type name and the error code name
-                error_code_entry_name = error_type_entry_name + "_" + error_code_entry_name
-                # Combine the entry type value and the error code value
-                error_code_entry_value = (error_type_entry.value << 16) + error_code_entry.value
-                # Add the enum entry to the combined ofp_error_code
-                # Note that the "OFPEC" prefix is arbitrary. It will be stripped
-                # off again during Java code generation, but there needs to be
-                # some/any prefix
-                error_code_entries.append(OFEnumEntry("OFPEC_" + error_code_entry_name, error_code_entry_value, {}))
-                error_type_map[error_code_entry_name] = error_type_entry_name
-        # We've collected all of the entries. Now we can add the enum to the IR
-        of_protocol.enums.append(OFEnum("ofp_error_code", error_code_entries, {'wire_type': 'uint32_t'}))
-
-        # We also need to patch the of_error_msg class to combine the 16-bit error
-        # type and code fields into a single 32-bit error code field
-        error_msg = find(lambda c: c.name == "of_error_msg", of_protocol.classes)
-        if error_msg == None:
-            raise Exception("of_error_msg class not found; OF version: " + str(version))
-        err_type_index = None
-        for i, member in enumerate(error_msg.members):
-            if member.name == "err_type":
-                # Keep track of the error type index so we can remove it once
-                # we've finished iterating
-                err_type_index = i
-            elif member.name == 'code':
-                # Change the code to be a 32-bit ofp_error_code enum value
-                error_msg.members[i] = OFDataMember("code", "ofp_error_code")
-        if err_type_index == None:
-            raise Exception("err_type member of of_error_msg not found; OF version: " + str(version))
-        del error_msg.members[err_type_index]
-
-
-def gen_error_type(enum_entry):
-    return "OFErrorType." + error_type_map[enum_entry.name]
-
 class JavaModel(object):
     # registry for enums that should not be generated
     # set(${java_enum_name})
@@ -301,7 +210,6 @@
     enum_metadata_map = defaultdict(lambda: JavaModel.OFEnumMetadata((), None),
             OFPortFeatures = OFEnumMetadata((OFEnumPropertyMetadata("PortSpeed", java_type.port_speed, gen_port_speed),), None),
             OFPortState = OFEnumMetadata((OFEnumPropertyMetadata("StpState", java_type.boolean, gen_stp_state),), None),
-            OFErrorCode = OFEnumMetadata((OFEnumPropertyMetadata("ErrorType", java_type.error_type, gen_error_type),), None),
     )
 
     @property
@@ -594,6 +502,8 @@
             return ("", "OFStatsRequest<{}>".format(re.sub(r'Request$', 'Reply', self.name)), None)
         elif re.match(r'OF.+StatsReply$', self.name):
             return ("", "OFStatsReply", None)
+        elif re.match(r'OF.+ErrorMsg$', self.name):
+            return ("", "OFErrorMsg", None)
         elif re.match(r'OFFlow(Add|Modify(Strict)?|Delete(Strict)?)$', self.name):
             return ("", "OFFlowMod", None)
         elif loxi_utils.class_is_message(self.c_name) and re.match(r'OFBsn.+$', self.name) and self.name != "OFBsnHeader":
@@ -1157,10 +1067,7 @@
     def __init__(self, c_name, version_enum_map):
         self.c_name = c_name
 
-        if c_name == "of_stats_types":
-            self.name = "OFStatsType"
-        else:
-            self.name   = "OF" + java_type.name_c_to_caps_camel("_".join(c_name.split("_")[1:]))
+        self.name   = "OF" + java_type.name_c_to_caps_camel("_".join(c_name.split("_")[1:]))
 
         # Port_features has constants that start with digits
         self.name_prefix = "PF_" if self.name == "OFPortFeatures" else ""
diff --git a/java_gen/java_type.py b/java_gen/java_type.py
index 1a28e42..5e8bacc 100644
--- a/java_gen/java_type.py
+++ b/java_gen/java_type.py
@@ -33,11 +33,12 @@
     else:
         return camel
 
-java_primitive_types = set("byte char short int long".split(" "))
+java_primitive_types = set("boolean byte char short int long".split(" "))
 
 ### info table about java primitive types, for casting literals in the source code
 # { name : (signed?, length_in_bits) }
 java_primitives_info = {
+        'boolean' : (False, 8, False),
         'byte' : (True, 8, True),
         'char' : (False, 16, True),
         'short' : (True, 16, True),
@@ -48,7 +49,7 @@
 def format_primitive_literal(t, value):
     """ Format a primitive numeric literal for inclusion in the
         java source code. Takes care of casting the literal
-        apropriately for correct representation despite Java's
+        appropriately for correct representation despite Java's
         signed-craziness
     """
     signed, bits, cast_needed = java_primitives_info[t]
@@ -427,7 +428,6 @@
 
 port_speed = JType("PortSpeed")
 error_type = JType("OFErrorType")
-boolean = JType("boolean").op(default="false")
 of_type = JType("OFType", 'byte') \
             .op(read='bb.readByte()', write='bb.writeByte($name)')
 action_type= gen_enum_jtype("OFActionType")\
@@ -438,6 +438,14 @@
                .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)
 buffer_id = JType("OFBufferId") \
             .op(read="OFBufferId.of(bb.readInt())", write="bb.writeInt($name.getInt())", default="OFBufferId.NO_BUFFER")
+boolean = JType("boolean", "byte") \
+        .op(read='(bb.readByte() != 0)',
+            write='bb.writeByte($name ? 1 : 0)',
+            default="false")
+datapath_id = JType("DatapathId") \
+        .op(read='DatapathId.of(bb.readLong())',
+            write='bb.writeLong($name.getLong())',
+            default='DatapathId.NONE')
 
 generic_t = JType("T")
 
@@ -533,7 +541,9 @@
                 'eth_type': eth_type, 'ip_dscp': ip_dscp, 'ip_proto': ip_proto,
                 'tcp_src': transport_port, 'tcp_dst': transport_port,
                 'in_port': of_port_match_v1
-                }
+                },
+        'of_bsn_set_l2_table_request': { 'l2_table_enable': boolean },
+        'of_bsn_set_l2_table_reply': { 'l2_table_enable': boolean },
 }
 
 
@@ -572,6 +582,12 @@
         return of_type
     elif field_name == "type" and re.match(r'of_action.*', obj_name):
         return action_type
+    elif field_name == "err_type":
+        return JType("OFErrorType", 'short') \
+            .op(read='bb.readShort()', write='bb.writeShort($name)')
+    elif field_name == "stats_type":
+        return JType("OFStatsType", 'short') \
+            .op(read='bb.readShort()', write='bb.writeShort($name)')
     elif field_name == "type" and re.match(r'of_instruction.*', obj_name):
         return instruction_type
     elif field_name == "table_id" and c_type == "uint8_t":
@@ -580,6 +596,13 @@
         return of_version
     elif field_name == "buffer_id" and c_type == "uint32_t":
         return buffer_id
+    elif field_name == 'datapath_id':
+        return datapath_id
+    elif field_name == 'actions' and obj_name == 'of_features_reply':
+        return JType("Set<OFActionType>") \
+            .op(read='ChannelUtilsVer10.readSupportedActions(bb)',
+                write='ChannelUtilsVer10.writeSupportedActions(bb, $name)',
+                default='ImmutableSet.<OFActionType>of()')
     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/protocol/ver10/ChannelUtilsVer10.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/protocol/ver10/ChannelUtilsVer10.java
index 7a42d20..3f2f23f 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/protocol/ver10/ChannelUtilsVer10.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/protocol/ver10/ChannelUtilsVer10.java
@@ -1,9 +1,13 @@
 package org.projectfloodlight.openflow.protocol.ver10;
 
+import java.util.EnumSet;
+import java.util.Set;
+
 import org.jboss.netty.buffer.ChannelBuffer;
 import org.projectfloodlight.openflow.exceptions.OFParseError;
 import org.projectfloodlight.openflow.protocol.match.Match;
 import org.projectfloodlight.openflow.protocol.ver10.OFMatchV1Ver10;
+import org.projectfloodlight.openflow.protocol.OFActionType;
 import org.projectfloodlight.openflow.protocol.OFBsnVportQInQ;
 
 /**
@@ -28,4 +32,63 @@
 
     }
 
+    public static Set<OFActionType> readSupportedActions(ChannelBuffer bb) {
+        int actions = bb.readInt();
+        EnumSet<OFActionType> supportedActions = EnumSet.noneOf(OFActionType.class);
+        if ((actions & (1 << OFActionTypeSerializerVer10.OUTPUT_VAL)) != 0)
+            supportedActions.add(OFActionType.OUTPUT);
+        if ((actions & (1 << OFActionTypeSerializerVer10.SET_VLAN_VID_VAL)) != 0)
+            supportedActions.add(OFActionType.SET_VLAN_VID);
+        if ((actions & (1 << OFActionTypeSerializerVer10.SET_VLAN_PCP_VAL)) != 0)
+            supportedActions.add(OFActionType.SET_VLAN_PCP);
+        if ((actions & (1 << OFActionTypeSerializerVer10.STRIP_VLAN_VAL)) != 0)
+            supportedActions.add(OFActionType.STRIP_VLAN);
+        if ((actions & (1 << OFActionTypeSerializerVer10.SET_DL_SRC_VAL)) != 0)
+            supportedActions.add(OFActionType.SET_DL_SRC);
+        if ((actions & (1 << OFActionTypeSerializerVer10.SET_DL_DST_VAL)) != 0)
+            supportedActions.add(OFActionType.SET_DL_DST);
+        if ((actions & (1 << OFActionTypeSerializerVer10.SET_NW_SRC_VAL)) != 0)
+            supportedActions.add(OFActionType.SET_NW_SRC);
+        if ((actions & (1 << OFActionTypeSerializerVer10.SET_NW_DST_VAL)) != 0)
+            supportedActions.add(OFActionType.SET_NW_DST);
+        if ((actions & (1 << OFActionTypeSerializerVer10.SET_NW_TOS_VAL)) != 0)
+            supportedActions.add(OFActionType.SET_NW_TOS);
+        if ((actions & (1 << OFActionTypeSerializerVer10.SET_TP_SRC_VAL)) != 0)
+            supportedActions.add(OFActionType.SET_TP_SRC);
+        if ((actions & (1 << OFActionTypeSerializerVer10.SET_TP_DST_VAL)) != 0)
+            supportedActions.add(OFActionType.SET_TP_DST);
+        if ((actions & (1 << OFActionTypeSerializerVer10.ENQUEUE_VAL)) != 0)
+            supportedActions.add(OFActionType.ENQUEUE);
+        return supportedActions;
+    }
+
+    public static void writeSupportedActions(ChannelBuffer bb,
+            Set<OFActionType> supportedActions) {
+        int supportedActionsVal = 0;
+        if (supportedActions.contains(OFActionType.OUTPUT))
+            supportedActionsVal |= (1 << OFActionTypeSerializerVer10.OUTPUT_VAL);
+        if (supportedActions.contains(OFActionType.SET_VLAN_VID))
+            supportedActionsVal |= (1 << OFActionTypeSerializerVer10.SET_VLAN_VID_VAL);
+        if (supportedActions.contains(OFActionType.SET_VLAN_PCP))
+            supportedActionsVal |= (1 << OFActionTypeSerializerVer10.SET_VLAN_PCP_VAL);
+        if (supportedActions.contains(OFActionType.STRIP_VLAN))
+            supportedActionsVal |= (1 << OFActionTypeSerializerVer10.STRIP_VLAN_VAL);
+        if (supportedActions.contains(OFActionType.SET_DL_SRC))
+            supportedActionsVal |= (1 << OFActionTypeSerializerVer10.SET_DL_SRC_VAL);
+        if (supportedActions.contains(OFActionType.SET_DL_DST))
+            supportedActionsVal |= (1 << OFActionTypeSerializerVer10.SET_DL_DST_VAL);
+        if (supportedActions.contains(OFActionType.SET_NW_SRC))
+            supportedActionsVal |= (1 << OFActionTypeSerializerVer10.SET_NW_SRC_VAL);
+        if (supportedActions.contains(OFActionType.SET_NW_DST))
+            supportedActionsVal |= (1 << OFActionTypeSerializerVer10.SET_NW_DST_VAL);
+        if (supportedActions.contains(OFActionType.SET_NW_TOS))
+            supportedActionsVal |= (1 << OFActionTypeSerializerVer10.SET_NW_TOS_VAL);
+        if (supportedActions.contains(OFActionType.SET_TP_SRC))
+            supportedActionsVal |= (1 << OFActionTypeSerializerVer10.SET_TP_SRC_VAL);
+        if (supportedActions.contains(OFActionType.SET_TP_DST))
+            supportedActionsVal |= (1 << OFActionTypeSerializerVer10.SET_TP_DST_VAL);
+        if (supportedActions.contains(OFActionType.ENQUEUE))
+            supportedActionsVal |= (1 << OFActionTypeSerializerVer10.ENQUEUE_VAL);
+        bb.writeInt(supportedActionsVal);
+    }
 }
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/DatapathId.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/DatapathId.java
new file mode 100644
index 0000000..aa7191a
--- /dev/null
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/DatapathId.java
@@ -0,0 +1,63 @@
+package org.projectfloodlight.openflow.types;
+
+import org.projectfloodlight.openflow.util.HexString;
+
+/**
+ * Abstraction of a datapath ID that can be set and/or accessed as either a
+ * long value or a colon-separated string.
+ * 
+ * @author Rob Vaterlaus <rob.vaterlaus@bigswitch.com>
+ */
+public class DatapathId {
+
+    public static final DatapathId NONE = new DatapathId(0);
+
+    private final long rawValue;
+
+    private DatapathId(long rawValue) {
+        this.rawValue = rawValue;
+    }
+
+    public static DatapathId of(long rawValue) {
+        return new DatapathId(rawValue);
+    }
+
+    public static DatapathId of(String s) {
+        return new DatapathId(HexString.toLong(s));
+    }
+
+    public long getLong() {
+        return rawValue;
+    }
+
+    public U64 getUnsignedLong() {
+        return U64.of(rawValue);
+    }
+
+    @Override
+    public String toString() {
+        return HexString.toHexString(rawValue);
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + (int) (rawValue ^ (rawValue >>> 32));
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        DatapathId other = (DatapathId) obj;
+        if (rawValue != other.rawValue)
+            return false;
+        return true;
+    }
+}
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFBufferId.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFBufferId.java
index 1684d8d..f255e7b 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFBufferId.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFBufferId.java
@@ -37,14 +37,6 @@
         return Long.toString(U32.f(rawValue));
     }
 
-    public void write4Bytes(ChannelBuffer c) {
-        c.writeInt(this.rawValue);
-    }
-
-    public static OFBufferId read4Bytes(ChannelBuffer c) throws OFParseError {
-        return OFBufferId.of(c.readInt());
-    }
-
     @Override
     public int hashCode() {
         final int prime = 31;
diff --git a/java_gen/templates/of_class.java b/java_gen/templates/of_class.java
index 8014ee7..ef927fd 100644
--- a/java_gen/templates/of_class.java
+++ b/java_gen/templates/of_class.java
@@ -391,6 +391,8 @@
         //:: for prop in msg.data_members:
         //:: if prop.java_type.pub_type == 'long':
         result = prime *  (int) (${prop.name} ^ (${prop.name} >>> 32));
+        //:: elif prop.java_type.pub_type == 'boolean':
+        result = prime * result + (${prop.name} ? 1231 : 1237);
         //:: elif prop.java_type.is_primitive:
         result = prime * result + ${prop.name};
         //:: elif prop.java_type.is_array:
diff --git a/java_gen/templates/of_factory_class.java b/java_gen/templates/of_factory_class.java
index eef1e04..af0777d 100644
--- a/java_gen/templates/of_factory_class.java
+++ b/java_gen/templates/of_factory_class.java
@@ -45,6 +45,10 @@
     private final XidGenerator xidGenerator = XidGenerators.global();
     //:: #endif
 
+    public OFVersion getOFVersion() {
+        return OFVersion.OF_${factory.version.of_version};
+    }
+
     //:: for name, clazz in factory.interface.sub_factories.items():
     public ${clazz} ${name}() {
         return ${clazz}Ver${factory.version.of_version}.INSTANCE;
diff --git a/java_gen/templates/of_factory_interface.java b/java_gen/templates/of_factory_interface.java
index 3d0d5af..e10a83a 100644
--- a/java_gen/templates/of_factory_interface.java
+++ b/java_gen/templates/of_factory_interface.java
@@ -58,11 +58,12 @@
     Match matchWildcardAll();
 //:: #endif
 
+    OFVersion getOFVersion();
     OFMessageReader<${factory.base_class}> getReader();
-
     OFVersion getVersion();
 
 //:: if factory.name == 'OFOxms':
+
     public <F extends OFValueType<F>> OFOxm<F> fromValue(F value, MatchField<F> field);
     public <F extends OFValueType<F>> OFOxm<F> fromValueAndMask(F value, F mask, MatchField<F> field);
     public <F extends OFValueType<F>> OFOxm<F> fromMasked(Masked<F> masked, MatchField<F> field);