blob: 3cdef2c67f6886d7add93e46d10173eda397858f [file] [log] [blame]
Andreas Wundsam40e14f72013-05-06 14:49:08 -07001import of_g
2import os
3import pdb
4import re
5
6import loxi_front_end.type_maps as type_maps
7import loxi_utils.loxi_utils as utils
8import py_gen.util as py_utils
9
10import java_gen.java_utils as java_utils
11from java_gen.java_model import *
12ignore_fields = ['version', 'xid', 'length', 'type' ]
13
14protected_fields = ['version', 'length']
15
16
17templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates')
18
19def render_template(out, name, **context):
20 prefix = '//::(?=[ \t]|$)'
21 utils.render_template(out, name, [templates_dir], context, prefix=prefix)
22
23def create_message_interfaces(message_names, basedir):
24 """ Create the base interfaces for OFMessages"""
25 for msg_name in message_names:
26 msg = JavaOFMessage(msg_name)
27
28 filename = os.path.join(basedir, "%s.java" % msg.interface_name)
29 dirname = os.path.dirname(filename)
30 if not os.path.exists(dirname):
31 os.makedirs(dirname)
32
33 with open(filename, "w") as f:
34 render_template(f, "message_interface.java", msg=msg)
35
36def create_of_type_enum(message_names, basedir):
37 all_versions = [ JavaOFVersion(v) for v in of_g.target_version_list ]
38 messages = sorted(filter(lambda msg: not msg.is_virtual and not msg.is_extension, [ JavaOFMessage(msg_name) for msg_name in message_names ]), key=lambda msg: msg.wire_type(all_versions[-1]))
39 filename = os.path.join(basedir, "../types/OFType.java")
40 dirname = os.path.dirname(filename)
41 if not os.path.exists(dirname):
42 os.makedirs(dirname)
43 with open(filename, "w") as f:
44 render_template(f, "of_type.java", all_messages=messages, all_versions = all_versions)
45
46def create_message_by_version(message_names, basedir):
47 """ Create the OF Messages for each version that implement the above interfaces"""
48 for msg_name in message_names:
49 msg = JavaOFMessage(msg_name)
50
51 for version in msg.all_versions():
52 filename = os.path.join(basedir, "%s.java" % msg.class_name_for_version(version))
53 with open(filename, "w") as f:
54 render_template(f, "message_class.java", msg=msg, version=version,
55 impl_class=msg.class_name_for_version(version))