blob: 776b8c541db711d60b938f61062327c0411a350c [file] [log] [blame]
Andreas Wundsam27303462013-07-16 12:52:35 -07001# 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
32import pdb
33import os
34import shutil
35
36import of_g
37from loxi_ir import *
38import lang_java
Andreas Wundsame916d6f2013-07-30 11:33:58 -070039import test_data
Yotam Harchol466b3212013-08-15 12:14:46 -070040from import_cleaner import ImportCleaner
Andreas Wundsam27303462013-07-16 12:52:35 -070041
42import loxi_utils.loxi_utils as loxi_utils
43
44import java_gen.java_model as java_model
45
46def gen_all_java(out, name):
Andreas Wundsamd83883e2013-07-25 14:50:51 -070047 basedir= '%s/openflowj' % of_g.options.install_dir
Andreas Wundsam27303462013-07-16 12:52:35 -070048 print "Outputting to %s" % basedir
49 if os.path.exists(basedir):
50 shutil.rmtree(basedir)
51 os.makedirs(basedir)
52 copy_prewrite_tree(basedir)
Andreas Wundsame916d6f2013-07-30 11:33:58 -070053 gen = JavaGenerator(basedir)
Andreas Wundsam27303462013-07-16 12:52:35 -070054 gen.create_of_interfaces()
55 gen.create_of_classes()
56 gen.create_of_const_enums()
Andreas Wundsam5204de22013-07-30 11:34:45 -070057 gen.create_of_factories()
Andreas Wundsam27303462013-07-16 12:52:35 -070058
Andreas Wundsam27303462013-07-16 12:52:35 -070059 out.close()
60
61
62class JavaGenerator(object):
63 templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates')
64
65 def __init__(self, basedir):
66 self.basedir = basedir
67 self.java_model = java_model.model
68
Andreas Wundsame916d6f2013-07-30 11:33:58 -070069 def render_class(self, clazz, template, src_dir=None, **context):
70 if not src_dir:
71 src_dir = "src/main/java/"
72
Andreas Wundsam27303462013-07-16 12:52:35 -070073 context['class_name'] = clazz.name
74 context['package'] = clazz.package
Andreas Wundsamd10e1c52013-08-01 22:02:49 -070075 context['template_dir'] = self.templates_dir
Andreas Wundsam27303462013-07-16 12:52:35 -070076
Andreas Wundsame916d6f2013-07-30 11:33:58 -070077 filename = os.path.join(self.basedir, src_dir, "%s/%s.java" % (clazz.package.replace(".", "/"), clazz.name))
Andreas Wundsam27303462013-07-16 12:52:35 -070078 dirname = os.path.dirname(filename)
79 if not os.path.exists(dirname):
80 os.makedirs(dirname)
81 prefix = '//::(?=[ \t]|$)'
82 print "filename: %s" % filename
83 with open(filename, "w") as f:
84 loxi_utils.render_template(f, template, [self.templates_dir], context, prefix=prefix)
Yotam Harchol466b3212013-08-15 12:14:46 -070085
86 try:
87 cleaner = ImportCleaner(filename)
88 cleaner.find_used_imports()
89 cleaner.rewrite_file(filename)
90 except:
91 print 'Cannot clean imports from file %s' % filename
92
Andreas Wundsam27303462013-07-16 12:52:35 -070093
94 def create_of_const_enums(self):
95 for enum in self.java_model.enums:
96 if enum.name in ["OFPort"]:
97 continue
98 self.render_class(clazz=enum,
99 template='const.java', enum=enum, all_versions=self.java_model.versions)
100
Andreas Wundsambf1dbbd2013-07-30 11:07:59 -0700101 for version in enum.versions:
Yotam Harchol791e4882013-09-05 16:32:56 -0700102 clazz = java_model.OFGenericClass(package="org.projectfloodlight.openflow.protocol.ver{}".format(version.of_version), name="{}SerializerVer{}".format(enum.name, version.of_version))
Andreas Wundsam7cfeac32013-09-17 13:53:48 -0700103
104 if enum.is_bitmask:
105 self.render_class(clazz=clazz, template="const_set_serializer.java", enum=enum, version=version)
106 else:
107 self.render_class(clazz=clazz, template="const_serializer.java", enum=enum, version=version)
Andreas Wundsambf1dbbd2013-07-30 11:07:59 -0700108
Andreas Wundsam27303462013-07-16 12:52:35 -0700109 def create_of_interfaces(self):
110 """ Create the base interfaces for of classes"""
111 for interface in self.java_model.interfaces:
112 #if not utils.class_is_message(interface.c_name):
113 # continue
114 self.render_class(clazz=interface,
115 template="of_interface.java", msg=interface)
116
117 def create_of_classes(self):
118 """ Create the OF classes with implementations for each of the interfaces and versions """
119 for interface in self.java_model.interfaces:
Andreas Wundsam27303462013-07-16 12:52:35 -0700120 for java_class in interface.versioned_classes:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700121 if self.java_model.generate_class(java_class):
122 if not java_class.is_virtual:
123 self.render_class(clazz=java_class,
124 template='of_class.java', version=java_class.version, msg=java_class,
125 impl_class=java_class.name)
Andreas Wundsam27303462013-07-16 12:52:35 -0700126
Andreas Wundsam001b1822013-08-02 22:25:55 -0700127 self.create_unit_test(java_class.unit_test)
128 else:
129 disc = java_class.discriminator
130 if disc:
131 self.render_class(clazz=java_class,
132 template='of_virtual_class.java', version=java_class.version, msg=java_class,
133 impl_class=java_class.name, model=self.java_model)
134 else:
135 print "Class %s virtual but no discriminator" % java_class.name
136 else:
137 print "Class %s ignored by generate_class" % java_class.name
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700138
Yotam Harchol466b3212013-08-15 12:14:46 -0700139 def create_unit_test(self, unit_tests):
140 if unit_tests.has_test_data:
141 for i in range(unit_tests.length):
142 unit_test = unit_tests.get_test_unit(i)
143 if unit_test.has_test_data:
144 self.render_class(clazz=unit_test,
145 template='unit_test.java', src_dir="src/test/java",
146 version=unit_test.java_class.version,
147 test=unit_test, msg=unit_test.java_class,
148 test_data=unit_test.test_data)
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700149
150 def create_of_factories(self):
Andreas Wundsame0d52be2013-08-22 07:52:13 -0700151 for factory in self.java_model.of_factories:
152 self.render_class(clazz=factory, template="of_factory_interface.java", factory=factory)
153 for factory_class in factory.factory_classes:
154 self.render_class(clazz=factory_class, template="of_factory_class.java", factory=factory_class, model=self.java_model)
Yotam Harchol791e4882013-09-05 16:32:56 -0700155 self.render_class(clazz=java_model.OFGenericClass(package="org.projectfloodlight.openflow.protocol", name="OFFactories"), template="of_factories.java", versions=self.java_model.versions)
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700156
Andreas Wundsam27303462013-07-16 12:52:35 -0700157def copy_prewrite_tree(basedir):
158 """ Recursively copy the directory structure from ./java_gen/pre-write
159 into $basedir"""
160 print "Copying pre-written files into %s" % basedir