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