Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -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 | |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 28 | from collections import defaultdict |
Rich Lane | 1cd9791 | 2014-09-26 15:51:38 -0700 | [diff] [blame] | 29 | import os |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 30 | import loxi_globals |
Andreas Wundsam | 5420b95 | 2013-11-15 13:41:01 -0800 | [diff] [blame] | 31 | import template_utils |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 32 | import loxi_utils.loxi_utils as utils |
| 33 | import util |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 34 | from loxi_ir import * |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 35 | |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 36 | # Map from inheritance root to module name |
| 37 | roots = { |
| 38 | 'of_header': 'message', |
| 39 | 'of_action': 'action', |
Rich Lane | 2c9938e | 2013-12-09 17:20:12 -0800 | [diff] [blame] | 40 | 'of_action_id': 'action_id', |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 41 | 'of_oxm': 'oxm', |
| 42 | 'of_instruction': 'instruction', |
Rich Lane | 2c9938e | 2013-12-09 17:20:12 -0800 | [diff] [blame] | 43 | 'of_instruction_id': 'instruction_id', |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 44 | 'of_meter_band': 'meter_band', |
Rich Lane | e632199 | 2013-12-03 13:04:52 -0800 | [diff] [blame] | 45 | 'of_bsn_tlv': 'bsn_tlv', |
Rich Lane | 2dd8ed2 | 2014-10-03 16:17:09 -0700 | [diff] [blame] | 46 | '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 Lane | babfc33 | 2014-10-17 18:05:19 -0700 | [diff] [blame] | 52 | 'of_async_config_prop': 'async_config_prop', |
| 53 | 'of_bundle_prop': 'bundle_prop', |
| 54 | #'of_queue_prop': 'queue_prop', |
| 55 | 'of_role_prop': 'role_prop', |
| 56 | #'of_table_feature_prop': 'table_feature_prop', |
| 57 | 'of_table_mod_prop': 'table_mod_prop', |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | # Return the module and class names for the generated Python class |
| 61 | def generate_pyname(ofclass): |
| 62 | for root, module_name in roots.items(): |
| 63 | if ofclass.name == root: |
| 64 | return module_name, module_name |
| 65 | elif ofclass.is_instanceof(root): |
| 66 | if root == 'of_header': |
| 67 | # The input files don't prefix message names |
| 68 | return module_name, ofclass.name[3:] |
| 69 | else: |
| 70 | return module_name, ofclass.name[len(root)+1:] |
| 71 | return 'common', ofclass.name[3:] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 72 | |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 73 | # Create intermediate representation, extended from the LOXI IR |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 74 | def build_ofclasses(version): |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 75 | modules = defaultdict(list) |
Andreas Wundsam | 5630c42 | 2013-11-15 13:43:11 -0800 | [diff] [blame] | 76 | for ofclass in loxi_globals.ir[version].classes: |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 77 | module_name, ofclass.pyname = generate_pyname(ofclass) |
| 78 | modules[module_name].append(ofclass) |
| 79 | return modules |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 80 | |
Rich Lane | 1cd9791 | 2014-09-26 15:51:38 -0700 | [diff] [blame] | 81 | def codegen(install_dir): |
| 82 | def render(name, template_name=None, **ctx): |
| 83 | if template_name is None: |
| 84 | template_name = os.path.basename(name) |
| 85 | with template_utils.open_output(install_dir, name) as out: |
| 86 | util.render_template(out, template_name, **ctx) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 87 | |
Rich Lane | 1cd9791 | 2014-09-26 15:51:38 -0700 | [diff] [blame] | 88 | render('__init__.py', template_name='toplevel_init.py') |
| 89 | render('pp.py') |
| 90 | render('generic_util.py') |
Rich Lane | 2ee3d16 | 2015-05-07 13:53:05 -0700 | [diff] [blame] | 91 | render('connection.py') |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 92 | |
Rich Lane | 6d75344 | 2014-10-08 13:56:12 -0700 | [diff] [blame] | 93 | for version in loxi_globals.OFVersions.all_supported: |
| 94 | subdir = 'of' + version.version.replace('.', '') |
Rich Lane | 1cd9791 | 2014-09-26 15:51:38 -0700 | [diff] [blame] | 95 | modules = build_ofclasses(version) |
Rich Lane | 2c9938e | 2013-12-09 17:20:12 -0800 | [diff] [blame] | 96 | |
Rich Lane | 1cd9791 | 2014-09-26 15:51:38 -0700 | [diff] [blame] | 97 | render(os.path.join(subdir, '__init__.py'), template_name='init.py', |
| 98 | version=version, modules=modules.keys()) |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 99 | |
Rich Lane | 1cd9791 | 2014-09-26 15:51:38 -0700 | [diff] [blame] | 100 | render(os.path.join(subdir, 'util.py'), version=version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 101 | |
Rich Lane | 1cd9791 | 2014-09-26 15:51:38 -0700 | [diff] [blame] | 102 | render(os.path.join(subdir, 'const.py'), version=version, |
| 103 | enums=loxi_globals.ir[version].enums) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 104 | |
Rich Lane | 1cd9791 | 2014-09-26 15:51:38 -0700 | [diff] [blame] | 105 | args_by_module = { |
| 106 | 'common': { 'extra_template' : '_common_extra.py' }, |
| 107 | 'message': { 'extra_template' : '_message_extra.py' }, |
| 108 | } |
Rich Lane | e02314c | 2013-05-02 16:42:04 -0700 | [diff] [blame] | 109 | |
Rich Lane | 1cd9791 | 2014-09-26 15:51:38 -0700 | [diff] [blame] | 110 | for name, ofclasses in modules.items(): |
| 111 | args = args_by_module.get(name, {}) |
| 112 | render(os.path.join(subdir, name + '.py'), template_name='module.py', |
Rich Lane | edbb4cb | 2015-01-20 15:42:25 -0800 | [diff] [blame] | 113 | version=version, ofclasses=ofclasses, subdir=subdir, |
Rich Lane | 1cd9791 | 2014-09-26 15:51:38 -0700 | [diff] [blame] | 114 | **args) |