java_gen: support enum types, sets of enum types
diff --git a/java_gen/codegen.py b/java_gen/codegen.py
index 1b9f445..b84556c 100644
--- a/java_gen/codegen.py
+++ b/java_gen/codegen.py
@@ -101,7 +101,11 @@
for version in enum.versions:
clazz = java_model.OFGenericClass(package="org.projectfloodlight.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)
+
+ if enum.is_bitmask:
+ self.render_class(clazz=clazz, template="const_set_serializer.java", enum=enum, version=version)
+ else:
+ 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"""
diff --git a/java_gen/java_model.py b/java_gen/java_model.py
index a882475..bf74d82 100644
--- a/java_gen/java_model.py
+++ b/java_gen/java_model.py
@@ -736,6 +736,8 @@
return self.enum_value
elif java_type == "OFOxmList":
return "OFOxmList.EMPTY"
+ elif re.match(r'Set.*', java_type):
+ return "Collections.emptySet()"
elif re.match(r'List.*', java_type):
return "Collections.emptyList()"
elif java_type == "boolean":
@@ -1010,6 +1012,11 @@
return java_type.u8
@property
+ @memoize
+ def is_bitmask(self):
+ return any(ir_enum.is_bitmask for ir_enum in self.version_enums.values())
+
+ @property
def versions(self):
return self.version_enums.keys()
diff --git a/java_gen/java_type.py b/java_gen/java_type.py
index 2369d01..0620df0 100644
--- a/java_gen/java_type.py
+++ b/java_gen/java_type.py
@@ -1,10 +1,13 @@
import errno
-import loxi_utils.loxi_utils as loxi_utils
import os
import re
import subprocess
import time
+from generic_utils import memoize
+import loxi_utils.loxi_utils as loxi_utils
+import of_g
+
def erase_type_annotation(class_name):
m=re.match(r'(.*)<.*>', class_name)
if m:
@@ -23,7 +26,9 @@
def name_c_to_caps_camel(name):
""" 'of_stats_reply' to 'OFStatsReply' """
camel = name_c_to_camel(name.title())
- if camel.startswith('Of'):
+ if camel.startswith('Ofp'):
+ return camel.replace('Ofp','OF',1)
+ elif camel.startswith('Of'):
return camel.replace('Of','OF',1)
else:
return camel
@@ -394,6 +399,21 @@
'of_oxm_mpls_tc_masked' : { 'value' : u8obj, 'value_mask' : u8obj },
}
+
+@memoize
+def enum_java_types():
+ enum_types = {}
+
+ for protocol in of_g.ir.values():
+ for enum in protocol.enums:
+ java_name = name_c_to_caps_camel(re.sub(r'_t$', "", enum.name))
+ java_type = java_name if not enum.is_bitmask else "Set<{}>".format(java_name)
+ enum_types[enum.name] = \
+ JType(java_type)\
+ .op(read = "{}SerializerVer$version.readFrom(bb)".format(java_name),
+ write ="{}SerializerVer$version.writeTo(bb, $name)".format(java_name))
+ return enum_types
+
def make_match_field_jtype(sub_type_name="?"):
return JType("MatchField<{}>".format(sub_type_name))
@@ -415,6 +435,7 @@
write='ChannelUtils.writeList(bb, $name)')
+
#### main entry point for conversion of LOXI types (c_types) Java types.
# FIXME: This badly needs a refactoring
@@ -437,6 +458,8 @@
return default_mtype_to_jtype_convert_map[c_type]
elif re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type):
return make_standard_list_jtype(c_type)
+ elif c_type in enum_java_types():
+ return enum_java_types()[c_type]
else:
print "WARN: Couldn't find java type conversion for '%s' in %s:%s" % (c_type, obj_name, field_name)
jtype = name_c_to_caps_camel(re.sub(r'_t$', "", c_type))
diff --git a/java_gen/templates/_imports.java b/java_gen/templates/_imports.java
index 669db00..ef683e6 100644
--- a/java_gen/templates/_imports.java
+++ b/java_gen/templates/_imports.java
@@ -1,6 +1,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import java.util.Set;
import java.util.Map;
import org.projectfloodlight.openflow.protocol.*;
import org.projectfloodlight.openflow.protocol.action.*;
diff --git a/java_gen/templates/const_set_serializer.java b/java_gen/templates/const_set_serializer.java
new file mode 100644
index 0000000..641b0c8
--- /dev/null
+++ b/java_gen/templates/const_set_serializer.java
@@ -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.java')
+
+//:: include('_autogen.java')
+
+package ${package};
+
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.Set;
+
+import org.projectfloodlight.openflow.types.*;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.projectfloodlight.openflow.exceptions.OFParseError;
+import org.projectfloodlight.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 Set<${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, Set<${enum.name}> set) {
+ ${wire_type.write_op(version=version, name="toWireValue(set)")};
+ }
+
+ public static Set<${enum.name}> ofWireValue(${int_wire_type} val) {
+ EnumSet<${enum.name}> set = EnumSet.noneOf(${enum.name}.class);
+
+ //:: for entry, _ in entries:
+ if((val & ${entry.name}_VAL) != 0)
+ set.add(${enum.name}.${entry.name});
+ //:: #endfor
+ return Collections.unmodifiableSet(set);
+ }
+
+ public static ${int_wire_type} toWireValue(Set<${enum.name}> set) {
+ ${int_wire_type} wireValue = 0;
+
+ for(${enum.name} e: set) {
+ switch(e) {
+ //:: for entry, _ in entries:
+ case ${entry.name}:
+ wireValue |= ${entry.name}_VAL;
+ //:: #endfor
+ default:
+ throw new IllegalArgumentException("Illegal enum value for type ${enum.name} in version ${version}: " + e);
+ }
+ }
+ return wireValue;
+ }
+
+}