blob: 6570938e67b5252f41304ccec0dc61b677a73618 [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
28from collections import namedtuple
Rich Lanec2685792013-04-30 14:08:33 -070029import struct
Rich Lanea06d0c32013-03-25 08:52:03 -070030import of_g
31import loxi_front_end.type_maps as type_maps
32import 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 Lane002e70c2013-05-09 17:08:16 -070037PyOFClass = namedtuple('PyOFClass', ['name', 'pyname', 'members', 'type_members',
Rich Laneb564efc2013-07-23 14:19:29 -070038 'min_length', 'is_fixed_length',
39 'has_internal_alignment', 'has_external_alignment'])
Rich Lanea06d0c32013-03-25 08:52:03 -070040
Rich Lane002e70c2013-05-09 17:08:16 -070041# Return the name for the generated Python class
42def generate_pyname(cls):
43 if utils.class_is_action(cls):
44 return cls[10:]
Rich Laneea693752013-03-18 11:05:45 -070045 elif utils.class_is_oxm(cls):
Rich Lane002e70c2013-05-09 17:08:16 -070046 return cls[7:]
Rich Laned82c0a62013-05-02 15:40:35 -070047 elif utils.class_is_meter_band(cls):
Rich Lane002e70c2013-05-09 17:08:16 -070048 return cls[14:]
Rich Lanee02314c2013-05-02 16:42:04 -070049 elif utils.class_is_instruction(cls):
Rich Lane002e70c2013-05-09 17:08:16 -070050 return cls[15:]
51 else:
52 return cls[3:]
Rich Lanea06d0c32013-03-25 08:52:03 -070053
Rich Lane002e70c2013-05-09 17:08:16 -070054# Create intermediate representation, extended from the LOXI IR
55# HACK the oftype member attribute is replaced with an OFType instance
Rich Lanea06d0c32013-03-25 08:52:03 -070056def build_ofclasses(version):
Rich Laneb915bec2013-05-09 16:14:45 -070057 blacklist = ["of_experimenter", "of_action_experimenter"]
Rich Lanea06d0c32013-03-25 08:52:03 -070058 ofclasses = []
Rich Lane002e70c2013-05-09 17:08:16 -070059 for ofclass in of_g.ir[version].classes:
60 cls = ofclass.name
Rich Laned82c0a62013-05-02 15:40:35 -070061 if type_maps.class_is_virtual(cls):
62 continue
Rich Lane002e70c2013-05-09 17:08:16 -070063 if cls in blacklist:
Rich Lanea06d0c32013-03-25 08:52:03 -070064 continue
Rich Lanea06d0c32013-03-25 08:52:03 -070065
Rich Lanea06d0c32013-03-25 08:52:03 -070066 members = []
Rich Lanea06d0c32013-03-25 08:52:03 -070067 type_members = []
Rich Lane8ca3b772013-04-30 13:36:55 -070068
Rich Lane002e70c2013-05-09 17:08:16 -070069 for m in ofclass.members:
70 if type(m) == OFTypeMember:
Rich Laneda11f8b2013-06-19 15:53:25 -070071 members.append(m)
Rich Lane8ca3b772013-04-30 13:36:55 -070072 type_members.append(members[-1])
Rich Lane002e70c2013-05-09 17:08:16 -070073 elif type(m) == OFLengthMember:
Rich Laneda11f8b2013-06-19 15:53:25 -070074 members.append(m)
Rich Lane002e70c2013-05-09 17:08:16 -070075 elif type(m) == OFFieldLengthMember:
Rich Laneda11f8b2013-06-19 15:53:25 -070076 members.append(m)
Rich Lane002e70c2013-05-09 17:08:16 -070077 elif type(m) == OFPadMember:
78 members.append(m)
79 elif type(m) == OFDataMember:
80 if utils.class_is_message(ofclass.name) and m.name == 'version':
81 # HACK move to frontend
82 members.append(OFTypeMember(
83 name=m.name,
Rich Laneda11f8b2013-06-19 15:53:25 -070084 oftype=m.oftype,
Rich Lane002e70c2013-05-09 17:08:16 -070085 value=version))
86 type_members.append(members[-1])
87 else:
Rich Laneda11f8b2013-06-19 15:53:25 -070088 members.append(m)
Rich Lanea06d0c32013-03-25 08:52:03 -070089
90 ofclasses.append(
Rich Lane002e70c2013-05-09 17:08:16 -070091 PyOFClass(name=cls,
92 pyname=generate_pyname(cls),
93 members=members,
94 type_members=type_members,
95 min_length=of_g.base_length[(cls, version)],
Rich Laneb564efc2013-07-23 14:19:29 -070096 is_fixed_length=(cls, version) in of_g.is_fixed_length,
97 has_internal_alignment=cls == 'of_action_set_field',
98 has_external_alignment=cls == 'of_match_v3'))
Rich Lanea06d0c32013-03-25 08:52:03 -070099 return ofclasses
100
101def generate_init(out, name, version):
Rich Laneea693752013-03-18 11:05:45 -0700102 util.render_template(out, 'init.py', version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -0700103
104def generate_action(out, name, version):
105 ofclasses = [x for x in build_ofclasses(version)
106 if utils.class_is_action(x.name)]
Rich Lane3f075972013-03-15 22:56:29 -0700107 util.render_template(out, 'action.py', ofclasses=ofclasses, version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -0700108
Rich Laneea693752013-03-18 11:05:45 -0700109def generate_oxm(out, name, version):
110 ofclasses = [x for x in build_ofclasses(version)
111 if utils.class_is_oxm(x.name)]
112 util.render_template(out, 'oxm.py', ofclasses=ofclasses, version=version)
113
Rich Lanea06d0c32013-03-25 08:52:03 -0700114def generate_common(out, name, version):
115 ofclasses = [x for x in build_ofclasses(version)
116 if not utils.class_is_message(x.name)
117 and not utils.class_is_action(x.name)
Rich Lanee02314c2013-05-02 16:42:04 -0700118 and not utils.class_is_instruction(x.name)
Rich Laned82c0a62013-05-02 15:40:35 -0700119 and not utils.class_is_meter_band(x.name)
Rich Laneea693752013-03-18 11:05:45 -0700120 and not utils.class_is_oxm(x.name)
Rich Lanea06d0c32013-03-25 08:52:03 -0700121 and not utils.class_is_list(x.name)]
Rich Lane3f075972013-03-15 22:56:29 -0700122 util.render_template(out, 'common.py', ofclasses=ofclasses, version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -0700123
124def generate_const(out, name, version):
Rich Lanedffcf852013-05-10 10:40:46 -0700125 util.render_template(out, 'const.py', version=version,
126 enums=of_g.ir[version].enums)
Rich Lanea06d0c32013-03-25 08:52:03 -0700127
Rich Lanee02314c2013-05-02 16:42:04 -0700128def generate_instruction(out, name, version):
129 ofclasses = [x for x in build_ofclasses(version)
130 if utils.class_is_instruction(x.name)]
131 util.render_template(out, 'instruction.py', ofclasses=ofclasses, version=version)
132
Rich Lanea06d0c32013-03-25 08:52:03 -0700133def generate_message(out, name, version):
134 ofclasses = [x for x in build_ofclasses(version)
135 if utils.class_is_message(x.name)]
136 util.render_template(out, 'message.py', ofclasses=ofclasses, version=version)
137
Rich Laned82c0a62013-05-02 15:40:35 -0700138def generate_meter_band(out, name, version):
139 ofclasses = [x for x in build_ofclasses(version)
140 if utils.class_is_meter_band(x.name)]
141 util.render_template(out, 'meter_band.py', ofclasses=ofclasses, version=version)
142
Rich Lanea06d0c32013-03-25 08:52:03 -0700143def generate_pp(out, name, version):
144 util.render_template(out, 'pp.py')
145
146def generate_util(out, name, version):
Rich Laneadb79832013-05-02 17:14:33 -0700147 util.render_template(out, 'util.py', version=version)