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

Conflicts:
	java_gen/java_model.py
	java_gen/java_type.py
diff --git a/java_gen/codegen.py b/java_gen/codegen.py
index b84556c..d94d78a 100644
--- a/java_gen/codegen.py
+++ b/java_gen/codegen.py
@@ -50,7 +50,7 @@
         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 282cf98..f2a1c01 100644
--- a/java_gen/java_model.py
+++ b/java_gen/java_model.py
@@ -46,10 +46,101 @@
 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})
-    enum_blacklist = set(("OFDefinitions",))
+    enum_blacklist = set(("OFDefinitions", "OFPortNo",))
     # registry for enum *entry* that should not be generated
     # map: ${java_enum_name} -> set(${java_entry_entry_name})
     enum_entry_blacklist = defaultdict(lambda: set(), OFFlowWildcards=set([ "NW_DST_BITS", "NW_SRC_BITS", "NW_SRC_SHIFT", "NW_DST_SHIFT" ]))
@@ -202,6 +293,7 @@
     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
diff --git a/java_gen/java_type.py b/java_gen/java_type.py
index ea1ce2a..87ce9d6 100644
--- a/java_gen/java_type.py
+++ b/java_gen/java_type.py
@@ -381,7 +381,9 @@
         .op(read='bb.readInt()',
             write='bb.writeInt($name)')
 
+
 port_speed = JType("PortSpeed")
+error_type = JType("OFErrorType")
 boolean = JType("boolean").op(default="false")
 
 generic_t = JType("T")
@@ -539,6 +541,9 @@
     elif field_name == "version" and c_type == "uint8_t":
         return JType("OFVersion", 'byte') \
             .op(read='bb.readByte()', write='bb.writeByte($name)')
+    elif field_name == "buffer_id" and c_type == "uint32_t":
+        return JType("OFBufferId") \
+            .op(read="OFBufferId.of(bb.readInt())", write="bb.writeInt($name.getInt())", default="OFBufferId.NO_BUFFER")
     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/OFBufferId.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFBufferId.java
new file mode 100644
index 0000000..99f3e7f
--- /dev/null
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/OFBufferId.java
@@ -0,0 +1,67 @@
+package org.projectfloodlight.openflow.types;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.projectfloodlight.openflow.annotations.Immutable;
+import org.projectfloodlight.openflow.exceptions.OFParseError;
+
+/**
+ * Abstraction of a buffer id in OpenFlow. Immutable.
+ *
+ * @author Rob Vaterlaus <rob.vaterlaus@bigswitch.com>
+ */
+@Immutable
+public class OFBufferId {
+
+    public static final OFBufferId NO_BUFFER = new OFBufferId(0xFFFFFFFF);
+
+    private final int rawValue;
+
+    private OFBufferId(int rawValue) {
+        this.rawValue = rawValue;
+    }
+
+    public static OFBufferId of(final int rawValue) {
+        if (rawValue == NO_BUFFER.getInt())
+            return NO_BUFFER;
+        return new OFBufferId(rawValue);
+    }
+
+    public int getInt() {
+        return rawValue;
+    }
+
+    @Override
+    public String toString() {
+        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;
+        int result = 1;
+        result = prime * result + rawValue;
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        OFBufferId other = (OFBufferId) obj;
+        if (rawValue != other.rawValue)
+            return false;
+        return true;
+    }
+}
diff --git a/java_gen/templates/const.java b/java_gen/templates/const.java
index 2cd0101..413c946 100644
--- a/java_gen/templates/const.java
+++ b/java_gen/templates/const.java
@@ -36,7 +36,6 @@
 //:: include("_imports.java", msg=enum)
 
 public enum ${class_name} {
-
 //:: for i, entry in enumerate(enum.entries):
 //::    if enum.metadata.properties:
 //::        params = "({})".format(", ".join(entry.constructor_params))
diff --git a/openflow_input/nicira_role b/openflow_input/nicira_role
index 1ed0b3d..f451d42 100644
--- a/openflow_input/nicira_role
+++ b/openflow_input/nicira_role
@@ -40,7 +40,7 @@
     uint32_t xid;
     uint32_t experimenter == 0x2320;
     uint32_t subtype == 10;
-    uint32_t role;         // 0 other, 1 master, 2 slave
+    enum ofp_nicira_controller_role role;         // 0 other, 1 master, 2 slave
 };
 
 struct of_nicira_controller_role_reply : of_nicira_header {
@@ -50,5 +50,5 @@
     uint32_t xid;
     uint32_t experimenter == 0x2320;
     uint32_t subtype == 11;
-    uint32_t role;         // 0 other, 1 master, 2 slave
+    enum ofp_nicira_controller_role role;         // 0 other, 1 master, 2 slave
 };
