Rich Lane | 2be7f5d | 2013-12-17 13:16:34 -0800 | [diff] [blame^] | 1 | """ |
| 2 | Test the BSN role status message |
| 3 | |
| 4 | This is a backport of the OpenFlow 1.4 functionality. |
| 5 | """ |
| 6 | import struct |
| 7 | import unittest |
| 8 | import logging |
| 9 | |
| 10 | import oftest |
| 11 | from oftest import config |
| 12 | import oftest.controller as controller |
| 13 | import ofp |
| 14 | import oftest.base_tests as base_tests |
| 15 | |
| 16 | from oftest.testutils import * |
| 17 | |
| 18 | def simple_role_request(test, role, gen=None, con=None): |
| 19 | """ |
| 20 | Send a role request we expect to succeed |
| 21 | """ |
| 22 | if con == None: |
| 23 | con = test.controller |
| 24 | request = ofp.message.role_request(role=role, generation_id=gen) |
| 25 | response, _ = con.transact(request) |
| 26 | test.assertTrue(isinstance(response, ofp.message.role_reply), "Expected a role reply") |
| 27 | if role != ofp.OFPCR_ROLE_NOCHANGE: |
| 28 | test.assertEquals(response.role, role) |
| 29 | if gen != None: |
| 30 | test.assertEquals(response.generation_id, gen) |
| 31 | return response.role, response.generation_id |
| 32 | |
| 33 | @disabled |
| 34 | @nonstandard |
| 35 | class RoleStatus(unittest.TestCase): |
| 36 | """ |
| 37 | Verify that when a connection becomes a master the existing master is |
| 38 | downgraded to slave and receives a role-status message. |
| 39 | |
| 40 | Requires the switch to attempt to connect in parallel to ports 6653 |
| 41 | and 6753 on the configured IP. |
| 42 | """ |
| 43 | |
| 44 | def setUp(self): |
| 45 | host = config["controller_host"] |
| 46 | self.controllers = [ |
| 47 | controller.Controller(host=host,port=6653), |
| 48 | controller.Controller(host=host,port=6753) |
| 49 | ] |
| 50 | |
| 51 | def runTest(self): |
| 52 | # Connect and handshake with both controllers |
| 53 | for con in self.controllers: |
| 54 | con.start() |
| 55 | if not con.connect(): |
| 56 | raise AssertionError("failed to connect controller %s" % str(con)) |
| 57 | reply, _ = con.transact(ofp.message.features_request()) |
| 58 | self.assertTrue(isinstance(reply, ofp.message.features_reply)) |
| 59 | |
| 60 | # Assert initial role and get generation IDs |
| 61 | role, gen0 = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE, con=self.controllers[0]) |
| 62 | self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL) |
| 63 | role, gen1 = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE, con=self.controllers[1]) |
| 64 | self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL) |
| 65 | |
| 66 | # Initial role assignment: controller 0 is master, controller 1 is slave |
| 67 | simple_role_request(self, ofp.OFPCR_ROLE_MASTER, gen0, con=self.controllers[0]) |
| 68 | simple_role_request(self, ofp.OFPCR_ROLE_SLAVE, gen1, con=self.controllers[1]) |
| 69 | self.verify_role(self.controllers[0], ofp.OFPCR_ROLE_MASTER) |
| 70 | self.verify_role(self.controllers[1], ofp.OFPCR_ROLE_SLAVE) |
| 71 | |
| 72 | # Controller 1 requests master |
| 73 | # Controller 0 becomes slave |
| 74 | simple_role_request(self, ofp.OFPCR_ROLE_MASTER, gen1, con=self.controllers[1]) |
| 75 | self.verify_role(self.controllers[0], ofp.OFPCR_ROLE_SLAVE) |
| 76 | self.verify_role(self.controllers[1], ofp.OFPCR_ROLE_MASTER) |
| 77 | |
| 78 | # Controller 0 should receive a bsn_role_status message |
| 79 | msg, _ = self.controllers[0].poll(ofp.OFPT_EXPERIMENTER) |
| 80 | self.assertIsInstance(msg, ofp.message.bsn_role_status) |
| 81 | self.assertEqual(msg.role, ofp.OFPCR_ROLE_SLAVE) |
| 82 | self.assertEqual(msg.reason, ofp.OFP_BSN_CONTROLLER_ROLE_REASON_MASTER_REQUEST) |
| 83 | self.assertEqual(msg.generation_id, gen1) |
| 84 | |
| 85 | def verify_role(self, con, role): |
| 86 | rcv_role, _ = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE, con=con) |
| 87 | self.assertEqual(rcv_role, role) |
| 88 | |
| 89 | def tearDown(self): |
| 90 | for con in self.controllers: |
| 91 | con.shutdown() |
| 92 | |