blob: 3253eaff22c089c4b63fee56f24cc696ed62394a [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
39
40import loxi_utils.loxi_utils as loxi_utils
41
42import java_gen.java_model as java_model
43
44def gen_all_java(out, name):
Andreas Wundsamac285ba2013-07-24 20:29:42 -070045 basedir='loxi_output/openflowj'
Andreas Wundsam27303462013-07-16 12:52:35 -070046 srcdir = "%s/src/main/java/" % basedir
47 print "Outputting to %s" % basedir
48 if os.path.exists(basedir):
49 shutil.rmtree(basedir)
50 os.makedirs(basedir)
51 copy_prewrite_tree(basedir)
52
53 gen = JavaGenerator(srcdir)
54 gen.create_of_interfaces()
55 gen.create_of_classes()
56 gen.create_of_const_enums()
57
58 with open('README.java-lang') as readme_src:
59 out.writelines(readme_src.readlines())
60 out.close()
61
62
63class JavaGenerator(object):
64 templates_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates')
65
66 def __init__(self, basedir):
67 self.basedir = basedir
68 self.java_model = java_model.model
69
70 def render_class(self, clazz, template, **context):
71 context['class_name'] = clazz.name
72 context['package'] = clazz.package
73
74 filename = os.path.join(self.basedir, "%s/%s.java" % (clazz.package.replace(".", "/"), clazz.name))
75 dirname = os.path.dirname(filename)
76 if not os.path.exists(dirname):
77 os.makedirs(dirname)
78 prefix = '//::(?=[ \t]|$)'
79 print "filename: %s" % filename
80 with open(filename, "w") as f:
81 loxi_utils.render_template(f, template, [self.templates_dir], context, prefix=prefix)
82
83 def create_of_const_enums(self):
84 for enum in self.java_model.enums:
85 if enum.name in ["OFPort"]:
86 continue
87 self.render_class(clazz=enum,
88 template='const.java', enum=enum, all_versions=self.java_model.versions)
89
90 def create_of_interfaces(self):
91 """ Create the base interfaces for of classes"""
92 for interface in self.java_model.interfaces:
93 #if not utils.class_is_message(interface.c_name):
94 # continue
95 self.render_class(clazz=interface,
96 template="of_interface.java", msg=interface)
97
98 def create_of_classes(self):
99 """ Create the OF classes with implementations for each of the interfaces and versions """
100 for interface in self.java_model.interfaces:
101 if not loxi_utils.class_is_message(interface.c_name) and not loxi_utils.class_is_oxm(interface.c_name):
102 continue
103 for java_class in interface.versioned_classes:
104 self.render_class(clazz=java_class,
105 template='of_class.java', version=java_class.version, msg=java_class,
106 impl_class=java_class.name)
107
108
109def mkdir_p(path):
110 """ Emulates `mkdir -p` """
111 try:
112 os.makedirs(path)
113 except OSError as exc: # Python >2.5
114 if exc.errno == errno.EEXIST:
115 pass
116 else: raise
117
118def copy_file_with_boiler_plate(src_name, dst_name, with_boiler=True):
119 with open("java_gen/pre-written/%s" % src_name, "r") as src:
120 with open(dst_name, "w") as dst:
121 if with_boiler:
122 print_boiler_plate(os.path.basename(dst_name), dst)
123 dst.writelines(src.readlines())
124
125def frob(s, **kwargs):
126 """ Step through string s and for each key in kwargs,
127 replace $key with kwargs[key] in s.
128 """
129 for k,v in kwargs.iteritems():
130 s = s.replace('$%s' % k, v)
131 return s
132
133def copy_prewrite_tree(basedir):
134 """ Recursively copy the directory structure from ./java_gen/pre-write
135 into $basedir"""
136 print "Copying pre-written files into %s" % basedir
Andreas Wundsamac285ba2013-07-24 20:29:42 -0700137 #subprocess.call("cd java_gen/pre-written && tar cpf - pom.xml | ( cd ../../%s && tar xvpf - )" % basedir,
138 # shell=True)
139 os.symlink(os.path.abspath("java_gen/pre-written/pom.xml"), "%s/pom.xml" % basedir)