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 | |
| 32 | import pdb |
| 33 | import os |
| 34 | import shutil |
| 35 | |
| 36 | import of_g |
| 37 | from loxi_ir import * |
| 38 | import lang_java |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 39 | import test_data |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 40 | from import_cleaner import ImportCleaner |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 41 | |
| 42 | import loxi_utils.loxi_utils as loxi_utils |
| 43 | |
| 44 | import java_gen.java_model as java_model |
| 45 | |
| 46 | def gen_all_java(out, name): |
Andreas Wundsam | d83883e | 2013-07-25 14:50:51 -0700 | [diff] [blame] | 47 | basedir= '%s/openflowj' % of_g.options.install_dir |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 48 | print "Outputting to %s" % basedir |
| 49 | if os.path.exists(basedir): |
| 50 | shutil.rmtree(basedir) |
| 51 | os.makedirs(basedir) |
| 52 | copy_prewrite_tree(basedir) |
| 53 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 54 | gen = JavaGenerator(basedir) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 55 | gen.create_of_interfaces() |
| 56 | gen.create_of_classes() |
| 57 | gen.create_of_const_enums() |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 58 | gen.create_of_factories() |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 59 | |
Andreas Wundsam | d83883e | 2013-07-25 14:50:51 -0700 | [diff] [blame] | 60 | with open('%s/README.java-lang' % os.path.dirname(__file__)) as readme_src: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 61 | out.writelines(readme_src.readlines()) |
| 62 | out.close() |
| 63 | |
| 64 | |
| 65 | class JavaGenerator(object): |
| 66 | templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates') |
| 67 | |
| 68 | def __init__(self, basedir): |
| 69 | self.basedir = basedir |
| 70 | self.java_model = java_model.model |
| 71 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 72 | def render_class(self, clazz, template, src_dir=None, **context): |
| 73 | if not src_dir: |
| 74 | src_dir = "src/main/java/" |
| 75 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 76 | context['class_name'] = clazz.name |
| 77 | context['package'] = clazz.package |
Andreas Wundsam | d10e1c5 | 2013-08-01 22:02:49 -0700 | [diff] [blame] | 78 | context['template_dir'] = self.templates_dir |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 79 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 80 | 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] | 81 | dirname = os.path.dirname(filename) |
| 82 | if not os.path.exists(dirname): |
| 83 | os.makedirs(dirname) |
| 84 | prefix = '//::(?=[ \t]|$)' |
| 85 | print "filename: %s" % filename |
| 86 | with open(filename, "w") as f: |
| 87 | loxi_utils.render_template(f, template, [self.templates_dir], context, prefix=prefix) |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 88 | |
| 89 | try: |
| 90 | cleaner = ImportCleaner(filename) |
| 91 | cleaner.find_used_imports() |
| 92 | cleaner.rewrite_file(filename) |
| 93 | except: |
| 94 | print 'Cannot clean imports from file %s' % filename |
| 95 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 96 | |
| 97 | def create_of_const_enums(self): |
| 98 | for enum in self.java_model.enums: |
| 99 | if enum.name in ["OFPort"]: |
| 100 | continue |
| 101 | self.render_class(clazz=enum, |
| 102 | template='const.java', enum=enum, all_versions=self.java_model.versions) |
| 103 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 104 | for version in enum.versions: |
| 105 | clazz = java_model.OFGenericClass(package="org.openflow.protocol.ver{}".format(version.of_version), name="{}SerializerVer{}".format(enum.name, version.of_version)) |
| 106 | self.render_class(clazz=clazz, template="const_serializer.java", enum=enum, version=version) |
| 107 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 108 | def create_of_interfaces(self): |
| 109 | """ Create the base interfaces for of classes""" |
| 110 | for interface in self.java_model.interfaces: |
| 111 | #if not utils.class_is_message(interface.c_name): |
| 112 | # continue |
| 113 | self.render_class(clazz=interface, |
| 114 | template="of_interface.java", msg=interface) |
| 115 | |
| 116 | def create_of_classes(self): |
| 117 | """ Create the OF classes with implementations for each of the interfaces and versions """ |
| 118 | for interface in self.java_model.interfaces: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 119 | for java_class in interface.versioned_classes: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 120 | if self.java_model.generate_class(java_class): |
| 121 | if not java_class.is_virtual: |
| 122 | self.render_class(clazz=java_class, |
| 123 | template='of_class.java', version=java_class.version, msg=java_class, |
| 124 | impl_class=java_class.name) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 125 | |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 126 | self.create_unit_test(java_class.unit_test) |
| 127 | else: |
| 128 | disc = java_class.discriminator |
| 129 | if disc: |
| 130 | self.render_class(clazz=java_class, |
| 131 | template='of_virtual_class.java', version=java_class.version, msg=java_class, |
| 132 | impl_class=java_class.name, model=self.java_model) |
| 133 | else: |
| 134 | print "Class %s virtual but no discriminator" % java_class.name |
| 135 | else: |
| 136 | print "Class %s ignored by generate_class" % java_class.name |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 137 | |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 138 | def create_unit_test(self, unit_tests): |
| 139 | if unit_tests.has_test_data: |
| 140 | for i in range(unit_tests.length): |
| 141 | unit_test = unit_tests.get_test_unit(i) |
| 142 | if unit_test.has_test_data: |
| 143 | self.render_class(clazz=unit_test, |
| 144 | template='unit_test.java', src_dir="src/test/java", |
| 145 | version=unit_test.java_class.version, |
| 146 | test=unit_test, msg=unit_test.java_class, |
| 147 | test_data=unit_test.test_data) |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 148 | |
| 149 | def create_of_factories(self): |
Andreas Wundsam | e0d52be | 2013-08-22 07:52:13 -0700 | [diff] [blame] | 150 | for factory in self.java_model.of_factories: |
| 151 | self.render_class(clazz=factory, template="of_factory_interface.java", factory=factory) |
| 152 | for factory_class in factory.factory_classes: |
| 153 | self.render_class(clazz=factory_class, template="of_factory_class.java", factory=factory_class, model=self.java_model) |
| 154 | self.render_class(clazz=java_model.OFGenericClass(package="org.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] | 155 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 156 | def copy_prewrite_tree(basedir): |
| 157 | """ Recursively copy the directory structure from ./java_gen/pre-write |
| 158 | into $basedir""" |
| 159 | print "Copying pre-written files into %s" % basedir |
Andreas Wundsam | ac285ba | 2013-07-24 20:29:42 -0700 | [diff] [blame] | 160 | #subprocess.call("cd java_gen/pre-written && tar cpf - pom.xml | ( cd ../../%s && tar xvpf - )" % basedir, |
| 161 | # shell=True) |
Andreas Wundsam | d83883e | 2013-07-25 14:50:51 -0700 | [diff] [blame] | 162 | os.symlink(os.path.abspath("%s/pre-written/pom.xml" % os.path.dirname(__file__)), "%s/pom.xml" % basedir) |