blob: 8febfde82ad3e676fcab4177b64705bf4186a371 [file] [log] [blame]
Rich Laned47e5a22013-05-09 14:21:16 -07001#!/usr/bin/env python
2# Copyright 2013, Big Switch Networks, Inc.
3#
4# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
5# the following special exception:
6#
7# LOXI Exception
8#
9# As a special exception to the terms of the EPL, you may distribute libraries
10# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
11# that copyright and licensing notices generated by LoxiGen are not altered or removed
12# from the LoxiGen Libraries and the notice provided below is (i) included in
13# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
14# documentation for the LoxiGen Libraries, if distributed in binary form.
15#
16# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
17#
18# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
19# a copy of the EPL at:
20#
21# http://www.eclipse.org/legal/epl-v10.html
22#
23# Unless required by applicable law or agreed to in writing, software
24# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
26# EPL for the specific language governing permissions and limitations
27# under the EPL.
28
Rich Lane569338c2013-06-17 15:51:24 -070029import sys
30import os
Rich Laned47e5a22013-05-09 14:21:16 -070031import unittest
Rich Lane569338c2013-06-17 15:51:24 -070032
33root_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..')
34sys.path.insert(0, root_dir)
35
Rich Laned47e5a22013-05-09 14:21:16 -070036import loxi_front_end.parser as parser
37import loxi_front_end.frontend as frontend
38from loxi_ir import *
39
40class FrontendTests(unittest.TestCase):
41 maxDiff = None
42
43 def test_simple(self):
44 ast = parser.parse("""
45#version 1
46
47enum ofp_port_config {
48 OFPPC_PORT_DOWN = 0x1,
49 OFPPC_NO_STP = 0x2,
50 OFPPC_NO_RECV = 0x4,
51 OFPPC_NO_RECV_STP = 0x8,
52 OFPPC_NO_FLOOD = 0x10,
53 OFPPC_NO_FWD = 0x20,
54 OFPPC_NO_PACKET_IN = 0x40,
55};
56
57#version 2
58
Andreas Wundsam7e5b0e72013-08-03 20:23:43 -070059struct of_echo_reply(align=8) {
Rich Laned47e5a22013-05-09 14:21:16 -070060 uint8_t version;
Rich Lane32142872013-05-09 21:16:47 -070061 uint8_t type == 3;
Rich Laned47e5a22013-05-09 14:21:16 -070062 uint16_t length;
63 uint32_t xid;
64 of_octets_t data;
65};
66
Andreas Wundsam7e5b0e72013-08-03 20:23:43 -070067enum ofp_queue_op_failed_code(wire_type=uint32, bitmask=False, complete=True) {
Rich Laned47e5a22013-05-09 14:21:16 -070068 OFPQOFC_BAD_PORT = 0,
69 OFPQOFC_BAD_QUEUE = 1,
70 OFPQOFC_EPERM = 2,
71};
72
73struct of_packet_queue {
74 uint32_t queue_id;
75 uint16_t len;
76 pad(2);
77 list(of_queue_prop_t) properties;
78};
79""")
80
81 # Not testing the parser, just making sure the AST is what we expect
82 expected_ast = [
83 ['metadata', 'version', '1'],
Andreas Wundsam529e6c52013-08-03 13:41:48 -070084 ['enum', 'ofp_port_config', [], [
85 ['OFPPC_PORT_DOWN', [], 1],
86 ['OFPPC_NO_STP', [], 2],
87 ['OFPPC_NO_RECV', [], 4],
88 ['OFPPC_NO_RECV_STP', [], 8],
89 ['OFPPC_NO_FLOOD', [], 16],
90 ['OFPPC_NO_FWD', [], 32],
91 ['OFPPC_NO_PACKET_IN', [], 64]]],
Rich Laned47e5a22013-05-09 14:21:16 -070092 ['metadata', 'version', '2'],
Andreas Wundsam7e5b0e72013-08-03 20:23:43 -070093 ['struct', 'of_echo_reply', [['align', '8']], None, [
Andreas Wundsamdfeb5942013-09-19 13:07:49 -070094 ['data', ['scalar', 'uint8_t'], 'version'],
95 ['type', ['scalar', 'uint8_t'], 'type', 3],
96 ['data', ['scalar', 'uint16_t'], 'length'],
97 ['data', ['scalar', 'uint32_t'], 'xid'],
98 ['data', ['scalar', 'of_octets_t'], 'data']]],
Andreas Wundsam7e5b0e72013-08-03 20:23:43 -070099 ['enum', 'ofp_queue_op_failed_code',
100 [['wire_type', 'uint32'], ['bitmask','False'], ['complete', 'True']], [
Andreas Wundsam529e6c52013-08-03 13:41:48 -0700101 ['OFPQOFC_BAD_PORT', [], 0],
102 ['OFPQOFC_BAD_QUEUE', [], 1],
103 ['OFPQOFC_EPERM', [], 2]]],
104 ['struct', 'of_packet_queue', [], None, [
Andreas Wundsamdfeb5942013-09-19 13:07:49 -0700105 ['data', ['scalar', 'uint32_t'], 'queue_id'],
106 ['data', ['scalar', 'uint16_t'], 'len'],
Rich Laned47e5a22013-05-09 14:21:16 -0700107 ['pad', 2],
Andreas Wundsamdfeb5942013-09-19 13:07:49 -0700108 ['data', ['list', 'list(of_queue_prop_t)'], 'properties']]],
Rich Laned47e5a22013-05-09 14:21:16 -0700109 ]
110 self.assertEquals(expected_ast, ast)
111
112 ofinput = frontend.create_ofinput(ast)
113 self.assertEquals(set([1, 2]), ofinput.wire_versions)
114 expected_classes = [
Andreas Wundsam529e6c52013-08-03 13:41:48 -0700115 OFClass(name='of_echo_reply', superclass=None, members=[
Rich Laned47e5a22013-05-09 14:21:16 -0700116 OFDataMember('version', 'uint8_t'), # XXX
Rich Lane32142872013-05-09 21:16:47 -0700117 OFTypeMember('type', 'uint8_t', 3),
Rich Laned47e5a22013-05-09 14:21:16 -0700118 OFLengthMember('length', 'uint16_t'),
119 OFDataMember('xid', 'uint32_t'),
Andreas Wundsam7e5b0e72013-08-03 20:23:43 -0700120 OFDataMember('data', 'of_octets_t')], virtual=False,
121 params={'align': '8'}),
Andreas Wundsam529e6c52013-08-03 13:41:48 -0700122 OFClass(name='of_packet_queue', superclass=None, members=[
Rich Laned47e5a22013-05-09 14:21:16 -0700123 OFDataMember('queue_id', 'uint32_t'),
124 OFLengthMember('len', 'uint16_t'),
125 OFPadMember(2),
Andreas Wundsam529e6c52013-08-03 13:41:48 -0700126 OFDataMember('properties', 'list(of_queue_prop_t)')], virtual=False, params={}),
Rich Laned47e5a22013-05-09 14:21:16 -0700127 ]
128 self.assertEquals(expected_classes, ofinput.classes)
129 expected_enums = [
Andreas Wundsam529e6c52013-08-03 13:41:48 -0700130 OFEnum(name='ofp_port_config', entries=[
131 OFEnumEntry('OFPPC_PORT_DOWN', 1, {}),
132 OFEnumEntry('OFPPC_NO_STP', 2, {}),
133 OFEnumEntry('OFPPC_NO_RECV', 4, {}),
134 OFEnumEntry('OFPPC_NO_RECV_STP', 8, {}),
135 OFEnumEntry('OFPPC_NO_FLOOD', 16, {}),
136 OFEnumEntry('OFPPC_NO_FWD', 32, {}),
137 OFEnumEntry('OFPPC_NO_PACKET_IN', 64, {})], params={}),
138 OFEnum(name='ofp_queue_op_failed_code', entries=[
139 OFEnumEntry('OFPQOFC_BAD_PORT', 0, {}),
140 OFEnumEntry('OFPQOFC_BAD_QUEUE', 1, {}),
Andreas Wundsam7e5b0e72013-08-03 20:23:43 -0700141 OFEnumEntry('OFPQOFC_EPERM', 2, {})],
142 params={'wire_type': 'uint32', 'bitmask': 'False', 'complete': 'True'}),
Rich Laned47e5a22013-05-09 14:21:16 -0700143 ]
144 self.assertEquals(expected_enums, ofinput.enums)
145
Rich Lanebd431502013-06-21 16:30:20 -0700146 def test_inheritance(self):
147 ast = parser.parse("""
148#version 1
149
150struct of_queue_prop {
Andreas Wundsam529e6c52013-08-03 13:41:48 -0700151 uint16_t type == ?;
Rich Lanebd431502013-06-21 16:30:20 -0700152 uint16_t len;
153 pad(4);
154};
155
156struct of_queue_prop_min_rate : of_queue_prop {
157 uint16_t type == 1;
158 uint16_t len;
159 pad(4);
160 uint16_t rate;
161 pad(6);
162};
163""")
164
165 # Not testing the parser, just making sure the AST is what we expect
166 expected_ast = [
167 ['metadata', 'version', '1'],
168
Andreas Wundsam529e6c52013-08-03 13:41:48 -0700169 ['struct', 'of_queue_prop', [], None, [
Andreas Wundsamdfeb5942013-09-19 13:07:49 -0700170 ['discriminator', ['scalar', 'uint16_t'], 'type'],
171 ['data', ['scalar', 'uint16_t'], 'len'],
Rich Lanebd431502013-06-21 16:30:20 -0700172 ['pad', 4]]],
173
Andreas Wundsam529e6c52013-08-03 13:41:48 -0700174 ['struct', 'of_queue_prop_min_rate', [], 'of_queue_prop', [
Andreas Wundsamdfeb5942013-09-19 13:07:49 -0700175 ['type', ['scalar', 'uint16_t'], 'type', 1],
176 ['data', ['scalar', 'uint16_t'], 'len'],
Rich Lanebd431502013-06-21 16:30:20 -0700177 ['pad', 4],
Andreas Wundsamdfeb5942013-09-19 13:07:49 -0700178 ['data', ['scalar', 'uint16_t'], 'rate'],
Rich Lanebd431502013-06-21 16:30:20 -0700179 ['pad', 6]]],
180 ]
181 self.assertEquals(expected_ast, ast)
182
183 ofinput = frontend.create_ofinput(ast)
184 expected_classes = [
Andreas Wundsam529e6c52013-08-03 13:41:48 -0700185 OFClass(name='of_queue_prop', superclass=None, members=[
186 OFDiscriminatorMember('type', 'uint16_t'),
Rich Lanebd431502013-06-21 16:30:20 -0700187 OFLengthMember('len', 'uint16_t'),
Andreas Wundsam529e6c52013-08-03 13:41:48 -0700188 OFPadMember(4)], virtual=True, params={}),
189 OFClass(name='of_queue_prop_min_rate', superclass='of_queue_prop', members= [
Rich Lanebd431502013-06-21 16:30:20 -0700190 OFTypeMember('type', 'uint16_t', 1),
191 OFLengthMember('len', 'uint16_t'),
192 OFPadMember(4),
193 OFDataMember('rate', 'uint16_t'),
Andreas Wundsam529e6c52013-08-03 13:41:48 -0700194 OFPadMember(6)], virtual=False, params= {}),
Rich Lanebd431502013-06-21 16:30:20 -0700195 ]
196 self.assertEquals(expected_classes, ofinput.classes)
197
Rich Laned47e5a22013-05-09 14:21:16 -0700198if __name__ == '__main__':
199 unittest.main()