diff --git a/openflow_input/standard-1.0 b/openflow_input/standard-1.0
index 806a6a9..1a23ce4 100644
--- a/openflow_input/standard-1.0
+++ b/openflow_input/standard-1.0
@@ -383,12 +383,12 @@
     of_port_no_t port_no;
     of_mac_addr_t hw_addr;
     of_port_name_t name;
-    uint32_t config;
-    uint32_t state;
-    uint32_t curr;
-    uint32_t advertised;
-    uint32_t supported;
-    uint32_t peer;
+    enum ofp_port_config config;
+    enum ofp_port_state state;
+    enum ofp_port_features curr;
+    enum ofp_port_features advertised;
+    enum ofp_port_features supported;
+    enum ofp_port_features peer;
 };
 
 struct of_features_request : of_header {
@@ -417,7 +417,7 @@
     uint8_t type == 12;
     uint16_t length;
     uint32_t xid;
-    uint8_t reason;
+    enum ofp_port_reason reason;
     pad(7);
     of_port_desc_t desc;
 };
diff --git a/openflow_input/standard-1.1 b/openflow_input/standard-1.1
index 9acbacf..937ac52 100644
--- a/openflow_input/standard-1.1
+++ b/openflow_input/standard-1.1
@@ -503,12 +503,12 @@
     of_mac_addr_t hw_addr;
     pad(2);
     of_port_name_t name;
-    uint32_t config;
-    uint32_t state;
-    uint32_t curr;
-    uint32_t advertised;
-    uint32_t supported;
-    uint32_t peer;
+    enum ofp_port_config config;
+    enum ofp_port_state state;
+    enum ofp_port_features curr;
+    enum ofp_port_features advertised;
+    enum ofp_port_features supported;
+    enum ofp_port_features peer;
     uint32_t curr_speed;
     uint32_t max_speed;
 };
@@ -539,7 +539,7 @@
     uint8_t type == 12;
     uint16_t length;
     uint32_t xid;
-    uint8_t reason;
+    enum ofp_port_reason reason;
     pad(7);
     of_port_desc_t desc;
 };
diff --git a/openflow_input/standard-1.2 b/openflow_input/standard-1.2
index 9752b7d..e8f95ee 100644
--- a/openflow_input/standard-1.2
+++ b/openflow_input/standard-1.2
@@ -543,12 +543,12 @@
     of_mac_addr_t hw_addr;
     pad(2);
     of_port_name_t name;
-    uint32_t config;
-    uint32_t state;
-    uint32_t curr;
-    uint32_t advertised;
-    uint32_t supported;
-    uint32_t peer;
+    enum ofp_port_config config;
+    enum ofp_port_state state;
+    enum ofp_port_features curr;
+    enum ofp_port_features advertised;
+    enum ofp_port_features supported;
+    enum ofp_port_features peer;
     uint32_t curr_speed;
     uint32_t max_speed;
 };
@@ -579,7 +579,7 @@
     uint8_t type == 12;
     uint16_t length;
     uint32_t xid;
-    uint8_t reason;
+    enum ofp_port_reason reason;
     pad(7);
     of_port_desc_t desc;
 };
diff --git a/openflow_input/standard-1.3 b/openflow_input/standard-1.3
index afe371e..24129aa 100644
--- a/openflow_input/standard-1.3
+++ b/openflow_input/standard-1.3
@@ -668,12 +668,12 @@
     of_mac_addr_t hw_addr;
     pad(2);
     of_port_name_t name;
-    uint32_t config;
-    uint32_t state;
-    uint32_t curr;
-    uint32_t advertised;
-    uint32_t supported;
-    uint32_t peer;
+    enum ofp_port_config config;
+    enum ofp_port_state state;
+    enum ofp_port_features curr;
+    enum ofp_port_features advertised;
+    enum ofp_port_features supported;
+    enum ofp_port_features peer;
     uint32_t curr_speed;
     uint32_t max_speed;
 };
@@ -704,7 +704,7 @@
     uint8_t type == 12;
     uint16_t length;
     uint32_t xid;
-    uint8_t reason;
+    enum ofp_port_reason reason;
     pad(7);
     of_port_desc_t desc;
 };
