Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [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. |
| 14 | |
| 15 | import unittest |
| 16 | from mock import patch, call, Mock, PropertyMock |
| 17 | import requests_mock |
| 18 | |
| 19 | import os, sys |
| 20 | |
| 21 | # Hack to load synchronizer framework |
| 22 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 23 | xos_dir=os.path.join(test_path, "../../..") |
| 24 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 25 | xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos") |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 26 | services_dir = os.path.join(xos_dir, "../../xos_services") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 27 | sys.path.append(xos_dir) |
| 28 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 29 | # END Hack to load synchronizer framework |
| 30 | |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 31 | # generate model from xproto |
| 32 | def get_models_fn(service_name, xproto_name): |
| 33 | name = os.path.join(service_name, "xos", xproto_name) |
| 34 | if os.path.exists(os.path.join(services_dir, name)): |
| 35 | return name |
| 36 | else: |
| 37 | name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name) |
| 38 | if os.path.exists(os.path.join(services_dir, name)): |
| 39 | return name |
| 40 | raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name)) |
| 41 | # END generate model from xproto |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 42 | |
| 43 | class TestSyncOLTDevice(unittest.TestCase): |
| 44 | |
| 45 | def setUp(self): |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 46 | |
| 47 | self.sys_path_save = sys.path |
| 48 | sys.path.append(xos_dir) |
| 49 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 50 | |
| 51 | # Setting up the config module |
| 52 | from xosconfig import Config |
| 53 | config = os.path.join(test_path, "../model_policies/test_config.yaml") |
| 54 | Config.clear() |
| 55 | Config.init(config, "synchronizer-config-schema.yaml") |
| 56 | # END Setting up the config module |
| 57 | |
| 58 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 59 | build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")]) |
| 60 | import synchronizers.new_base.modelaccessor |
| 61 | from sync_olt_device import SyncOLTDevice |
| 62 | self.sync_step = SyncOLTDevice |
| 63 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 64 | # create a mock service instance |
| 65 | o = Mock() |
| 66 | o.volt_service.voltha_url = "voltha_url" |
| 67 | o.volt_service.voltha_user = "voltha_user" |
| 68 | o.volt_service.voltha_pass = "voltha_pass" |
| 69 | o.volt_service.p_onos_url = "p_onos_url" |
| 70 | o.volt_service.p_onos_user = "p_onos_user" |
| 71 | o.volt_service.p_onos_pass = "p_onos_pass" |
| 72 | |
| 73 | o.device_type = "ponsim_olt" |
| 74 | o.host = "172.17.0.1" |
| 75 | o.port = "50060" |
| 76 | o.uplink = "129" |
| 77 | o.vlan = "3" |
| 78 | o.driver = "pmc-olt" |
| 79 | |
| 80 | o.tologdict.return_value = {'name': "Mock VOLTServiceInstance"} |
| 81 | |
| 82 | o.save.return_value = "Saved" |
| 83 | |
| 84 | self.o = o |
| 85 | |
| 86 | def tearDown(self): |
| 87 | self.o = None |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 88 | sys.path = self.sys_path_save |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 89 | |
| 90 | def test_format_url(self): |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 91 | url = self.sync_step.format_url("onf.com") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 92 | self.assertEqual(url, "http://onf.com") |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 93 | url = self.sync_step.format_url("http://onf.com") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 94 | self.assertEqual(url, "http://onf.com") |
| 95 | |
| 96 | def test_get_voltha_info(self): |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 97 | voltha_dict = self.sync_step.get_voltha_info(self.o) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 98 | |
| 99 | self.assertEqual(voltha_dict["url"], "http://voltha_url") |
| 100 | self.assertEqual(voltha_dict["user"], "voltha_user") |
| 101 | self.assertEqual(voltha_dict["pass"], "voltha_pass") |
| 102 | |
| 103 | def test_get_onos_info(self): |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 104 | p_onos_dict = self.sync_step.get_p_onos_info(self.o) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 105 | |
| 106 | self.assertEqual(p_onos_dict["url"], "http://p_onos_url") |
| 107 | self.assertEqual(p_onos_dict["user"], "p_onos_user") |
| 108 | self.assertEqual(p_onos_dict["pass"], "p_onos_pass") |
| 109 | |
| 110 | @requests_mock.Mocker() |
| 111 | def test_get_of_id_from_device(self, m): |
| 112 | logical_devices = { |
| 113 | "items": [ |
| 114 | {"root_device_id": "123", "id": "abc"}, |
| 115 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 116 | ] |
| 117 | } |
| 118 | m.get("http://voltha_url/api/v1/logical_devices", status_code=200, json=logical_devices) |
| 119 | self.o.device_id = "123" |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 120 | of_id = self.sync_step.get_of_id_from_device(self.o) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 121 | self.assertEqual(of_id, "abc") |
| 122 | |
| 123 | with self.assertRaises(Exception) as e: |
| 124 | self.o.device_id = "idonotexist" |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 125 | self.sync_step.get_of_id_from_device(self.o) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 126 | self.assertEqual(e.exception.message, "Can't find a logical device for device id: idonotexist") |
| 127 | |
| 128 | @requests_mock.Mocker() |
| 129 | def test_sync_record_fail_add(self, m): |
| 130 | """ |
| 131 | Should print an error if we can't add the device in VOLTHA |
| 132 | """ |
| 133 | m.post("http://voltha_url/api/v1/devices", status_code=500, text="MockError") |
| 134 | |
| 135 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 136 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 137 | self.assertEqual(e.exception.message, "Failed to add device: MockError") |
| 138 | |
| 139 | @requests_mock.Mocker() |
| 140 | def test_sync_record_fail_no_id(self, m): |
| 141 | """ |
| 142 | Should print an error if VOLTHA does not return the device id |
| 143 | """ |
| 144 | m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": ""}) |
| 145 | |
| 146 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 147 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 148 | self.assertEqual(e.exception.message, "VOLTHA Device Id is empty, this probably means that the device is already provisioned in VOLTHA") |
| 149 | |
| 150 | @requests_mock.Mocker() |
| 151 | def test_sync_record_fail_enable(self, m): |
| 152 | """ |
| 153 | Should print an error if device.enable fails |
| 154 | """ |
| 155 | m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": "123"}) |
| 156 | m.post("http://voltha_url/api/v1/devices/123/enable", status_code=500, text="EnableError") |
| 157 | |
| 158 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 159 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 160 | self.assertEqual(e.exception.message, "Failed to enable device: EnableError") |
| 161 | |
| 162 | @requests_mock.Mocker() |
| 163 | def test_sync_record_success(self, m): |
| 164 | """ |
| 165 | If device.enable succed should fetch the state, retrieve the of_id and push it to ONOS |
| 166 | """ |
| 167 | m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": "123"}) |
| 168 | m.post("http://voltha_url/api/v1/devices/123/enable", status_code=200) |
| 169 | m.get("http://voltha_url/api/v1/devices/123", json={"oper_status": "ENABLED", "admin_state": "ACTIVE"}) |
| 170 | logical_devices = { |
| 171 | "items": [ |
| 172 | {"root_device_id": "123", "id": "abc"}, |
| 173 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 174 | ] |
| 175 | } |
| 176 | m.get("http://voltha_url/api/v1/logical_devices", status_code=200, json=logical_devices) |
| 177 | |
| 178 | def match_onos_req(req): |
| 179 | r = req.json()['devices'] |
| 180 | if not r['abc']: |
| 181 | return False |
| 182 | else: |
| 183 | if not r['abc']['basic']['driver'] == 'pmc-olt': |
| 184 | return False |
| 185 | if not r['abc']['accessDevice']['vlan'] == "3" or not r['abc']['accessDevice']['uplink'] == "129": |
| 186 | return False |
| 187 | return True |
| 188 | |
| 189 | m.post("http://p_onos_url/onos/v1/network/configuration/", status_code=200, additional_matcher=match_onos_req, json={}) |
| 190 | |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 191 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 192 | self.assertEqual(self.o.admin_state, "ACTIVE") |
| 193 | self.assertEqual(self.o.oper_status, "ENABLED") |
| 194 | self.assertEqual(self.o.of_id, "abc") |
| 195 | self.o.save.assert_called_once() |
| 196 | |
| 197 | @requests_mock.Mocker() |
| 198 | def test_delete_record(self, m): |
| 199 | self.o.of_id = "abc" |
| 200 | self.o.device_id = "123" |
| 201 | |
| 202 | m.delete("http://p_onos_url/onos/v1/network/configuration/devices/abc", status_code=200) |
| 203 | m.post("http://voltha_url/api/v1/devices/123/disable", status_code=200) |
| 204 | m.delete("http://voltha_url/api/v1/devices/123/delete", status_code=200) |
| 205 | |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame^] | 206 | self.sync_step().delete_record(self.o) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 207 | |
| 208 | # we don't need to assert here, if there are no exceptions it succeded |
| 209 | |
| 210 | if __name__ == "__main__": |
| 211 | unittest.main() |