java_gen: new enum serializers
diff --git a/java_gen/codegen.py b/java_gen/codegen.py
index 8009c02..258b81c 100644
--- a/java_gen/codegen.py
+++ b/java_gen/codegen.py
@@ -91,6 +91,10 @@
self.render_class(clazz=enum,
template='const.java', enum=enum, all_versions=self.java_model.versions)
+ for version in enum.versions:
+ clazz = java_model.OFGenericClass(package="org.openflow.protocol.ver{}".format(version.of_version), name="{}SerializerVer{}".format(enum.name, version.of_version))
+ self.render_class(clazz=clazz, template="const_serializer.java", enum=enum, version=version)
+
def create_of_interfaces(self):
""" Create the base interfaces for of classes"""
for interface in self.java_model.interfaces:
diff --git a/java_gen/java_model.py b/java_gen/java_model.py
index 81c4a0d..6f5cfaa 100644
--- a/java_gen/java_model.py
+++ b/java_gen/java_model.py
@@ -78,17 +78,18 @@
@property
@memoize
def enums(self):
- enum_entry_version_value_map = collections.defaultdict(lambda: collections.defaultdict(lambda: collections.OrderedDict()))
+ name_version_enum_map = OrderedDefaultDict(lambda: OrderedDict())
for version in self.versions:
of_protocol = of_g.ir[version.int_version]
for enum in of_protocol.enums:
- for entry_name, entry_value in enum.values:
- enum_entry_version_value_map[enum.name][entry_name][version] = entry_value
+ name_version_enum_map[enum.name][version] = enum
- enums = [ JavaEnum(name, entry_version_value_map) for name, entry_version_value_map
- in enum_entry_version_value_map.items() ]
+ enums = [ JavaEnum(name, version_enum_map) for name, version_enum_map,
+ in name_version_enum_map.items() ]
+ # inelegant - need java name here
+ enums = [ enum for enum in enums if enum.name not in self.enum_blacklist ]
return enums
@memoize
@@ -531,17 +532,37 @@
#######################################################################
class JavaEnum(object):
- def __init__(self, c_name, entry_version_value_map):
+ def __init__(self, c_name, version_enum_map):
self.c_name = c_name
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 ""
+ self.version_enums = version_enum_map
+
+ entry_name_version_value_map = OrderedDefaultDict(lambda: OrderedDict())
+ for version, ir_enum in version_enum_map.items():
+ for ir_entry in ir_enum.entries:
+ if "virtual" in ir_entry.params:
+ continue
+ entry_name_version_value_map[ir_entry.name][version] = ir_entry.value
+
self.entries = [ JavaEnumEntry(self, name, version_value_map)
- for (name, version_value_map) in entry_version_value_map.items() ]
+ for (name, version_value_map) in entry_name_version_value_map.items() ]
self.package = "org.openflow.protocol"
+ def wire_type(self, version):
+ ir_enum = self.version_enums[version]
+ if "wire_type" in ir_enum.params:
+ return java_type.convert_enum_wire_type_to_jtype(ir_enum.params["wire_type"])
+ else:
+ return java_type.u8
+
+ @property
+ def versions(self):
+ return self.version_enums.keys()
+
@memoize
def entry_by_name(self, name):
try:
@@ -566,11 +587,18 @@
# values: Map JavaVersion->Value
class JavaEnumEntry(object):
def __init__(self, enum, name, values):
+ self.enum = enum
self.name = enum.name_prefix + "_".join(name.split("_")[1:]).upper()
self.values = values
+ def has_value(self, version):
+ return version in self.values
+
def value(self, version):
- res = self.version_value_map[version]
+ return self.values[version]
+
+ def format_value(self, version):
+ res = self.enum.wire_type(version).format_value(self.values[version])
return res
def all_values(self, versions, not_present=None):
diff --git a/java_gen/java_type.py b/java_gen/java_type.py
index 8d0d7bb..2d8a547 100644
--- a/java_gen/java_type.py
+++ b/java_gen/java_type.py
@@ -54,6 +54,21 @@
self.ops[version] = VersionOp(version, read, write)
return self
+ def cast(self, min):
+ """ declares that the value has to be cast to itself for values >= min.
+ This is to deal with Java signedness """
+ def format_cast_value(value):
+ if value >= min:
+ return "(%s) 0x%x" % (self.pub_type, value)
+ else:
+ return "0x%x" % (value)
+
+ self.format_value = format_cast_value
+ return self
+
+ def format_value(self, value):
+ return value
+
@property
def public_type(self):
""" return the public type """
@@ -185,6 +200,8 @@
ipv6 = JType("IPv6") \
.op(read="IPv6.read16Bytes(bb)", \
write="$name.write16Bytes(bb)")
+packetin_reason = JType("OFPacketInReason")\
+ .op(read="OFPacketInReasonSerializerVer$version.readFrom(bb)", write="OFPacketInReasonSerializerVer$version.writeTo(bb, $name)")
default_mtype_to_jtype_convert_map = {
'uint8_t' : u8,
@@ -222,12 +239,23 @@
## This is where we drop in special case handling for certain types
exceptions = {
- 'OFPacketIn': {
- 'data' : octets
+ 'of_packet_in': {
+ 'data' : octets,
+ 'reason': packetin_reason
},
}
+enum_wire_types = {
+ "uint8_t": JType("byte").op(read="bb.readByte()", write="bb.writeByte($name)").cast(min=1<<7),
+ "uint16_t": JType("short").op(read="bb.readShort()", write="bb.writeShort($name)").cast(min=1<<15),
+ "uint32_t": JType("int").op(read="bb.readInt()", write="bb.writeInt($name)").cast(min=1<<31),
+ "uint64_t": JType("long").op(read="bb.readLong()", write="bb.writeLong($name)").cast(min=1<<31)
+}
+
+def convert_enum_wire_type_to_jtype(wire_type):
+ return enum_wire_types[wire_type]
+
def make_standard_list_jtype(c_type):
m = re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type)
if not m:
diff --git a/java_gen/templates/const.java b/java_gen/templates/const.java
index 7870529..11c09ab 100644
--- a/java_gen/templates/const.java
+++ b/java_gen/templates/const.java
@@ -37,16 +37,7 @@
public enum ${class_name} {
//:: for i, entry in enumerate(enum.entries):
-//:: values = [ ("0x%x" % val) if val is not None else "-1" for val in entry.all_values(all_versions) ]
- ${entry.name}(new int[] { ${ ", ".join( values) } } )${ ", " if i < len(enum.entries)-1 else ";" }
+ ${entry.name}${ ", " if i < len(enum.entries)-1 else ";" }
//:: #endfor
- private final int[] wireValues;
- ${class_name}(int[] wireValues) {
- this.wireValues = wireValues;
- }
-
- public int getWireValue(OFVersion version) {
- return this.wireValues[version.getWireVersion()];
- }
}
diff --git a/java_gen/templates/const_serializer.java b/java_gen/templates/const_serializer.java
new file mode 100644
index 0000000..4e21070
--- /dev/null
+++ b/java_gen/templates/const_serializer.java
@@ -0,0 +1,85 @@
+//:: # 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.java')
+
+//:: include('_autogen.java')
+
+package ${package};
+
+import org.openflow.types.*;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.openflow.exceptions.OFParseError;
+import org.openflow.protocol.OFVersion;
+import ${enum.package}.${enum.name};
+
+public class ${class_name} {
+ //:: wire_type = enum.wire_type(version)
+ //:: int_wire_type = enum.wire_type(version).pub_type
+ //:: entries = sorted([ (entry, entry.value(version)) for entry in enum.entries if entry.has_value(version) ], lambda (_,va), (_2,vb): va.__cmp__(vb))
+
+ //:: for entry, _ in entries:
+ public final static ${int_wire_type} ${entry.name}_VAL = ${entry.format_value(version)};
+ //:: #endfor
+
+ public static ${enum.name} readFrom(ChannelBuffer bb) throws OFParseError {
+ try {
+ return ofWireValue(${wire_type.read_op(version)});
+ } catch (IllegalArgumentException e) {
+ throw new OFParseError(e);
+ }
+ }
+
+ public static void writeTo(ChannelBuffer bb, ${enum.name} e) {
+ ${wire_type.write_op(version=version, name="toWireValue(e)")};
+ }
+
+ public static ${enum.name} ofWireValue(${int_wire_type} val) {
+ switch(val) {
+ //:: for entry, _ in entries:
+ case ${entry.name}_VAL:
+ return ${enum.name}.${entry.name};
+ //:: #endfor
+ default:
+ throw new IllegalArgumentException("Illegal wire value for type ${enum.name} in version ${version}: " + val);
+ }
+ }
+
+ public static ${int_wire_type} toWireValue(${enum.name} e) {
+ switch(e) {
+ //:: for entry, _ in entries:
+ case ${entry.name}:
+ return ${entry.name}_VAL;
+ //:: #endfor
+ default:
+ throw new IllegalArgumentException("Illegal enum value for type ${enum.name} in version ${version}: " + e);
+ }
+ }
+
+}