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 |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 16 | import functools |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 17 | from mock import patch, call, Mock, PropertyMock |
| 18 | import requests_mock |
| 19 | |
| 20 | import os, sys |
| 21 | |
| 22 | # Hack to load synchronizer framework |
| 23 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 24 | xos_dir=os.path.join(test_path, "../../..") |
| 25 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 26 | xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos") |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 27 | services_dir = os.path.join(xos_dir, "../../xos_services") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 28 | sys.path.append(xos_dir) |
| 29 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 30 | # END of hack to load synchronizer framework |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 31 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 32 | # Generate model from xproto |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 33 | def get_models_fn(service_name, xproto_name): |
| 34 | name = os.path.join(service_name, "xos", xproto_name) |
| 35 | if os.path.exists(os.path.join(services_dir, name)): |
| 36 | return name |
| 37 | else: |
| 38 | name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name) |
| 39 | if os.path.exists(os.path.join(services_dir, name)): |
| 40 | return name |
| 41 | raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name)) |
| 42 | # END generate model from xproto |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 43 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 44 | def match_onos_req(req): |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 45 | request = req.json()['devices'] |
| 46 | if not request['of:0000000ce2314000']: |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 47 | return False |
| 48 | else: |
Luca Prete | 244e6ec | 2018-07-02 14:30:24 +0200 | [diff] [blame] | 49 | if not request['of:0000000ce2314000']['basic']['driver'] == 'voltha': |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 50 | return False |
Matteo Scandolo | 8091294 | 2018-07-25 20:51:30 -0700 | [diff] [blame] | 51 | if not request['of:0000000ce2314000']['accessDevice']['vlan'] == 1 or not request['of:0000000ce2314000']['accessDevice']['uplink'] == "129": |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 52 | return False |
| 53 | return True |
| 54 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 55 | def match_json(desired, req): |
| 56 | if desired!=req.json(): |
| 57 | raise Exception("Got request %s, but body is not matching" % req.url) |
| 58 | return False |
| 59 | return True |
| 60 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 61 | class TestSyncOLTDevice(unittest.TestCase): |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 62 | def setUp(self): |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 63 | global DeferredException |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 64 | self.sys_path_save = sys.path |
| 65 | sys.path.append(xos_dir) |
| 66 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 67 | |
| 68 | # Setting up the config module |
| 69 | from xosconfig import Config |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 70 | config = os.path.join(test_path, "../test_config.yaml") |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 71 | Config.clear() |
| 72 | Config.init(config, "synchronizer-config-schema.yaml") |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 73 | # END setting up the config module |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 74 | |
| 75 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 76 | # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")]) |
| 77 | |
| 78 | # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order |
| 79 | # and apparently it is not overriding the generated model accessor |
| 80 | build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"), |
| 81 | get_models_fn("vsg", "vsg.xproto"), |
| 82 | get_models_fn("../profiles/rcord", "rcord.xproto")]) |
| 83 | |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 84 | import synchronizers.new_base.modelaccessor |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 85 | from sync_olt_device import SyncOLTDevice, DeferredException |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 86 | self.sync_step = SyncOLTDevice |
| 87 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 88 | pon_port = Mock() |
| 89 | pon_port.port_id = "00ff00" |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 90 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 91 | # Create a mock OLTDevice |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 92 | o = Mock() |
| 93 | o.volt_service.voltha_url = "voltha_url" |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 94 | o.volt_service.voltha_port = 1234 |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 95 | o.volt_service.voltha_user = "voltha_user" |
| 96 | o.volt_service.voltha_pass = "voltha_pass" |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 97 | |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame^] | 98 | o.volt_service.onos_voltha_port = 4321 |
| 99 | o.volt_service.onos_voltha_url = "onos" |
| 100 | o.volt_service.onos_voltha_user = "karaf" |
| 101 | o.volt_service.onos_voltha_pass = "karaf" |
| 102 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 103 | o.device_type = "ponsim_olt" |
| 104 | o.host = "172.17.0.1" |
| 105 | o.port = "50060" |
| 106 | o.uplink = "129" |
Luca Prete | 244e6ec | 2018-07-02 14:30:24 +0200 | [diff] [blame] | 107 | o.driver = "voltha" |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame^] | 108 | o.name = "Test Device" |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 109 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 110 | # feedback state |
| 111 | o.device_id = None |
| 112 | o.admin_state = None |
| 113 | o.oper_status = None |
| 114 | o.of_id = None |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 115 | o.id = 1 |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 116 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 117 | o.tologdict.return_value = {'name': "Mock VOLTServiceInstance"} |
| 118 | |
| 119 | o.save.return_value = "Saved" |
| 120 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 121 | o.pon_ports.all.return_value = [pon_port] |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 122 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 123 | self.o = o |
| 124 | |
| 125 | def tearDown(self): |
| 126 | self.o = None |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 127 | sys.path = self.sys_path_save |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 128 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 129 | @requests_mock.Mocker() |
| 130 | def test_get_of_id_from_device(self, m): |
| 131 | logical_devices = { |
| 132 | "items": [ |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 133 | {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"}, |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 134 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 135 | ] |
| 136 | } |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 137 | 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] | 138 | self.o.device_id = "123" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 139 | self.o = self.sync_step.get_ids_from_logical_device(self.o) |
| 140 | self.assertEqual(self.o.of_id, "0001000ce2314000") |
| 141 | self.assertEqual(self.o.dp_id, "of:0000000ce2314000") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 142 | |
| 143 | with self.assertRaises(Exception) as e: |
| 144 | self.o.device_id = "idonotexist" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 145 | self.sync_step.get_ids_from_logical_device(self.o) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 146 | 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] | 147 | |
| 148 | @requests_mock.Mocker() |
| 149 | def test_sync_record_fail_add(self, m): |
| 150 | """ |
| 151 | Should print an error if we can't add the device in VOLTHA |
| 152 | """ |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 153 | 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] | 154 | |
| 155 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 156 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 157 | self.assertEqual(e.exception.message, "Failed to add OLT device: MockError") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 158 | |
| 159 | @requests_mock.Mocker() |
| 160 | def test_sync_record_fail_no_id(self, m): |
| 161 | """ |
| 162 | Should print an error if VOLTHA does not return the device id |
| 163 | """ |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 164 | 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] | 165 | |
| 166 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 167 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 168 | 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] | 169 | |
| 170 | @requests_mock.Mocker() |
| 171 | def test_sync_record_fail_enable(self, m): |
| 172 | """ |
| 173 | Should print an error if device.enable fails |
| 174 | """ |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 175 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json={"id": "123"}) |
| 176 | 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] | 177 | |
| 178 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 179 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 180 | |
| 181 | self.assertEqual(e.exception.message, "Failed to enable OLT device: EnableError") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 182 | |
| 183 | @requests_mock.Mocker() |
| 184 | def test_sync_record_success(self, m): |
| 185 | """ |
| 186 | If device.enable succed should fetch the state, retrieve the of_id and push it to ONOS |
| 187 | """ |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 188 | |
| 189 | expected_conf = { |
| 190 | "type": self.o.device_type, |
| 191 | "host_and_port": "%s:%s" % (self.o.host, self.o.port) |
| 192 | } |
| 193 | |
| 194 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json={"id": "123"}, additional_matcher=functools.partial(match_json, expected_conf)) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 195 | m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200) |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 196 | m.get("http://voltha_url:1234/api/v1/devices/123", json={"oper_status": "ACTIVE", "admin_state": "ENABLED"}) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 197 | logical_devices = { |
| 198 | "items": [ |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 199 | {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"}, |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 200 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 201 | ] |
| 202 | } |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 203 | 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] | 204 | |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame^] | 205 | onos_expected_conf = { |
| 206 | "devices": { |
| 207 | "of:0000000ce2314000": { |
| 208 | "basic": { |
| 209 | "name": self.o.name |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=onos_expected_conf, |
| 215 | additional_matcher=functools.partial(match_json, onos_expected_conf)) |
| 216 | |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 217 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 218 | self.assertEqual(self.o.admin_state, "ENABLED") |
| 219 | self.assertEqual(self.o.oper_status, "ACTIVE") |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 220 | self.assertEqual(self.o.of_id, "0001000ce2314000") |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 221 | self.assertEqual(self.o.save.call_count, 2) # we're updating the backend_status when activating and then adding logical device ids |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 222 | |
| 223 | @requests_mock.Mocker() |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 224 | def test_sync_record_success_mac_address(self, m): |
| 225 | """ |
| 226 | A device should be pre-provisioned via mac_address, the the process is the same |
| 227 | """ |
| 228 | |
| 229 | del self.o.host |
| 230 | del self.o.port |
| 231 | self.o.mac_address = "00:0c:e2:31:40:00" |
| 232 | |
| 233 | expected_conf = { |
| 234 | "type": self.o.device_type, |
| 235 | "mac_address": self.o.mac_address |
| 236 | } |
| 237 | |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame^] | 238 | onos_expected_conf = { |
| 239 | "devices": { |
| 240 | "of:0000000ce2314000": { |
| 241 | "basic": { |
| 242 | "name": self.o.name |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=onos_expected_conf, |
| 248 | additional_matcher=functools.partial(match_json, onos_expected_conf)) |
| 249 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 250 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json={"id": "123"}, |
| 251 | additional_matcher=functools.partial(match_json, expected_conf)) |
| 252 | m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200) |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 253 | m.get("http://voltha_url:1234/api/v1/devices/123", json={"oper_status": "ACTIVE", "admin_state": "ENABLED"}) |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 254 | logical_devices = { |
| 255 | "items": [ |
| 256 | {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"}, |
| 257 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 258 | ] |
| 259 | } |
| 260 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices) |
| 261 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 262 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 263 | self.assertEqual(self.o.admin_state, "ENABLED") |
| 264 | self.assertEqual(self.o.oper_status, "ACTIVE") |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 265 | self.assertEqual(self.o.of_id, "0001000ce2314000") |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 266 | self.assertEqual(self.o.save.call_count, 2) |
| 267 | |
| 268 | @requests_mock.Mocker() |
| 269 | def test_sync_record_enable_timeout(self, m): |
| 270 | """ |
| 271 | If device.enable fails we need to tell the suer |
| 272 | """ |
| 273 | |
| 274 | expected_conf = { |
| 275 | "type": self.o.device_type, |
| 276 | "host_and_port": "%s:%s" % (self.o.host, self.o.port) |
| 277 | } |
| 278 | |
| 279 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json={"id": "123"}, |
| 280 | additional_matcher=functools.partial(match_json, expected_conf)) |
| 281 | m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200) |
| 282 | m.get("http://voltha_url:1234/api/v1/devices/123", [ |
| 283 | {"json": {"oper_status": "ACTIVATING", "admin_state": "ENABLED"}, "status_code": 200}, |
| 284 | {"json": {"oper_status": "ERROR", "admin_state": "FAILED"}, "status_code": 200} |
| 285 | ]) |
| 286 | |
| 287 | logical_devices = { |
| 288 | "items": [ |
| 289 | {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"}, |
| 290 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 291 | ] |
| 292 | } |
| 293 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices) |
| 294 | |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 295 | with self.assertRaises(Exception) as e: |
| 296 | self.sync_step().sync_record(self.o) |
| 297 | |
| 298 | self.assertEqual(e.exception.message, "It was not possible to activate OLTDevice with id 1") |
| 299 | self.assertEqual(self.o.oper_status, "ERROR") |
| 300 | self.assertEqual(self.o.admin_state, "FAILED") |
| 301 | self.assertEqual(self.o.save.call_count, 1) |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 302 | |
| 303 | @requests_mock.Mocker() |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 304 | def test_sync_record_already_existing_in_voltha(self, m): |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 305 | # mock device feedback state |
| 306 | self.o.device_id = "123" |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 307 | self.o.admin_state = "ENABLED" |
| 308 | self.o.oper_status = "ACTIVE" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 309 | self.o.dp_id = "of:0000000ce2314000" |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 310 | self.o.of_id = "0001000ce2314000" |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 311 | |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame^] | 312 | expected_conf = { |
| 313 | "devices": { |
| 314 | self.o.dp_id: { |
| 315 | "basic": { |
| 316 | "name": self.o.name |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=expected_conf, |
| 322 | additional_matcher=functools.partial(match_json, expected_conf)) |
| 323 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 324 | self.sync_step().sync_record(self.o) |
| 325 | self.o.save.assert_not_called() |
| 326 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 327 | @requests_mock.Mocker() |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 328 | def test_delete_record(self, m): |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 329 | self.o.of_id = "0001000ce2314000" |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 330 | self.o.device_id = "123" |
| 331 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 332 | m.post("http://voltha_url:1234/api/v1/devices/123/disable", status_code=200) |
| 333 | 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] | 334 | |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 335 | self.sync_step().delete_record(self.o) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 336 | |
Matteo Scandolo | 563891c | 2018-08-21 11:56:32 -0700 | [diff] [blame] | 337 | self.assertEqual(m.call_count, 2) |
| 338 | |
| 339 | @requests_mock.Mocker() |
| 340 | def test_delete_unsynced_record(self, m): |
| 341 | |
| 342 | self.sync_step().delete_record(self.o) |
| 343 | |
| 344 | self.assertEqual(m.call_count, 0) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 345 | |
| 346 | if __name__ == "__main__": |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 347 | unittest.main() |