blob: 66e0454467c66af19a644650f9d5fcbea0400651 [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',
38 'min_length', 'is_fixed_length'])
Rich Lanea06d0c32013-03-25 08:52:03 -070039
Rich Lane002e70c2013-05-09 17:08:16 -070040# Return the name for the generated Python class
41def generate_pyname(cls):
42 if utils.class_is_action(cls):
43 return cls[10:]
Rich Laneea693752013-03-18 11:05:45 -070044 elif utils.class_is_oxm(cls):
Rich Lane002e70c2013-05-09 17:08:16 -070045 return cls[7:]
Rich Laned82c0a62013-05-02 15:40:35 -070046 elif utils.class_is_meter_band(cls):
Rich Lane002e70c2013-05-09 17:08:16 -070047 return cls[14:]
Rich Lanee02314c2013-05-02 16:42:04 -070048 elif utils.class_is_instruction(cls):
Rich Lane002e70c2013-05-09 17:08:16 -070049 return cls[15:]
50 else:
51 return cls[3:]
Rich Lanea06d0c32013-03-25 08:52:03 -070052
Rich Lane002e70c2013-05-09 17:08:16 -070053# Create intermediate representation, extended from the LOXI IR
54# HACK the oftype member attribute is replaced with an OFType instance
Rich Lanea06d0c32013-03-25 08:52:03 -070055def build_ofclasses(version):
Rich Laneb915bec2013-05-09 16:14:45 -070056 blacklist = ["of_experimenter", "of_action_experimenter"]
Rich Lanea06d0c32013-03-25 08:52:03 -070057 ofclasses = []
Rich Lane002e70c2013-05-09 17:08:16 -070058 for ofclass in of_g.ir[version].classes:
59 cls = ofclass.name
Rich Laned82c0a62013-05-02 15:40:35 -070060 if type_maps.class_is_virtual(cls):
61 continue
Rich Lane002e70c2013-05-09 17:08:16 -070062 if cls in blacklist:
Rich Lanea06d0c32013-03-25 08:52:03 -070063 continue
Rich Lanea06d0c32013-03-25 08:52:03 -070064
Rich Lanea06d0c32013-03-25 08:52:03 -070065 members = []
Rich Lanea06d0c32013-03-25 08:52:03 -070066 type_members = []
Rich Lane8ca3b772013-04-30 13:36:55 -070067
Rich Lane002e70c2013-05-09 17:08:16 -070068 for m in ofclass.members:
69 if type(m) == OFTypeMember:
70 members.append(OFTypeMember(
71 name=m.name,
72 oftype=oftype.OFType(m.oftype, version),
73 value=m.value))
Rich Lane8ca3b772013-04-30 13:36:55 -070074 type_members.append(members[-1])
Rich Lane002e70c2013-05-09 17:08:16 -070075 elif type(m) == OFLengthMember:
76 members.append(OFLengthMember(
77 name=m.name,
78 oftype=oftype.OFType(m.oftype, version)))
79 elif type(m) == OFFieldLengthMember:
80 members.append(OFFieldLengthMember(
81 name=m.name,
82 oftype=oftype.OFType(m.oftype, version),
83 field_name=m.field_name))
84 elif type(m) == OFPadMember:
85 members.append(m)
86 elif type(m) == OFDataMember:
87 if utils.class_is_message(ofclass.name) and m.name == 'version':
88 # HACK move to frontend
89 members.append(OFTypeMember(
90 name=m.name,
91 oftype=oftype.OFType(m.oftype, version),
92 value=version))
93 type_members.append(members[-1])
94 else:
95 members.append(OFDataMember(
96 name=m.name,
97 oftype=oftype.OFType(m.oftype, version)))
Rich Lanea06d0c32013-03-25 08:52:03 -070098
99 ofclasses.append(
Rich Lane002e70c2013-05-09 17:08:16 -0700100 PyOFClass(name=cls,
101 pyname=generate_pyname(cls),
102 members=members,
103 type_members=type_members,
104 min_length=of_g.base_length[(cls, version)],
105 is_fixed_length=(cls, version) in of_g.is_fixed_length))
Rich Lanea06d0c32013-03-25 08:52:03 -0700106 return ofclasses
107
108def generate_init(out, name, version):
Rich Laneea693752013-03-18 11:05:45 -0700109 util.render_template(out, 'init.py', version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -0700110
111def generate_action(out, name, version):
112 ofclasses = [x for x in build_ofclasses(version)
113 if utils.class_is_action(x.name)]
Rich Lane3f075972013-03-15 22:56:29 -0700114 util.render_template(out, 'action.py', ofclasses=ofclasses, version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -0700115
Rich Laneea693752013-03-18 11:05:45 -0700116def generate_oxm(out, name, version):
117 ofclasses = [x for x in build_ofclasses(version)
118 if utils.class_is_oxm(x.name)]
119 util.render_template(out, 'oxm.py', ofclasses=ofclasses, version=version)
120
Rich Lanea06d0c32013-03-25 08:52:03 -0700121def generate_common(out, name, version):
122 ofclasses = [x for x in build_ofclasses(version)
123 if not utils.class_is_message(x.name)
124 and not utils.class_is_action(x.name)
Rich Lanee02314c2013-05-02 16:42:04 -0700125 and not utils.class_is_instruction(x.name)
Rich Laned82c0a62013-05-02 15:40:35 -0700126 and not utils.class_is_meter_band(x.name)
Rich Laneea693752013-03-18 11:05:45 -0700127 and not utils.class_is_oxm(x.name)
Rich Lanea06d0c32013-03-25 08:52:03 -0700128 and not utils.class_is_list(x.name)]
Rich Lane3f075972013-03-15 22:56:29 -0700129 util.render_template(out, 'common.py', ofclasses=ofclasses, version=version)
Rich Lanea06d0c32013-03-25 08:52:03 -0700130
131def generate_const(out, name, version):
132 groups = {}
133 for (group, idents) in of_g.identifiers_by_group.items():
134 items = []
135 for ident in idents:
136 info = of_g.identifiers[ident]
137 if version in info["values_by_version"]:
138 items.append((info["ofp_name"], info["values_by_version"][version]))
139 if items:
140 groups[group] = items
141 util.render_template(out, 'const.py', version=version, groups=groups)
142
Rich Lanee02314c2013-05-02 16:42:04 -0700143def generate_instruction(out, name, version):
144 ofclasses = [x for x in build_ofclasses(version)
145 if utils.class_is_instruction(x.name)]
146 util.render_template(out, 'instruction.py', ofclasses=ofclasses, version=version)
147
Rich Lanea06d0c32013-03-25 08:52:03 -0700148def generate_message(out, name, version):
149 ofclasses = [x for x in build_ofclasses(version)
150 if utils.class_is_message(x.name)]
151 util.render_template(out, 'message.py', ofclasses=ofclasses, version=version)
152
Rich Laned82c0a62013-05-02 15:40:35 -0700153def generate_meter_band(out, name, version):
154 ofclasses = [x for x in build_ofclasses(version)
155 if utils.class_is_meter_band(x.name)]
156 util.render_template(out, 'meter_band.py', ofclasses=ofclasses, version=version)
157
Rich Lanea06d0c32013-03-25 08:52:03 -0700158def generate_pp(out, name, version):
159 util.render_template(out, 'pp.py')
160
161def generate_util(out, name, version):
Rich Laneadb79832013-05-02 17:14:33 -0700162 util.render_template(out, 'util.py', version=version)