diff --git a/test_data/of10/flow_add.data b/test_data/of10/flow_add.data
index d1a4dd3..127e1fc 100644
--- a/test_data/of10/flow_add.data
+++ b/test_data/of10/flow_add.data
@@ -147,6 +147,7 @@
     )
     .setIdleTimeout(5)
     .setFlags(Sets.immutableEnumSet(OFFlowModFlags.CHECK_OVERLAP))
+    .setBufferId(OFBufferId.of(0))
     .setActions(
         ImmutableList.of(
             factory.actions().output(OFPort.FLOOD, 0),
diff --git a/test_data/of10/packet_in.data b/test_data/of10/packet_in.data
index bd89cf8..a8de82c 100644
--- a/test_data/of10/packet_in.data
+++ b/test_data/of10/packet_in.data
@@ -30,7 +30,7 @@
 -- java
 builder
    .setXid(0x12345678)
-   .setBufferId(0xabcdef01L)
+   .setBufferId(OFBufferId.of(0xabcdef01))
    .setTotalLen(9)
    .setInPort(OFPort.LOCAL)
    .setReason(OFPacketInReason.ACTION)
diff --git a/test_data/of13/flow_add.data b/test_data/of13/flow_add.data
index 692a7b9..f86f4ae 100644
--- a/test_data/of13/flow_add.data
+++ b/test_data/of13/flow_add.data
@@ -80,7 +80,7 @@
     .setIdleTimeout(5)
     .setHardTimeout(10)
     .setPriority(6000)
-    .setBufferId(50)
+    .setBufferId(OFBufferId.of(50))
     .setOutPort(OFPort.of(6))
     .setOutGroup(8)
     .setFlags(ImmutableSet.<OFFlowModFlags>of())
diff --git a/test_data/of13/flow_delete.data b/test_data/of13/flow_delete.data
index 3762fab..6dee43b 100644
--- a/test_data/of13/flow_delete.data
+++ b/test_data/of13/flow_delete.data
@@ -80,7 +80,7 @@
     .setIdleTimeout(5)
     .setHardTimeout(10)
     .setPriority(6000)
-    .setBufferId(50)
+    .setBufferId(OFBufferId.of(50))
     .setOutPort(OFPort.of(6))
     .setOutGroup(8)
     .setFlags(ImmutableSet.<OFFlowModFlags>of())
diff --git a/test_data/of13/flow_delete_strict.data b/test_data/of13/flow_delete_strict.data
index 7a0347c..5da11a2 100644
--- a/test_data/of13/flow_delete_strict.data
+++ b/test_data/of13/flow_delete_strict.data
@@ -80,7 +80,7 @@
     .setIdleTimeout(5)
     .setHardTimeout(10)
     .setPriority(6000)
-    .setBufferId(50)
+    .setBufferId(OFBufferId.of(50))
     .setOutPort(OFPort.of(6))
     .setOutGroup(8)
     .setFlags(ImmutableSet.<OFFlowModFlags>of())
diff --git a/test_data/of13/flow_modify.data b/test_data/of13/flow_modify.data
index 620af3d..7f7f7a6 100644
--- a/test_data/of13/flow_modify.data
+++ b/test_data/of13/flow_modify.data
@@ -80,7 +80,7 @@
     .setIdleTimeout(5)
     .setHardTimeout(10)
     .setPriority(6000)
-    .setBufferId(50)
+    .setBufferId(OFBufferId.of(50))
     .setOutPort(OFPort.of(6))
     .setOutGroup(8)
     .setFlags(ImmutableSet.<OFFlowModFlags>of())
diff --git a/test_data/of13/flow_modify_strict.data b/test_data/of13/flow_modify_strict.data
index 79814e3..f288d5f 100644
--- a/test_data/of13/flow_modify_strict.data
+++ b/test_data/of13/flow_modify_strict.data
@@ -80,7 +80,7 @@
     .setIdleTimeout(5)
     .setHardTimeout(10)
     .setPriority(6000)
-    .setBufferId(50)
+    .setBufferId(OFBufferId.of(50))
     .setOutPort(OFPort.of(6))
     .setOutGroup(8)
     .setFlags(ImmutableSet.<OFFlowModFlags>of())
diff --git a/test_data/of13/packet_in.data b/test_data/of13/packet_in.data
index 1ec0422..b32bbee 100644
--- a/test_data/of13/packet_in.data
+++ b/test_data/of13/packet_in.data
@@ -33,7 +33,7 @@
 -- java
 builder
    .setXid(0x12345678)
-   .setBufferId(100)
+   .setBufferId(OFBufferId.of(100))
    .setTotalLen(17000)
    .setReason(OFPacketInReason.ACTION)
    .setTableId((byte) 20)