blob: 609a262f5b6de0541a395ef0f544a6d97af6f58d [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
59struct of_echo_reply {
60 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
67enum ofp_queue_op_failed_code {
68 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'],
84 ['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]]],
92 ['metadata', 'version', '2'],
Rich Lanebd431502013-06-21 16:30:20 -070093 ['struct', 'of_echo_reply', None, [
Rich Lanef424e972013-05-09 21:00:13 -070094 ['data', 'uint8_t', 'version'],
Rich Lane32142872013-05-09 21:16:47 -070095 ['type', 'uint8_t', 'type', 3],
Rich Lanef424e972013-05-09 21:00:13 -070096 ['data', 'uint16_t', 'length'],
97 ['data', 'uint32_t', 'xid'],
98 ['data', 'of_octets_t', 'data']]],
Rich Laned47e5a22013-05-09 14:21:16 -070099 ['enum', 'ofp_queue_op_failed_code', [
100 ['OFPQOFC_BAD_PORT', 0],
101 ['OFPQOFC_BAD_QUEUE', 1],
102 ['OFPQOFC_EPERM', 2]]],
Rich Lanebd431502013-06-21 16:30:20 -0700103 ['struct', 'of_packet_queue', None, [
Rich Lanef424e972013-05-09 21:00:13 -0700104 ['data', 'uint32_t', 'queue_id'],
105 ['data', 'uint16_t', 'len'],
Rich Laned47e5a22013-05-09 14:21:16 -0700106 ['pad', 2],
Rich Lanef424e972013-05-09 21:00:13 -0700107 ['data', 'list(of_queue_prop_t)', 'properties']]],
Rich Laned47e5a22013-05-09 14:21:16 -0700108 ]
109 self.assertEquals(expected_ast, ast)
110
111 ofinput = frontend.create_ofinput(ast)
112 self.assertEquals(set([1, 2]), ofinput.wire_versions)
113 expected_classes = [
Rich Lanebd431502013-06-21 16:30:20 -0700114 OFClass('of_echo_reply', None, [
Rich Laned47e5a22013-05-09 14:21:16 -0700115 OFDataMember('version', 'uint8_t'), # XXX
Rich Lane32142872013-05-09 21:16:47 -0700116 OFTypeMember('type', 'uint8_t', 3),
Rich Laned47e5a22013-05-09 14:21:16 -0700117 OFLengthMember('length', 'uint16_t'),
118 OFDataMember('xid', 'uint32_t'),
119 OFDataMember('data', 'of_octets_t')]),
Rich Lanebd431502013-06-21 16:30:20 -0700120 OFClass('of_packet_queue', None, [
Rich Laned47e5a22013-05-09 14:21:16 -0700121 OFDataMember('queue_id', 'uint32_t'),
122 OFLengthMember('len', 'uint16_t'),
123 OFPadMember(2),
124 OFDataMember('properties', 'list(of_queue_prop_t)')]),
125 ]
126 self.assertEquals(expected_classes, ofinput.classes)
127 expected_enums = [
128 OFEnum('ofp_port_config', [
129 ('OFPPC_PORT_DOWN', 1),
130 ('OFPPC_NO_STP', 2),
131 ('OFPPC_NO_RECV', 4),
132 ('OFPPC_NO_RECV_STP', 8),
133 ('OFPPC_NO_FLOOD', 16),
134 ('OFPPC_NO_FWD', 32),
135 ('OFPPC_NO_PACKET_IN', 64)]),
136 OFEnum('ofp_queue_op_failed_code', [
137 ('OFPQOFC_BAD_PORT', 0),
138 ('OFPQOFC_BAD_QUEUE', 1),
139 ('OFPQOFC_EPERM', 2)]),
140 ]
141 self.assertEquals(expected_enums, ofinput.enums)
142
Rich Lanebd431502013-06-21 16:30:20 -0700143 def test_inheritance(self):
144 ast = parser.parse("""
145#version 1
146
147struct of_queue_prop {
148 uint16_t type;
149 uint16_t len;
150 pad(4);
151};
152
153struct of_queue_prop_min_rate : of_queue_prop {
154 uint16_t type == 1;
155 uint16_t len;
156 pad(4);
157 uint16_t rate;
158 pad(6);
159};
160""")
161
162 # Not testing the parser, just making sure the AST is what we expect
163 expected_ast = [
164 ['metadata', 'version', '1'],
165
166 ['struct', 'of_queue_prop', None, [
167 ['data', 'uint16_t', 'type'],
168 ['data', 'uint16_t', 'len'],
169 ['pad', 4]]],
170
171 ['struct', 'of_queue_prop_min_rate', 'of_queue_prop', [
172 ['type', 'uint16_t', 'type', 1],
173 ['data', 'uint16_t', 'len'],
174 ['pad', 4],
175 ['data', 'uint16_t', 'rate'],
176 ['pad', 6]]],
177 ]
178 self.assertEquals(expected_ast, ast)
179
180 ofinput = frontend.create_ofinput(ast)
181 expected_classes = [
182 OFClass('of_queue_prop', None, [
183 OFDataMember('type', 'uint16_t'),
184 OFLengthMember('len', 'uint16_t'),
185 OFPadMember(4)]),
186 OFClass('of_queue_prop_min_rate', 'of_queue_prop', [
187 OFTypeMember('type', 'uint16_t', 1),
188 OFLengthMember('len', 'uint16_t'),
189 OFPadMember(4),
190 OFDataMember('rate', 'uint16_t'),
191 OFPadMember(6)]),
192 ]
193 self.assertEquals(expected_classes, ofinput.classes)
194
Rich Laned47e5a22013-05-09 14:21:16 -0700195if __name__ == '__main__':
196 unittest.main()