Takahiro Suzuki | 2b66b94 | 2020-12-17 11:58:14 +0900 | [diff] [blame^] | 1 | # Copyright 2020-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. |
| 14 | |
| 15 | import unittest |
| 16 | from mock import patch, Mock |
| 17 | import json |
| 18 | |
| 19 | import os |
| 20 | import sys |
| 21 | |
| 22 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 23 | |
| 24 | |
| 25 | class TestSyncOLTDevice(unittest.TestCase): |
| 26 | |
| 27 | def setUp(self): |
| 28 | |
| 29 | self.sys_path_save = sys.path |
| 30 | |
| 31 | # Setting up the config module |
| 32 | from xosconfig import Config |
| 33 | config = os.path.join(test_path, "../test_config.yaml") |
| 34 | Config.clear() |
| 35 | Config.init(config, "synchronizer-config-schema.yaml") |
| 36 | # END Setting up the config module |
| 37 | |
| 38 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 39 | mock_modelaccessor_config(test_path, [("ntt-workflow-driver", "ntt-workflow-driver.xproto"), |
| 40 | ("olt-service", "volt.xproto"), |
| 41 | ("rcord", "rcord.xproto")]) |
| 42 | |
| 43 | import xossynchronizer.modelaccessor |
| 44 | import mock_modelaccessor |
| 45 | reload(mock_modelaccessor) # in case nose2 loaded it in a previous test |
| 46 | reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test |
| 47 | |
| 48 | from xossynchronizer.modelaccessor import model_accessor |
| 49 | from onu_event import ONUEventStep |
| 50 | |
| 51 | # import all class names to globals |
| 52 | for (k, v) in model_accessor.all_model_classes.items(): |
| 53 | globals()[k] = v |
| 54 | |
| 55 | self.model_accessor = model_accessor |
| 56 | self.log = Mock() |
| 57 | |
| 58 | self.event_step = ONUEventStep(model_accessor=self.model_accessor, log=self.log) |
| 59 | |
| 60 | self.event = Mock() |
| 61 | self.event_dict = { |
| 62 | 'status': 'activated', |
| 63 | 'serialNumber': 'BRCM1234', |
| 64 | 'deviceId': 'of:109299321', |
| 65 | 'portNumber': '16' |
| 66 | } |
| 67 | self.event.value = json.dumps(self.event_dict) |
| 68 | |
| 69 | self.pppoe = NttWorkflowDriverService(name="ntt-workflow-driver") |
| 70 | |
| 71 | self.pon_port = PONPort() |
| 72 | self.pon_port.port_no = 1234 |
| 73 | |
| 74 | self.onu = ONUDevice() |
| 75 | self.onu.pon_port = self.pon_port |
| 76 | self.onu.serial_number = "BRCM1234" |
| 77 | self.onu.mac_address = "0a0a0a" |
| 78 | |
| 79 | self.technologyProfile = TechnologyProfile() |
| 80 | self.technologyProfile.profile_id = 64 |
| 81 | self.technologyProfile.profile_value = '{"profile_type": "EPON","epon_attribute": {"package_type": "A"}}' |
| 82 | |
| 83 | def tearDown(self): |
| 84 | sys.path = self.sys_path_save |
| 85 | |
| 86 | def test_create_instance(self): |
| 87 | |
| 88 | with patch.object(NttWorkflowDriverServiceInstance.objects, "get_items") as ntt_si_mock, \ |
| 89 | patch.object(NttWorkflowDriverOltInformation.objects, "get_items") as ntt_oi_mock, \ |
| 90 | patch.object(NttWorkflowDriverService.objects, "get_items") as service_mock, \ |
| 91 | patch.object(NttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save, \ |
| 92 | patch.object(ONUDevice.objects, "get_items") as onu_mock, \ |
| 93 | patch.object(TechnologyProfile.objects, "get_items") as technologyProfile_mock: |
| 94 | |
| 95 | ntt_si_mock.return_value = [] |
| 96 | ntt_oi_mock.return_value = [] |
| 97 | service_mock.return_value = [self.pppoe] |
| 98 | onu_mock.return_value = [self.onu] |
| 99 | technologyProfile_mock.return_value = [self.technologyProfile] |
| 100 | |
| 101 | self.event_step.process_event(self.event) |
| 102 | |
| 103 | ntt_si = mock_save.call_args[0][0] |
| 104 | |
| 105 | self.assertEqual(mock_save.call_count, 1) |
| 106 | |
| 107 | self.assertEqual(ntt_si.serial_number, self.event_dict['serialNumber']) |
| 108 | self.assertEqual(ntt_si.of_dpid, self.event_dict['deviceId']) |
| 109 | self.assertEqual(ntt_si.uni_port_id, long(self.event_dict['portNumber'])) |
| 110 | # Receiving an ONU event doesn't change the admin_onu_state until the model policy runs |
| 111 | self.assertEqual(ntt_si.admin_onu_state, "AWAITING") |
| 112 | self.assertEqual(ntt_si.oper_onu_status, "ENABLED") |
| 113 | |
| 114 | def test_reuse_instance(self): |
| 115 | |
| 116 | si = NttWorkflowDriverServiceInstance( |
| 117 | serial_number=self.event_dict["serialNumber"], |
| 118 | of_dpid="foo", |
| 119 | uni_port_id="foo" |
| 120 | ) |
| 121 | |
| 122 | oi = NttWorkflowDriverOltInformation( |
| 123 | of_dpid="of:109299321" |
| 124 | ) |
| 125 | |
| 126 | with patch.object(NttWorkflowDriverServiceInstance.objects, "get_items") as ntt_si_mock, \ |
| 127 | patch.object(NttWorkflowDriverOltInformation.objects, "get_items") as ntt_oi_mock, \ |
| 128 | patch.object(NttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save: |
| 129 | |
| 130 | ntt_si_mock.return_value = [si] |
| 131 | ntt_oi_mock.return_value = [oi] |
| 132 | |
| 133 | self.event_step.process_event(self.event) |
| 134 | |
| 135 | ntt_si = mock_save.call_args[0][0] |
| 136 | |
| 137 | self.assertEqual(mock_save.call_count, 1) |
| 138 | |
| 139 | self.assertEqual(ntt_si.serial_number, self.event_dict['serialNumber']) |
| 140 | self.assertEqual(ntt_si.of_dpid, self.event_dict['deviceId']) |
| 141 | self.assertEqual(ntt_si.uni_port_id, long(self.event_dict['portNumber'])) |
| 142 | # Receiving an ONU event doesn't change the admin_onu_state until the model policy runs |
| 143 | self.assertEqual(ntt_si.admin_onu_state, "AWAITING") |
| 144 | self.assertEqual(ntt_si.oper_onu_status, "ENABLED") |
| 145 | |
| 146 | def test_disable_onu(self): |
| 147 | self.event_dict = { |
| 148 | 'status': 'disabled', |
| 149 | 'serialNumber': 'BRCM1234', |
| 150 | 'deviceId': 'of:109299321', |
| 151 | 'portNumber': '16', |
| 152 | } |
| 153 | |
| 154 | si = NttWorkflowDriverServiceInstance( |
| 155 | serial_number=self.event_dict["serialNumber"], |
| 156 | of_dpid="foo", |
| 157 | uni_port_id="foo", |
| 158 | admin_onu_state="ENABLED", |
| 159 | oper_onu_status="ENABLED", |
| 160 | ) |
| 161 | |
| 162 | oi = NttWorkflowDriverOltInformation( |
| 163 | of_dpid="of:109299321" |
| 164 | ) |
| 165 | |
| 166 | self.event.value = json.dumps(self.event_dict) |
| 167 | |
| 168 | with patch.object(NttWorkflowDriverServiceInstance.objects, "get_items") as ntt_si_mock, \ |
| 169 | patch.object(NttWorkflowDriverOltInformation.objects, "get_items") as ntt_oi_mock, \ |
| 170 | patch.object(NttWorkflowDriverServiceInstance, "save_changed_fields", autospec=True) as mock_save: |
| 171 | ntt_si_mock.return_value = [si] |
| 172 | ntt_oi_mock.return_value = [oi] |
| 173 | |
| 174 | self.event_step.process_event(self.event) |
| 175 | |
| 176 | ntt_si = mock_save.call_args[0][0] |
| 177 | |
| 178 | self.assertEqual(mock_save.call_count, 1) |
| 179 | |
| 180 | # Receiving an ONU event doesn't change the admin_onu_state until the model policy runs |
| 181 | self.assertEqual(ntt_si.admin_onu_state, 'ENABLED') |
| 182 | self.assertEqual(ntt_si.oper_onu_status, 'DISABLED') |
| 183 | |
| 184 | def test_enable_onu(self): |
| 185 | self.event_dict = { |
| 186 | 'status': 'activated', |
| 187 | 'serialNumber': 'BRCM1234', |
| 188 | 'deviceId': 'of:109299321', |
| 189 | 'portNumber': '16', |
| 190 | } |
| 191 | |
| 192 | si = NttWorkflowDriverServiceInstance( |
| 193 | serial_number=self.event_dict["serialNumber"], |
| 194 | of_dpid="foo", |
| 195 | uni_port_id="foo", |
| 196 | admin_onu_state="DISABLED", |
| 197 | oper_onu_status="DISABLED", |
| 198 | ) |
| 199 | |
| 200 | oi = NttWorkflowDriverOltInformation( |
| 201 | of_dpid="of:109299321" |
| 202 | ) |
| 203 | |
| 204 | self.event.value = json.dumps(self.event_dict) |
| 205 | |
| 206 | with patch.object(NttWorkflowDriverServiceInstance.objects, "get_items") as ntt_si_mock, \ |
| 207 | patch.object(NttWorkflowDriverOltInformation.objects, "get_items") as ntt_oi_mock, \ |
| 208 | patch.object(NttWorkflowDriverServiceInstance, "save_changed_fields", autospec=True) as mock_save: |
| 209 | ntt_si_mock.return_value = [si] |
| 210 | ntt_oi_mock.return_value = [oi] |
| 211 | |
| 212 | self.event_step.process_event(self.event) |
| 213 | |
| 214 | ntt_si = mock_save.call_args[0][0] |
| 215 | |
| 216 | self.assertEqual(mock_save.call_count, 1) |
| 217 | |
| 218 | # Receiving an ONU event doesn't change the admin_onu_state until the model policy runs |
| 219 | self.assertEqual(ntt_si.admin_onu_state, 'DISABLED') |
| 220 | self.assertEqual(ntt_si.oper_onu_status, 'ENABLED') |
| 221 | |
| 222 | |
| 223 | |
| 224 | if __name__ == '__main__': |
| 225 | sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) # for import of helpers.py |
| 226 | unittest.main() |