java_loxi: big refactoring and clean-up
diff --git a/java_gen/templates/_field_accessors.java b/java_gen/templates/_field_accessors.java
new file mode 100644
index 0000000..e8afe47
--- /dev/null
+++ b/java_gen/templates/_field_accessors.java
@@ -0,0 +1,33 @@
+//:: for prop in msg.interface.members:
+ @Override
+ public ${prop.java_type.public_type} get${prop.title_name}()${ "" if prop in msg.members else "throws UnsupportedOperationException"} {
+//:: if prop in msg.members:
+//:: version_prop = msg.get_member(prop.name)
+//:: if version_prop.is_fixed_value:
+ return ${version_prop.enum_value};
+//:: elif version_prop.is_length_value:
+ // FIXME: Hacky and inperformant way to determine a message length. Should be replaced with something better
+ ChannelBuffer c = new LengthCountingPseudoChannelBuffer();
+ WRITER.write(c, ${ "this" if not builder else "({0}) this.getMessage()".format(msg.name) });
+ return c.writerIndex();
+//:: else:
+ return ${version_prop.name};
+//:: #endif
+//:: else:
+ throw new UnsupportedOperationException("Property ${prop.name} not supported in version #{version}");
+//:: #endif
+ }
+
+//:: if generate_setters and prop.is_writeable:
+ @Override
+ public ${msg.interface.name}.Builder set${prop.title_name}(${prop.java_type.public_type} ${prop.name})${ "" if prop in msg.members else " throws UnsupportedOperationException"} {
+//:: if prop in msg.members:
+ this.${prop.name} = ${prop.name};
+ this.${prop.name}Set = true;
+ return this;
+//:: else:
+ throw new UnsupportedOperationException("Property ${prop.name} not supported in version #{version}");
+//:: #endif
+ }
+//:: #endif
+//:: #endfor
diff --git a/java_gen/templates/_imports.java b/java_gen/templates/_imports.java
new file mode 100644
index 0000000..7700546
--- /dev/null
+++ b/java_gen/templates/_imports.java
@@ -0,0 +1,12 @@
+import java.util.Collections;
+import java.util.List;
+import org.openflow.protocol.*;
+import org.openflow.protocol.action.*;
+import org.openflow.protocol.instruction.*;
+import org.openflow.protocol.match.*;
+import org.openflow.protocol.oxm.*;
+import org.openflow.types.*;
+import org.openflow.types.*;
+import org.openflow.util.*;
+import org.openflow.exceptions.*;
+import org.jboss.netty.buffer.ChannelBuffer;
diff --git a/java_gen/templates/of_type.java b/java_gen/templates/const.java
similarity index 74%
rename from java_gen/templates/of_type.java
rename to java_gen/templates/const.java
index 951a648..7870529 100644
--- a/java_gen/templates/of_type.java
+++ b/java_gen/templates/const.java
@@ -31,21 +31,22 @@
//:: include('_autogen.java')
-package org.openflow.types;
+package ${package};
import org.openflow.protocol.OFVersion;
-public enum OFType {
-//:: for i, msg in enumerate(all_messages):
- ${msg.constant_name}(new byte[] { ${ ", ".join( [str(msg.wire_type(version)) for version in all_versions ]) } } )${ ", " if i < len(all_messages)-1 else ";" }
+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 ";" }
//:: #endfor
- byte[] wireTypes;
- OFType(byte[] wireTypes) {
- this.wireTypes = wireTypes;
+ private final int[] wireValues;
+ ${class_name}(int[] wireValues) {
+ this.wireValues = wireValues;
}
- public byte getWireType(OFVersion version) {
- return this.wireTypes[version.getWireVersion()];
+ public int getWireValue(OFVersion version) {
+ return this.wireValues[version.getWireVersion()];
}
}
diff --git a/java_gen/templates/message_class.java b/java_gen/templates/message_class.java
deleted file mode 100644
index 2b4801c..0000000
--- a/java_gen/templates/message_class.java
+++ /dev/null
@@ -1,258 +0,0 @@
-//:: # 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;
-import java.util.Collections;
-import java.util.List;
-import org.openflow.protocol.actions.OFAction;
-import org.openflow.protocol.instructions.OFInstruction;
-import org.openflow.protocol.match.*;
-import org.openflow.types.*;
-import org.openflow.types.*;
-import org.openflow.util.*;
-import org.openflow.exceptions.*;
-import org.jboss.netty.buffer.ChannelBuffer;
-
-class ${impl_class} implements ${msg.interface_name} {
-//:: if msg.is_fixed_length(version):
- private static final int LENGTH = ${msg.min_length(version) };
-//:: else:
- private static final int MINIMUM_LENGTH = ${msg.min_length(version) };
-//:: #endif
-
-//:: for prop in msg.properties_for_version(version):
- private final static ${prop.java_type.public_type} ${prop.default_name} = ${prop.default_value};
-//:: #end
- private boolean xidSet;
- private final int xid;
-
- // OF message fields
-//:: for prop in msg.properties_for_version(version):
- private final ${prop.java_type.public_type} ${prop.name};
-//:: #endfor
-
- // Constructor
- ${impl_class}(${
- ", ".join(["int xid" ] + [ "%s %s" %(prop.java_type.public_type, prop.name) for prop in msg.properties_for_version(version) ])}) {
- this.xidSet = true;
- this.xid = xid;
-//:: for prop in msg.properties_for_version(version):
- this.${prop.name} = ${prop.name};
-//:: #endfor
- }
-
- ${impl_class}(${
- ", ".join("%s %s" %(prop.java_type.public_type, prop.name) for prop in msg.properties_for_version(version)) }) {
- this.xidSet = false;
- this.xid = 0;
-//:: for prop in msg.properties_for_version(version):
- this.${prop.name} = ${prop.name};
-//:: #endfor
- }
-
- @Override
- public int getXid() {
- return xid;
- }
-
- @Override
- public boolean isXidSet() {
- return xidSet;
- }
-
- @Override
- public OFType getType() {
- return OFType.${msg.constant_name};
- }
-
- @Override
- public OFVersion getVersion() {
- return OFVersion.${version.constant_version};
- }
-
- // Accessors for OF message fields
-//:: for prop in msg.all_properties():
- @Override
- public ${prop.java_type.public_type} get${prop.title_name}()${ "" if msg.property_in_version(prop, version) else "throws UnsupportedOperationException"} {
-//:: if msg.property_in_version(prop, version):
- return ${prop.name};
-//:: else:
- throw new UnsupportedOperationException("Property ${prop.name} not supported in version #{version}");
-//:: #endif
- }
-//:: #endfor
-
- public ${msg.interface_name}.Builder createBuilder() {
- return new BuilderImplWithParent(this);
- }
-
- static class BuilderImplWithParent implements ${msg.interface_name}.Builder {
- final ${impl_class} parentMessage;
- private boolean xidSet;
- private int xid;
-
- // OF message fields
-//:: for prop in msg.properties_for_version(version):
- private boolean ${prop.name}Set;
- private ${prop.java_type.public_type} ${prop.name};
-//:: #endfor
-
- BuilderImplWithParent(${impl_class} parentMessage) {
- this.parentMessage = parentMessage;
- }
-
-//:: for prop in msg.all_properties():
- @Override
- public ${prop.java_type.public_type} get${prop.title_name}()${ "" if msg.property_in_version(prop, version) else " throws UnsupportedOperationException"} {
-//:: if msg.property_in_version(prop, version):
- return ${prop.name};
-//:: else:
- throw new UnsupportedOperationException("Property ${prop.name} not supported in version #{version}");
-//:: #endif
- }
- @Override
- public ${msg.interface_name}.Builder set${prop.title_name}(${prop.java_type.public_type} ${prop.name})${ "" if msg.property_in_version(prop, version) else " throws UnsupportedOperationException"} {
-//:: if msg.property_in_version(prop, version):
- this.${prop.name} = ${prop.name};
- this.${prop.name}Set = true;
- return this;
-//:: else:
- throw new UnsupportedOperationException("Property ${prop.name} not supported in version #{version}");
-//:: #endif
- }
-//:: #endfor
- @Override
- public ${msg.interface_name} getMessage() {
- if(this.xidSet) {
- return new ${impl_class}(
- ${",\n ".join(
- [ "xid" ] +
- [ "this.{0}Set ? this.{0} : parentMessage.{0}".format(prop.name)
- for prop in msg.properties_for_version(version)])}
- );
- } else {
- return new ${impl_class}(
- ${",\n ".join(
- [ "this.{0}Set ? this.{0} : parentMessage.{0}".format(prop.name)
- for prop in msg.properties_for_version(version)])}
- );
- }
- }
- }
-
- static class BuilderImpl implements ${msg.interface_name}.Builder {
- private boolean xidSet;
- private int xid;
-
- // OF message fields
-//:: for prop in msg.properties_for_version(version):
- private boolean ${prop.name}Set;
- private ${prop.java_type.public_type} ${prop.name};
-//:: #endfor
-
-//:: for prop in msg.all_properties():
- @Override
- public ${prop.java_type.public_type} get${prop.title_name}()${ "" if msg.property_in_version(prop, version) else " throws UnsupportedOperationException"} {
-//:: if msg.property_in_version(prop, version):
- return ${prop.name};
-//:: else:
- throw new UnsupportedOperationException("Property ${prop.name} not supported in version #{version}");
-//:: #endif
- }
- @Override
- public ${msg.interface_name}.Builder set${prop.title_name}(${prop.java_type.public_type} ${prop.name})${ "" if msg.property_in_version(prop, version) else " throws UnsupportedOperationException"} {
-//:: if msg.property_in_version(prop, version):
- this.${prop.name} = ${prop.name};
- this.${prop.name}Set = true;
- return this;
-//:: else:
- throw new UnsupportedOperationException("Property ${prop.name} not supported in version #{version}");
-//:: #endif
- }
-//:: #endfor
- @Override
- public ${msg.interface_name} getMessage() {
- if(this.xidSet) {
- return new ${impl_class}(
- ${",\n ".join(
- [ "xid" ] +
- [ "this.{0}Set ? this.{0} : {1}.{2}".format(prop.name, impl_class, prop.default_name)
- for prop in msg.properties_for_version(version)])}
- );
- } else {
- return new ${impl_class}(
- ${",\n ".join(
- [ "this.{0}Set ? this.{0} : {1}.{2}".format(prop.name, impl_class, prop.default_name)
- for prop in msg.properties_for_version(version)])}
- );
- }
- }
- }
-
- final static Reader READER = new Reader();
- static class Reader implements OFMessageReader<${msg.interface_name}> {
- @Override
- public ${msg.interface_name} readFrom(ChannelBuffer bb) throws OFParseError {
- byte version = bb.readByte();
- if (version != (byte) ${version.int_version})
- throw new OFParseError("Wrong version: Expected=${version.int_version}, got="+version);
-
- byte type = bb.readByte();
- if(type != ${msg.wire_type(version)})
- throw new OFParseError("Wrong message type: Expected=${msg.constant_name}, got="+type);
-
- int length = bb.readUnsignedShort();
-//:: if msg.is_fixed_length(version):
- if(length != LENGTH)
- throw new OFParseError("Wrong message length: Expected="+LENGTH +", got="+length);
-//:: else:
- if(length < MINIMUM_LENGTH)
- throw new OFParseError("Insufficient message length: minimum length="+MINIMUM_LENGTH +", got="+length);
-//:: #endif
- int xid = bb.readInt();
-//:: for prop in msg.properties_for_version(version, skip_pads=False):
-//:: if prop.is_pad:
- // pad: ${prop.length} bytes
- bb.skipBytes(${prop.length});
-//:: else:
- ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version)};
-//:: #endif
-//:: #endfor
- return new ${impl_class}(
- ${",\n ".join(
- [ "xid" ] + [ prop.name for prop in msg.properties_for_version(version)])}
- );
- }
- }
-
-}
diff --git a/java_gen/templates/of_class.java b/java_gen/templates/of_class.java
new file mode 100644
index 0000000..51fbf5c
--- /dev/null
+++ b/java_gen/templates/of_class.java
@@ -0,0 +1,209 @@
+//:: # 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.
+//::
+//:: from loxi_ir import *
+//:: import itertools
+//:: import of_g
+//:: include('_copyright.java')
+
+//:: include('_autogen.java')
+
+package ${msg.package};
+
+//:: include("_imports.java", msg=msg)
+
+class ${impl_class} implements ${msg.interface.name} {
+ // version: ${version}
+ private final static byte WIRE_VERSION = ${version.int_version};
+//:: if msg.is_fixed_length:
+ private final static int LENGTH = ${msg.length};
+//:: else:
+ private final static int MINIMUM_LENGTH = ${msg.min_length};
+//:: #endif
+
+//:: for prop in msg.data_members:
+ private final static ${prop.java_type.public_type} ${prop.default_name} = ${prop.default_value};
+//:: #end
+
+ // OF message fields
+//:: for prop in msg.data_members:
+ private final ${prop.java_type.public_type} ${prop.name};
+//:: #endfor
+
+ ${impl_class}(${
+ ", ".join("%s %s" %(prop.java_type.public_type, prop.name) for prop in msg.data_members) }) {
+//:: for prop in msg.data_members:
+ this.${prop.name} = ${prop.name};
+//:: #endfor
+ }
+
+ // Accessors for OF message fields
+//:: include("_field_accessors.java", msg=msg, generate_setters=False, builder=False)
+
+
+ public ${msg.name}.Builder createBuilder() {
+ return new BuilderImplWithParent(this);
+ }
+
+ static class BuilderImplWithParent implements ${msg.interface.name}.Builder {
+ final ${impl_class} parentMessage;
+
+ // OF message fields
+//:: for prop in msg.data_members:
+ private boolean ${prop.name}Set;
+ private ${prop.java_type.public_type} ${prop.name};
+//:: #endfor
+
+ BuilderImplWithParent(${impl_class} parentMessage) {
+ this.parentMessage = parentMessage;
+ }
+
+//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True)
+
+ @Override
+ public ${msg.interface.name} getMessage() {
+ return new ${impl_class}(
+ ${",\n ".join(
+ [ "this.{0}Set ? this.{0} : parentMessage.{0}".format(prop.name)
+ for prop in msg.data_members])}
+ );
+ }
+ }
+
+ static class BuilderImpl implements ${msg.interface.name}.Builder {
+ // OF message fields
+//:: for prop in msg.data_members:
+ private boolean ${prop.name}Set;
+ private ${prop.java_type.public_type} ${prop.name};
+//:: #endfor
+
+//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True)
+//
+ @Override
+ public ${msg.interface.name} getMessage() {
+ return new ${impl_class}(
+ ${",\n ".join(
+ [ "this.{0}Set ? this.{0} : {1}.{2}".format(prop.name, impl_class, prop.default_name)
+ for prop in msg.data_members])}
+ );
+ }
+ }
+
+ final static Reader READER = new Reader();
+ static class Reader implements OFMessageReader<${msg.interface.name}> {
+ @Override
+ public ${msg.interface.name} readFrom(ChannelBuffer bb) throws OFParseError {
+//:: fields_with_length_member = {}
+//:: for prop in msg.members:
+//:: if prop.is_data:
+ ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version,
+ length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)};
+//:: elif prop.is_pad:
+ // pad: ${prop.length} bytes
+ bb.skipBytes(${prop.length});
+//:: elif prop.is_fixed_value:
+ // fixed value property ${prop.name} == ${prop.value}
+ ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version)};
+ if(${prop.name} != ${prop.value})
+ throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
+//:: elif prop.is_length_value:
+ ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version)};
+ if(${prop.name} < MINIMUM_LENGTH)
+ throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name});
+//:: elif prop.is_field_length_value:
+//:: fields_with_length_member[prop.member.field_name] = prop.name
+ int ${prop.name} = ${prop.java_type.read_op(version)};
+//:: else:
+ // fixme: todo ${prop.name}
+//:: #endif
+//:: #endfor
+ return new ${impl_class}(
+ ${",\n ".join(
+ [ prop.name for prop in msg.data_members])}
+ );
+ }
+ }
+
+ public int writeTo(ChannelBuffer bb) {
+ return WRITER.write(bb, this);
+ }
+
+ final static Writer WRITER = new Writer();
+ static class Writer implements OFMessageWriter<${impl_class}> {
+ @Override
+ public int write(ChannelBuffer bb, ${impl_class} message) {
+//:: if not msg.is_fixed_length:
+ int startIndex = bb.readerIndex();
+//:: #end
+
+//:: fields_with_length_member = {}
+//:: for prop in msg.members:
+//:: if prop.c_name in fields_with_length_member:
+ int ${prop.name}StartIndex = bb.writerIndex();
+//:: #endif
+//:: if prop.is_data:
+ ${prop.java_type.write_op(version, "message." + prop.name)};
+//:: elif prop.is_pad:
+ // pad: ${prop.length} bytes
+ bb.writeZero(${prop.length});
+//:: elif prop.is_fixed_value:
+ // fixed value property ${prop.name} = ${prop.value}
+ ${prop.java_type.write_op(version, prop.value)};
+//:: elif prop.is_length_value:
+ // ${prop.name} is length of variable message, will be updated at the end
+ ${prop.java_type.write_op(version, 0)};
+//:: elif prop.is_field_length_value:
+//:: fields_with_length_member[prop.member.field_name] = prop.name
+ // ${prop.name} is length indicator for ${prop.member.field_name}, will be
+ // udpated when ${prop.member.field_name} has been written
+ int ${prop.name}Index = bb.writerIndex();
+ ${prop.java_type.write_op(version, 0)};
+//:: else:
+ // FIXME: todo write ${prop.name}
+//:: #endif
+//:: if prop.c_name in fields_with_length_member:
+//:: length_member_name = fields_with_length_member[prop.c_name]
+ // update field length member ${length_member_name}
+ int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex;
+ bb.setShort(${length_member_name}Index, ${prop.name}Length);
+//:: #endif
+//:: #endfor
+
+//:: if msg.is_fixed_length:
+ return LENGTH;
+//:: else:
+ // update length field
+ int length = bb.writerIndex() - startIndex;
+ bb.setShort(startIndex + 2, length);
+ return length;
+//:: #end
+
+ }
+ }
+
+
+}
diff --git a/java_gen/templates/message_interface.java b/java_gen/templates/of_interface.java
similarity index 71%
rename from java_gen/templates/message_interface.java
rename to java_gen/templates/of_interface.java
index 904be95..e8e15c0 100644
--- a/java_gen/templates/message_interface.java
+++ b/java_gen/templates/of_interface.java
@@ -31,32 +31,25 @@
//:: include('_autogen.java')
-package org.openflow.protocol;
-import java.util.List;
-import org.openflow.protocol.match.*;
-import org.openflow.protocol.actions.OFAction;
-import org.openflow.protocol.instructions.OFInstruction;
-import org.openflow.types.*;
-import org.openflow.util.*;
-import org.openflow.exceptions.OFUnsupported;
+package ${msg.package};
-public interface ${msg.interface_name} extends OFMessage {
- int getXid();
- boolean isXidSet();
- OFType getType();
- OFVersion getVersion();
+//:: include("_imports.java", msg=msg)
-//:: for prop in msg.all_properties():
- ${prop.java_type.public_type} get${prop.title_name}()${ "" if prop.is_universal else "throws UnsupportedOperationException"};
+public interface ${msg.name} ${"extends %s" % msg.parent_interface if msg.parent_interface else ""} {
+//:: for prop in (prop for prop in msg.members):
+ ${prop.java_type.public_type} get${prop.title_name}()${ "" if prop.is_universal else " throws UnsupportedOperationException"};
//:: #endfor
- Builder createBuilder();
+ int writeTo(ChannelBuffer channelBuffer);
- public interface Builder {
- ${msg.interface_name} getMessage();
-//:: for prop in msg.all_properties():
- ${prop.java_type.public_type} get${prop.title_name}()${ "" if prop.is_universal else "throws UnsupportedOperationException"};
+ Builder createBuilder();
+ public interface Builder ${"extends %s.Builder" % msg.parent_interface if msg.parent_interface else ""} {
+ ${msg.name} getMessage();
+//:: for prop in msg.members:
+ ${prop.java_type.public_type} get${prop.title_name}()${ "" if prop.is_universal else " throws UnsupportedOperationException"};
+//:: if prop.is_writeable:
Builder set${prop.title_name}(${prop.java_type.public_type} ${prop.name})${ "" if prop.is_universal else " throws UnsupportedOperationException"};
+//:: #endif
//:: #endfor
}