Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 1 | #!/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 Lane | 569338c | 2013-06-17 15:51:24 -0700 | [diff] [blame] | 29 | import sys |
| 30 | import os |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 31 | import unittest |
Rich Lane | 569338c | 2013-06-17 15:51:24 -0700 | [diff] [blame] | 32 | |
| 33 | root_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..') |
| 34 | sys.path.insert(0, root_dir) |
| 35 | |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 36 | import loxi_front_end.parser as parser |
| 37 | import loxi_front_end.frontend as frontend |
| 38 | from loxi_ir import * |
| 39 | |
| 40 | class FrontendTests(unittest.TestCase): |
| 41 | maxDiff = None |
| 42 | |
| 43 | def test_simple(self): |
| 44 | ast = parser.parse(""" |
| 45 | #version 1 |
| 46 | |
| 47 | enum 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 Wundsam | 7e5b0e7 | 2013-08-03 20:23:43 -0700 | [diff] [blame] | 59 | struct of_echo_reply(align=8) { |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 60 | uint8_t version; |
Rich Lane | 3214287 | 2013-05-09 21:16:47 -0700 | [diff] [blame] | 61 | uint8_t type == 3; |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 62 | uint16_t length; |
| 63 | uint32_t xid; |
| 64 | of_octets_t data; |
| 65 | }; |
| 66 | |
Andreas Wundsam | 7e5b0e7 | 2013-08-03 20:23:43 -0700 | [diff] [blame] | 67 | enum ofp_queue_op_failed_code(wire_type=uint32, bitmask=False, complete=True) { |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 68 | OFPQOFC_BAD_PORT = 0, |
| 69 | OFPQOFC_BAD_QUEUE = 1, |
| 70 | OFPQOFC_EPERM = 2, |
| 71 | }; |
| 72 | |
| 73 | struct 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 Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 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]]], |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 92 | ['metadata', 'version', '2'], |
Andreas Wundsam | 7e5b0e7 | 2013-08-03 20:23:43 -0700 | [diff] [blame] | 93 | ['struct', 'of_echo_reply', [['align', '8']], None, [ |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 94 | ['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 Wundsam | 7e5b0e7 | 2013-08-03 20:23:43 -0700 | [diff] [blame] | 99 | ['enum', 'ofp_queue_op_failed_code', |
| 100 | [['wire_type', 'uint32'], ['bitmask','False'], ['complete', 'True']], [ |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 101 | ['OFPQOFC_BAD_PORT', [], 0], |
| 102 | ['OFPQOFC_BAD_QUEUE', [], 1], |
| 103 | ['OFPQOFC_EPERM', [], 2]]], |
| 104 | ['struct', 'of_packet_queue', [], None, [ |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 105 | ['data', ['scalar', 'uint32_t'], 'queue_id'], |
| 106 | ['data', ['scalar', 'uint16_t'], 'len'], |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 107 | ['pad', 2], |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 108 | ['data', ['list', 'list(of_queue_prop_t)'], 'properties']]], |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 109 | ] |
| 110 | self.assertEquals(expected_ast, ast) |
| 111 | |
Rich Lane | 72875d6 | 2013-11-13 12:39:53 -0800 | [diff] [blame] | 112 | ofinput = frontend.create_ofinput("test", ast) |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 113 | self.assertEquals(set([1, 2]), ofinput.wire_versions) |
| 114 | expected_classes = [ |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 115 | OFClass(name='of_echo_reply', superclass=None, members=[ |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 116 | OFDataMember('version', 'uint8_t'), # XXX |
Rich Lane | 3214287 | 2013-05-09 21:16:47 -0700 | [diff] [blame] | 117 | OFTypeMember('type', 'uint8_t', 3), |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 118 | OFLengthMember('length', 'uint16_t'), |
| 119 | OFDataMember('xid', 'uint32_t'), |
Andreas Wundsam | 7e5b0e7 | 2013-08-03 20:23:43 -0700 | [diff] [blame] | 120 | OFDataMember('data', 'of_octets_t')], virtual=False, |
| 121 | params={'align': '8'}), |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 122 | OFClass(name='of_packet_queue', superclass=None, members=[ |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 123 | OFDataMember('queue_id', 'uint32_t'), |
| 124 | OFLengthMember('len', 'uint16_t'), |
| 125 | OFPadMember(2), |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 126 | OFDataMember('properties', 'list(of_queue_prop_t)')], virtual=False, params={}), |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 127 | ] |
| 128 | self.assertEquals(expected_classes, ofinput.classes) |
| 129 | expected_enums = [ |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 130 | 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 Wundsam | 7e5b0e7 | 2013-08-03 20:23:43 -0700 | [diff] [blame] | 141 | OFEnumEntry('OFPQOFC_EPERM', 2, {})], |
| 142 | params={'wire_type': 'uint32', 'bitmask': 'False', 'complete': 'True'}), |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 143 | ] |
| 144 | self.assertEquals(expected_enums, ofinput.enums) |
| 145 | |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 146 | def test_inheritance(self): |
| 147 | ast = parser.parse(""" |
| 148 | #version 1 |
| 149 | |
| 150 | struct of_queue_prop { |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 151 | uint16_t type == ?; |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 152 | uint16_t len; |
| 153 | pad(4); |
| 154 | }; |
| 155 | |
| 156 | struct 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 Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 169 | ['struct', 'of_queue_prop', [], None, [ |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 170 | ['discriminator', ['scalar', 'uint16_t'], 'type'], |
| 171 | ['data', ['scalar', 'uint16_t'], 'len'], |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 172 | ['pad', 4]]], |
| 173 | |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 174 | ['struct', 'of_queue_prop_min_rate', [], 'of_queue_prop', [ |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 175 | ['type', ['scalar', 'uint16_t'], 'type', 1], |
| 176 | ['data', ['scalar', 'uint16_t'], 'len'], |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 177 | ['pad', 4], |
Andreas Wundsam | dfeb594 | 2013-09-19 13:07:49 -0700 | [diff] [blame] | 178 | ['data', ['scalar', 'uint16_t'], 'rate'], |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 179 | ['pad', 6]]], |
| 180 | ] |
| 181 | self.assertEquals(expected_ast, ast) |
| 182 | |
Rich Lane | 72875d6 | 2013-11-13 12:39:53 -0800 | [diff] [blame] | 183 | ofinput = frontend.create_ofinput("test", ast) |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 184 | expected_classes = [ |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 185 | OFClass(name='of_queue_prop', superclass=None, members=[ |
| 186 | OFDiscriminatorMember('type', 'uint16_t'), |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 187 | OFLengthMember('len', 'uint16_t'), |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 188 | OFPadMember(4)], virtual=True, params={}), |
| 189 | OFClass(name='of_queue_prop_min_rate', superclass='of_queue_prop', members= [ |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 190 | OFTypeMember('type', 'uint16_t', 1), |
| 191 | OFLengthMember('len', 'uint16_t'), |
| 192 | OFPadMember(4), |
| 193 | OFDataMember('rate', 'uint16_t'), |
Andreas Wundsam | 529e6c5 | 2013-08-03 13:41:48 -0700 | [diff] [blame] | 194 | OFPadMember(6)], virtual=False, params= {}), |
Rich Lane | bd43150 | 2013-06-21 16:30:20 -0700 | [diff] [blame] | 195 | ] |
| 196 | self.assertEquals(expected_classes, ofinput.classes) |
| 197 | |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 198 | if __name__ == '__main__': |
| 199 | unittest.main() |