Skip to content

Commit

Permalink
Merge into master from pull request #108:
Browse files Browse the repository at this point in the history
Some documentation improvements, add support for logging, OFPortBitMap fixes (#108)
  • Loading branch information
abat committed Nov 6, 2013
2 parents c4da04d + 37461d2 commit 48b72df
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ wireshark: .loxi_ts.wireshark

clean:
rm -rf loxi_output # only delete generated files in the default directory
rm -f loxigen.log loxigen-test.log .loxi_ts.c .loxi_ts.python .loxi_ts.java
rm -f loxigen.log loxigen-test.log .loxi_ts.*

debug:
@echo "LOXI_OUTPUT_DIR=\"${LOXI_OUTPUT_DIR}\""
Expand Down
12 changes: 7 additions & 5 deletions java_gen/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from loxi_ir import *
import lang_java
import test_data
from collections import namedtuple
from import_cleaner import ImportCleaner

import loxi_utils.loxi_utils as loxi_utils
Expand All @@ -52,20 +53,21 @@ def gen_all_java(out, name):
shutil.rmtree(basedir)
os.makedirs(basedir)
copy_prewrite_tree(basedir)
gen = JavaGenerator(basedir)
gen = JavaGenerator(basedir, JavaGeneratorOptions(instrument=True))
gen.create_of_interfaces()
gen.create_of_classes()
gen.create_of_const_enums()
gen.create_of_factories()


JavaGeneratorOptions = namedtuple("JavaGeneratorOptions", ("instrument",))

class JavaGenerator(object):
templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates')

def __init__(self, basedir):
def __init__(self, basedir, gen_opts):
self.basedir = basedir
self.java_model = java_model.model
self.gen_opts = gen_opts

def render_class(self, clazz, template, src_dir=None, **context):
if not src_dir:
Expand All @@ -74,6 +76,7 @@ def render_class(self, clazz, template, src_dir=None, **context):
context['class_name'] = clazz.name
context['package'] = clazz.package
context['template_dir'] = self.templates_dir
context['genopts']= self.gen_opts

filename = os.path.join(self.basedir, src_dir, "%s/%s.java" % (clazz.package.replace(".", "/"), clazz.name))
dirname = os.path.dirname(filename)
Expand All @@ -83,14 +86,13 @@ def render_class(self, clazz, template, src_dir=None, **context):
print "filename: %s" % filename
with open(filename, "w") as f:
loxi_utils.render_template(f, template, [self.templates_dir], context, prefix=prefix)

try:
cleaner = ImportCleaner(filename)
cleaner.find_used_imports()
cleaner.rewrite_file(filename)
except:
print 'Cannot clean imports from file %s' % filename


def create_of_const_enums(self):
for enum in self.java_model.enums:
Expand Down
2 changes: 1 addition & 1 deletion java_gen/java_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ def name(self):

@property
def variable_name(self):
return self.name[3:]
return self.name[2].lower() + self.name[3:]

@property
def length(self):
Expand Down
5 changes: 5 additions & 0 deletions java_gen/pre-written/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.projectfloodlight.openflow.protocol;

public final class OFInstrumentationOptions {
public static final boolean TRACE_READS = false;

private OFInstrumentationOptions() { }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.projectfloodlight.openflow.types;

import javax.annotation.concurrent.Immutable;


/** User-facing object representing a bitmap of ports that can be matched on.
* This is implemented by the custom BSN OXM type of_oxm_bsn_in_ports_182.
Expand All @@ -20,8 +22,22 @@
* The second term cannot be represented in OXM, the second can.
*
* That said, all that craziness is hidden from the user of this object.
*
* <h2>Usage</h2>
* OFPortBitmap is meant to be used with MatchField <tt>BSN_IN_PORTS_128</tt> in place
* of the raw type Masked&lt;OFBitMask128&gt;.
*
* <h3>Example:</h3>:
* <pre>
* OFPortBitMap portBitMap;
* Match.Builder matchBuilder;
* // initialize
* matchBuilder.setMasked(MatchField.BSN_IN_PORTS_128, portBitmap);
* </pre>
*
* @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
*/
@Immutable
public class OFPortBitMap extends Masked<OFBitMask128> {

private OFPortBitMap(OFBitMask128 mask) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

import org.jboss.netty.buffer.ChannelBuffer;
import org.projectfloodlight.openflow.exceptions.OFParseError;
import org.projectfloodlight.openflow.protocol.OFInstrumentationOptions;
import org.projectfloodlight.openflow.protocol.OFMessageReader;
import org.projectfloodlight.openflow.protocol.Writeable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
Expand All @@ -18,6 +21,7 @@
*/

public class ChannelUtils {
private static final Logger logger = LoggerFactory.getLogger(ChannelUtils.class);
public static String readFixedLengthString(ChannelBuffer bb, int length) {
byte[] dst = new byte[length];
bb.readBytes(dst, 0, length);
Expand Down Expand Up @@ -56,8 +60,15 @@ static public void writeBytes(final ChannelBuffer bb,
public static <T> List<T> readList(ChannelBuffer bb, int length, OFMessageReader<T> reader) throws OFParseError {
int end = bb.readerIndex() + length;
Builder<T> builder = ImmutableList.<T>builder();
if(OFInstrumentationOptions.TRACE_READS)
if(logger.isTraceEnabled())
logger.trace("readList(length={}, reader={})", length, reader.getClass());
while(bb.readerIndex() < end) {
builder.add(reader.readFrom(bb));
T read = reader.readFrom(bb);
if(OFInstrumentationOptions.TRACE_READS)
if(logger.isTraceEnabled())
logger.trace("readList: read={}, left={}", read, end - bb.readerIndex());
builder.add(read);
}
if(bb.readerIndex() != end) {
throw new IllegalStateException("Overread length: length="+length + " overread by "+ (bb.readerIndex() - end) + " reader: "+reader);
Expand Down
2 changes: 2 additions & 0 deletions java_gen/templates/_imports.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.projectfloodlight.openflow.types.*;
import org.projectfloodlight.openflow.util.*;
import org.projectfloodlight.openflow.exceptions.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import com.google.common.collect.ImmutableList;
Expand Down
3 changes: 2 additions & 1 deletion java_gen/templates/custom/OFMatchV3Ver13.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private static boolean supportsField(MatchField<?> field) {
case IPV6_SRC:
case IPV6_DST:
case IPV6_FLABEL:
case BSN_IN_PORTS_128:
return true;
default:
return false;
Expand Down Expand Up @@ -106,4 +107,4 @@ public boolean isPartiallyMasked(MatchField<?> field) {
OFOxm<?> oxm = this.oxmList.get(field);

return oxm != null && oxm.isMasked();
}
}
21 changes: 20 additions & 1 deletion java_gen/templates/of_class.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
//:: include("_imports.java", msg=msg)

class ${impl_class} implements ${msg.interface.inherited_declaration()} {
//:: if genopts.instrument:
private static final Logger logger = LoggerFactory.getLogger(${impl_class}.class);
//:: #endif
// version: ${version}
final static byte WIRE_VERSION = ${version.int_version};
//:: if msg.is_fixed_length:
Expand Down Expand Up @@ -216,6 +219,11 @@ static class Reader implements OFMessageReader<${msg.interface.name}> {
bb.readerIndex(start);
return null;
}
//:: if genopts.instrument:
if(OFInstrumentationOptions.TRACE_READS)
if(logger.isTraceEnabled())
logger.trace("readFrom - length={}", ${prop.name});
//:: #endif
//:: 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, pub_type=False)};
Expand All @@ -242,11 +250,22 @@ static class Reader implements OFMessageReader<${msg.interface.name}> {
//:: if os.path.exists("%s/custom/%s.Reader_normalize_stanza.java" % (template_dir, msg.name)):
//:: include("custom/%s.Reader_normalize_stanza.java" % msg.name, msg=msg, has_parent=False)
//:: #endif
return new ${impl_class}(
${impl_class} ${msg.variable_name} = new ${impl_class}(
${",\n ".join(
[ prop.name for prop in msg.data_members])}
);
//:: if genopts.instrument:
if(OFInstrumentationOptions.TRACE_READS)
if(logger.isTraceEnabled())
logger.trace("readFrom - read={}", ${msg.variable_name});
//:: #endif
return ${msg.variable_name};
//:: else:
//:: if genopts.instrument:
if(OFInstrumentationOptions.TRACE_READS)
if(logger.isTraceEnabled())
logger.trace("readFrom - returning shared instance={}", INSTANCE);
//:: #endif
return INSTANCE;
//:: #endif
}
Expand Down

0 comments on commit 48b72df

Please sign in to comment.