blob: c093a95179d7889dc0123ad7bd67074e987ad2b6 [file] [log] [blame]
Rich Laned47e5a22013-05-09 14:21:16 -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
Rich Lane4d9f0f62013-05-09 15:50:57 -070028import copy
Rich Laned47e5a22013-05-09 14:21:16 -070029import of_g
30import loxi_front_end.type_maps as type_maps
Rich Lane4d9f0f62013-05-09 15:50:57 -070031from loxi_ir import *
Rich Laned47e5a22013-05-09 14:21:16 -070032
33class InputError(Exception):
34 pass
35
Rich Lane4d9f0f62013-05-09 15:50:57 -070036def create_member(m_ast):
37 if m_ast[0] == 'pad':
38 return OFPadMember(length=m_ast[1])
Rich Lane32142872013-05-09 21:16:47 -070039 elif m_ast[0] == 'type':
40 return OFTypeMember(name=m_ast[2], oftype=m_ast[1], value=m_ast[3])
Rich Lanef424e972013-05-09 21:00:13 -070041 elif m_ast[0] == 'data':
42 if m_ast[2] == 'length' or m_ast[2] == 'len': # Should be moved to parser
43 return OFLengthMember(name=m_ast[2], oftype=m_ast[1])
44 elif m_ast[2] == 'actions_len':
45 # HACK only usage so far
46 return OFFieldLengthMember(name=m_ast[2], oftype=m_ast[1], field_name='actions')
47 else:
48 return OFDataMember(name=m_ast[2], oftype=m_ast[1])
Andreas Wundsam780e0c92013-08-02 17:48:27 -070049 elif m_ast[0] == 'discriminator':
50 return OFDiscriminatorMember(name=m_ast[2], oftype=m_ast[1])
Andreas Wundsam7adebd22013-08-01 22:14:14 -070051 else:
Andreas Wundsam7933beb2013-08-02 22:36:42 -070052 raise InputError("Dont know how to create member: %s" % m_ast[0])
Rich Lane4d9f0f62013-05-09 15:50:57 -070053
Rich Laned47e5a22013-05-09 14:21:16 -070054def create_ofinput(ast):
55 """
56 Create an OFInput from an AST
57
58 @param ast An AST as returned by loxi_front_end.parser.parse
59
60 @returns An OFInput object
61 """
62
Rich Lane4d9f0f62013-05-09 15:50:57 -070063 ofinput = OFInput(wire_versions=set(), classes=[], enums=[])
Rich Laned47e5a22013-05-09 14:21:16 -070064
Rich Lane4d9f0f62013-05-09 15:50:57 -070065 for decl_ast in ast:
66 if decl_ast[0] == 'struct':
Andreas Wundsam7933beb2013-08-02 22:36:42 -070067 # 0: "struct"
Andreas Wundsamfef7d5f2013-08-01 22:15:44 -070068 # 1: name
69 # 2: potentially list of [param_name, param_value]
Andreas Wundsam780e0c92013-08-02 17:48:27 -070070 # 3: super_class or None
Andreas Wundsam7933beb2013-08-02 22:36:42 -070071 # 4: list of members
Andreas Wundsam780e0c92013-08-02 17:48:27 -070072 superclass = decl_ast[3]
Andreas Wundsamfef7d5f2013-08-01 22:15:44 -070073 members = [create_member(m_ast) for m_ast in decl_ast[4]]
Andreas Wundsam780e0c92013-08-02 17:48:27 -070074
75 discriminators = [ m for m in members if isinstance(m, OFDiscriminatorMember) ]
76 if len(discriminators) > 1:
Andreas Wundsam7933beb2013-08-02 22:36:42 -070077 raise InputError("%s: Cannot support more than one discriminator by class - got %s" %
Andreas Wundsam780e0c92013-08-02 17:48:27 -070078 (decl_ast[1], repr(discriminators)))
79 ofclass = OFClass(name=decl_ast[1], members=members, superclass=superclass,
80 virtual = len(discriminators) > 0,
Andreas Wundsamfef7d5f2013-08-01 22:15:44 -070081 params = { param: value for param, value in decl_ast[2] })
Rich Lane4d9f0f62013-05-09 15:50:57 -070082 ofinput.classes.append(ofclass)
Rich Lane4d9f0f62013-05-09 15:50:57 -070083 if decl_ast[0] == 'enum':
Andreas Wundsam4ee51462013-07-30 11:00:37 -070084 # 0: "enum"
85 # 1: name
86 # 2: potentially list of [param_name, param_value]
87 # 3: list of [constant_name, constant_value]+
88 enum = OFEnum(name=decl_ast[1],
89 entries=[OFEnumEntry(name=x[0], value=x[2], params={param:value for param, value in x[1] }) for x in decl_ast[3]],
90 params = { param: value for param, value in decl_ast[2] }
91 )
Rich Lane4d9f0f62013-05-09 15:50:57 -070092 ofinput.enums.append(enum)
93 elif decl_ast[0] == 'metadata':
94 if decl_ast[1] == 'version':
95 if decl_ast[2] == 'any':
Rich Laned47e5a22013-05-09 14:21:16 -070096 ofinput.wire_versions.update(of_g.wire_ver_map.keys())
Rich Lane4d9f0f62013-05-09 15:50:57 -070097 elif int(decl_ast[2]) in of_g.supported_wire_protos:
98 ofinput.wire_versions.add(int(decl_ast[2]))
Rich Laned47e5a22013-05-09 14:21:16 -070099 else:
Rich Lane4d9f0f62013-05-09 15:50:57 -0700100 raise InputError("Unrecognized wire protocol version %r" % decl_ast[2])
Rich Laned47e5a22013-05-09 14:21:16 -0700101 found_wire_version = True
102
103 if not ofinput.wire_versions:
104 raise InputError("Missing #version metadata")
105
106 return ofinput