blob: 957738ab2cd2c181efdd802741594d69a82204dc [file] [log] [blame]
Rich Lane2be7f5d2013-12-17 13:16:34 -08001"""
2Test the BSN role status message
3
4This is a backport of the OpenFlow 1.4 functionality.
5"""
6import struct
7import unittest
8import logging
9
10import oftest
11from oftest import config
12import oftest.controller as controller
13import ofp
14import oftest.base_tests as base_tests
15
16from oftest.testutils import *
17
18def 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
35class 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