Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -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 | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 28 | import copy |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 29 | import of_g |
| 30 | import loxi_front_end.type_maps as type_maps |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 31 | from loxi_ir import * |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 32 | |
| 33 | class InputError(Exception): |
| 34 | pass |
| 35 | |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 36 | # TODO handle type members |
| 37 | def create_member(m_ast): |
| 38 | if m_ast[0] == 'pad': |
| 39 | return OFPadMember(length=m_ast[1]) |
| 40 | elif m_ast[1] == 'length' or m_ast[1] == 'len': # Should be moved to parser |
| 41 | return OFLengthMember(name=m_ast[1], oftype=m_ast[0]) |
| 42 | elif m_ast[1] == 'actions_len': |
| 43 | # HACK only usage so far |
| 44 | return OFFieldLengthMember(name=m_ast[1], oftype=m_ast[0], field_name='actions') |
| 45 | else: |
| 46 | return OFDataMember(name=m_ast[1], oftype=m_ast[0]) |
| 47 | |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 48 | def create_ofinput(ast): |
| 49 | """ |
| 50 | Create an OFInput from an AST |
| 51 | |
| 52 | @param ast An AST as returned by loxi_front_end.parser.parse |
| 53 | |
| 54 | @returns An OFInput object |
| 55 | """ |
| 56 | |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 57 | ofinput = OFInput(wire_versions=set(), classes=[], enums=[]) |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 58 | |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 59 | for decl_ast in ast: |
| 60 | if decl_ast[0] == 'struct': |
| 61 | members = [create_member(m_ast) for m_ast in decl_ast[2]] |
| 62 | ofclass = OFClass(name=decl_ast[1], members=members) |
| 63 | ofinput.classes.append(ofclass) |
| 64 | if ofclass.name in type_maps.inheritance_map: |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 65 | # Clone class into header class and add to list |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 66 | # TODO figure out if these are actually used |
| 67 | ofclass_header = OFClass(ofclass.name + '_header', |
| 68 | copy.deepcopy(members)) |
| 69 | ofinput.classes.append(ofclass_header) |
| 70 | if decl_ast[0] == 'enum': |
| 71 | enum = OFEnum(name=decl_ast[1], values=[(x[0], x[1]) for x in decl_ast[2]]) |
| 72 | ofinput.enums.append(enum) |
| 73 | elif decl_ast[0] == 'metadata': |
| 74 | if decl_ast[1] == 'version': |
| 75 | if decl_ast[2] == 'any': |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 76 | ofinput.wire_versions.update(of_g.wire_ver_map.keys()) |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 77 | elif int(decl_ast[2]) in of_g.supported_wire_protos: |
| 78 | ofinput.wire_versions.add(int(decl_ast[2])) |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 79 | else: |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 80 | raise InputError("Unrecognized wire protocol version %r" % decl_ast[2]) |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 81 | found_wire_version = True |
| 82 | |
| 83 | if not ofinput.wire_versions: |
| 84 | raise InputError("Missing #version metadata") |
| 85 | |
| 86 | return ofinput |