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 | |
| 28 | import of_g |
| 29 | import loxi_front_end.type_maps as type_maps |
| 30 | |
| 31 | class InputError(Exception): |
| 32 | pass |
| 33 | |
| 34 | def create_ofinput(ast): |
| 35 | """ |
| 36 | Create an OFInput from an AST |
| 37 | |
| 38 | @param ast An AST as returned by loxi_front_end.parser.parse |
| 39 | |
| 40 | @returns An OFInput object |
| 41 | """ |
| 42 | |
| 43 | ofinput = of_g.OFInput() |
| 44 | |
| 45 | # Now for each structure, generate lists for each member |
| 46 | for s in ast: |
| 47 | if s[0] == 'struct': |
| 48 | name = s[1].replace("ofp_", "of_", 1) |
| 49 | members = [dict(m_type=x[0], name=x[1]) for x in s[2]] |
| 50 | ofinput.classes[name] = members |
| 51 | ofinput.ordered_classes.append(name) |
| 52 | if name in type_maps.inheritance_map: |
| 53 | # Clone class into header class and add to list |
| 54 | ofinput.classes[name + "_header"] = members[:] |
| 55 | ofinput.ordered_classes.append(name + "_header") |
| 56 | if s[0] == 'enum': |
| 57 | name = s[1] |
| 58 | members = s[2] |
| 59 | ofinput.enums[name] = [(x[0], x[1]) for x in members] |
| 60 | elif s[0] == 'metadata': |
| 61 | if s[1] == 'version': |
| 62 | if s[2] == 'any': |
| 63 | ofinput.wire_versions.update(of_g.wire_ver_map.keys()) |
| 64 | elif int(s[2]) in of_g.supported_wire_protos: |
| 65 | ofinput.wire_versions.add(int(s[2])) |
| 66 | else: |
| 67 | raise InputError("Unrecognized wire protocol version %s" % repr(s[2])) |
| 68 | found_wire_version = True |
| 69 | |
| 70 | if not ofinput.wire_versions: |
| 71 | raise InputError("Missing #version metadata") |
| 72 | |
| 73 | return ofinput |