blob: 8d01579f93ff5144676adca352de250c4f34313b [file] [log] [blame]
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -08001# 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 Baker22b46c52018-11-15 15:15:29 -080015from requests import ConnectionError
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080016import unittest
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070017import functools
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080018from mock import patch, call, Mock, PropertyMock
19import requests_mock
20
21import os, sys
22
23# Hack to load synchronizer framework
24test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
25xos_dir=os.path.join(test_path, "../../..")
26if not os.path.exists(os.path.join(test_path, "new_base")):
27 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070028 services_dir = os.path.join(xos_dir, "../../xos_services")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080029sys.path.append(xos_dir)
30sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
Luca Preteca974c82018-05-01 18:06:16 -070031# END of hack to load synchronizer framework
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080032
Luca Preteca974c82018-05-01 18:06:16 -070033# Generate model from xproto
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070034def 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 Scandolo4a8b4d62018-03-06 17:18:46 -080044
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070045def 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 Scandolo4a8b4d62018-03-06 17:18:46 -080051class TestSyncOLTDevice(unittest.TestCase):
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080052 def setUp(self):
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070053 global DeferredException
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070054 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 Scandolof7ebb112018-09-18 16:17:22 -070060 config = os.path.join(test_path, "../test_config.yaml")
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070061 Config.clear()
62 Config.init(config, "synchronizer-config-schema.yaml")
Luca Preteca974c82018-05-01 18:06:16 -070063 # END setting up the config module
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070064
65 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
Matteo Scandolo19466a02018-05-16 17:43:39 -070066 # 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 Scandoloce27e9c2018-04-06 10:06:53 -070074 import synchronizers.new_base.modelaccessor
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070075 from sync_olt_device import SyncOLTDevice, DeferredException
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070076 self.sync_step = SyncOLTDevice
77
Matteo Scandolob8621cd2018-04-04 17:12:37 -070078 pon_port = Mock()
79 pon_port.port_id = "00ff00"
Matteo Scandolob8621cd2018-04-04 17:12:37 -070080
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070081 # Create a mock OLTDevice
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080082 o = Mock()
83 o.volt_service.voltha_url = "voltha_url"
Luca Preteca974c82018-05-01 18:06:16 -070084 o.volt_service.voltha_port = 1234
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080085 o.volt_service.voltha_user = "voltha_user"
86 o.volt_service.voltha_pass = "voltha_pass"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080087
Matteo Scandoloa79395f2018-10-08 13:34:49 -070088 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 Scandolo4a8b4d62018-03-06 17:18:46 -080093 o.device_type = "ponsim_olt"
94 o.host = "172.17.0.1"
95 o.port = "50060"
Scott Baker0bbbfd12018-12-11 07:01:06 +000096 o.uplink = "129"
Luca Prete244e6ec2018-07-02 14:30:24 +020097 o.driver = "voltha"
Matteo Scandoloa79395f2018-10-08 13:34:49 -070098 o.name = "Test Device"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080099
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700100 # feedback state
101 o.device_id = None
102 o.admin_state = None
103 o.oper_status = None
104 o.of_id = None
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700105 o.id = 1
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700106
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800107 o.tologdict.return_value = {'name': "Mock VOLTServiceInstance"}
108
109 o.save.return_value = "Saved"
110
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700111 o.pon_ports.all.return_value = [pon_port]
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700112
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800113 self.o = o
114
Matteo Scandolod6fce512018-10-16 10:35:29 -0700115 self.voltha_devices_response = {"id": "123", "serial_number": "foobar"}
116
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800117 def tearDown(self):
118 self.o = None
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700119 sys.path = self.sys_path_save
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800120
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800121 @requests_mock.Mocker()
122 def test_get_of_id_from_device(self, m):
123 logical_devices = {
124 "items": [
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700125 {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800126 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
127 ]
128 }
Luca Preteca974c82018-05-01 18:06:16 -0700129 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800130 self.o.device_id = "123"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700131 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 Scandolo4a8b4d62018-03-06 17:18:46 -0800134
135 with self.assertRaises(Exception) as e:
136 self.o.device_id = "idonotexist"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700137 self.sync_step.get_ids_from_logical_device(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700138 self.assertEqual(e.exception.message, "Can't find a logical_device for OLT device id: idonotexist")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800139
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 Preteca974c82018-05-01 18:06:16 -0700145 m.post("http://voltha_url:1234/api/v1/devices", status_code=500, text="MockError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800146
147 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700148 self.sync_step().sync_record(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700149 self.assertEqual(e.exception.message, "Failed to add OLT device: MockError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800150
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 Preteca974c82018-05-01 18:06:16 -0700156 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json={"id": ""})
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800157
158 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700159 self.sync_step().sync_record(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700160 self.assertEqual(e.exception.message, "VOLTHA Device Id is empty. This probably means that the OLT device is already provisioned in VOLTHA")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800161
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 Scandolod6fce512018-10-16 10:35:29 -0700167 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response)
Luca Preteca974c82018-05-01 18:06:16 -0700168 m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=500, text="EnableError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800169
170 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700171 self.sync_step().sync_record(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700172
173 self.assertEqual(e.exception.message, "Failed to enable OLT device: EnableError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800174
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 Scandolo2ed64b92018-06-18 10:32:56 -0700180
181 expected_conf = {
182 "type": self.o.device_type,
183 "host_and_port": "%s:%s" % (self.o.host, self.o.port)
184 }
185
Matteo Scandolod6fce512018-10-16 10:35:29 -0700186 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 Preteca974c82018-05-01 18:06:16 -0700187 m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200)
Matteo Scandolo45876652018-10-16 16:03:17 -0700188 m.get("http://voltha_url:1234/api/v1/devices/123", json={"oper_status": "ACTIVE", "admin_state": "ENABLED", "serial_number": "foobar"})
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800189 logical_devices = {
190 "items": [
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700191 {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800192 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
193 ]
194 }
Luca Preteca974c82018-05-01 18:06:16 -0700195 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800196
Matteo Scandoloa79395f2018-10-08 13:34:49 -0700197 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 Scandoloce27e9c2018-04-06 10:06:53 -0700209 self.sync_step().sync_record(self.o)
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700210 self.assertEqual(self.o.admin_state, "ENABLED")
211 self.assertEqual(self.o.oper_status, "ACTIVE")
Matteo Scandolo45876652018-10-16 16:03:17 -0700212 self.assertEqual(self.o.serial_number, "foobar")
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700213 self.assertEqual(self.o.of_id, "0001000ce2314000")
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700214 self.assertEqual(self.o.save.call_count, 2) # we're updating the backend_status when activating and then adding logical device ids
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800215
216 @requests_mock.Mocker()
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700217 def test_sync_record_success_mac_address(self, m):
218 """
219 A device should be pre-provisioned via mac_address, the the process is the same
220 """
221
222 del self.o.host
223 del self.o.port
224 self.o.mac_address = "00:0c:e2:31:40:00"
225
226 expected_conf = {
227 "type": self.o.device_type,
228 "mac_address": self.o.mac_address
229 }
230
Matteo Scandoloa79395f2018-10-08 13:34:49 -0700231 onos_expected_conf = {
232 "devices": {
233 "of:0000000ce2314000": {
234 "basic": {
235 "name": self.o.name
236 }
237 }
238 }
239 }
240 m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=onos_expected_conf,
241 additional_matcher=functools.partial(match_json, onos_expected_conf))
242
Matteo Scandolod6fce512018-10-16 10:35:29 -0700243 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response,
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700244 additional_matcher=functools.partial(match_json, expected_conf))
245 m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200)
Matteo Scandolo45876652018-10-16 16:03:17 -0700246 m.get("http://voltha_url:1234/api/v1/devices/123", json={"oper_status": "ACTIVE", "admin_state": "ENABLED", "serial_number": "foobar"})
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700247 logical_devices = {
248 "items": [
249 {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
250 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
251 ]
252 }
253 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
254
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700255 self.sync_step().sync_record(self.o)
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700256 self.assertEqual(self.o.admin_state, "ENABLED")
257 self.assertEqual(self.o.oper_status, "ACTIVE")
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700258 self.assertEqual(self.o.of_id, "0001000ce2314000")
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700259 self.assertEqual(self.o.save.call_count, 2)
260
261 @requests_mock.Mocker()
262 def test_sync_record_enable_timeout(self, m):
263 """
264 If device.enable fails we need to tell the suer
265 """
266
267 expected_conf = {
268 "type": self.o.device_type,
269 "host_and_port": "%s:%s" % (self.o.host, self.o.port)
270 }
271
Matteo Scandolod6fce512018-10-16 10:35:29 -0700272 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response,
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700273 additional_matcher=functools.partial(match_json, expected_conf))
274 m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200)
275 m.get("http://voltha_url:1234/api/v1/devices/123", [
Matteo Scandolo45876652018-10-16 16:03:17 -0700276 {"json": {"oper_status": "ACTIVATING", "admin_state": "ENABLED", "serial_number": "foobar"}, "status_code": 200},
277 {"json": {"oper_status": "ERROR", "admin_state": "FAILED", "serial_number": "foobar"}, "status_code": 200}
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700278 ])
279
280 logical_devices = {
281 "items": [
282 {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
283 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
284 ]
285 }
286 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
287
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700288 with self.assertRaises(Exception) as e:
289 self.sync_step().sync_record(self.o)
290
291 self.assertEqual(e.exception.message, "It was not possible to activate OLTDevice with id 1")
292 self.assertEqual(self.o.oper_status, "ERROR")
293 self.assertEqual(self.o.admin_state, "FAILED")
294 self.assertEqual(self.o.save.call_count, 1)
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700295
296 @requests_mock.Mocker()
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700297 def test_sync_record_already_existing_in_voltha(self, m):
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700298 # mock device feedback state
299 self.o.device_id = "123"
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700300 self.o.admin_state = "ENABLED"
301 self.o.oper_status = "ACTIVE"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700302 self.o.dp_id = "of:0000000ce2314000"
Matteo Scandolo2c144932018-05-04 14:06:24 -0700303 self.o.of_id = "0001000ce2314000"
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700304
Matteo Scandoloa79395f2018-10-08 13:34:49 -0700305 expected_conf = {
306 "devices": {
307 self.o.dp_id: {
308 "basic": {
309 "name": self.o.name
310 }
311 }
312 }
313 }
314 m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=expected_conf,
315 additional_matcher=functools.partial(match_json, expected_conf))
316
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700317 self.sync_step().sync_record(self.o)
318 self.o.save.assert_not_called()
319
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700320 @requests_mock.Mocker()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800321 def test_delete_record(self, m):
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700322 self.o.of_id = "0001000ce2314000"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800323 self.o.device_id = "123"
324
Luca Preteca974c82018-05-01 18:06:16 -0700325 m.post("http://voltha_url:1234/api/v1/devices/123/disable", status_code=200)
326 m.delete("http://voltha_url:1234/api/v1/devices/123/delete", status_code=200)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800327
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700328 self.sync_step().delete_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800329
Matteo Scandolo563891c2018-08-21 11:56:32 -0700330 self.assertEqual(m.call_count, 2)
331
Scott Baker22b46c52018-11-15 15:15:29 -0800332 @patch('requests.post')
333 def test_delete_record_connectionerror(self, m):
334 self.o.of_id = "0001000ce2314000"
335 self.o.device_id = "123"
336
337 m.side_effect = ConnectionError()
338
339 self.sync_step().delete_record(self.o)
340
341 # No exception thrown, as ConnectionError will be caught
342
343
Matteo Scandolo563891c2018-08-21 11:56:32 -0700344 @requests_mock.Mocker()
345 def test_delete_unsynced_record(self, m):
346
347 self.sync_step().delete_record(self.o)
348
349 self.assertEqual(m.call_count, 0)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800350
351if __name__ == "__main__":
Luca Preteca974c82018-05-01 18:06:16 -0700352 unittest.main()