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