blob: 18ed99b068e58a61f9f04bbc995c8288dade0f3b [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
15import unittest
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070016import functools
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080017from mock import patch, call, Mock, PropertyMock
18import requests_mock
19
20import os, sys
21
22# Hack to load synchronizer framework
23test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
24xos_dir=os.path.join(test_path, "../../..")
25if not os.path.exists(os.path.join(test_path, "new_base")):
26 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070027 services_dir = os.path.join(xos_dir, "../../xos_services")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080028sys.path.append(xos_dir)
29sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
Luca Preteca974c82018-05-01 18:06:16 -070030# END of hack to load synchronizer framework
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080031
Luca Preteca974c82018-05-01 18:06:16 -070032# Generate model from xproto
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070033def 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 Scandolo4a8b4d62018-03-06 17:18:46 -080043
Matteo Scandolob8621cd2018-04-04 17:12:37 -070044def match_onos_req(req):
Luca Preteca974c82018-05-01 18:06:16 -070045 request = req.json()['devices']
46 if not request['of:0000000ce2314000']:
Matteo Scandolob8621cd2018-04-04 17:12:37 -070047 return False
48 else:
Luca Prete244e6ec2018-07-02 14:30:24 +020049 if not request['of:0000000ce2314000']['basic']['driver'] == 'voltha':
Matteo Scandolob8621cd2018-04-04 17:12:37 -070050 return False
Matteo Scandolo80912942018-07-25 20:51:30 -070051 if not request['of:0000000ce2314000']['accessDevice']['vlan'] == 1 or not request['of:0000000ce2314000']['accessDevice']['uplink'] == "129":
Matteo Scandolob8621cd2018-04-04 17:12:37 -070052 return False
53 return True
54
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070055def 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 Scandolo4a8b4d62018-03-06 17:18:46 -080061class TestSyncOLTDevice(unittest.TestCase):
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080062 def setUp(self):
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070063 global DeferredException
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070064 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 Scandolof7ebb112018-09-18 16:17:22 -070070 config = os.path.join(test_path, "../test_config.yaml")
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070071 Config.clear()
72 Config.init(config, "synchronizer-config-schema.yaml")
Luca Preteca974c82018-05-01 18:06:16 -070073 # END setting up the config module
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070074
75 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
Matteo Scandolo19466a02018-05-16 17:43:39 -070076 # 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 Scandoloce27e9c2018-04-06 10:06:53 -070084 import synchronizers.new_base.modelaccessor
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070085 from sync_olt_device import SyncOLTDevice, DeferredException
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070086 self.sync_step = SyncOLTDevice
87
Matteo Scandolob8621cd2018-04-04 17:12:37 -070088 pon_port = Mock()
89 pon_port.port_id = "00ff00"
Matteo Scandolob8621cd2018-04-04 17:12:37 -070090
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070091 # Create a mock OLTDevice
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080092 o = Mock()
93 o.volt_service.voltha_url = "voltha_url"
Luca Preteca974c82018-05-01 18:06:16 -070094 o.volt_service.voltha_port = 1234
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080095 o.volt_service.voltha_user = "voltha_user"
96 o.volt_service.voltha_pass = "voltha_pass"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080097
Matteo Scandoloa79395f2018-10-08 13:34:49 -070098 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 Scandolo4a8b4d62018-03-06 17:18:46 -0800103 o.device_type = "ponsim_olt"
104 o.host = "172.17.0.1"
105 o.port = "50060"
106 o.uplink = "129"
Luca Prete244e6ec2018-07-02 14:30:24 +0200107 o.driver = "voltha"
Matteo Scandoloa79395f2018-10-08 13:34:49 -0700108 o.name = "Test Device"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800109
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700110 # feedback state
111 o.device_id = None
112 o.admin_state = None
113 o.oper_status = None
114 o.of_id = None
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700115 o.id = 1
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700116
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800117 o.tologdict.return_value = {'name': "Mock VOLTServiceInstance"}
118
119 o.save.return_value = "Saved"
120
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700121 o.pon_ports.all.return_value = [pon_port]
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700122
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800123 self.o = o
124
Matteo Scandolod6fce512018-10-16 10:35:29 -0700125 self.voltha_devices_response = {"id": "123", "serial_number": "foobar"}
126
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800127 def tearDown(self):
128 self.o = None
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700129 sys.path = self.sys_path_save
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800130
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800131 @requests_mock.Mocker()
132 def test_get_of_id_from_device(self, m):
133 logical_devices = {
134 "items": [
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700135 {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800136 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
137 ]
138 }
Luca Preteca974c82018-05-01 18:06:16 -0700139 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800140 self.o.device_id = "123"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700141 self.o = self.sync_step.get_ids_from_logical_device(self.o)
142 self.assertEqual(self.o.of_id, "0001000ce2314000")
143 self.assertEqual(self.o.dp_id, "of:0000000ce2314000")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800144
145 with self.assertRaises(Exception) as e:
146 self.o.device_id = "idonotexist"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700147 self.sync_step.get_ids_from_logical_device(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700148 self.assertEqual(e.exception.message, "Can't find a logical_device for OLT device id: idonotexist")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800149
150 @requests_mock.Mocker()
151 def test_sync_record_fail_add(self, m):
152 """
153 Should print an error if we can't add the device in VOLTHA
154 """
Luca Preteca974c82018-05-01 18:06:16 -0700155 m.post("http://voltha_url:1234/api/v1/devices", status_code=500, text="MockError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800156
157 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700158 self.sync_step().sync_record(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700159 self.assertEqual(e.exception.message, "Failed to add OLT device: MockError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800160
161 @requests_mock.Mocker()
162 def test_sync_record_fail_no_id(self, m):
163 """
164 Should print an error if VOLTHA does not return the device id
165 """
Luca Preteca974c82018-05-01 18:06:16 -0700166 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json={"id": ""})
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800167
168 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700169 self.sync_step().sync_record(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700170 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 -0800171
172 @requests_mock.Mocker()
173 def test_sync_record_fail_enable(self, m):
174 """
175 Should print an error if device.enable fails
176 """
Matteo Scandolod6fce512018-10-16 10:35:29 -0700177 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response)
Luca Preteca974c82018-05-01 18:06:16 -0700178 m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=500, text="EnableError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800179
180 with self.assertRaises(Exception) as e:
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700181 self.sync_step().sync_record(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700182
183 self.assertEqual(e.exception.message, "Failed to enable OLT device: EnableError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800184
185 @requests_mock.Mocker()
186 def test_sync_record_success(self, m):
187 """
188 If device.enable succed should fetch the state, retrieve the of_id and push it to ONOS
189 """
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700190
191 expected_conf = {
192 "type": self.o.device_type,
193 "host_and_port": "%s:%s" % (self.o.host, self.o.port)
194 }
195
Matteo Scandolod6fce512018-10-16 10:35:29 -0700196 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 -0700197 m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200)
Matteo Scandolo45876652018-10-16 16:03:17 -0700198 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 -0800199 logical_devices = {
200 "items": [
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700201 {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800202 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
203 ]
204 }
Luca Preteca974c82018-05-01 18:06:16 -0700205 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800206
Matteo Scandoloa79395f2018-10-08 13:34:49 -0700207 onos_expected_conf = {
208 "devices": {
209 "of:0000000ce2314000": {
210 "basic": {
211 "name": self.o.name
212 }
213 }
214 }
215 }
216 m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=onos_expected_conf,
217 additional_matcher=functools.partial(match_json, onos_expected_conf))
218
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700219 self.sync_step().sync_record(self.o)
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700220 self.assertEqual(self.o.admin_state, "ENABLED")
221 self.assertEqual(self.o.oper_status, "ACTIVE")
Matteo Scandolo45876652018-10-16 16:03:17 -0700222 self.assertEqual(self.o.serial_number, "foobar")
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700223 self.assertEqual(self.o.of_id, "0001000ce2314000")
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700224 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 -0800225
226 @requests_mock.Mocker()
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700227 def test_sync_record_success_mac_address(self, m):
228 """
229 A device should be pre-provisioned via mac_address, the the process is the same
230 """
231
232 del self.o.host
233 del self.o.port
234 self.o.mac_address = "00:0c:e2:31:40:00"
235
236 expected_conf = {
237 "type": self.o.device_type,
238 "mac_address": self.o.mac_address
239 }
240
Matteo Scandoloa79395f2018-10-08 13:34:49 -0700241 onos_expected_conf = {
242 "devices": {
243 "of:0000000ce2314000": {
244 "basic": {
245 "name": self.o.name
246 }
247 }
248 }
249 }
250 m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=onos_expected_conf,
251 additional_matcher=functools.partial(match_json, onos_expected_conf))
252
Matteo Scandolod6fce512018-10-16 10:35:29 -0700253 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response,
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700254 additional_matcher=functools.partial(match_json, expected_conf))
255 m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200)
Matteo Scandolo45876652018-10-16 16:03:17 -0700256 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 -0700257 logical_devices = {
258 "items": [
259 {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
260 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
261 ]
262 }
263 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
264
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700265 self.sync_step().sync_record(self.o)
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700266 self.assertEqual(self.o.admin_state, "ENABLED")
267 self.assertEqual(self.o.oper_status, "ACTIVE")
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700268 self.assertEqual(self.o.of_id, "0001000ce2314000")
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700269 self.assertEqual(self.o.save.call_count, 2)
270
271 @requests_mock.Mocker()
272 def test_sync_record_enable_timeout(self, m):
273 """
274 If device.enable fails we need to tell the suer
275 """
276
277 expected_conf = {
278 "type": self.o.device_type,
279 "host_and_port": "%s:%s" % (self.o.host, self.o.port)
280 }
281
Matteo Scandolod6fce512018-10-16 10:35:29 -0700282 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response,
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700283 additional_matcher=functools.partial(match_json, expected_conf))
284 m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200)
285 m.get("http://voltha_url:1234/api/v1/devices/123", [
Matteo Scandolo45876652018-10-16 16:03:17 -0700286 {"json": {"oper_status": "ACTIVATING", "admin_state": "ENABLED", "serial_number": "foobar"}, "status_code": 200},
287 {"json": {"oper_status": "ERROR", "admin_state": "FAILED", "serial_number": "foobar"}, "status_code": 200}
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700288 ])
289
290 logical_devices = {
291 "items": [
292 {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
293 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
294 ]
295 }
296 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
297
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700298 with self.assertRaises(Exception) as e:
299 self.sync_step().sync_record(self.o)
300
301 self.assertEqual(e.exception.message, "It was not possible to activate OLTDevice with id 1")
302 self.assertEqual(self.o.oper_status, "ERROR")
303 self.assertEqual(self.o.admin_state, "FAILED")
304 self.assertEqual(self.o.save.call_count, 1)
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700305
306 @requests_mock.Mocker()
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700307 def test_sync_record_already_existing_in_voltha(self, m):
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700308 # mock device feedback state
309 self.o.device_id = "123"
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700310 self.o.admin_state = "ENABLED"
311 self.o.oper_status = "ACTIVE"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700312 self.o.dp_id = "of:0000000ce2314000"
Matteo Scandolo2c144932018-05-04 14:06:24 -0700313 self.o.of_id = "0001000ce2314000"
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700314
Matteo Scandoloa79395f2018-10-08 13:34:49 -0700315 expected_conf = {
316 "devices": {
317 self.o.dp_id: {
318 "basic": {
319 "name": self.o.name
320 }
321 }
322 }
323 }
324 m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=expected_conf,
325 additional_matcher=functools.partial(match_json, expected_conf))
326
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700327 self.sync_step().sync_record(self.o)
328 self.o.save.assert_not_called()
329
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700330 @requests_mock.Mocker()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800331 def test_delete_record(self, m):
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700332 self.o.of_id = "0001000ce2314000"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800333 self.o.device_id = "123"
334
Luca Preteca974c82018-05-01 18:06:16 -0700335 m.post("http://voltha_url:1234/api/v1/devices/123/disable", status_code=200)
336 m.delete("http://voltha_url:1234/api/v1/devices/123/delete", status_code=200)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800337
Matteo Scandoloce27e9c2018-04-06 10:06:53 -0700338 self.sync_step().delete_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800339
Matteo Scandolo563891c2018-08-21 11:56:32 -0700340 self.assertEqual(m.call_count, 2)
341
342 @requests_mock.Mocker()
343 def test_delete_unsynced_record(self, m):
344
345 self.sync_step().delete_record(self.o)
346
347 self.assertEqual(m.call_count, 0)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800348
349if __name__ == "__main__":
Luca Preteca974c82018-05-01 18:06:16 -0700350 unittest.main()