blob: a664d8718186f069fb6c89c1f5f2e8a0474cae4e [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -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
Rich Lane9d98adf2013-11-29 18:37:24 -080028from collections import defaultdict
Rich Lane1cd97912014-09-26 15:51:38 -070029import os
Andreas Wundsam5630c422013-11-15 13:43:11 -080030import loxi_globals
Andreas Wundsam5420b952013-11-15 13:41:01 -080031import template_utils
Rich Lanea06d0c32013-03-25 08:52:03 -070032import loxi_utils.loxi_utils as utils
33import util
Rich Lane002e70c2013-05-09 17:08:16 -070034from loxi_ir import *
Rich Lanea06d0c32013-03-25 08:52:03 -070035
Rich Lane9d98adf2013-11-29 18:37:24 -080036# Map from inheritance root to module name
37roots = {
38 'of_header': 'message',
39 'of_action': 'action',
Rich Lane2c9938e2013-12-09 17:20:12 -080040 'of_action_id': 'action_id',
Rich Lane9d98adf2013-11-29 18:37:24 -080041 'of_oxm': 'oxm',
42 'of_instruction': 'instruction',
Rich Lane2c9938e2013-12-09 17:20:12 -080043 'of_instruction_id': 'instruction_id',
Rich Lane9d98adf2013-11-29 18:37:24 -080044 'of_meter_band': 'meter_band',
Rich Lanee6321992013-12-03 13:04:52 -080045 'of_bsn_tlv': 'bsn_tlv',
Rich Lane2dd8ed22014-10-03 16:17:09 -070046 'of_port_desc_prop': 'port_desc_prop',
47 'of_port_stats_prop': 'port_stats_prop',
48 'of_port_mod_prop': 'port_mod_prop',
49 'of_table_mod_prop': 'table_mod_prop',
50 'of_queue_desc_prop': 'queue_desc_prop',
51 'of_queue_stats_prop': 'queue_stats_prop',
Rich Lane9d98adf2013-11-29 18:37:24 -080052}
53
54# Return the module and class names for the generated Python class
55def generate_pyname(ofclass):
56 for root, module_name in roots.items():
57 if ofclass.name == root:
58 return module_name, module_name
59 elif ofclass.is_instanceof(root):
60 if root == 'of_header':
61 # The input files don't prefix message names
62 return module_name, ofclass.name[3:]
63 else:
64 return module_name, ofclass.name[len(root)+1:]
65 return 'common', ofclass.name[3:]
Rich Lanea06d0c32013-03-25 08:52:03 -070066
Rich Lane002e70c2013-05-09 17:08:16 -070067# Create intermediate representation, extended from the LOXI IR
Rich Lanea06d0c32013-03-25 08:52:03 -070068def build_ofclasses(version):
Rich Lane9d98adf2013-11-29 18:37:24 -080069 modules = defaultdict(list)
Andreas Wundsam5630c422013-11-15 13:43:11 -080070 for ofclass in loxi_globals.ir[version].classes:
Rich Lane9d98adf2013-11-29 18:37:24 -080071 module_name, ofclass.pyname = generate_pyname(ofclass)
72 modules[module_name].append(ofclass)
73 return modules
Rich Lanea06d0c32013-03-25 08:52:03 -070074
Rich Lane1cd97912014-09-26 15:51:38 -070075def codegen(install_dir):
76 def render(name, template_name=None, **ctx):
77 if template_name is None:
78 template_name = os.path.basename(name)
79 with template_utils.open_output(install_dir, name) as out:
80 util.render_template(out, template_name, **ctx)
Rich Lanea06d0c32013-03-25 08:52:03 -070081
Rich Lane1cd97912014-09-26 15:51:38 -070082 render('__init__.py', template_name='toplevel_init.py')
83 render('pp.py')
84 render('generic_util.py')
Rich Lanea06d0c32013-03-25 08:52:03 -070085
Rich Lane6d753442014-10-08 13:56:12 -070086 for version in loxi_globals.OFVersions.all_supported:
87 subdir = 'of' + version.version.replace('.', '')
Rich Lane1cd97912014-09-26 15:51:38 -070088 modules = build_ofclasses(version)
Rich Lane2c9938e2013-12-09 17:20:12 -080089
Rich Lane1cd97912014-09-26 15:51:38 -070090 render(os.path.join(subdir, '__init__.py'), template_name='init.py',
91 version=version, modules=modules.keys())
Rich Laneea693752013-03-18 11:05:45 -070092
Rich Lane1cd97912014-09-26 15:51:38 -070093 render(os.path.join(subdir, 'util.py'), version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -070094
Rich Lane1cd97912014-09-26 15:51:38 -070095 render(os.path.join(subdir, 'const.py'), version=version,
96 enums=loxi_globals.ir[version].enums)
Rich Lanea06d0c32013-03-25 08:52:03 -070097
Rich Lane1cd97912014-09-26 15:51:38 -070098 args_by_module = {
99 'common': { 'extra_template' : '_common_extra.py' },
100 'message': { 'extra_template' : '_message_extra.py' },
101 }
Rich Lanee02314c2013-05-02 16:42:04 -0700102
Rich Lane1cd97912014-09-26 15:51:38 -0700103 for name, ofclasses in modules.items():
104 args = args_by_module.get(name, {})
105 render(os.path.join(subdir, name + '.py'), template_name='module.py',
106 version=version, ofclasses=ofclasses, modules=modules.keys(),
107 **args)