Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1 | # Copyright 2013, Big Switch Networks, Inc. |
| 2 | # |
| 3 | # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| 4 | # the following special exception: |
| 5 | # |
| 6 | # LOXI Exception |
| 7 | # |
| 8 | # As a special exception to the terms of the EPL, you may distribute libraries |
| 9 | # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| 10 | # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| 11 | # from the LoxiGen Libraries and the notice provided below is (i) included in |
| 12 | # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| 13 | # documentation for the LoxiGen Libraries, if distributed in binary form. |
| 14 | # |
| 15 | # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| 16 | # |
| 17 | # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| 18 | # a copy of the EPL at: |
| 19 | # |
| 20 | # http://www.eclipse.org/legal/epl-v10.html |
| 21 | # |
| 22 | # Unless required by applicable law or agreed to in writing, software |
| 23 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 25 | # EPL for the specific language governing permissions and limitations |
| 26 | # under the EPL. |
| 27 | |
| 28 | """ |
| 29 | @brief Main Java Generation module |
| 30 | """ |
| 31 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 32 | import logging |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 33 | import pdb |
| 34 | import os |
| 35 | import shutil |
| 36 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 37 | import loxi_globals |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 38 | from loxi_ir import * |
| 39 | import lang_java |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 40 | import test_data |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 41 | from collections import namedtuple |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 42 | from import_cleaner import ImportCleaner |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 43 | |
Andreas Wundsam | 5420b95 | 2013-11-15 13:41:01 -0800 | [diff] [blame] | 44 | import template_utils |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 45 | import loxi_utils.loxi_utils as loxi_utils |
| 46 | |
| 47 | import java_gen.java_model as java_model |
| 48 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 49 | logger = logging.getLogger(__name__) |
| 50 | |
| 51 | def gen_all_java(install_dir): |
| 52 | basedir= '%s/openflowj' % install_dir |
| 53 | logger.info("Outputting to %s" % basedir) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 54 | if os.path.exists(basedir): |
| 55 | shutil.rmtree(basedir) |
| 56 | os.makedirs(basedir) |
| 57 | copy_prewrite_tree(basedir) |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 58 | gen = JavaGenerator(basedir, JavaGeneratorOptions(instrument=True)) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 59 | gen.create_of_interfaces() |
| 60 | gen.create_of_classes() |
| 61 | gen.create_of_const_enums() |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 62 | gen.create_of_factories() |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 63 | |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 64 | JavaGeneratorOptions = namedtuple("JavaGeneratorOptions", ("instrument",)) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 65 | |
| 66 | class JavaGenerator(object): |
| 67 | templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates') |
| 68 | |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 69 | def __init__(self, basedir, gen_opts): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 70 | self.basedir = basedir |
| 71 | self.java_model = java_model.model |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 72 | self.gen_opts = gen_opts |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 73 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 74 | def render_class(self, clazz, template, src_dir=None, **context): |
| 75 | if not src_dir: |
Andreas Wundsam | f83c3a7 | 2013-11-19 15:49:31 -0800 | [diff] [blame] | 76 | src_dir = "gen-src/main/java/" |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 77 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 78 | context['class_name'] = clazz.name |
| 79 | context['package'] = clazz.package |
Andreas Wundsam | d10e1c5 | 2013-08-01 22:02:49 -0700 | [diff] [blame] | 80 | context['template_dir'] = self.templates_dir |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 81 | context['genopts']= self.gen_opts |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 82 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 83 | filename = os.path.join(self.basedir, src_dir, "%s/%s.java" % (clazz.package.replace(".", "/"), clazz.name)) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 84 | dirname = os.path.dirname(filename) |
| 85 | if not os.path.exists(dirname): |
| 86 | os.makedirs(dirname) |
| 87 | prefix = '//::(?=[ \t]|$)' |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 88 | logger.debug("rendering filename: %s" % filename) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 89 | with open(filename, "w") as f: |
Andreas Wundsam | 5420b95 | 2013-11-15 13:41:01 -0800 | [diff] [blame] | 90 | template_utils.render_template(f, template, [self.templates_dir], context, prefix=prefix) |
Andreas Wundsam | 70aa549 | 2013-10-23 15:26:53 -0700 | [diff] [blame] | 91 | |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 92 | try: |
| 93 | cleaner = ImportCleaner(filename) |
| 94 | cleaner.find_used_imports() |
| 95 | cleaner.rewrite_file(filename) |
| 96 | except: |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 97 | logger.info('Cannot clean imports from file %s' % filename) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 98 | |
| 99 | def create_of_const_enums(self): |
| 100 | for enum in self.java_model.enums: |
| 101 | if enum.name in ["OFPort"]: |
| 102 | continue |
| 103 | self.render_class(clazz=enum, |
| 104 | template='const.java', enum=enum, all_versions=self.java_model.versions) |
| 105 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 106 | for version in enum.versions: |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 107 | clazz = java_model.OFGenericClass(package="org.projectfloodlight.openflow.protocol.ver{}".format(version.dotless_version), name="{}SerializerVer{}".format(enum.name, version.dotless_version)) |
Andreas Wundsam | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 108 | |
| 109 | if enum.is_bitmask: |
| 110 | self.render_class(clazz=clazz, template="const_set_serializer.java", enum=enum, version=version) |
| 111 | else: |
| 112 | self.render_class(clazz=clazz, template="const_serializer.java", enum=enum, version=version) |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 113 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 114 | def create_of_interfaces(self): |
| 115 | """ Create the base interfaces for of classes""" |
| 116 | for interface in self.java_model.interfaces: |
| 117 | #if not utils.class_is_message(interface.c_name): |
| 118 | # continue |
| 119 | self.render_class(clazz=interface, |
| 120 | template="of_interface.java", msg=interface) |
| 121 | |
| 122 | def create_of_classes(self): |
| 123 | """ Create the OF classes with implementations for each of the interfaces and versions """ |
| 124 | for interface in self.java_model.interfaces: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 125 | for java_class in interface.versioned_classes: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 126 | if self.java_model.generate_class(java_class): |
| 127 | if not java_class.is_virtual: |
| 128 | self.render_class(clazz=java_class, |
| 129 | template='of_class.java', version=java_class.version, msg=java_class, |
| 130 | impl_class=java_class.name) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 131 | |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 132 | self.create_unit_test(java_class.unit_test) |
| 133 | else: |
| 134 | disc = java_class.discriminator |
| 135 | if disc: |
| 136 | self.render_class(clazz=java_class, |
| 137 | template='of_virtual_class.java', version=java_class.version, msg=java_class, |
| 138 | impl_class=java_class.name, model=self.java_model) |
| 139 | else: |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 140 | logger.warn("Class %s virtual but no discriminator" % java_class.name) |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 141 | else: |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 142 | logger.info("Class %s ignored by generate_class" % java_class.name) |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 143 | |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 144 | def create_unit_test(self, unit_tests): |
| 145 | if unit_tests.has_test_data: |
| 146 | for i in range(unit_tests.length): |
| 147 | unit_test = unit_tests.get_test_unit(i) |
| 148 | if unit_test.has_test_data: |
| 149 | self.render_class(clazz=unit_test, |
Andreas Wundsam | f83c3a7 | 2013-11-19 15:49:31 -0800 | [diff] [blame] | 150 | template='unit_test.java', src_dir="gen-src/test/java", |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 151 | version=unit_test.java_class.version, |
| 152 | test=unit_test, msg=unit_test.java_class, |
| 153 | test_data=unit_test.test_data) |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 154 | |
| 155 | def create_of_factories(self): |
Andreas Wundsam | e0d52be | 2013-08-22 07:52:13 -0700 | [diff] [blame] | 156 | for factory in self.java_model.of_factories: |
| 157 | self.render_class(clazz=factory, template="of_factory_interface.java", factory=factory) |
| 158 | for factory_class in factory.factory_classes: |
| 159 | self.render_class(clazz=factory_class, template="of_factory_class.java", factory=factory_class, model=self.java_model) |
Yotam Harchol | 791e488 | 2013-09-05 16:32:56 -0700 | [diff] [blame] | 160 | self.render_class(clazz=java_model.OFGenericClass(package="org.projectfloodlight.openflow.protocol", name="OFFactories"), template="of_factories.java", versions=self.java_model.versions) |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 161 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 162 | def copy_prewrite_tree(basedir): |
| 163 | """ Recursively copy the directory structure from ./java_gen/pre-write |
| 164 | into $basedir""" |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 165 | logger.info("Copying pre-written files into %s" % basedir) |