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 | |
| 28 | from collections import namedtuple |
| 29 | import of_g |
| 30 | import loxi_front_end.type_maps as type_maps |
| 31 | import loxi_utils.loxi_utils as utils |
| 32 | import util |
| 33 | import oftype |
| 34 | |
| 35 | OFClass = namedtuple('OFClass', ['name', 'pyname', |
| 36 | 'members', 'length_member', 'type_members', |
| 37 | 'min_length', 'is_fixed_length']) |
| 38 | Member = namedtuple('Member', ['name', 'oftype', 'offset', 'skip']) |
| 39 | LengthMember = namedtuple('LengthMember', ['name', 'oftype', 'offset']) |
| 40 | TypeMember = namedtuple('TypeMember', ['name', 'oftype', 'offset', 'value']) |
| 41 | |
| 42 | def get_type_values(cls, version): |
| 43 | """ |
| 44 | Returns a map from the name of the type member to its value. |
| 45 | """ |
| 46 | type_values = {} |
| 47 | |
| 48 | # Primary wire type |
| 49 | if utils.class_is_message(cls): |
| 50 | type_values['version'] = 'const.OFP_VERSION' |
| 51 | type_values['type'] = util.constant_for_value(version, "ofp_type", util.primary_wire_type(cls, version)) |
| 52 | if cls in type_maps.flow_mod_list: |
| 53 | type_values['_command'] = util.constant_for_value(version, "ofp_flow_mod_command", |
| 54 | type_maps.flow_mod_types[version][cls[8:]]) |
| 55 | if cls in type_maps.stats_request_list: |
| 56 | type_values['stats_type'] = util.constant_for_value(version, "ofp_stats_types", |
| 57 | type_maps.stats_types[version][cls[3:-14]]) |
| 58 | if cls in type_maps.stats_reply_list: |
| 59 | type_values['stats_type'] = util.constant_for_value(version, "ofp_stats_types", |
| 60 | type_maps.stats_types[version][cls[3:-12]]) |
| 61 | if type_maps.message_is_extension(cls, version): |
| 62 | type_values['experimenter'] = '%#x' % type_maps.extension_to_experimenter_id(cls) |
| 63 | type_values['subtype'] = type_maps.extension_message_to_subtype(cls, version) |
| 64 | elif utils.class_is_action(cls): |
| 65 | type_values['type'] = util.constant_for_value(version, "ofp_action_type", util.primary_wire_type(cls, version)) |
| 66 | if type_maps.action_is_extension(cls, version): |
| 67 | type_values['experimenter'] = '%#x' % type_maps.extension_to_experimenter_id(cls) |
| 68 | type_values['subtype'] = type_maps.extension_action_to_subtype(cls, version) |
| 69 | elif utils.class_is_queue_prop(cls): |
| 70 | type_values['type'] = util.constant_for_value(version, "ofp_queue_properties", util.primary_wire_type(cls, version)) |
| 71 | |
| 72 | return type_values |
| 73 | |
| 74 | # Create intermediate representation |
| 75 | def build_ofclasses(version): |
| 76 | blacklist = ["of_action", "of_action_header", "of_header", "of_queue_prop", |
| 77 | "of_queue_prop_header", "of_experimenter", "of_action_experimenter"] |
| 78 | ofclasses = [] |
| 79 | for cls in of_g.standard_class_order: |
| 80 | if version not in of_g.unified[cls] or cls in blacklist: |
| 81 | continue |
| 82 | unified_class = util.lookup_unified_class(cls, version) |
| 83 | |
| 84 | # Name for the generated Python class |
| 85 | if utils.class_is_action(cls): |
| 86 | pyname = cls[10:] |
| 87 | else: |
| 88 | pyname = cls[3:] |
| 89 | |
| 90 | type_values = get_type_values(cls, version) |
| 91 | members = [] |
| 92 | |
| 93 | length_member = None |
| 94 | type_members = [] |
| 95 | |
| 96 | for member in unified_class['members']: |
| 97 | if member['name'] in ['length', 'len']: |
| 98 | length_member = LengthMember(name=member['name'], |
| 99 | offset=member['offset'], |
| 100 | oftype=oftype.OFType(member['m_type'], version)) |
| 101 | elif member['name'] in type_values: |
| 102 | type_members.append(TypeMember(name=member['name'], |
| 103 | offset=member['offset'], |
| 104 | oftype=oftype.OFType(member['m_type'], version), |
| 105 | value=type_values[member['name']])) |
| 106 | else: |
| 107 | # HACK ensure member names are unique |
| 108 | if member['name'] == "pad" and \ |
| 109 | [x for x in members if x.name == 'pad']: |
| 110 | m_name = "pad2" |
| 111 | else: |
| 112 | m_name = member['name'] |
| 113 | members.append(Member(name=m_name, |
| 114 | oftype=oftype.OFType(member['m_type'], version), |
| 115 | offset=member['offset'], |
| 116 | skip=member['name'] in of_g.skip_members)) |
| 117 | |
| 118 | ofclasses.append( |
| 119 | OFClass(name=cls, |
| 120 | pyname=pyname, |
| 121 | members=members, |
| 122 | length_member=length_member, |
| 123 | type_members=type_members, |
| 124 | min_length=of_g.base_length[(cls, version)], |
| 125 | is_fixed_length=(cls, version) in of_g.is_fixed_length)) |
| 126 | return ofclasses |
| 127 | |
| 128 | def generate_init(out, name, version): |
| 129 | util.render_template(out, 'init.py') |
| 130 | |
| 131 | def generate_action(out, name, version): |
| 132 | ofclasses = [x for x in build_ofclasses(version) |
| 133 | if utils.class_is_action(x.name)] |
| 134 | util.render_template(out, 'action.py', ofclasses=ofclasses) |
| 135 | |
| 136 | def generate_common(out, name, version): |
| 137 | ofclasses = [x for x in build_ofclasses(version) |
| 138 | if not utils.class_is_message(x.name) |
| 139 | and not utils.class_is_action(x.name) |
| 140 | and not utils.class_is_list(x.name)] |
| 141 | util.render_template(out, 'common.py', ofclasses=ofclasses) |
| 142 | |
| 143 | def generate_const(out, name, version): |
| 144 | groups = {} |
| 145 | for (group, idents) in of_g.identifiers_by_group.items(): |
| 146 | items = [] |
| 147 | for ident in idents: |
| 148 | info = of_g.identifiers[ident] |
| 149 | if version in info["values_by_version"]: |
| 150 | items.append((info["ofp_name"], info["values_by_version"][version])) |
| 151 | if items: |
| 152 | groups[group] = items |
| 153 | util.render_template(out, 'const.py', version=version, groups=groups) |
| 154 | |
| 155 | def generate_message(out, name, version): |
| 156 | ofclasses = [x for x in build_ofclasses(version) |
| 157 | if utils.class_is_message(x.name)] |
| 158 | util.render_template(out, 'message.py', ofclasses=ofclasses, version=version) |
| 159 | |
| 160 | def generate_pp(out, name, version): |
| 161 | util.render_template(out, 'pp.py') |
| 162 | |
| 163 | def generate_util(out, name, version): |
| 164 | util.render_template(out, 'util.py') |