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