Matteo Scandolo | a229eca | 2017-08-08 13:05:28 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Rich Lane | 2be7f5d | 2013-12-17 13:16:34 -0800 | [diff] [blame] | 17 | """ |
| 18 | Test the BSN role status message |
| 19 | |
| 20 | This is a backport of the OpenFlow 1.4 functionality. |
| 21 | """ |
| 22 | import struct |
| 23 | import unittest |
| 24 | import logging |
| 25 | |
| 26 | import oftest |
| 27 | from oftest import config |
| 28 | import oftest.controller as controller |
| 29 | import ofp |
| 30 | import oftest.base_tests as base_tests |
| 31 | |
| 32 | from oftest.testutils import * |
| 33 | |
| 34 | def simple_role_request(test, role, gen=None, con=None): |
| 35 | """ |
| 36 | Send a role request we expect to succeed |
| 37 | """ |
| 38 | if con == None: |
| 39 | con = test.controller |
| 40 | request = ofp.message.role_request(role=role, generation_id=gen) |
| 41 | response, _ = con.transact(request) |
| 42 | test.assertTrue(isinstance(response, ofp.message.role_reply), "Expected a role reply") |
| 43 | if role != ofp.OFPCR_ROLE_NOCHANGE: |
| 44 | test.assertEquals(response.role, role) |
| 45 | if gen != None: |
| 46 | test.assertEquals(response.generation_id, gen) |
| 47 | return response.role, response.generation_id |
| 48 | |
| 49 | @disabled |
| 50 | @nonstandard |
| 51 | class RoleStatus(unittest.TestCase): |
| 52 | """ |
| 53 | Verify that when a connection becomes a master the existing master is |
| 54 | downgraded to slave and receives a role-status message. |
| 55 | |
| 56 | Requires the switch to attempt to connect in parallel to ports 6653 |
| 57 | and 6753 on the configured IP. |
| 58 | """ |
| 59 | |
| 60 | def setUp(self): |
| 61 | host = config["controller_host"] |
| 62 | self.controllers = [ |
| 63 | controller.Controller(host=host,port=6653), |
| 64 | controller.Controller(host=host,port=6753) |
| 65 | ] |
| 66 | |
| 67 | def runTest(self): |
| 68 | # Connect and handshake with both controllers |
| 69 | for con in self.controllers: |
| 70 | con.start() |
| 71 | if not con.connect(): |
| 72 | raise AssertionError("failed to connect controller %s" % str(con)) |
| 73 | reply, _ = con.transact(ofp.message.features_request()) |
| 74 | self.assertTrue(isinstance(reply, ofp.message.features_reply)) |
| 75 | |
| 76 | # Assert initial role and get generation IDs |
| 77 | role, gen0 = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE, con=self.controllers[0]) |
| 78 | self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL) |
| 79 | role, gen1 = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE, con=self.controllers[1]) |
| 80 | self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL) |
| 81 | |
| 82 | # Initial role assignment: controller 0 is master, controller 1 is slave |
| 83 | simple_role_request(self, ofp.OFPCR_ROLE_MASTER, gen0, con=self.controllers[0]) |
| 84 | simple_role_request(self, ofp.OFPCR_ROLE_SLAVE, gen1, con=self.controllers[1]) |
| 85 | self.verify_role(self.controllers[0], ofp.OFPCR_ROLE_MASTER) |
| 86 | self.verify_role(self.controllers[1], ofp.OFPCR_ROLE_SLAVE) |
| 87 | |
| 88 | # Controller 1 requests master |
| 89 | # Controller 0 becomes slave |
| 90 | simple_role_request(self, ofp.OFPCR_ROLE_MASTER, gen1, con=self.controllers[1]) |
| 91 | self.verify_role(self.controllers[0], ofp.OFPCR_ROLE_SLAVE) |
| 92 | self.verify_role(self.controllers[1], ofp.OFPCR_ROLE_MASTER) |
| 93 | |
| 94 | # Controller 0 should receive a bsn_role_status message |
| 95 | msg, _ = self.controllers[0].poll(ofp.OFPT_EXPERIMENTER) |
| 96 | self.assertIsInstance(msg, ofp.message.bsn_role_status) |
| 97 | self.assertEqual(msg.role, ofp.OFPCR_ROLE_SLAVE) |
| 98 | self.assertEqual(msg.reason, ofp.OFP_BSN_CONTROLLER_ROLE_REASON_MASTER_REQUEST) |
| 99 | self.assertEqual(msg.generation_id, gen1) |
| 100 | |
| 101 | def verify_role(self, con, role): |
| 102 | rcv_role, _ = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE, con=con) |
| 103 | self.assertEqual(rcv_role, role) |
| 104 | |
| 105 | def tearDown(self): |
| 106 | for con in self.controllers: |
| 107 | con.shutdown() |
| 108 | |