java_gen: create OFFactory
first shot at factory interface for OF Objects. Will have to be
refined later.
diff --git a/java_gen/codegen.py b/java_gen/codegen.py
index 8b8afa1..8009c02 100644
--- a/java_gen/codegen.py
+++ b/java_gen/codegen.py
@@ -54,6 +54,7 @@
gen.create_of_interfaces()
gen.create_of_classes()
gen.create_of_const_enums()
+ gen.create_of_factories()
with open('%s/README.java-lang' % os.path.dirname(__file__)) as readme_src:
out.writelines(readme_src.readlines())
@@ -101,11 +102,9 @@
def create_of_classes(self):
""" Create the OF classes with implementations for each of the interfaces and versions """
for interface in self.java_model.interfaces:
- if interface.name == "OFTableMod":
- continue
- if not loxi_utils.class_is_message(interface.c_name) and not loxi_utils.class_is_oxm(interface.c_name):
- continue
for java_class in interface.versioned_classes:
+ if not self.java_model.generate_class(java_class):
+ continue
self.render_class(clazz=java_class,
template='of_class.java', version=java_class.version, msg=java_class,
impl_class=java_class.name)
diff --git a/java_gen/java_model.py b/java_gen/java_model.py
index ccb118d..81c4a0d 100644
--- a/java_gen/java_model.py
+++ b/java_gen/java_model.py
@@ -29,24 +29,26 @@
# A lot of this stuff could/should probably be merged with the python utilities
import collections
-from collections import namedtuple, defaultdict
+from collections import namedtuple, defaultdict, OrderedDict
import logging
import os
import pdb
import re
-from generic_utils import find, memoize, OrderedSet
+from generic_utils import find, memoize, OrderedSet, OrderedDefaultDict
import of_g
from loxi_ir import *
import loxi_front_end.type_maps as type_maps
-import loxi_utils.loxi_utils as utils
+import loxi_utils.loxi_utils as loxi_utils
import py_gen.util as py_utils
+import test_data
import java_gen.java_type as java_type
class JavaModel(object):
- write_blacklist = defaultdict(lambda: set(), OFOxm=set(('typeLen',)))
- virtual_interfaces = set(['OFOxm' ])
+ enum_blacklist = set(("OFDefinitions","OFFlowWildcards"))
+ write_blacklist = defaultdict(lambda: set(), OFOxm=set(('typeLen',)), OFAction=set(('type',)))
+ virtual_interfaces = set(['OFOxm', 'OFAction' ])
@property
@memoize
@@ -56,12 +58,15 @@
@property
@memoize
def interfaces(self):
- version_map_per_class = collections.defaultdict(lambda: {})
+ version_map_per_class = collections.OrderedDict()
for raw_version, of_protocol in of_g.ir.items():
jversion = JavaOFVersion(of_protocol.wire_version)
for of_class in of_protocol.classes:
+ if not of_class.name in version_map_per_class:
+ version_map_per_class[of_class.name] = collections.OrderedDict()
+
version_map_per_class[of_class.name][jversion] = of_class
interfaces = []
@@ -93,6 +98,41 @@
except KeyError:
raise KeyError("Could not find enum with name %s" % name)
+ @property
+ @memoize
+ def of_factory(self):
+ return OFFactory(
+ package="org.openflow.protocol",
+ name="OFFactory",
+ members=self.interfaces)
+
+ def generate_class(self, clazz):
+ if clazz.interface.is_virtual:
+ return False
+ if clazz.interface.name == "OFTableMod":
+ return False
+ if loxi_utils.class_is_message(clazz.interface.c_name):
+ return True
+ if loxi_utils.class_is_oxm(clazz.interface.c_name):
+ return True
+ else:
+ return False
+
+
+class OFFactory(namedtuple("OFFactory", ("package", "name", "members"))):
+ @property
+ def factory_classes(self):
+ return [ OFFactoryClass(
+ package="org.openflow.protocol.ver{}".format(version.of_version),
+ name="OFFactoryVer{}".format(version.of_version),
+ interface=self,
+ version=version
+ ) for version in model.versions ]
+
+
+OFGenericClass = namedtuple("OFGenericClass", ("package", "name"))
+OFFactoryClass = namedtuple("OFFactory", ("package", "name", "interface", "version"))
+
model = JavaModel()
#######################################################################
@@ -139,7 +179,7 @@
self.c_name = c_name
self.version_map = version_map
self.name = java_type.name_c_to_caps_camel(c_name)
- self.builder_name = self.name + "Builder"
+ self.variable_name = self.name[2].lower() + self.name[3:]
self.constant_name = c_name.upper().replace("OF_", "")
pck_suffix, parent_interface = self.class_info()
@@ -152,15 +192,15 @@
def class_info(self):
if re.match(r'OFFlow(Add|Modify(Strict)?|Delete(Strict)?)$', self.name):
return ("", "OFFlowMod")
- elif utils.class_is_message(self.c_name):
+ elif loxi_utils.class_is_message(self.c_name):
return ("", "OFMessage")
- elif utils.class_is_action(self.c_name):
+ elif loxi_utils.class_is_action(self.c_name):
return ("action", "OFAction")
- elif utils.class_is_oxm(self.c_name):
+ elif loxi_utils.class_is_oxm(self.c_name):
return ("oxm", "OFOxm")
- elif utils.class_is_instruction(self.c_name):
+ elif loxi_utils.class_is_instruction(self.c_name):
return ("instruction", "OFInstruction")
- elif utils.class_is_meter_band(self.c_name):
+ elif loxi_utils.class_is_meter_band(self.c_name):
return ("meterband", "OFMeterBand")
else:
return ("", None)
@@ -188,10 +228,17 @@
return self.name in model.virtual_interfaces
@property
+ def is_universal(self):
+ return len(self.all_versions) == len(model.versions)
+
+ @property
@memoize
def all_versions(self):
return self.version_map.keys()
+ def has_version(self, version):
+ return version in self.version_map
+
def versioned_class(self, version):
return JavaOFClass(self, version, self.version_map[version])
@@ -230,6 +277,10 @@
return "%sVer%s" % (self.interface.name, self.version.of_version)
@property
+ def variable_name(self):
+ return self.name[3:]
+
+ @property
def length(self):
if self.is_fixed_length:
return self.min_length
@@ -409,7 +460,7 @@
if hasattr(self.member, "length"):
return self.member.length
else:
- count, base = utils.type_dec_to_count_base(self.member.type)
+ count, base = loxi_utils.type_dec_to_count_base(self.member.type)
return of_g.of_base_types[base]['bytes'] * count
@staticmethod
diff --git a/java_gen/templates/_imports.java b/java_gen/templates/_imports.java
index 7700546..50ba198 100644
--- a/java_gen/templates/_imports.java
+++ b/java_gen/templates/_imports.java
@@ -1,7 +1,9 @@
+import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.openflow.protocol.*;
import org.openflow.protocol.action.*;
+import org.openflow.protocol.meterband.*;
import org.openflow.protocol.instruction.*;
import org.openflow.protocol.match.*;
import org.openflow.protocol.oxm.*;
@@ -10,3 +12,4 @@
import org.openflow.util.*;
import org.openflow.exceptions.*;
import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
diff --git a/java_gen/templates/_singleton.java b/java_gen/templates/_singleton.java
new file mode 100644
index 0000000..8ea9f01
--- /dev/null
+++ b/java_gen/templates/_singleton.java
@@ -0,0 +1,10 @@
+
+ private ${msg.name}() {}
+
+ private final static class Holder {
+ private static final ${msg.name} INSTANCE = new ${msg.name}();
+ }
+
+ public static ${msg.name} getInstance() {
+ return Holder.INSTANCE;
+ }
diff --git a/java_gen/templates/of_class.java b/java_gen/templates/of_class.java
index 772858a..a5487d2 100644
--- a/java_gen/templates/of_class.java
+++ b/java_gen/templates/of_class.java
@@ -65,11 +65,11 @@
//:: include("_field_accessors.java", msg=msg, generate_setters=False, builder=False)
- public ${msg.name}.Builder createBuilder() {
- return new BuilderImplWithParent(this);
+ public ${msg.interface.name}.Builder createBuilder() {
+ return new BuilderWithParent(this);
}
- static class BuilderImplWithParent implements ${msg.interface.name}.Builder {
+ static class BuilderWithParent implements ${msg.interface.name}.Builder {
final ${impl_class} parentMessage;
// OF message fields
@@ -78,7 +78,7 @@
private ${prop.java_type.public_type} ${prop.name};
//:: #endfor
- BuilderImplWithParent(${impl_class} parentMessage) {
+ BuilderWithParent(${impl_class} parentMessage) {
this.parentMessage = parentMessage;
}
@@ -94,7 +94,7 @@
}
}
- static class BuilderImpl implements ${msg.interface.name}.Builder {
+ static class Builder implements ${msg.interface.name}.Builder {
// OF message fields
//:: for prop in msg.data_members:
private boolean ${prop.name}Set;
diff --git a/java_gen/templates/of_factories.java b/java_gen/templates/of_factories.java
new file mode 100644
index 0000000..cce134d
--- /dev/null
+++ b/java_gen/templates/of_factories.java
@@ -0,0 +1,49 @@
+//:: # 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 org.openflow.protocol;
+
+//:: include("_imports.java")
+
+public final class OFFactories {
+ public static OFFactory getFactory(OFVersion version) {
+ switch(version) {
+ //:: for v in versions:
+ case ${v.constant_version}:
+ return org.openflow.protocol.ver${v.of_version}.OFFactoryVer${v.of_version}.getInstance();
+ //:: #endfor
+ default:
+ throw new IllegalArgumentException("Unknown version: "+version);
+ }
+ }
+}
diff --git a/java_gen/templates/of_factory_class.java b/java_gen/templates/of_factory_class.java
new file mode 100644
index 0000000..8620787
--- /dev/null
+++ b/java_gen/templates/of_factory_class.java
@@ -0,0 +1,56 @@
+//:: # 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 ${factory.package};
+
+//:: include("_imports.java")
+
+public class ${factory.name} implements ${factory.interface.name} {
+//:: for i in factory.interface.members:
+ //:: if i.is_virtual:
+ //:: continue
+ //:: #endif
+
+//:: if i.has_version(factory.version) and model.generate_class(i.versioned_class(factory.version)):
+ public ${i.name}.Builder create${i.name[2:]}Builder() {
+ return new ${i.versioned_class(factory.version).name}.Builder();
+ }
+//:: else:
+ public ${i.name}.Builder create${i.name[2:]}Builder() throws UnsupportedOperationException {
+ throw new UnsupportedOperationException("${i.name} not supported in version ${factory.version}");
+ }
+//:: #endif
+//:: #endfor
+
+ //:: include("_singleton.java", msg=factory)
+}
diff --git a/java_gen/templates/of_factory_interface.java b/java_gen/templates/of_factory_interface.java
new file mode 100644
index 0000000..467504e
--- /dev/null
+++ b/java_gen/templates/of_factory_interface.java
@@ -0,0 +1,45 @@
+//:: # 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 org.openflow.protocol;
+
+//:: include("_imports.java")
+
+public interface ${factory.name} {
+//:: for i in factory.members:
+ //:: if i.is_virtual:
+ //:: continue
+ //:: #endif
+ ${i.name}.Builder create${i.name[2:]}Builder()${ "" if i.is_universal else " throws UnsupportedOperationException"};
+//:: #endfor
+}