Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 14 | from unittest import TestCase, main |
| 15 | from of_protocol_handler import OpenFlowProtocolHandler |
| 16 | import loxi.of13 as ofp |
| 17 | |
| 18 | class TestOF_Protocol_handler(TestCase): |
| 19 | |
| 20 | def gen_packet_in(self): |
| 21 | packet_in = 1 |
| 22 | return packet_in |
| 23 | |
| 24 | def gen_device(self): |
| 25 | device =lambda: None |
| 26 | device.id = "1" |
| 27 | device.datapath_id = 1 |
| 28 | device.desc = '{mfr_desc: "cord porject" hw_desc: "simualted pon" sw_desc: "simualted pon"\ |
| 29 | serial_num: "2f150d56afa2405eba3ba24e33ce8df9" dp_desc: "n/a"}' |
| 30 | device.switch_features = '{ n_buffers: 256 n_tables: 2 capabilities: 15 }' |
| 31 | device.root_device_id = "a245bd8bb8b8" |
| 32 | return device |
| 33 | |
| 34 | def gen_generic_obj(self): |
| 35 | generic_obj = lambda: None |
| 36 | return generic_obj |
| 37 | |
| 38 | def gen_role_req(self): |
| 39 | req = self.gen_generic_obj() |
| 40 | req.role = ofp.OFPCR_ROLE_MASTER |
| 41 | return req |
| 42 | |
Koray Kokten | 8592a23 | 2018-08-27 07:41:14 +0000 | [diff] [blame] | 43 | def test_handle_flow_mod_request_role_slave(self): |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 44 | generic_obj = self.gen_generic_obj() |
| 45 | device = self.gen_device() |
| 46 | of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) |
| 47 | of_proto_handler.role = ofp.OFPCR_ROLE_SLAVE |
| 48 | with self.assertRaises(Exception) as context: |
| 49 | of_proto_handler.handle_flow_mod_request(generic_obj) |
| 50 | print context.exception |
sathishg | 23ac35c | 2017-10-14 03:21:43 +0530 | [diff] [blame] | 51 | self.assertTrue('\'function\' object has no attribute \'send\'' in str(context.exception)) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 52 | |
| 53 | def test_handle_flow_mod_request_role_master(self): |
| 54 | generic_obj = self.gen_generic_obj() |
| 55 | device = self.gen_device() |
| 56 | of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) |
| 57 | of_proto_handler.role = ofp.OFPCR_ROLE_MASTER |
| 58 | of_proto_handler.handle_flow_mod_request(generic_obj) |
| 59 | |
Koray Kokten | 8592a23 | 2018-08-27 07:41:14 +0000 | [diff] [blame] | 60 | def test_handle_meter_mod_request_role_slave(self): |
| 61 | generic_obj = self.gen_generic_obj() |
| 62 | device = self.gen_device() |
| 63 | of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) |
| 64 | of_proto_handler.role = ofp.OFPCR_ROLE_SLAVE |
| 65 | with self.assertRaises(Exception) as context: |
| 66 | of_proto_handler.handle_meter_mod_request(generic_obj) |
| 67 | print context.exception |
| 68 | self.assertTrue('\'function\' object has no attribute \'send\'' in str(context.exception)) |
| 69 | |
| 70 | def test_handle_meter_mod_request_role_master(self): |
| 71 | generic_obj = self.gen_generic_obj() |
| 72 | device = self.gen_device() |
| 73 | of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) |
| 74 | of_proto_handler.role = ofp.OFPCR_ROLE_MASTER |
| 75 | of_proto_handler.handle_meter_mod_request(generic_obj) |
| 76 | |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 77 | def test_handle_role_request(self): |
| 78 | generic_obj = self.gen_generic_obj() |
| 79 | req = self.gen_role_req() |
| 80 | device = self.gen_device() |
| 81 | of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) |
| 82 | with self.assertRaises(Exception) as context: |
| 83 | of_proto_handler.handle_role_request(req) |
| 84 | self.assertEqual(of_proto_handler.role,req.role) |
| 85 | print context.exception |
sathishg | 23ac35c | 2017-10-14 03:21:43 +0530 | [diff] [blame] | 86 | self.assertTrue('\'function\' object has no attribute \'generation_is_defined\'' in str(context.exception)) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 87 | |
| 88 | def test_forward_packet_in_role_none(self): |
| 89 | packet_in = self.gen_packet_in() |
| 90 | generic_obj = self.gen_generic_obj() |
| 91 | device = self.gen_device() |
| 92 | of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) |
| 93 | of_proto_handler.forward_packet_in(packet_in) |
| 94 | |
| 95 | def test_forward_packet_in_role_master(self): |
| 96 | packet_in = self.gen_packet_in() |
| 97 | generic_obj = self.gen_generic_obj() |
| 98 | device = self.gen_device() |
| 99 | of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) |
| 100 | of_proto_handler.role = ofp.OFPCR_ROLE_MASTER |
| 101 | with self.assertRaises(Exception) as context: |
| 102 | of_proto_handler.forward_packet_in(packet_in) |
| 103 | print context.exception |
sathishg | 23ac35c | 2017-10-14 03:21:43 +0530 | [diff] [blame] | 104 | self.assertTrue('\'function\' object has no attribute \'send\'' in str(context.exception)) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 105 | |
Girish Gowdru | e20da0e | 2019-01-28 20:22:05 -0800 | [diff] [blame] | 106 | def test_handle_meter_features_request_in_role_master(self): |
| 107 | generic_obj = self.gen_generic_obj() |
| 108 | device = self.gen_device() |
| 109 | of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) |
| 110 | of_proto_handler.role = ofp.OFPCR_ROLE_MASTER |
| 111 | with self.assertRaises(Exception) as context: |
| 112 | of_proto_handler.handle_meter_features_request(generic_obj) |
| 113 | print context.exception |
| 114 | self.assertTrue('\'function\' object has no attribute \'send\'' in str(context.exception)) |
| 115 | |
| 116 | def test_handle_meter_features_request_in_role_slave(self): |
| 117 | generic_obj = self.gen_generic_obj() |
| 118 | device = self.gen_device() |
| 119 | of_proto_handler = OpenFlowProtocolHandler(device.datapath_id, device.id, generic_obj, generic_obj, generic_obj) |
| 120 | of_proto_handler.role = ofp.OFPCR_ROLE_SLAVE |
| 121 | with self.assertRaises(Exception) as context: |
| 122 | of_proto_handler.handle_meter_features_request(generic_obj) |
| 123 | print |
| 124 | context.exception |
| 125 | self.assertTrue('\'function\' object has no attribute \'send\'' in str(context.exception)) |
| 126 | |
| 127 | |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 128 | if __name__ == '__main__': |
sathishg | 23ac35c | 2017-10-14 03:21:43 +0530 | [diff] [blame] | 129 | main() |