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 | |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 15 | from requests import ConnectionError |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 16 | import unittest |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 17 | import functools |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 18 | from mock import patch, call, Mock, PropertyMock |
| 19 | import requests_mock |
| 20 | |
| 21 | import os, sys |
| 22 | |
| 23 | # Hack to load synchronizer framework |
| 24 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 25 | xos_dir=os.path.join(test_path, "../../..") |
| 26 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 27 | xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos") |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 28 | services_dir = os.path.join(xos_dir, "../../xos_services") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 29 | sys.path.append(xos_dir) |
| 30 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 31 | # END of hack to load synchronizer framework |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 32 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 33 | # Generate model from xproto |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 34 | def get_models_fn(service_name, xproto_name): |
| 35 | name = os.path.join(service_name, "xos", xproto_name) |
| 36 | if os.path.exists(os.path.join(services_dir, name)): |
| 37 | return name |
| 38 | else: |
| 39 | name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name) |
| 40 | if os.path.exists(os.path.join(services_dir, name)): |
| 41 | return name |
| 42 | raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name)) |
| 43 | # END generate model from xproto |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 44 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 45 | def match_json(desired, req): |
| 46 | if desired!=req.json(): |
| 47 | raise Exception("Got request %s, but body is not matching" % req.url) |
| 48 | return False |
| 49 | return True |
| 50 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 51 | class TestSyncOLTDevice(unittest.TestCase): |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 52 | def setUp(self): |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 53 | global DeferredException |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 54 | self.sys_path_save = sys.path |
| 55 | sys.path.append(xos_dir) |
| 56 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 57 | |
| 58 | # Setting up the config module |
| 59 | from xosconfig import Config |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 60 | config = os.path.join(test_path, "../test_config.yaml") |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 61 | Config.clear() |
| 62 | Config.init(config, "synchronizer-config-schema.yaml") |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 63 | # END setting up the config module |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 64 | |
| 65 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 66 | # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")]) |
| 67 | |
| 68 | # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order |
| 69 | # and apparently it is not overriding the generated model accessor |
| 70 | build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"), |
| 71 | get_models_fn("vsg", "vsg.xproto"), |
| 72 | get_models_fn("../profiles/rcord", "rcord.xproto")]) |
| 73 | |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 74 | import synchronizers.new_base.modelaccessor |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 75 | from sync_olt_device import SyncOLTDevice, DeferredException |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 76 | self.sync_step = SyncOLTDevice |
| 77 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 78 | pon_port = Mock() |
| 79 | pon_port.port_id = "00ff00" |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 80 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 81 | # Create a mock OLTDevice |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 82 | o = Mock() |
| 83 | o.volt_service.voltha_url = "voltha_url" |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 84 | o.volt_service.voltha_port = 1234 |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 85 | o.volt_service.voltha_user = "voltha_user" |
| 86 | o.volt_service.voltha_pass = "voltha_pass" |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 87 | |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame] | 88 | o.volt_service.onos_voltha_port = 4321 |
| 89 | o.volt_service.onos_voltha_url = "onos" |
| 90 | o.volt_service.onos_voltha_user = "karaf" |
| 91 | o.volt_service.onos_voltha_pass = "karaf" |
| 92 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 93 | o.device_type = "ponsim_olt" |
| 94 | o.host = "172.17.0.1" |
| 95 | o.port = "50060" |
Scott Baker | 0bbbfd1 | 2018-12-11 07:01:06 +0000 | [diff] [blame] | 96 | o.uplink = "129" |
Luca Prete | 244e6ec | 2018-07-02 14:30:24 +0200 | [diff] [blame] | 97 | o.driver = "voltha" |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame] | 98 | o.name = "Test Device" |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame^] | 99 | o.admin_state = "ENABLED" |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 100 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 101 | # feedback state |
| 102 | o.device_id = None |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 103 | o.oper_status = None |
| 104 | o.of_id = None |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 105 | o.id = 1 |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 106 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 107 | o.tologdict.return_value = {'name': "Mock VOLTServiceInstance"} |
| 108 | |
| 109 | o.save.return_value = "Saved" |
| 110 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 111 | o.pon_ports.all.return_value = [pon_port] |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 112 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 113 | self.o = o |
| 114 | |
Matteo Scandolo | d6fce51 | 2018-10-16 10:35:29 -0700 | [diff] [blame] | 115 | self.voltha_devices_response = {"id": "123", "serial_number": "foobar"} |
| 116 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 117 | def tearDown(self): |
| 118 | self.o = None |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 119 | sys.path = self.sys_path_save |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 120 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 121 | @requests_mock.Mocker() |
| 122 | def test_get_of_id_from_device(self, m): |
| 123 | logical_devices = { |
| 124 | "items": [ |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 125 | {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"}, |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 126 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 127 | ] |
| 128 | } |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 129 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 130 | self.o.device_id = "123" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 131 | self.o = self.sync_step.get_ids_from_logical_device(self.o) |
| 132 | self.assertEqual(self.o.of_id, "0001000ce2314000") |
| 133 | self.assertEqual(self.o.dp_id, "of:0000000ce2314000") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 134 | |
| 135 | with self.assertRaises(Exception) as e: |
| 136 | self.o.device_id = "idonotexist" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 137 | self.sync_step.get_ids_from_logical_device(self.o) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 138 | self.assertEqual(e.exception.message, "Can't find a logical_device for OLT device id: idonotexist") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 139 | |
| 140 | @requests_mock.Mocker() |
| 141 | def test_sync_record_fail_add(self, m): |
| 142 | """ |
| 143 | Should print an error if we can't add the device in VOLTHA |
| 144 | """ |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 145 | m.post("http://voltha_url:1234/api/v1/devices", status_code=500, text="MockError") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 146 | |
| 147 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 148 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 149 | self.assertEqual(e.exception.message, "Failed to add OLT device: MockError") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 150 | |
| 151 | @requests_mock.Mocker() |
| 152 | def test_sync_record_fail_no_id(self, m): |
| 153 | """ |
| 154 | Should print an error if VOLTHA does not return the device id |
| 155 | """ |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 156 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json={"id": ""}) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 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 | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 160 | self.assertEqual(e.exception.message, "VOLTHA Device Id is empty. This probably means that the OLT device is already provisioned in VOLTHA") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 161 | |
| 162 | @requests_mock.Mocker() |
| 163 | def test_sync_record_fail_enable(self, m): |
| 164 | """ |
| 165 | Should print an error if device.enable fails |
| 166 | """ |
Matteo Scandolo | d6fce51 | 2018-10-16 10:35:29 -0700 | [diff] [blame] | 167 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 168 | m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=500, text="EnableError") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 169 | |
| 170 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 171 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 172 | |
| 173 | self.assertEqual(e.exception.message, "Failed to enable OLT device: EnableError") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 174 | |
| 175 | @requests_mock.Mocker() |
| 176 | def test_sync_record_success(self, m): |
| 177 | """ |
| 178 | If device.enable succed should fetch the state, retrieve the of_id and push it to ONOS |
| 179 | """ |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 180 | |
| 181 | expected_conf = { |
| 182 | "type": self.o.device_type, |
| 183 | "host_and_port": "%s:%s" % (self.o.host, self.o.port) |
| 184 | } |
| 185 | |
Matteo Scandolo | d6fce51 | 2018-10-16 10:35:29 -0700 | [diff] [blame] | 186 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response, additional_matcher=functools.partial(match_json, expected_conf)) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 187 | m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200) |
Matteo Scandolo | 4587665 | 2018-10-16 16:03:17 -0700 | [diff] [blame] | 188 | m.get("http://voltha_url:1234/api/v1/devices/123", json={"oper_status": "ACTIVE", "admin_state": "ENABLED", "serial_number": "foobar"}) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 189 | logical_devices = { |
| 190 | "items": [ |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 191 | {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"}, |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 192 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 193 | ] |
| 194 | } |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 195 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 196 | |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame] | 197 | onos_expected_conf = { |
| 198 | "devices": { |
| 199 | "of:0000000ce2314000": { |
| 200 | "basic": { |
| 201 | "name": self.o.name |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=onos_expected_conf, |
| 207 | additional_matcher=functools.partial(match_json, onos_expected_conf)) |
| 208 | |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 209 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 210 | self.assertEqual(self.o.admin_state, "ENABLED") |
| 211 | self.assertEqual(self.o.oper_status, "ACTIVE") |
Matteo Scandolo | 4587665 | 2018-10-16 16:03:17 -0700 | [diff] [blame] | 212 | self.assertEqual(self.o.serial_number, "foobar") |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 213 | self.assertEqual(self.o.of_id, "0001000ce2314000") |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame^] | 214 | |
| 215 | # One save during preprovision |
| 216 | # One save during activation to set backend_status to "Waiting for device to activate" |
| 217 | # One save after activation has succeeded |
| 218 | self.assertEqual(self.o.save.call_count, 3) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 219 | |
| 220 | @requests_mock.Mocker() |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 221 | def test_sync_record_success_mac_address(self, m): |
| 222 | """ |
| 223 | A device should be pre-provisioned via mac_address, the the process is the same |
| 224 | """ |
| 225 | |
| 226 | del self.o.host |
| 227 | del self.o.port |
| 228 | self.o.mac_address = "00:0c:e2:31:40:00" |
| 229 | |
| 230 | expected_conf = { |
| 231 | "type": self.o.device_type, |
| 232 | "mac_address": self.o.mac_address |
| 233 | } |
| 234 | |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame] | 235 | onos_expected_conf = { |
| 236 | "devices": { |
| 237 | "of:0000000ce2314000": { |
| 238 | "basic": { |
| 239 | "name": self.o.name |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=onos_expected_conf, |
| 245 | additional_matcher=functools.partial(match_json, onos_expected_conf)) |
| 246 | |
Matteo Scandolo | d6fce51 | 2018-10-16 10:35:29 -0700 | [diff] [blame] | 247 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response, |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 248 | additional_matcher=functools.partial(match_json, expected_conf)) |
| 249 | m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200) |
Matteo Scandolo | 4587665 | 2018-10-16 16:03:17 -0700 | [diff] [blame] | 250 | m.get("http://voltha_url:1234/api/v1/devices/123", json={"oper_status": "ACTIVE", "admin_state": "ENABLED", "serial_number": "foobar"}) |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 251 | logical_devices = { |
| 252 | "items": [ |
| 253 | {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"}, |
| 254 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 255 | ] |
| 256 | } |
| 257 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices) |
| 258 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 259 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 260 | self.assertEqual(self.o.admin_state, "ENABLED") |
| 261 | self.assertEqual(self.o.oper_status, "ACTIVE") |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 262 | self.assertEqual(self.o.of_id, "0001000ce2314000") |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame^] | 263 | |
| 264 | # One save during preprovision |
| 265 | # One save during activation to set backend_status to "Waiting for device to activate" |
| 266 | # One save after activation has succeeded |
| 267 | self.assertEqual(self.o.save.call_count, 3) |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 268 | |
| 269 | @requests_mock.Mocker() |
| 270 | def test_sync_record_enable_timeout(self, m): |
| 271 | """ |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame^] | 272 | If device activation fails we need to tell the user. |
| 273 | |
| 274 | OLT will be preprovisioned. |
| 275 | OLT will return "ERROR" for oper_status during activate and will eventually exceed retries.s |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 276 | """ |
| 277 | |
| 278 | expected_conf = { |
| 279 | "type": self.o.device_type, |
| 280 | "host_and_port": "%s:%s" % (self.o.host, self.o.port) |
| 281 | } |
| 282 | |
Matteo Scandolo | d6fce51 | 2018-10-16 10:35:29 -0700 | [diff] [blame] | 283 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response, |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 284 | additional_matcher=functools.partial(match_json, expected_conf)) |
| 285 | m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200) |
| 286 | m.get("http://voltha_url:1234/api/v1/devices/123", [ |
Matteo Scandolo | 4587665 | 2018-10-16 16:03:17 -0700 | [diff] [blame] | 287 | {"json": {"oper_status": "ACTIVATING", "admin_state": "ENABLED", "serial_number": "foobar"}, "status_code": 200}, |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame^] | 288 | {"json": {"oper_status": "ERROR", "admin_state": "ENABLED", "serial_number": "foobar"}, "status_code": 200} |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 289 | ]) |
| 290 | |
| 291 | logical_devices = { |
| 292 | "items": [ |
| 293 | {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"}, |
| 294 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 295 | ] |
| 296 | } |
| 297 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices) |
| 298 | |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 299 | with self.assertRaises(Exception) as e: |
| 300 | self.sync_step().sync_record(self.o) |
| 301 | |
| 302 | self.assertEqual(e.exception.message, "It was not possible to activate OLTDevice with id 1") |
| 303 | self.assertEqual(self.o.oper_status, "ERROR") |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame^] | 304 | self.assertEqual(self.o.admin_state, "ENABLED") |
| 305 | self.assertEqual(self.o.device_id, "123") |
| 306 | self.assertEqual(self.o.serial_number, "foobar") |
| 307 | |
| 308 | # One save from preprovision to set device_id, serial_number |
| 309 | # One save from activate to set backend_status to "Waiting for device to be activated" |
| 310 | self.assertEqual(self.o.save.call_count, 2) |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 311 | |
| 312 | @requests_mock.Mocker() |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 313 | def test_sync_record_already_existing_in_voltha(self, m): |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame^] | 314 | """ |
| 315 | If device.admin_state == "ENABLED" and oper_status == "ACTIVE", then the OLT should not be reactivated. |
| 316 | """ |
| 317 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 318 | # mock device feedback state |
| 319 | self.o.device_id = "123" |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 320 | self.o.admin_state = "ENABLED" |
| 321 | self.o.oper_status = "ACTIVE" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 322 | self.o.dp_id = "of:0000000ce2314000" |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 323 | self.o.of_id = "0001000ce2314000" |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 324 | |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame] | 325 | expected_conf = { |
| 326 | "devices": { |
| 327 | self.o.dp_id: { |
| 328 | "basic": { |
| 329 | "name": self.o.name |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=expected_conf, |
| 335 | additional_matcher=functools.partial(match_json, expected_conf)) |
| 336 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 337 | self.sync_step().sync_record(self.o) |
| 338 | self.o.save.assert_not_called() |
| 339 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 340 | @requests_mock.Mocker() |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame^] | 341 | def test_sync_record_deactivate(self, m): |
| 342 | """ |
| 343 | If device.admin_state == "DISABLED" and oper_status == "ACTIVE", then OLT should be deactivated. |
| 344 | """ |
| 345 | |
| 346 | expected_conf = { |
| 347 | "type": self.o.device_type, |
| 348 | "host_and_port": "%s:%s" % (self.o.host, self.o.port) |
| 349 | } |
| 350 | |
| 351 | # Make it look like we have an active OLT that we are deactivating. |
| 352 | self.o.admin_state = "DISABLED" |
| 353 | self.o.oper_status = "ACTIVE" |
| 354 | self.o.serial_number = "foobar" |
| 355 | self.o.device_id = "123" |
| 356 | self.o.of_id = "0001000ce2314000" |
| 357 | |
| 358 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response, additional_matcher=functools.partial(match_json, expected_conf)) |
| 359 | m.post("http://voltha_url:1234/api/v1/devices/123/disable", status_code=200) |
| 360 | |
| 361 | self.sync_step().sync_record(self.o) |
| 362 | |
| 363 | # No saves as state has not changed (will eventually be saved by synchronizer framework to update backend_status) |
| 364 | self.assertEqual(self.o.save.call_count, 0) |
| 365 | |
| 366 | # Make sure disable was called |
| 367 | urls = [x.url for x in m.request_history] |
| 368 | self.assertIn("http://voltha_url:1234/api/v1/devices/123/disable", urls) |
| 369 | |
| 370 | @requests_mock.Mocker() |
| 371 | def test_sync_record_deactivate_already_inactive(self, m): |
| 372 | """ |
| 373 | If device.admin_state == "DISABLED" and device.oper_status == "UNKNOWN", then the device is already deactivated |
| 374 | and VOLTHA should not be called. |
| 375 | """ |
| 376 | |
| 377 | expected_conf = { |
| 378 | "type": self.o.device_type, |
| 379 | "host_and_port": "%s:%s" % (self.o.host, self.o.port) |
| 380 | } |
| 381 | |
| 382 | # Make it look like we have an active OLT that we are deactivating. |
| 383 | self.o.admin_state = "DISABLED" |
| 384 | self.o.oper_status = "UNKNOWN" |
| 385 | self.o.serial_number = "foobar" |
| 386 | self.o.device_id = "123" |
| 387 | self.o.of_id = "0001000ce2314000" |
| 388 | |
| 389 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response, additional_matcher=functools.partial(match_json, expected_conf)) |
| 390 | |
| 391 | self.sync_step().sync_record(self.o) |
| 392 | |
| 393 | # No saves as state has not changed (will eventually be saved by synchronizer framework to update backend_status) |
| 394 | self.assertEqual(self.o.save.call_count, 0) |
| 395 | |
| 396 | @requests_mock.Mocker() |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 397 | def test_delete_record(self, m): |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 398 | self.o.of_id = "0001000ce2314000" |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 399 | self.o.device_id = "123" |
| 400 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 401 | m.post("http://voltha_url:1234/api/v1/devices/123/disable", status_code=200) |
| 402 | m.delete("http://voltha_url:1234/api/v1/devices/123/delete", status_code=200) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 403 | |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 404 | self.sync_step().delete_record(self.o) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 405 | |
Matteo Scandolo | 563891c | 2018-08-21 11:56:32 -0700 | [diff] [blame] | 406 | self.assertEqual(m.call_count, 2) |
| 407 | |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 408 | @patch('requests.post') |
| 409 | def test_delete_record_connectionerror(self, m): |
| 410 | self.o.of_id = "0001000ce2314000" |
| 411 | self.o.device_id = "123" |
| 412 | |
| 413 | m.side_effect = ConnectionError() |
| 414 | |
| 415 | self.sync_step().delete_record(self.o) |
| 416 | |
| 417 | # No exception thrown, as ConnectionError will be caught |
| 418 | |
| 419 | |
Matteo Scandolo | 563891c | 2018-08-21 11:56:32 -0700 | [diff] [blame] | 420 | @requests_mock.Mocker() |
| 421 | def test_delete_unsynced_record(self, m): |
| 422 | |
| 423 | self.sync_step().delete_record(self.o) |
| 424 | |
| 425 | self.assertEqual(m.call_count, 0) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 426 | |
| 427 | if __name__ == "__main__": |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 428 | unittest.main() |