Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 1 | import of_g |
| 2 | import os |
| 3 | import pdb |
| 4 | import re |
| 5 | |
| 6 | import loxi_front_end.type_maps as type_maps |
| 7 | import loxi_utils.loxi_utils as utils |
| 8 | import py_gen.util as py_utils |
| 9 | |
| 10 | import java_gen.java_utils as java_utils |
| 11 | from java_gen.java_model import * |
| 12 | ignore_fields = ['version', 'xid', 'length', 'type' ] |
| 13 | |
| 14 | protected_fields = ['version', 'length'] |
| 15 | |
| 16 | |
| 17 | templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates') |
| 18 | |
| 19 | def render_template(out, name, **context): |
| 20 | prefix = '//::(?=[ \t]|$)' |
| 21 | utils.render_template(out, name, [templates_dir], context, prefix=prefix) |
| 22 | |
| 23 | def 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 | |
| 36 | def 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 | |
| 46 | def 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)) |