blob: 59d09a98cde493c5bcb634c1e82be629c1fd2983 [file] [log] [blame]
Matteo Scandoloa229eca2017-08-08 13:05:28 -07001
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 Lane0bd61022015-03-04 10:57:11 -080017"""
18Test the role status message
19"""
20import struct
21import unittest
22import logging
23
24import oftest
25from oftest import config
26import oftest.controller as controller
27import ofp
28import oftest.base_tests as base_tests
29
30from oftest.testutils import *
31
32def simple_role_request(test, role, gen=None, con=None):
33 """
34 Send a role request we expect to succeed
35 """
36 if con == None:
37 con = test.controller
38 request = ofp.message.role_request(role=role, generation_id=gen)
39 response, _ = con.transact(request)
40 test.assertTrue(isinstance(response, ofp.message.role_reply), "Expected a role reply")
41 if role != ofp.OFPCR_ROLE_NOCHANGE:
42 test.assertEquals(response.role, role)
43 if gen != None:
44 test.assertEquals(response.generation_id, gen)
45 return response.role, response.generation_id
46
47@disabled
48class RoleStatus(unittest.TestCase):
49 """
50 Verify that when a connection becomes a master the existing master is
51 downgraded to slave and receives a role-status message.
52
53 Requires the switch to attempt to connect in parallel to ports 6653
54 and 6753 on the configured IP.
55 """
56
57 def setUp(self):
58 host = config["controller_host"]
59 self.controllers = [
60 controller.Controller(host=host,port=6653),
61 controller.Controller(host=host,port=6753)
62 ]
63
64 def runTest(self):
65 # Connect and handshake with both controllers
66 for con in self.controllers:
67 con.start()
68 if not con.connect():
69 raise AssertionError("failed to connect controller %s" % str(con))
70 reply, _ = con.transact(ofp.message.features_request())
71 self.assertTrue(isinstance(reply, ofp.message.features_reply))
72
73 # Assert initial role and get generation IDs
74 role, gen0 = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE, con=self.controllers[0])
75 self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL)
76 role, gen1 = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE, con=self.controllers[1])
77 self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL)
78
79 # Initial role assignment: controller 0 is master, controller 1 is slave
80 simple_role_request(self, ofp.OFPCR_ROLE_MASTER, gen0, con=self.controllers[0])
81 simple_role_request(self, ofp.OFPCR_ROLE_SLAVE, gen1, con=self.controllers[1])
82 self.verify_role(self.controllers[0], ofp.OFPCR_ROLE_MASTER)
83 self.verify_role(self.controllers[1], ofp.OFPCR_ROLE_SLAVE)
84
85 # Controller 1 requests master
86 # Controller 0 becomes slave
87 simple_role_request(self, ofp.OFPCR_ROLE_MASTER, gen1, con=self.controllers[1])
88 self.verify_role(self.controllers[0], ofp.OFPCR_ROLE_SLAVE)
89 self.verify_role(self.controllers[1], ofp.OFPCR_ROLE_MASTER)
90
91 # Controller 0 should receive a role_status message
92 msg, _ = self.controllers[0].poll(ofp.message.role_status)
93 self.assertIsInstance(msg, ofp.message.role_status)
94 self.assertEqual(msg.role, ofp.OFPCR_ROLE_SLAVE)
95 self.assertEqual(msg.reason, ofp.OFPCRR_MASTER_REQUEST)
96 self.assertEqual(msg.generation_id, gen1)
97
98 def verify_role(self, con, role):
99 rcv_role, _ = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE, con=con)
100 self.assertEqual(rcv_role, role)
101
102 def tearDown(self):
103 for con in self.controllers:
104 con.shutdown()
105