blob: 1ba0b8fd6af7317d978257768a87528388f493a6 [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
Andreas Wundsam76db0062013-11-15 13:34:41 -080032import logging
Andreas Wundsam27303462013-07-16 12:52:35 -070033import pdb
34import os
35import shutil
36
Andreas Wundsam76db0062013-11-15 13:34:41 -080037import loxi_globals
Andreas Wundsam27303462013-07-16 12:52:35 -070038from loxi_ir import *
39import lang_java
Andreas Wundsame916d6f2013-07-30 11:33:58 -070040import test_data
Andreas Wundsam70aa5492013-10-23 15:26:53 -070041from collections import namedtuple
Yotam Harchol466b3212013-08-15 12:14:46 -070042from import_cleaner import ImportCleaner
Andreas Wundsam27303462013-07-16 12:52:35 -070043
44import loxi_utils.loxi_utils as loxi_utils
45
46import java_gen.java_model as java_model
47
Andreas Wundsam76db0062013-11-15 13:34:41 -080048logger = logging.getLogger(__name__)
49
50def gen_all_java(install_dir):
51 basedir= '%s/openflowj' % install_dir
52 logger.info("Outputting to %s" % basedir)
Andreas Wundsam27303462013-07-16 12:52:35 -070053 if os.path.exists(basedir):
54 shutil.rmtree(basedir)
55 os.makedirs(basedir)
56 copy_prewrite_tree(basedir)
Andreas Wundsam70aa5492013-10-23 15:26:53 -070057 gen = JavaGenerator(basedir, JavaGeneratorOptions(instrument=True))
Andreas Wundsam27303462013-07-16 12:52:35 -070058 gen.create_of_interfaces()
59 gen.create_of_classes()
60 gen.create_of_const_enums()
Andreas Wundsam5204de22013-07-30 11:34:45 -070061 gen.create_of_factories()
Andreas Wundsam27303462013-07-16 12:52:35 -070062
Andreas Wundsam70aa5492013-10-23 15:26:53 -070063JavaGeneratorOptions = namedtuple("JavaGeneratorOptions", ("instrument",))
Andreas Wundsam27303462013-07-16 12:52:35 -070064
65class JavaGenerator(object):
66 templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates')
67
Andreas Wundsam70aa5492013-10-23 15:26:53 -070068 def __init__(self, basedir, gen_opts):
Andreas Wundsam27303462013-07-16 12:52:35 -070069 self.basedir = basedir
70 self.java_model = java_model.model
Andreas Wundsam70aa5492013-10-23 15:26:53 -070071 self.gen_opts = gen_opts
Andreas Wundsam27303462013-07-16 12:52:35 -070072
Andreas Wundsame916d6f2013-07-30 11:33:58 -070073 def render_class(self, clazz, template, src_dir=None, **context):
74 if not src_dir:
75 src_dir = "src/main/java/"
76
Andreas Wundsam27303462013-07-16 12:52:35 -070077 context['class_name'] = clazz.name
78 context['package'] = clazz.package
Andreas Wundsamd10e1c52013-08-01 22:02:49 -070079 context['template_dir'] = self.templates_dir
Andreas Wundsam70aa5492013-10-23 15:26:53 -070080 context['genopts']= self.gen_opts
Andreas Wundsam27303462013-07-16 12:52:35 -070081
Andreas Wundsame916d6f2013-07-30 11:33:58 -070082 filename = os.path.join(self.basedir, src_dir, "%s/%s.java" % (clazz.package.replace(".", "/"), clazz.name))
Andreas Wundsam27303462013-07-16 12:52:35 -070083 dirname = os.path.dirname(filename)
84 if not os.path.exists(dirname):
85 os.makedirs(dirname)
86 prefix = '//::(?=[ \t]|$)'
87 print "filename: %s" % filename
88 with open(filename, "w") as f:
89 loxi_utils.render_template(f, template, [self.templates_dir], context, prefix=prefix)
Andreas Wundsam70aa5492013-10-23 15:26:53 -070090
Yotam Harchol466b3212013-08-15 12:14:46 -070091 try:
92 cleaner = ImportCleaner(filename)
93 cleaner.find_used_imports()
94 cleaner.rewrite_file(filename)
95 except:
96 print 'Cannot clean imports from file %s' % filename
Andreas Wundsam27303462013-07-16 12:52:35 -070097
98 def create_of_const_enums(self):
99 for enum in self.java_model.enums:
100 if enum.name in ["OFPort"]:
101 continue
102 self.render_class(clazz=enum,
103 template='const.java', enum=enum, all_versions=self.java_model.versions)
104
Andreas Wundsambf1dbbd2013-07-30 11:07:59 -0700105 for version in enum.versions:
Yotam Harchol791e4882013-09-05 16:32:56 -0700106 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 -0700107
108 if enum.is_bitmask:
109 self.render_class(clazz=clazz, template="const_set_serializer.java", enum=enum, version=version)
110 else:
111 self.render_class(clazz=clazz, template="const_serializer.java", enum=enum, version=version)
Andreas Wundsambf1dbbd2013-07-30 11:07:59 -0700112
Andreas Wundsam27303462013-07-16 12:52:35 -0700113 def create_of_interfaces(self):
114 """ Create the base interfaces for of classes"""
115 for interface in self.java_model.interfaces:
116 #if not utils.class_is_message(interface.c_name):
117 # continue
118 self.render_class(clazz=interface,
119 template="of_interface.java", msg=interface)
120
121 def create_of_classes(self):
122 """ Create the OF classes with implementations for each of the interfaces and versions """
123 for interface in self.java_model.interfaces:
Andreas Wundsam27303462013-07-16 12:52:35 -0700124 for java_class in interface.versioned_classes:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700125 if self.java_model.generate_class(java_class):
126 if not java_class.is_virtual:
127 self.render_class(clazz=java_class,
128 template='of_class.java', version=java_class.version, msg=java_class,
129 impl_class=java_class.name)
Andreas Wundsam27303462013-07-16 12:52:35 -0700130
Andreas Wundsam001b1822013-08-02 22:25:55 -0700131 self.create_unit_test(java_class.unit_test)
132 else:
133 disc = java_class.discriminator
134 if disc:
135 self.render_class(clazz=java_class,
136 template='of_virtual_class.java', version=java_class.version, msg=java_class,
137 impl_class=java_class.name, model=self.java_model)
138 else:
139 print "Class %s virtual but no discriminator" % java_class.name
140 else:
141 print "Class %s ignored by generate_class" % java_class.name
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700142
Yotam Harchol466b3212013-08-15 12:14:46 -0700143 def create_unit_test(self, unit_tests):
144 if unit_tests.has_test_data:
145 for i in range(unit_tests.length):
146 unit_test = unit_tests.get_test_unit(i)
147 if unit_test.has_test_data:
148 self.render_class(clazz=unit_test,
149 template='unit_test.java', src_dir="src/test/java",
150 version=unit_test.java_class.version,
151 test=unit_test, msg=unit_test.java_class,
152 test_data=unit_test.test_data)
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700153
154 def create_of_factories(self):
Andreas Wundsame0d52be2013-08-22 07:52:13 -0700155 for factory in self.java_model.of_factories:
156 self.render_class(clazz=factory, template="of_factory_interface.java", factory=factory)
157 for factory_class in factory.factory_classes:
158 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 -0700159 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 -0700160
Andreas Wundsam27303462013-07-16 12:52:35 -0700161def copy_prewrite_tree(basedir):
162 """ Recursively copy the directory structure from ./java_gen/pre-write
163 into $basedir"""
164 print "Copying pre-written files into %s" % basedir