blob: 0898018fdcc01ba960d1b0d26ae197042b7e7918 [file] [log] [blame]
Matteo Scandolod44ca992018-05-17 15:02:10 -07001# 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 -080015import functools
Matteo Scandolod44ca992018-05-17 15:02:10 -070016import unittest
17from 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")
27 services_dir = os.path.join(xos_dir, "../../xos_services")
28sys.path.append(xos_dir)
29sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
30# END Hack to load synchronizer framework
31
32# generate model from xproto
33def 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
43
44class TestPullONUDevice(unittest.TestCase):
45
46 def setUp(self):
47 global DeferredException
48
49 self.sys_path_save = sys.path
50 sys.path.append(xos_dir)
51 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
52
53 # Setting up the config module
54 from xosconfig import Config
Matteo Scandolof7ebb112018-09-18 16:17:22 -070055 config = os.path.join(test_path, "../test_config.yaml")
Matteo Scandolod44ca992018-05-17 15:02:10 -070056 Config.clear()
57 Config.init(config, "synchronizer-config-schema.yaml")
58 # END Setting up the config module
59
60 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
61 # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")])
62
63 # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order
64 # and apparently it is not overriding the generated model accessor
65 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"),
66 get_models_fn("vsg", "vsg.xproto"),
67 get_models_fn("../profiles/rcord", "rcord.xproto")])
68 import synchronizers.new_base.modelaccessor
69 from pull_onus import ONUDevicePullStep, model_accessor
70
71 # import all class names to globals
72 for (k, v) in model_accessor.all_model_classes.items():
73 globals()[k] = v
74
75 self.sync_step = ONUDevicePullStep
76
77 # mock volt service
78 self.volt_service = Mock()
79 self.volt_service.id = "volt_service_id"
80 self.volt_service.voltha_url = "voltha_url"
81 self.volt_service.voltha_user = "voltha_user"
82 self.volt_service.voltha_pass = "voltha_pass"
83 self.volt_service.voltha_port = 1234
84
85 # mock OLTDevice
86 self.olt = Mock()
87 self.olt.id = 1
88
Scott Baker22b46c52018-11-15 15:15:29 -080089 # second mock OLTDevice
90 self.olt2 = Mock()
91 self.olt2.id = 2
92
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070093 # mock pon port
94 self.pon_port = Mock()
95 self.pon_port.id = 1
96
Scott Baker22b46c52018-11-15 15:15:29 -080097 # mock pon port
98 self.pon_port2 = Mock()
99 self.pon_port2.id = 2
100
Matteo Scandolod44ca992018-05-17 15:02:10 -0700101 # mock voltha responses
102 self.devices = {
103 "items": [
104 {
105 "id": "0001130158f01b2d",
106 "type": "broadcom_onu",
107 "vendor": "Broadcom",
108 "serial_number": "BRCM22222222",
109 "vendor_id": "BRCM",
110 "adapter": "broadcom_onu",
111 "vlan": 0,
112 "admin_state": "ENABLED",
113 "oper_status": "ACTIVE",
114 "connect_status": "REACHABLE",
Scott Baker3db0eef2019-01-15 11:56:41 -0800115 "reason": "starting-omci",
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700116 "parent_id": "00010fc93996afea",
117 "parent_port_no": 1
Matteo Scandolod44ca992018-05-17 15:02:10 -0700118 }
119 ]
120 }
121
Scott Baker22b46c52018-11-15 15:15:29 -0800122 self.two_devices = {
123 "items": [
124 {
125 "id": "0001130158f01b2d",
126 "type": "broadcom_onu",
127 "vendor": "Broadcom",
128 "serial_number": "BRCM22222222",
129 "vendor_id": "BRCM",
130 "adapter": "broadcom_onu",
131 "vlan": 0,
132 "admin_state": "ENABLED",
133 "oper_status": "ACTIVE",
134 "connect_status": "REACHABLE",
Scott Baker3db0eef2019-01-15 11:56:41 -0800135 "reason": "starting-omci",
Scott Baker22b46c52018-11-15 15:15:29 -0800136 "parent_id": "00010fc93996afea",
137 "parent_port_no": 1
138 },
139 {
140 "id": "0001130158f01b2e",
141 "type": "broadcom_onu",
142 "vendor": "Broadcom",
143 "serial_number": "BRCM22222223",
144 "vendor_id": "BRCM",
145 "adapter": "broadcom_onu",
146 "vlan": 0,
147 "admin_state": "ENABLED",
148 "oper_status": "ACTIVE",
149 "connect_status": "REACHABLE",
Scott Baker3db0eef2019-01-15 11:56:41 -0800150 "reason": "omci-admin-lock",
Scott Baker22b46c52018-11-15 15:15:29 -0800151 "parent_id": "00010fc93996afeb",
152 "parent_port_no": 1
153 }
154 ],
155 }
156
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700157 # TODO add ports
158 self.ports = {
159 "items": []
160 }
161
Matteo Scandolod44ca992018-05-17 15:02:10 -0700162 def tearDown(self):
163 sys.path = self.sys_path_save
164
165 @requests_mock.Mocker()
166 def test_missing_volt_service(self, m):
167 self.assertFalse(m.called)
168
169 @requests_mock.Mocker()
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700170 def test_pull(self, m):
Matteo Scandolod44ca992018-05-17 15:02:10 -0700171
172 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
173 patch.object(OLTDevice.objects, "get") as mock_olt_device, \
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700174 patch.object(PONPort.objects, "get") as mock_pon_port, \
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700175 patch.object(ONUDevice, "save", autospec=True) as mock_save:
Matteo Scandolod44ca992018-05-17 15:02:10 -0700176 olt_service_mock.return_value = [self.volt_service]
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700177 mock_pon_port.return_value = self.pon_port
Matteo Scandolod44ca992018-05-17 15:02:10 -0700178 mock_olt_device.return_value = self.olt
179
180 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices)
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700181 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2d/ports", status_code=200, json=self.ports)
Matteo Scandolod44ca992018-05-17 15:02:10 -0700182
183 self.sync_step().pull_records()
184
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700185 saved_onu = mock_save.call_args[0][0]
Matteo Scandolod44ca992018-05-17 15:02:10 -0700186
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700187 self.assertEqual(saved_onu.admin_state, "ENABLED")
188 self.assertEqual(saved_onu.oper_status, "ACTIVE")
189 self.assertEqual(saved_onu.connect_status, "REACHABLE")
Scott Baker3db0eef2019-01-15 11:56:41 -0800190 self.assertEqual(saved_onu.reason, "starting-omci")
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700191 self.assertEqual(saved_onu.device_type, "broadcom_onu")
192 self.assertEqual(saved_onu.vendor, "Broadcom")
193 self.assertEqual(saved_onu.device_id, "0001130158f01b2d")
Matteo Scandolod44ca992018-05-17 15:02:10 -0700194
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700195 self.assertEqual(mock_save.call_count, 1)
Matteo Scandolod44ca992018-05-17 15:02:10 -0700196
Scott Baker22b46c52018-11-15 15:15:29 -0800197 @requests_mock.Mocker()
198 def test_pull_bad_pon(self, m):
199
200 def olt_side_effect(device_id):
201 # fail the first onu device
202 if device_id=="00010fc93996afea":
203 return self.olt
204 else:
205 return self.olt2
206
207 def pon_port_side_effect(mock_pon_port, port_no, olt_device_id):
208 # fail the first onu device
209 if olt_device_id==1:
210 raise IndexError()
211 return self.pon_port2
212
213 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
214 patch.object(OLTDevice.objects, "get") as mock_olt_device, \
215 patch.object(PONPort.objects, "get") as mock_pon_port, \
216 patch.object(ONUDevice, "save", autospec=True) as mock_save:
217 olt_service_mock.return_value = [self.volt_service]
218 mock_pon_port.side_effect = functools.partial(pon_port_side_effect, self.pon_port)
219 mock_olt_device.side_effect = olt_side_effect
220
221 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.two_devices)
222 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2d/ports", status_code=200, json=self.ports)
223 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2e/ports", status_code=200, json=self.ports)
224
225 self.sync_step().pull_records()
226
227 self.assertEqual(mock_save.call_count, 1)
228 saved_onu = mock_save.call_args[0][0]
229
230 # we should get the second onu in self.two_onus
231
232 self.assertEqual(saved_onu.admin_state, "ENABLED")
233 self.assertEqual(saved_onu.oper_status, "ACTIVE")
234 self.assertEqual(saved_onu.connect_status, "REACHABLE")
Scott Baker3db0eef2019-01-15 11:56:41 -0800235 self.assertEqual(saved_onu.reason, "omci-admin-lock")
Scott Baker22b46c52018-11-15 15:15:29 -0800236 self.assertEqual(saved_onu.device_type, "broadcom_onu")
237 self.assertEqual(saved_onu.vendor, "Broadcom")
238 self.assertEqual(saved_onu.device_id, "0001130158f01b2e")
239
240 self.assertEqual(mock_save.call_count, 1)
241
242 @requests_mock.Mocker()
243 def test_pull_bad_olt(self, m):
244
245 def olt_side_effect(device_id):
246 # fail the first onu device
247 if device_id=="00010fc93996afea":
248 raise IndexError()
249 else:
250 return self.olt2
251
252 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
253 patch.object(OLTDevice.objects, "get") as mock_olt_device, \
254 patch.object(PONPort.objects, "get") as mock_pon_port, \
255 patch.object(ONUDevice, "save", autospec=True) as mock_save:
256 olt_service_mock.return_value = [self.volt_service]
257 mock_pon_port.return_value = self.pon_port2
258 mock_olt_device.side_effect = olt_side_effect
259
260 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.two_devices)
261 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2d/ports", status_code=200, json=self.ports)
262 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2e/ports", status_code=200, json=self.ports)
263
264 self.sync_step().pull_records()
265
266 self.assertEqual(mock_save.call_count, 1)
267 saved_onu = mock_save.call_args[0][0]
268
269 # we should get the second onu in self.two_onus
270
271 self.assertEqual(saved_onu.admin_state, "ENABLED")
272 self.assertEqual(saved_onu.oper_status, "ACTIVE")
273 self.assertEqual(saved_onu.connect_status, "REACHABLE")
Scott Baker3db0eef2019-01-15 11:56:41 -0800274 self.assertEqual(saved_onu.reason, "omci-admin-lock")
Scott Baker22b46c52018-11-15 15:15:29 -0800275 self.assertEqual(saved_onu.device_type, "broadcom_onu")
276 self.assertEqual(saved_onu.vendor, "Broadcom")
277 self.assertEqual(saved_onu.device_id, "0001130158f01b2e")
278
279 self.assertEqual(mock_save.call_count, 1)
280
Matteo Scandolod44ca992018-05-17 15:02:10 -0700281
282if __name__ == "__main__":
283 unittest.main()