Rich Lane | 3f07597 | 2013-03-15 22:56:29 -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 | import unittest |
Rich Lane | 4c76498 | 2013-05-01 16:12:22 -0700 | [diff] [blame] | 29 | import difflib |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 30 | |
| 31 | try: |
| 32 | import loxi.of13 as ofp |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 33 | from loxi.generic_util import OFReader |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 34 | except ImportError: |
| 35 | exit("loxi package not found. Try setting PYTHONPATH.") |
| 36 | |
Rich Lane | 4c76498 | 2013-05-01 16:12:22 -0700 | [diff] [blame] | 37 | # Human-friendly format for binary strings. 8 bytes per line. |
| 38 | def format_binary(buf): |
| 39 | byts = map(ord, buf) |
| 40 | lines = [[]] |
| 41 | for byt in byts: |
| 42 | if len(lines[-1]) == 8: |
| 43 | lines.append([]) |
| 44 | lines[-1].append(byt) |
| 45 | return '\n'.join([' '.join(['%02x' % y for y in x]) for x in lines]) |
| 46 | |
| 47 | def diff(a, b): |
| 48 | return '\n'.join(difflib.ndiff(a.splitlines(), b.splitlines())) |
| 49 | |
| 50 | # Test serialization / deserialization of a sample object. |
| 51 | # Depends on the __eq__ method being correct. |
| 52 | def test_serialization(obj, buf): |
| 53 | packed = obj.pack() |
| 54 | if packed != buf: |
| 55 | a = format_binary(buf) |
| 56 | b = format_binary(packed) |
| 57 | raise AssertionError("Serialization of %s failed\nExpected:\n%s\nActual:\n%s\nDiff:\n%s" % \ |
| 58 | (type(obj).__name__, a, b, diff(a, b))) |
| 59 | unpacked = type(obj).unpack(buf) |
| 60 | if obj != unpacked: |
| 61 | a = obj.show() |
| 62 | b = unpacked.show() |
| 63 | raise AssertionError("Deserialization of %s failed\nExpected:\n%s\nActual:\n%s\nDiff:\n%s" % \ |
| 64 | (type(obj).__name__, a, b, diff(a, b))) |
| 65 | |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 66 | class TestImports(unittest.TestCase): |
| 67 | def test_toplevel(self): |
| 68 | import loxi |
| 69 | self.assertTrue(hasattr(loxi, "ProtocolError")) |
Rich Lane | 00549ea | 2013-04-25 13:33:16 -0700 | [diff] [blame] | 70 | self.assertEquals(loxi.version_names[4], "1.3") |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 71 | ofp = loxi.protocol(4) |
| 72 | self.assertEquals(ofp.OFP_VERSION, 4) |
| 73 | self.assertTrue(hasattr(ofp, "action")) |
| 74 | self.assertTrue(hasattr(ofp, "common")) |
| 75 | self.assertTrue(hasattr(ofp, "const")) |
| 76 | self.assertTrue(hasattr(ofp, "message")) |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 77 | self.assertTrue(hasattr(ofp, "oxm")) |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 78 | |
| 79 | def test_version(self): |
| 80 | import loxi |
| 81 | self.assertTrue(hasattr(loxi.of13, "ProtocolError")) |
| 82 | self.assertTrue(hasattr(loxi.of13, "OFP_VERSION")) |
| 83 | self.assertEquals(loxi.of13.OFP_VERSION, 4) |
| 84 | self.assertTrue(hasattr(loxi.of13, "action")) |
| 85 | self.assertTrue(hasattr(loxi.of13, "common")) |
| 86 | self.assertTrue(hasattr(loxi.of13, "const")) |
| 87 | self.assertTrue(hasattr(loxi.of13, "message")) |
Rich Lane | a22233e | 2013-04-25 13:18:41 -0700 | [diff] [blame] | 88 | self.assertTrue(hasattr(loxi.of13, "oxm")) |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 89 | |
Rich Lane | e90685c | 2013-04-05 17:27:41 -0700 | [diff] [blame] | 90 | class TestCommon(unittest.TestCase): |
| 91 | sample_hello_elem_buf = ''.join([ |
| 92 | '\x00\x01', # type |
| 93 | '\x00\x0c', # length |
| 94 | '\x01\x23\x45\x67', # bitmaps[0] |
| 95 | '\x89\xab\xcd\xef', # bitmaps[1] |
| 96 | ]) |
| 97 | |
| 98 | def test_hello_elem_versionbitmap_pack(self): |
| 99 | obj = ofp.hello_elem_versionbitmap(bitmaps=[ofp.uint32(0x01234567),ofp.uint32(0x89abcdef)]) |
| 100 | self.assertEquals(self.sample_hello_elem_buf, obj.pack()) |
| 101 | |
| 102 | def test_hello_elem_versionbitmap_unpack(self): |
| 103 | obj = ofp.hello_elem_versionbitmap.unpack(self.sample_hello_elem_buf) |
| 104 | self.assertEquals(len(obj.bitmaps), 2) |
| 105 | self.assertEquals(obj.bitmaps[0], ofp.uint32(0x01234567)) |
| 106 | self.assertEquals(obj.bitmaps[1], ofp.uint32(0x89abcdef)) |
| 107 | |
| 108 | def test_list_hello_elem_unpack(self): |
| 109 | buf = ''.join([ |
| 110 | '\x00\x01\x00\x04', # versionbitmap |
| 111 | '\x00\x00\x00\x04', # unknown type |
| 112 | '\x00\x01\x00\x04', # versionbitmap |
| 113 | ]) |
Rich Lane | 57026dc | 2013-05-01 10:13:16 -0700 | [diff] [blame] | 114 | l = ofp.unpack_list_hello_elem(OFReader(buf)) |
Rich Lane | e90685c | 2013-04-05 17:27:41 -0700 | [diff] [blame] | 115 | self.assertEquals(len(l), 2) |
| 116 | self.assertTrue(isinstance(l[0], ofp.hello_elem_versionbitmap)) |
| 117 | self.assertTrue(isinstance(l[1], ofp.hello_elem_versionbitmap)) |
| 118 | |
Rich Lane | 4c76498 | 2013-05-01 16:12:22 -0700 | [diff] [blame] | 119 | class TestMessages(unittest.TestCase): |
| 120 | def test_hello(self): |
| 121 | obj = ofp.message.hello( |
| 122 | xid=0x12345678, |
| 123 | elements=[ |
| 124 | ofp.hello_elem_versionbitmap( |
| 125 | bitmaps=[ofp.uint32(1), ofp.uint32(2)]), |
| 126 | ofp.hello_elem_versionbitmap( |
| 127 | bitmaps=[ofp.uint32(3), ofp.uint32(4)])]) |
| 128 | buf = ''.join([ |
| 129 | '\x04', '\x00', # version, type |
| 130 | '\x00\x20', # length |
| 131 | '\x12\x34\x56\x78', # xid |
| 132 | '\x00\x01', # elements[0].type |
| 133 | '\x00\x0c', # elements[0].length |
| 134 | '\x00\x00\x00\x01', # elements[0].bitmaps[0] |
| 135 | '\x00\x00\x00\x02', # elements[0].bitmaps[1] |
| 136 | '\x00\x01', # elements[1].type |
| 137 | '\x00\x0c', # elements[1].length |
| 138 | '\x00\x00\x00\x03', # elements[1].bitmaps[0] |
| 139 | '\x00\x00\x00\x04', # elements[1].bitmaps[1] |
| 140 | ]) |
| 141 | test_serialization(obj, buf) |
| 142 | |
| 143 | def test_error(self): |
| 144 | obj = ofp.message.error_msg( |
| 145 | xid=0x12345678, |
| 146 | err_type=ofp.OFPET_BAD_MATCH, |
| 147 | code=ofp.OFPBMC_BAD_MASK, |
| 148 | data="abc") |
| 149 | buf = ''.join([ |
| 150 | '\x04', '\x01', # version, type |
| 151 | '\x00\x0f', # length |
| 152 | '\x12\x34\x56\x78', # xid |
| 153 | '\x00\x04', # err_type |
| 154 | '\x00\x08', # code |
| 155 | 'abc', # data |
| 156 | ]) |
| 157 | test_serialization(obj, buf) |
| 158 | |
| 159 | def test_echo_request(self): |
| 160 | obj = ofp.message.echo_request( |
| 161 | xid=0x12345678, |
| 162 | data="abc") |
| 163 | buf = ''.join([ |
| 164 | '\x04', '\x02', # version, type |
| 165 | '\x00\x0b', # length |
| 166 | '\x12\x34\x56\x78', # xid |
| 167 | 'abc', # data |
| 168 | ]) |
| 169 | test_serialization(obj, buf) |
| 170 | |
| 171 | def test_echo_reply(self): |
| 172 | obj = ofp.message.echo_reply( |
| 173 | xid=0x12345678, |
| 174 | data="abc") |
| 175 | buf = ''.join([ |
| 176 | '\x04', '\x03', # version, type |
| 177 | '\x00\x0b', # length |
| 178 | '\x12\x34\x56\x78', # xid |
| 179 | 'abc', # data |
| 180 | ]) |
| 181 | test_serialization(obj, buf) |
| 182 | |
| 183 | def test_features_request(self): |
| 184 | obj = ofp.message.features_request(xid=0x12345678) |
| 185 | buf = ''.join([ |
| 186 | '\x04', '\x05', # version, type |
| 187 | '\x00\x08', # length |
| 188 | '\x12\x34\x56\x78', # xid |
| 189 | ]) |
| 190 | test_serialization(obj, buf) |
| 191 | |
| 192 | def test_features_reply(self): |
| 193 | obj = ofp.message.features_reply( |
| 194 | xid=0x12345678, |
| 195 | datapath_id=0xFEDCBA9876543210, |
| 196 | n_buffers=64, |
| 197 | n_tables=200, |
| 198 | auxiliary_id=5, |
| 199 | capabilities=ofp.OFPC_FLOW_STATS|ofp.OFPC_PORT_BLOCKED, |
| 200 | reserved=0) |
| 201 | buf = ''.join([ |
| 202 | '\x04', '\x06', # version, type |
| 203 | '\x00\x20', # length |
| 204 | '\x12\x34\x56\x78', # xid |
| 205 | '\xfe\xdc\xba\x98\x76\x54\x32\x10', # datapath_id |
| 206 | '\x00\x00\x00\x40', # n_buffers |
| 207 | '\xc8', # n_tables |
| 208 | '\x05', # auxiliary_id |
| 209 | '\x00\x00', # pad |
| 210 | '\x00\x00\x01\x01', # capabilities |
| 211 | '\x00\x00\x00\x00', # reserved |
| 212 | ]) |
| 213 | test_serialization(obj, buf) |
| 214 | |
| 215 | def test_get_config_request(self): |
| 216 | obj = ofp.message.get_config_request(xid=0x12345678) |
| 217 | buf = ''.join([ |
| 218 | '\x04', '\x07', # version, type |
| 219 | '\x00\x08', # length |
| 220 | '\x12\x34\x56\x78', # xid |
| 221 | ]) |
| 222 | test_serialization(obj, buf) |
| 223 | |
| 224 | def test_get_config_reply(self): |
| 225 | obj = ofp.message.get_config_reply( |
| 226 | xid=0x12345678, |
| 227 | flags=ofp.OFPC_FRAG_REASM, |
| 228 | miss_send_len=0xffff) |
| 229 | buf = ''.join([ |
| 230 | '\x04', '\x08', # version, type |
| 231 | '\x00\x0c', # length |
| 232 | '\x12\x34\x56\x78', # xid |
| 233 | '\x00\x02', # flags |
| 234 | '\xff\xff', # miss_send_len |
| 235 | ]) |
| 236 | test_serialization(obj, buf) |
| 237 | |
| 238 | def test_set_config(self): |
| 239 | obj = ofp.message.set_config( |
| 240 | xid=0x12345678, |
| 241 | flags=ofp.OFPC_FRAG_REASM, |
| 242 | miss_send_len=0xffff) |
| 243 | buf = ''.join([ |
| 244 | '\x04', '\x09', # version, type |
| 245 | '\x00\x0c', # length |
| 246 | '\x12\x34\x56\x78', # xid |
| 247 | '\x00\x02', # flags |
| 248 | '\xff\xff', # miss_send_len |
| 249 | ]) |
| 250 | test_serialization(obj, buf) |
| 251 | |
| 252 | def test_packet_in(self): |
| 253 | obj = ofp.message.packet_in( |
| 254 | xid=0x12345678, |
| 255 | buffer_id=100, |
| 256 | total_len=17000, |
| 257 | reason=ofp.OFPR_ACTION, |
| 258 | table_id=20, |
| 259 | cookie=0xFEDCBA9876543210, |
| 260 | match=ofp.match(oxm_list=[ |
| 261 | ofp.oxm.arp_op(value=1), |
| 262 | ofp.oxm.in_port_masked(value=4, value_mask=5)]), |
| 263 | data="abc") |
| 264 | buf = ''.join([ |
| 265 | '\x04', '\x0a', # version, type |
| 266 | '\x00\x35', # length |
| 267 | '\x12\x34\x56\x78', # xid |
| 268 | '\x00\x00\x00\x64', # buffer_id |
| 269 | '\x42\x68', # total_len |
| 270 | '\x01', # reason |
| 271 | '\x14', # table_id |
| 272 | '\xfe\xdc\xba\x98\x76\x54\x32\x10', # cookie |
| 273 | '\x00\x01', # match.type |
| 274 | '\x00\x16', # match.length |
| 275 | '\x80\x00\x2A\x02', # match.oxm_list[0].type_len |
| 276 | '\x00\x01', # match.oxm_list[0].value |
| 277 | '\x80\x00\x01\x08', # match.oxm_list[1].type_len |
| 278 | '\x00\x00\x00\x04', # match.oxm_list[1].value |
| 279 | '\x00\x00\x00\x05', # match.oxm_list[1].mask |
| 280 | '\x00\x00', # match.pad |
| 281 | '\x00\x00', # pad |
| 282 | 'abc', # data |
| 283 | ]) |
| 284 | test_serialization(obj, buf) |
| 285 | |
| 286 | def test_flow_removed(self): |
| 287 | obj = ofp.message.flow_removed( |
| 288 | xid=0x12345678, |
| 289 | cookie=0xFEDCBA9876543210, |
| 290 | priority=17000, |
| 291 | reason=ofp.OFPRR_DELETE, |
| 292 | table_id=20, |
| 293 | duration_sec=10, |
| 294 | duration_nsec=1000, |
| 295 | idle_timeout=5, |
| 296 | hard_timeout=30, |
| 297 | packet_count=1, |
| 298 | byte_count=2, |
| 299 | match=ofp.match(oxm_list=[ |
| 300 | ofp.oxm.arp_op(value=1), |
| 301 | ofp.oxm.in_port_masked(value=4, value_mask=5)])) |
| 302 | buf = ''.join([ |
| 303 | '\x04', '\x0b', # version, type |
| 304 | '\x00\x48', # length |
| 305 | '\x12\x34\x56\x78', # xid |
| 306 | '\xfe\xdc\xba\x98\x76\x54\x32\x10', # cookie |
| 307 | '\x42\x68', # priority |
| 308 | '\x02', # reason |
| 309 | '\x14', # table_id |
| 310 | '\x00\x00\x00\x0a', # duration_sec |
| 311 | '\x00\x00\x03\xe8', # duration_nsec |
| 312 | '\x00\x05', # idle_timeout |
| 313 | '\x00\x1e', # hard_timeout |
| 314 | '\x00\x00\x00\x00\x00\x00\x00\x01', # packet_count |
| 315 | '\x00\x00\x00\x00\x00\x00\x00\x02', # byte_count |
| 316 | '\x00\x01', # match.type |
| 317 | '\x00\x16', # match.length |
| 318 | '\x80\x00\x2A\x02', # match.oxm_list[0].type_len |
| 319 | '\x00\x01', # match.oxm_list[0].value |
| 320 | '\x80\x00\x01\x08', # match.oxm_list[1].type_len |
| 321 | '\x00\x00\x00\x04', # match.oxm_list[1].value |
| 322 | '\x00\x00\x00\x05', # match.oxm_list[1].mask |
| 323 | '\x00\x00', # match.pad |
| 324 | ]) |
| 325 | test_serialization(obj, buf) |
| 326 | |
| 327 | def test_port_status(self): |
| 328 | obj = ofp.message.port_status( |
| 329 | xid=0x12345678, |
| 330 | reason=ofp.OFPPR_MODIFY, |
| 331 | desc=ofp.port_desc( |
| 332 | port_no=4, |
| 333 | hw_addr=[1,2,3,4,5,6], |
| 334 | name="foo", |
| 335 | config=ofp.OFPPC_NO_FWD|ofp.OFPPC_NO_RECV, |
| 336 | state=ofp.OFPPS_BLOCKED, |
| 337 | curr=ofp.OFPPF_10MB_HD, |
| 338 | advertised=ofp.OFPPF_10MB_FD, |
| 339 | supported=ofp.OFPPF_100MB_HD, |
| 340 | peer=ofp.OFPPF_100MB_FD, |
| 341 | curr_speed=10, |
| 342 | max_speed=20)) |
| 343 | buf = ''.join([ |
| 344 | '\x04', '\x0c', # version, type |
| 345 | '\x00\x50', # length |
| 346 | '\x12\x34\x56\x78', # xid |
| 347 | '\x02', # reason |
| 348 | '\x00' * 7, # pad |
| 349 | '\x00\x00\x00\x04', # port_no |
| 350 | '\x00' * 4, # pad |
| 351 | '\x01\x02\x03\x04\x05\x06', # hw_addr |
| 352 | '\x00' * 2, # pad |
| 353 | 'foo' + '\x00' * 13, # name |
| 354 | '\x00\x00\x00\x24', # config |
| 355 | '\x00\x00\x00\x02', # state |
| 356 | '\x00\x00\x00\x01', # curr |
| 357 | '\x00\x00\x00\x02', # advertised |
| 358 | '\x00\x00\x00\x04', # supported |
| 359 | '\x00\x00\x00\x08', # peer |
| 360 | '\x00\x00\x00\x0a', # curr_speed |
| 361 | '\x00\x00\x00\x14', # max_speed |
| 362 | ]) |
| 363 | test_serialization(obj, buf) |
| 364 | |
| 365 | def test_packet_out(self): |
Rich Lane | a4d3d2d | 2013-05-01 16:57:00 -0700 | [diff] [blame] | 366 | obj = ofp.message.packet_out( |
| 367 | xid=0x12345678, |
| 368 | buffer_id=100, |
| 369 | in_port=4, |
| 370 | actions=[ |
| 371 | ofp.action.output(port=2, max_len=0xffff), |
| 372 | ofp.action.dec_nw_ttl()], |
| 373 | data="abc") |
| 374 | buf = ''.join([ |
| 375 | '\x04', '\x0d', # version, type |
| 376 | '\x00\x33', # length |
| 377 | '\x12\x34\x56\x78', # xid |
| 378 | '\x00\x00\x00\x64', # buffer_id |
| 379 | '\x00\x00\x00\x04', # in_port |
| 380 | '\x00\x18', # actions_len |
| 381 | '\x00' * 6, # pad |
| 382 | '\x00\x00', # actions[0].type |
| 383 | '\x00\x10', # actions[0].length |
| 384 | '\x00\x00\x00\x02', # actions[0].port |
| 385 | '\xff\xff', # actions[0].max_len |
| 386 | '\x00' * 6, # pad |
| 387 | '\x00\x18', # actions[1].type |
| 388 | '\x00\x08', # actions[1].length |
| 389 | '\x00' * 4, # pad |
| 390 | 'abc', # data |
| 391 | ]) |
| 392 | test_serialization(obj, buf) |
Rich Lane | 4c76498 | 2013-05-01 16:12:22 -0700 | [diff] [blame] | 393 | |
| 394 | |
| 395 | ## Flow-mods |
| 396 | |
| 397 | def test_flow_add(self): |
| 398 | # TODO |
| 399 | pass |
| 400 | |
| 401 | def test_flow_modify(self): |
| 402 | # TODO |
| 403 | pass |
| 404 | |
| 405 | def test_flow_modify_strict(self): |
| 406 | # TODO |
| 407 | pass |
| 408 | |
| 409 | def test_flow_delete(self): |
| 410 | # TODO |
| 411 | pass |
| 412 | |
| 413 | def test_flow_delete_strict(self): |
| 414 | # TODO |
| 415 | pass |
| 416 | |
| 417 | |
| 418 | def test_group_mod(self): |
Rich Lane | 8e27ec7 | 2013-05-02 11:04:31 -0700 | [diff] [blame] | 419 | obj = ofp.message.group_mod( |
| 420 | xid=0x12345678, |
| 421 | command=ofp.OFPGC_MODIFY, |
| 422 | group_type=ofp.OFPGT_FF, |
| 423 | group_id=5, |
| 424 | buckets=[ |
| 425 | ofp.bucket( |
| 426 | weight=1, |
| 427 | watch_port=5, |
| 428 | watch_group=0xffffffff, |
| 429 | actions=[ |
| 430 | ofp.action.output(port=5, max_len=0), |
| 431 | ofp.action.output(port=6, max_len=0)]), |
| 432 | ofp.bucket( |
| 433 | weight=1, |
| 434 | watch_port=6, |
| 435 | watch_group=0xffffffff, |
| 436 | actions=[ |
| 437 | ofp.action.output(port=5, max_len=0), |
| 438 | ofp.action.output(port=6, max_len=0)])]) |
| 439 | buf = ''.join([ |
| 440 | '\x04', '\x0f', # version, type |
| 441 | '\x00\x70', # length |
| 442 | '\x12\x34\x56\x78', # xid |
| 443 | '\x00\x01', # command |
| 444 | '\x03', # group_type |
| 445 | '\x00', # pad |
| 446 | '\x00\x00\x00\x05', # group_id |
| 447 | '\x00\x30', # buckets[0].len |
| 448 | '\x00\x01', # buckets[0].weight |
| 449 | '\x00\x00\x00\x05', # buckets[0].watch_port |
| 450 | '\xff\xff\xff\xff', # buckets[0].watch_group |
| 451 | '\x00' * 4, # pad |
| 452 | '\x00\x00', # buckets[0].actions[0].type |
| 453 | '\x00\x10', # buckets[0].actions[0].len |
| 454 | '\x00\x00\x00\x05', # buckets[0].actions[0].port |
| 455 | '\x00\x00', # buckets[0].actions[0].max_len |
| 456 | '\x00' * 6, # pad |
| 457 | '\x00\x00', # buckets[0].actions[1].type |
| 458 | '\x00\x10', # buckets[0].actions[1].len |
| 459 | '\x00\x00\x00\x06', # buckets[0].actions[1].port |
| 460 | '\x00\x00', # buckets[0].actions[1].max_len |
| 461 | '\x00' * 6, # pad |
| 462 | '\x00\x30', # buckets[1].len |
| 463 | '\x00\x01', # buckets[1].weight |
| 464 | '\x00\x00\x00\x06', # buckets[1].watch_port |
| 465 | '\xff\xff\xff\xff', # buckets[1].watch_group |
| 466 | '\x00' * 4, # pad |
| 467 | '\x00\x00', # buckets[1].actions[0].type |
| 468 | '\x00\x10', # buckets[1].actions[0].len |
| 469 | '\x00\x00\x00\x05', # buckets[1].actions[0].port |
| 470 | '\x00\x00', # buckets[1].actions[0].max_len |
| 471 | '\x00' * 6, # pad |
| 472 | '\x00\x00', # buckets[1].actions[1].type |
| 473 | '\x00\x10', # buckets[1].actions[1].len |
| 474 | '\x00\x00\x00\x06', # buckets[1].actions[1].port |
| 475 | '\x00\x00', # buckets[1].actions[1].max_len |
| 476 | '\x00' * 6, # pad |
| 477 | ]) |
| 478 | test_serialization(obj, buf) |
Rich Lane | 4c76498 | 2013-05-01 16:12:22 -0700 | [diff] [blame] | 479 | |
| 480 | def test_port_mod(self): |
| 481 | # TODO |
| 482 | pass |
| 483 | |
| 484 | def test_table_mod(self): |
| 485 | # TODO |
| 486 | pass |
| 487 | |
| 488 | |
| 489 | ## Multipart messages |
| 490 | |
| 491 | def test_desc_stats_request(self): |
| 492 | # TODO |
| 493 | pass |
| 494 | |
| 495 | def test_desc_stats_reply(self): |
| 496 | # TODO |
| 497 | pass |
| 498 | |
| 499 | def test_flow_stats_request(self): |
| 500 | # TODO |
| 501 | pass |
| 502 | |
| 503 | def test_flow_stats_reply(self): |
| 504 | # TODO |
| 505 | pass |
| 506 | |
| 507 | def test_aggregate_stats_request(self): |
| 508 | # TODO |
| 509 | pass |
| 510 | |
| 511 | def test_aggregate_stats_reply(self): |
| 512 | # TODO |
| 513 | pass |
| 514 | |
| 515 | def test_port_stats_request(self): |
| 516 | # TODO |
| 517 | pass |
| 518 | |
| 519 | def test_port_stats_reply(self): |
| 520 | # TODO |
| 521 | pass |
| 522 | |
| 523 | def test_queue_stats_request(self): |
| 524 | # TODO |
| 525 | pass |
| 526 | |
| 527 | def test_queue_stats_reply(self): |
| 528 | # TODO |
| 529 | pass |
| 530 | |
| 531 | def test_group_stats_request(self): |
| 532 | # TODO |
| 533 | pass |
| 534 | |
| 535 | def test_group_stats_reply(self): |
Rich Lane | 42bf98c | 2013-05-02 14:48:32 -0700 | [diff] [blame] | 536 | obj = ofp.message.group_stats_reply( |
| 537 | xid=0x12345678, |
| 538 | flags=0, |
| 539 | entries=[ |
| 540 | ofp.group_stats_entry( |
| 541 | group_id=1, |
| 542 | ref_count=8, |
| 543 | packet_count=16, |
| 544 | byte_count=32, |
| 545 | duration_sec=20, |
| 546 | duration_nsec=100, |
| 547 | bucket_stats=[ |
| 548 | ofp.bucket_counter(packet_count=1, byte_count=2), |
| 549 | ofp.bucket_counter(packet_count=3, byte_count=4)]), |
| 550 | ofp.group_stats_entry( |
| 551 | group_id=1, |
| 552 | ref_count=8, |
| 553 | packet_count=16, |
| 554 | byte_count=32, |
| 555 | duration_sec=20, |
| 556 | duration_nsec=100, |
| 557 | bucket_stats=[])]) |
| 558 | buf = ''.join([ |
| 559 | '\x04', '\x13', # version, type |
| 560 | '\x00\x80', # length |
| 561 | '\x12\x34\x56\x78', # xid |
| 562 | '\x00\x06', # stats_type |
| 563 | '\x00\x00', # flags |
| 564 | '\x00' * 4, # pad |
| 565 | '\x00\x48', # entries[0].length |
| 566 | '\x00' * 2, # pad |
| 567 | '\x00\x00\x00\x01', # entries[0].group_id |
| 568 | '\x00\x00\x00\x08', # entries[0].ref_count |
| 569 | '\x00' * 4, # pad |
| 570 | '\x00\x00\x00\x00\x00\x00\x00\x10', # entries[0].packet_count |
| 571 | '\x00\x00\x00\x00\x00\x00\x00\x20', # entries[0].byte_count |
| 572 | '\x00\x00\x00\x14', # entries[0].duration_sec |
| 573 | '\x00\x00\x00\x64', # entries[0].duration_nsec |
| 574 | '\x00\x00\x00\x00\x00\x00\x00\x01', # entries[0].bucket_stats[0].packet_count |
| 575 | '\x00\x00\x00\x00\x00\x00\x00\x02', # entries[0].bucket_stats[0].byte_count |
| 576 | '\x00\x00\x00\x00\x00\x00\x00\x03', # entries[0].bucket_stats[1].packet_count |
| 577 | '\x00\x00\x00\x00\x00\x00\x00\x04', # entries[0].bucket_stats[1].byte_count |
| 578 | '\x00\x28', # entries[0].length |
| 579 | '\x00' * 2, # pad |
| 580 | '\x00\x00\x00\x01', # entries[0].group_id |
| 581 | '\x00\x00\x00\x08', # entries[0].ref_count |
| 582 | '\x00' * 4, # pad |
| 583 | '\x00\x00\x00\x00\x00\x00\x00\x10', # entries[0].packet_count |
| 584 | '\x00\x00\x00\x00\x00\x00\x00\x20', # entries[0].byte_count |
| 585 | '\x00\x00\x00\x14', # entries[0].duration_sec |
| 586 | '\x00\x00\x00\x64', # entries[0].duration_nsec |
| 587 | ]) |
| 588 | test_serialization(obj, buf) |
Rich Lane | 4c76498 | 2013-05-01 16:12:22 -0700 | [diff] [blame] | 589 | |
| 590 | def test_group_desc_stats_request(self): |
| 591 | # TODO |
| 592 | pass |
| 593 | |
| 594 | def test_group_desc_stats_reply(self): |
Rich Lane | 9b38d11 | 2013-05-02 14:35:40 -0700 | [diff] [blame] | 595 | obj = ofp.message.group_desc_stats_reply( |
| 596 | xid=0x12345678, |
| 597 | flags=0, |
| 598 | entries=[ |
| 599 | ofp.group_desc_stats_entry( |
| 600 | type=ofp.OFPGT_FF, |
| 601 | group_id=1, |
| 602 | buckets=[ |
| 603 | ofp.bucket( |
| 604 | weight=1, |
| 605 | watch_port=5, |
| 606 | watch_group=0xffffffff, |
| 607 | actions=[ |
| 608 | ofp.action.output(port=5, max_len=0), |
| 609 | ofp.action.output(port=6, max_len=0)]), |
| 610 | ofp.bucket( |
| 611 | weight=1, |
| 612 | watch_port=6, |
| 613 | watch_group=0xffffffff, |
| 614 | actions=[ |
| 615 | ofp.action.output(port=5, max_len=0), |
| 616 | ofp.action.output(port=6, max_len=0)])]), |
| 617 | ofp.group_desc_stats_entry(type=ofp.OFPGT_FF, group_id=2, buckets=[])]) |
| 618 | buf = ''.join([ |
| 619 | '\x04', '\x13', # version, type |
| 620 | '\x00\x80', # length |
| 621 | '\x12\x34\x56\x78', # xid |
| 622 | '\x00\x07', # stats_type |
| 623 | '\x00\x00', # flags |
| 624 | '\x00' * 4, # pad |
| 625 | '\x00\x68', # entries[0].length |
| 626 | '\x03', # entries[0].group_type |
| 627 | '\x00', # entries[0].pad |
| 628 | '\x00\x00\x00\x01', # entries[0].group_id |
| 629 | '\x00\x30', # entries[0].buckets[0].len |
| 630 | '\x00\x01', # entries[0].buckets[0].weight |
| 631 | '\x00\x00\x00\x05', # entries[0].buckets[0].watch_port |
| 632 | '\xff\xff\xff\xff', # entries[0].buckets[0].watch_group |
| 633 | '\x00' * 4, # entries[0].pad |
| 634 | '\x00\x00', # entries[0].buckets[0].actions[0].type |
| 635 | '\x00\x10', # entries[0].buckets[0].actions[0].len |
| 636 | '\x00\x00\x00\x05', # entries[0].buckets[0].actions[0].port |
| 637 | '\x00\x00', # entries[0].buckets[0].actions[0].max_len |
| 638 | '\x00' * 6, # entries[0].pad |
| 639 | '\x00\x00', # entries[0].buckets[0].actions[1].type |
| 640 | '\x00\x10', # entries[0].buckets[0].actions[1].len |
| 641 | '\x00\x00\x00\x06', # entries[0].buckets[0].actions[1].port |
| 642 | '\x00\x00', # entries[0].buckets[0].actions[1].max_len |
| 643 | '\x00' * 6, # entries[0].pad |
| 644 | '\x00\x30', # entries[0].buckets[1].len |
| 645 | '\x00\x01', # entries[0].buckets[1].weight |
| 646 | '\x00\x00\x00\x06', # entries[0].buckets[1].watch_port |
| 647 | '\xff\xff\xff\xff', # entries[0].buckets[1].watch_group |
| 648 | '\x00' * 4, # entries[0].pad |
| 649 | '\x00\x00', # entries[0].buckets[1].actions[0].type |
| 650 | '\x00\x10', # entries[0].buckets[1].actions[0].len |
| 651 | '\x00\x00\x00\x05', # entries[0].buckets[1].actions[0].port |
| 652 | '\x00\x00', # entries[0].buckets[1].actions[0].max_len |
| 653 | '\x00' * 6, # entries[0].pad |
| 654 | '\x00\x00', # entries[0].buckets[1].actions[1].type |
| 655 | '\x00\x10', # entries[0].buckets[1].actions[1].len |
| 656 | '\x00\x00\x00\x06', # entries[0].buckets[1].actions[1].port |
| 657 | '\x00\x00', # entries[0].buckets[1].actions[1].max_len |
| 658 | '\x00' * 6, # entries[0].pad |
| 659 | '\x00\x08', # entries[1].length |
| 660 | '\x03', # entries[1].group_type |
| 661 | '\x00', # entries[1].pad |
| 662 | '\x00\x00\x00\x02', # entries[1].group_id |
| 663 | ]) |
| 664 | test_serialization(obj, buf) |
Rich Lane | 4c76498 | 2013-05-01 16:12:22 -0700 | [diff] [blame] | 665 | |
| 666 | def test_group_features_stats_request(self): |
| 667 | # TODO |
| 668 | pass |
| 669 | |
| 670 | def test_group_features_stats_reply(self): |
| 671 | # TODO |
| 672 | pass |
| 673 | |
| 674 | def test_meter_stats_request(self): |
| 675 | # TODO |
| 676 | pass |
| 677 | |
| 678 | def test_meter_stats_reply(self): |
| 679 | # TODO |
| 680 | pass |
| 681 | |
| 682 | def test_meter_config_stats_request(self): |
| 683 | # TODO |
| 684 | pass |
| 685 | |
| 686 | def test_meter_config_stats_reply(self): |
Rich Lane | d82c0a6 | 2013-05-02 15:40:35 -0700 | [diff] [blame^] | 687 | obj = ofp.message.meter_config_stats_reply( |
| 688 | xid=0x12345678, |
| 689 | flags=0, |
| 690 | entries=[ |
| 691 | ofp.meter_band.drop(rate=1, burst_size=2), |
| 692 | ofp.meter_band.dscp_remark(rate=3, burst_size=4, prec_level=5)]) |
| 693 | buf = ''.join([ |
| 694 | '\x04', '\x13', # version, type |
| 695 | '\x00\x30', # length |
| 696 | '\x12\x34\x56\x78', # xid |
| 697 | '\x00\x0a', # stats_type |
| 698 | '\x00\x00', # flags |
| 699 | '\x00' * 4, # pad |
| 700 | '\x00\x01', # entries[0].type |
| 701 | '\x00\x10', # entries[0].length |
| 702 | '\x00\x00\x00\x01', # entries[0].rate |
| 703 | '\x00\x00\x00\x02', # entries[0].burst_size |
| 704 | '\x00' * 4, # pad |
| 705 | '\x00\x02', # entries[1].type |
| 706 | '\x00\x10', # entries[1].length |
| 707 | '\x00\x00\x00\x03', # entries[1].rate |
| 708 | '\x00\x00\x00\x04', # entries[1].burst_size |
| 709 | '\x05', # entries[1].prec_level |
| 710 | '\x00' * 3, # pad |
| 711 | ]) |
| 712 | test_serialization(obj, buf) |
Rich Lane | 4c76498 | 2013-05-01 16:12:22 -0700 | [diff] [blame] | 713 | |
| 714 | def test_meter_features_stats_request(self): |
| 715 | # TODO |
| 716 | pass |
| 717 | |
| 718 | def test_meter_features_stats_reply(self): |
| 719 | # TODO |
| 720 | pass |
| 721 | |
| 722 | def test_table_features_stats_request(self): |
| 723 | # TODO |
| 724 | pass |
| 725 | |
| 726 | def test_table_features_stats_reply(self): |
| 727 | # TODO |
| 728 | pass |
| 729 | |
| 730 | def test_port_desc_stats_request(self): |
| 731 | # TODO |
| 732 | pass |
| 733 | |
| 734 | def test_port_desc_stats_reply(self): |
| 735 | # TODO |
| 736 | pass |
| 737 | |
| 738 | |
| 739 | def test_barrier_request(self): |
| 740 | # TODO |
| 741 | pass |
| 742 | |
| 743 | def test_barrier_reply(self): |
| 744 | # TODO |
| 745 | pass |
| 746 | |
| 747 | def test_queue_get_config_request(self): |
| 748 | # TODO |
| 749 | pass |
| 750 | |
| 751 | def test_queue_get_config_reply(self): |
| 752 | # TODO |
| 753 | pass |
| 754 | |
| 755 | def test_role_request(self): |
| 756 | # TODO |
| 757 | pass |
| 758 | |
| 759 | def test_role_reply(self): |
| 760 | # TODO |
| 761 | pass |
| 762 | |
| 763 | def test_get_async_request(self): |
| 764 | # TODO |
| 765 | pass |
| 766 | |
| 767 | def test_get_async_reply(self): |
| 768 | # TODO |
| 769 | pass |
| 770 | |
| 771 | def test_set_async(self): |
| 772 | # TODO |
| 773 | pass |
| 774 | |
| 775 | def test_meter_mod(self): |
| 776 | # TODO |
| 777 | pass |
| 778 | |
| 779 | # TODO test experimenter messages |
| 780 | |
| 781 | |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 782 | class TestOXM(unittest.TestCase): |
| 783 | def test_oxm_in_phy_port_pack(self): |
| 784 | import loxi.of13 as ofp |
| 785 | obj = ofp.oxm.in_phy_port(value=42) |
| 786 | expected = ''.join([ |
| 787 | '\x80\x00', # class |
| 788 | '\x02', # type/masked |
Rich Lane | 82e9f6e | 2013-04-25 17:32:22 -0700 | [diff] [blame] | 789 | '\x04', # length |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 790 | '\x00\x00\x00\x2a' # value |
| 791 | ]) |
| 792 | self.assertEquals(expected, obj.pack()) |
| 793 | |
| 794 | def test_oxm_in_phy_port_masked_pack(self): |
| 795 | import loxi.of13 as ofp |
| 796 | obj = ofp.oxm.in_phy_port_masked(value=42, value_mask=0xaabbccdd) |
| 797 | expected = ''.join([ |
| 798 | '\x80\x00', # class |
| 799 | '\x03', # type/masked |
Rich Lane | 82e9f6e | 2013-04-25 17:32:22 -0700 | [diff] [blame] | 800 | '\x08', # length |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 801 | '\x00\x00\x00\x2a', # value |
| 802 | '\xaa\xbb\xcc\xdd' # mask |
| 803 | ]) |
| 804 | self.assertEquals(expected, obj.pack()) |
| 805 | |
Rich Lane | 4180564 | 2013-03-19 15:00:26 -0700 | [diff] [blame] | 806 | def test_oxm_ipv6_dst_pack(self): |
| 807 | import loxi.of13 as ofp |
| 808 | obj = ofp.oxm.ipv6_dst(value='\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0d\x0f') |
| 809 | expected = ''.join([ |
| 810 | '\x80\x00', # class |
| 811 | '\x36', # type/masked |
Rich Lane | 82e9f6e | 2013-04-25 17:32:22 -0700 | [diff] [blame] | 812 | '\x10', # length |
Rich Lane | 4180564 | 2013-03-19 15:00:26 -0700 | [diff] [blame] | 813 | '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0d\x0f', # value |
| 814 | ]) |
| 815 | self.assertEquals(expected, obj.pack()) |
| 816 | |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 817 | class TestAllOF13(unittest.TestCase): |
| 818 | """ |
| 819 | Round-trips every class through serialization/deserialization. |
| 820 | Not a replacement for handcoded tests because it only uses the |
| 821 | default member values. |
| 822 | """ |
| 823 | |
| 824 | def setUp(self): |
Rich Lane | ea69375 | 2013-03-18 11:05:45 -0700 | [diff] [blame] | 825 | mods = [ofp.action,ofp.message,ofp.common,ofp.oxm] |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 826 | self.klasses = [klass for mod in mods |
| 827 | for klass in mod.__dict__.values() |
| 828 | if hasattr(klass, 'show')] |
| 829 | self.klasses.sort(key=lambda x: str(x)) |
| 830 | |
| 831 | def test_serialization(self): |
| 832 | expected_failures = [ |
Rich Lane | 8692ecd | 2013-05-02 11:33:53 -0700 | [diff] [blame] | 833 | ofp.common.flow_stats_entry, |
Rich Lane | 8692ecd | 2013-05-02 11:33:53 -0700 | [diff] [blame] | 834 | ofp.common.table_feature_prop_apply_actions, |
| 835 | ofp.common.table_feature_prop_apply_actions_miss, |
| 836 | ofp.common.table_feature_prop_instructions, |
| 837 | ofp.common.table_feature_prop_instructions_miss, |
| 838 | ofp.common.table_feature_prop_write_actions, |
| 839 | ofp.common.table_feature_prop_write_actions_miss, |
| 840 | ofp.common.table_features, |
| 841 | ofp.message.flow_add, |
| 842 | ofp.message.flow_delete, |
| 843 | ofp.message.flow_delete_strict, |
| 844 | ofp.message.flow_modify, |
| 845 | ofp.message.flow_modify_strict, |
Rich Lane | a018605 | 2013-05-01 14:18:39 -0700 | [diff] [blame] | 846 | ofp.message.meter_features_stats_reply, |
Rich Lane | 8692ecd | 2013-05-02 11:33:53 -0700 | [diff] [blame] | 847 | ofp.message.meter_stats_reply, |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 848 | ofp.message.table_features_stats_reply, |
| 849 | ofp.message.table_features_stats_request, |
| 850 | ] |
| 851 | for klass in self.klasses: |
| 852 | def fn(): |
| 853 | obj = klass() |
| 854 | if hasattr(obj, "xid"): obj.xid = 42 |
| 855 | buf = obj.pack() |
| 856 | obj2 = klass.unpack(buf) |
| 857 | self.assertEquals(obj, obj2) |
| 858 | if klass in expected_failures: |
| 859 | self.assertRaises(Exception, fn) |
| 860 | else: |
| 861 | fn() |
| 862 | |
| 863 | def test_show(self): |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 864 | expected_failures = [] |
Rich Lane | 3f07597 | 2013-03-15 22:56:29 -0700 | [diff] [blame] | 865 | for klass in self.klasses: |
| 866 | def fn(): |
| 867 | obj = klass() |
| 868 | if hasattr(obj, "xid"): obj.xid = 42 |
| 869 | obj.show() |
| 870 | if klass in expected_failures: |
| 871 | self.assertRaises(Exception, fn) |
| 872 | else: |
| 873 | fn() |
| 874 | |
| 875 | if __name__ == '__main__': |
| 876 | unittest.main() |