| # Copyright 2013, Big Switch Networks, Inc. |
| # |
| # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| # the following special exception: |
| # |
| # LOXI Exception |
| # |
| # As a special exception to the terms of the EPL, you may distribute libraries |
| # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| # from the LoxiGen Libraries and the notice provided below is (i) included in |
| # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| # documentation for the LoxiGen Libraries, if distributed in binary form. |
| # |
| # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| # |
| # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| # a copy of the EPL at: |
| # |
| # http://www.eclipse.org/legal/epl-v10.html |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| # EPL for the specific language governing permissions and limitations |
| # under the EPL. |
| |
| import of_g |
| import loxi_front_end.type_maps as type_maps |
| |
| class InputError(Exception): |
| pass |
| |
| def create_ofinput(ast): |
| """ |
| Create an OFInput from an AST |
| |
| @param ast An AST as returned by loxi_front_end.parser.parse |
| |
| @returns An OFInput object |
| """ |
| |
| ofinput = of_g.OFInput() |
| |
| # Now for each structure, generate lists for each member |
| for s in ast: |
| if s[0] == 'struct': |
| name = s[1].replace("ofp_", "of_", 1) |
| members = [dict(m_type=x[0], name=x[1]) for x in s[2]] |
| ofinput.classes[name] = members |
| ofinput.ordered_classes.append(name) |
| if name in type_maps.inheritance_map: |
| # Clone class into header class and add to list |
| ofinput.classes[name + "_header"] = members[:] |
| ofinput.ordered_classes.append(name + "_header") |
| if s[0] == 'enum': |
| name = s[1] |
| members = s[2] |
| ofinput.enums[name] = [(x[0], x[1]) for x in members] |
| elif s[0] == 'metadata': |
| if s[1] == 'version': |
| if s[2] == 'any': |
| ofinput.wire_versions.update(of_g.wire_ver_map.keys()) |
| elif int(s[2]) in of_g.supported_wire_protos: |
| ofinput.wire_versions.add(int(s[2])) |
| else: |
| raise InputError("Unrecognized wire protocol version %s" % repr(s[2])) |
| found_wire_version = True |
| |
| if not ofinput.wire_versions: |
| raise InputError("Missing #version metadata") |
| |
| return ofinput |