blob: 9c32e7ff332a8e6b4d91db6fe86cea7b5e34ead5 [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
Andreas Wundsam5630c422013-11-15 13:43:11 -080029import loxi_globals
Rich Lanec2685792013-04-30 14:08:33 -070030import struct
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
34import oftype
Rich Lane002e70c2013-05-09 17:08:16 -070035from loxi_ir import *
Rich Lanea06d0c32013-03-25 08:52:03 -070036
Rich Lane9d98adf2013-11-29 18:37:24 -080037modules_by_version = {}
Rich Lanefa931272013-11-10 17:43:50 -080038
Rich Lane9d98adf2013-11-29 18:37:24 -080039# Map from inheritance root to module name
40roots = {
41 'of_header': 'message',
42 'of_action': 'action',
Rich Lane2c9938e2013-12-09 17:20:12 -080043 'of_action_id': 'action_id',
Rich Lane9d98adf2013-11-29 18:37:24 -080044 'of_oxm': 'oxm',
45 'of_instruction': 'instruction',
Rich Lane2c9938e2013-12-09 17:20:12 -080046 'of_instruction_id': 'instruction_id',
Rich Lane9d98adf2013-11-29 18:37:24 -080047 'of_meter_band': 'meter_band',
Rich Lanee6321992013-12-03 13:04:52 -080048 'of_bsn_tlv': 'bsn_tlv',
Rich Lane9d98adf2013-11-29 18:37:24 -080049}
50
51# Return the module and class names for the generated Python class
52def generate_pyname(ofclass):
53 for root, module_name in roots.items():
54 if ofclass.name == root:
55 return module_name, module_name
56 elif ofclass.is_instanceof(root):
57 if root == 'of_header':
58 # The input files don't prefix message names
59 return module_name, ofclass.name[3:]
60 else:
61 return module_name, ofclass.name[len(root)+1:]
62 return 'common', ofclass.name[3:]
Rich Lanea06d0c32013-03-25 08:52:03 -070063
Rich Lane002e70c2013-05-09 17:08:16 -070064# Create intermediate representation, extended from the LOXI IR
Rich Lanea06d0c32013-03-25 08:52:03 -070065def build_ofclasses(version):
Rich Lane9d98adf2013-11-29 18:37:24 -080066 modules = defaultdict(list)
Andreas Wundsam5630c422013-11-15 13:43:11 -080067 for ofclass in loxi_globals.ir[version].classes:
Rich Lane9d98adf2013-11-29 18:37:24 -080068 module_name, ofclass.pyname = generate_pyname(ofclass)
69 modules[module_name].append(ofclass)
70 return modules
Rich Lanea06d0c32013-03-25 08:52:03 -070071
72def generate_init(out, name, version):
Rich Laneea693752013-03-18 11:05:45 -070073 util.render_template(out, 'init.py', version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -070074
75def generate_action(out, name, version):
Rich Lane3b9b28e2013-11-29 20:00:45 -080076 util.render_template(out, 'module.py',
Rich Lane9d98adf2013-11-29 18:37:24 -080077 ofclasses=modules_by_version[version]['action'],
78 version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -070079
Rich Lane2c9938e2013-12-09 17:20:12 -080080def generate_action_id(out, name, version):
81 util.render_template(out, 'module.py',
82 ofclasses=modules_by_version[version]['action_id'],
83 version=version)
84
Rich Laneea693752013-03-18 11:05:45 -070085def generate_oxm(out, name, version):
Rich Lane3b9b28e2013-11-29 20:00:45 -080086 util.render_template(out, 'module.py',
Rich Lane9d98adf2013-11-29 18:37:24 -080087 ofclasses=modules_by_version[version]['oxm'],
88 version=version)
Rich Laneea693752013-03-18 11:05:45 -070089
Rich Lanea06d0c32013-03-25 08:52:03 -070090def generate_common(out, name, version):
Rich Lane3b9b28e2013-11-29 20:00:45 -080091 util.render_template(out, 'module.py',
Rich Lane9d98adf2013-11-29 18:37:24 -080092 ofclasses=modules_by_version[version]['common'],
Rich Lane3b9b28e2013-11-29 20:00:45 -080093 version=version,
94 extra_template='_common_extra.py')
Rich Lanea06d0c32013-03-25 08:52:03 -070095
96def generate_const(out, name, version):
Rich Lanedffcf852013-05-10 10:40:46 -070097 util.render_template(out, 'const.py', version=version,
Andreas Wundsam5630c422013-11-15 13:43:11 -080098 enums=loxi_globals.ir[version].enums)
Rich Lanea06d0c32013-03-25 08:52:03 -070099
Rich Lanee02314c2013-05-02 16:42:04 -0700100def generate_instruction(out, name, version):
Rich Lane3b9b28e2013-11-29 20:00:45 -0800101 util.render_template(out, 'module.py',
Rich Lane9d98adf2013-11-29 18:37:24 -0800102 ofclasses=modules_by_version[version]['instruction'],
103 version=version)
Rich Lanee02314c2013-05-02 16:42:04 -0700104
Rich Lane2c9938e2013-12-09 17:20:12 -0800105def generate_instruction_id(out, name, version):
106 util.render_template(out, 'module.py',
107 ofclasses=modules_by_version[version]['instruction_id'],
108 version=version)
109
Rich Lanea06d0c32013-03-25 08:52:03 -0700110def generate_message(out, name, version):
Rich Lane3b9b28e2013-11-29 20:00:45 -0800111 util.render_template(out, 'module.py',
Rich Lane9d98adf2013-11-29 18:37:24 -0800112 ofclasses=modules_by_version[version]['message'],
Rich Lane3b9b28e2013-11-29 20:00:45 -0800113 version=version,
114 extra_template='_message_extra.py')
Rich Lanea06d0c32013-03-25 08:52:03 -0700115
Rich Laned82c0a62013-05-02 15:40:35 -0700116def generate_meter_band(out, name, version):
Rich Lane3b9b28e2013-11-29 20:00:45 -0800117 util.render_template(out, 'module.py',
Rich Lane9d98adf2013-11-29 18:37:24 -0800118 ofclasses=modules_by_version[version]['meter_band'],
119 version=version)
Rich Laned82c0a62013-05-02 15:40:35 -0700120
Rich Lanea06d0c32013-03-25 08:52:03 -0700121def generate_util(out, name, version):
Rich Laneadb79832013-05-02 17:14:33 -0700122 util.render_template(out, 'util.py', version=version)
Rich Lanefa931272013-11-10 17:43:50 -0800123
Rich Lanee6321992013-12-03 13:04:52 -0800124def generate_bsn_tlv(out, name, version):
125 util.render_template(out, 'module.py',
126 ofclasses=modules_by_version[version]['bsn_tlv'],
127 version=version)
128
Rich Lanefa931272013-11-10 17:43:50 -0800129def init():
Andreas Wundsam5630c422013-11-15 13:43:11 -0800130 for version in loxi_globals.OFVersions.target_versions:
Rich Lane9d98adf2013-11-29 18:37:24 -0800131 modules_by_version[version] = build_ofclasses(version)