blob: 7de68cbbbc260ed16747085289f615b875e220c9 [file] [log] [blame]
Matteo Scandoload0c1752018-08-09 15:47:16 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17import unittest
Scott Bakerc2a633d2019-04-01 19:27:41 -070018from mock import patch
Matteo Scandoload0c1752018-08-09 15:47:16 -070019
Scott Bakerc2a633d2019-04-01 19:27:41 -070020import os
21import sys
Matteo Scandoload0c1752018-08-09 15:47:16 -070022
Scott Bakerc2a633d2019-04-01 19:27:41 -070023test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Matteo Scandoload0c1752018-08-09 15:47:16 -070024
Matteo Scandoload0c1752018-08-09 15:47:16 -070025
26class TestModelPolicyAttWorkflowDriverServiceInstance(unittest.TestCase):
27 def setUp(self):
28
29 self.sys_path_save = sys.path
Matteo Scandoload0c1752018-08-09 15:47:16 -070030
31 config = os.path.join(test_path, "../test_config.yaml")
32 from xosconfig import Config
33 Config.clear()
34 Config.init(config, 'synchronizer-config-schema.yaml')
35
Scott Baker71d20472019-02-01 12:05:35 -080036 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
37 mock_modelaccessor_config(test_path, [("att-workflow-driver", "att-workflow-driver.xproto"),
38 ("olt-service", "volt.xproto"),
Matteo Scandoload6c8942019-02-14 13:02:08 -080039 ("rcord", "rcord.xproto")])
Matteo Scandoload0c1752018-08-09 15:47:16 -070040
Scott Baker71d20472019-02-01 12:05:35 -080041 import xossynchronizer.modelaccessor
42 import mock_modelaccessor
Scott Bakerc2a633d2019-04-01 19:27:41 -070043 reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
Scott Baker71d20472019-02-01 12:05:35 -080044 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
45
46 from xossynchronizer.modelaccessor import model_accessor
47 from model_policy_att_workflow_driver_serviceinstance import AttWorkflowDriverServiceInstancePolicy, AttHelpers
48 self.AttHelpers = AttHelpers
Matteo Scandoload0c1752018-08-09 15:47:16 -070049
Matteo Scandoload0c1752018-08-09 15:47:16 -070050 # import all class names to globals
51 for (k, v) in model_accessor.all_model_classes.items():
52 globals()[k] = v
53
Scott Bakerc2a633d2019-04-01 19:27:41 -070054 # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to
55 # creation of tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
Matteo Scandoload0c1752018-08-09 15:47:16 -070056 model_accessor.reset_all_object_stores()
57
Scott Baker71d20472019-02-01 12:05:35 -080058 self.policy = AttWorkflowDriverServiceInstancePolicy(model_accessor=model_accessor)
Matteo Scandoload0c1752018-08-09 15:47:16 -070059 self.si = AttWorkflowDriverServiceInstance()
60 self.si.owner = AttWorkflowDriverService()
Matteo Scandoloea529092018-09-11 16:36:39 -070061 self.si.serial_number = "BRCM1234"
Matteo Scandoload0c1752018-08-09 15:47:16 -070062
63 def tearDown(self):
64 sys.path = self.sys_path_save
Matteo Scandoload0c1752018-08-09 15:47:16 -070065
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -070066 def test_update_onu(self):
67
68 onu = ONUDevice(
69 serial_number="BRCM1234",
70 admin_state="ENABLED"
71 )
72 with patch.object(ONUDevice.objects, "get_items") as get_onu, \
Scott Bakerc2a633d2019-04-01 19:27:41 -070073 patch.object(onu, "save") as onu_save:
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -070074 get_onu.return_value = [onu]
75
76 self.policy.update_onu("brcm1234", "ENABLED")
77 onu_save.assert_not_called()
78
79 self.policy.update_onu("brcm1234", "DISABLED")
80 self.assertEqual(onu.admin_state, "DISABLED")
Scott Bakerc2a633d2019-04-01 19:27:41 -070081 onu_save.assert_called_with(
82 always_update_timestamp=True, update_fields=[
83 'admin_state', 'serial_number', 'updated'])
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -070084
Matteo Scandoload0c1752018-08-09 15:47:16 -070085 def test_enable_onu(self):
Scott Baker71d20472019-02-01 12:05:35 -080086 with patch.object(self.AttHelpers, "validate_onu") as validate_onu, \
Scott Bakerc2a633d2019-04-01 19:27:41 -070087 patch.object(self.policy, "update_onu") as update_onu:
Matteo Scandoloea529092018-09-11 16:36:39 -070088 validate_onu.return_value = [True, "valid onu"]
Matteo Scandoload0c1752018-08-09 15:47:16 -070089
Andy Bavierafaf1762019-01-16 09:41:43 -070090 self.policy.process_onu_state(self.si)
Matteo Scandoload0c1752018-08-09 15:47:16 -070091
Matteo Scandoloea529092018-09-11 16:36:39 -070092 update_onu.assert_called_once()
93 update_onu.assert_called_with("BRCM1234", "ENABLED")
Matteo Scandoload0c1752018-08-09 15:47:16 -070094
Matteo Scandoloea529092018-09-11 16:36:39 -070095 self.assertIn("valid onu", self.si.status_message)
Matteo Scandoload0c1752018-08-09 15:47:16 -070096
Matteo Scandoloea529092018-09-11 16:36:39 -070097 def test_disable_onu(self):
Scott Baker71d20472019-02-01 12:05:35 -080098 with patch.object(self.AttHelpers, "validate_onu") as validate_onu, \
Scott Bakerc2a633d2019-04-01 19:27:41 -070099 patch.object(self.policy, "update_onu") as update_onu:
Matteo Scandoloea529092018-09-11 16:36:39 -0700100 validate_onu.return_value = [False, "invalid onu"]
Matteo Scandoload0c1752018-08-09 15:47:16 -0700101
Andy Bavierafaf1762019-01-16 09:41:43 -0700102 self.policy.process_onu_state(self.si)
Matteo Scandoloea529092018-09-11 16:36:39 -0700103
104 update_onu.assert_called_once()
105 update_onu.assert_called_with("BRCM1234", "DISABLED")
106
107 self.assertIn("invalid onu", self.si.status_message)
108
109 def test_handle_update_validate_onu(self):
110 """
111 Testing that handle_update calls validate_onu with the correct parameters
112 when necessary
113 """
Andy Bavierafaf1762019-01-16 09:41:43 -0700114 with patch.object(self.policy, "process_onu_state") as process_onu_state, \
Scott Bakerc2a633d2019-04-01 19:27:41 -0700115 patch.object(self.policy, "update_onu") as update_onu, \
116 patch.object(self.policy, "get_subscriber") as get_subscriber:
Matteo Scandoloea529092018-09-11 16:36:39 -0700117 update_onu.return_value = None
118 get_subscriber.return_value = None
119
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -0700120 self.si.admin_onu_state = "AWAITING"
121 self.si.oper_onu_status = "AWAITING"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700122 self.policy.handle_update(self.si)
Andy Bavierafaf1762019-01-16 09:41:43 -0700123 process_onu_state.assert_called_with(self.si)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700124
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -0700125 self.si.admin_onu_state = "ENABLED"
126 self.si.oper_onu_status = "ENABLED"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700127 self.policy.handle_update(self.si)
Andy Bavierafaf1762019-01-16 09:41:43 -0700128 process_onu_state.assert_called_with(self.si)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700129
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -0700130 self.si.admin_onu_state = "DISABLED"
131 self.si.oper_onu_status = "DISABLED"
Matteo Scandoloea529092018-09-11 16:36:39 -0700132 self.policy.handle_update(self.si)
Andy Bavierafaf1762019-01-16 09:41:43 -0700133 process_onu_state.assert_called_with(self.si)
134
Matteo Scandoloea529092018-09-11 16:36:39 -0700135 def test_get_subscriber(self):
136
137 sub = RCORDSubscriber(
138 onu_device="BRCM1234"
139 )
140
141 with patch.object(RCORDSubscriber.objects, "get_items") as get_subscribers:
142 get_subscribers.return_value = [sub]
143
144 res = self.policy.get_subscriber("BRCM1234")
145 self.assertEqual(res, sub)
146
147 res = self.policy.get_subscriber("brcm1234")
148 self.assertEqual(res, sub)
149
150 res = self.policy.get_subscriber("foo")
151 self.assertEqual(res, None)
152
153 def test_update_subscriber(self):
154
155 sub = RCORDSubscriber(
156 onu_device="BRCM1234"
157 )
158
159 self.si.status_message = "some content"
160
161 with patch.object(sub, "save") as sub_save:
162 self.si.authentication_state = "AWAITING"
163 self.policy.update_subscriber(sub, self.si)
164 self.assertEqual(sub.status, "awaiting-auth")
Matteo Scandoloea529092018-09-11 16:36:39 -0700165 sub_save.assert_called()
166 sub_save.reset_mock()
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700167 sub.status = None
Matteo Scandoloea529092018-09-11 16:36:39 -0700168
169 self.si.authentication_state = "REQUESTED"
170 self.policy.update_subscriber(sub, self.si)
171 self.assertEqual(sub.status, "awaiting-auth")
Matteo Scandoloea529092018-09-11 16:36:39 -0700172 sub_save.assert_called()
173 sub_save.reset_mock()
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700174 sub.status = None
Matteo Scandoloea529092018-09-11 16:36:39 -0700175
176 self.si.authentication_state = "STARTED"
177 self.policy.update_subscriber(sub, self.si)
178 self.assertEqual(sub.status, "awaiting-auth")
Matteo Scandoloea529092018-09-11 16:36:39 -0700179 sub_save.assert_called()
180 sub_save.reset_mock()
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700181 sub.status = None
Matteo Scandoloea529092018-09-11 16:36:39 -0700182
183 self.si.authentication_state = "APPROVED"
184 self.policy.update_subscriber(sub, self.si)
185 self.assertEqual(sub.status, "enabled")
Matteo Scandoloea529092018-09-11 16:36:39 -0700186 sub_save.assert_called()
187 sub_save.reset_mock()
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700188 sub.status = None
Matteo Scandoloea529092018-09-11 16:36:39 -0700189
190 self.si.authentication_state = "DENIED"
191 self.policy.update_subscriber(sub, self.si)
192 self.assertEqual(sub.status, "auth-failed")
Matteo Scandoloea529092018-09-11 16:36:39 -0700193 sub_save.assert_called()
194 sub_save.reset_mock()
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700195 sub.status = None
196
197 def test_update_subscriber_not(self):
198 sub = RCORDSubscriber(
199 onu_device="BRCM1234"
200 )
201
202 with patch.object(sub, "save") as sub_save:
203 sub.status = "awaiting-auth"
204 self.si.authentication_state = "AWAITING"
205 self.policy.update_subscriber(sub, self.si)
206 sub_save.assert_not_called()
207
208 sub.status = "awaiting-auth"
209 self.si.authentication_state = "REQUESTED"
210 self.policy.update_subscriber(sub, self.si)
211 sub_save.assert_not_called()
212
213 sub.status = "awaiting-auth"
214 self.si.authentication_state = "STARTED"
215 self.policy.update_subscriber(sub, self.si)
216 sub_save.assert_not_called()
217
218 sub.status = "enabled"
219 self.si.authentication_state = "APPROVED"
220 self.policy.update_subscriber(sub, self.si)
221 sub_save.assert_not_called()
222
223 sub.status = "auth-failed"
224 self.si.authentication_state = "DENIED"
225 self.policy.update_subscriber(sub, self.si)
226 sub_save.assert_not_called()
227
Matteo Scandolo74f63302018-11-01 14:05:01 -0700228 def test_update_subscriber_dhcp_with_exiting_ip(self):
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700229 sub = RCORDSubscriber(
Matteo Scandolo74f63302018-11-01 14:05:01 -0700230 id=10,
231 onu_device="BRCM1234"
232 )
233
234 ip = RCORDIpAddress(
235 subscriber_id=sub.id,
236 ip='10.11.2.23'
237 )
238
Andy Bavier8ed30c92018-12-11 13:46:25 -0700239 self.si.authentication_state = "APPROVED"
Matteo Scandolo74f63302018-11-01 14:05:01 -0700240 self.si.dhcp_state = "DHCPACK"
241 self.si.ip_address = "10.11.2.23"
242 self.si.mac_address = "4321"
243
244 with patch.object(sub, "save") as sub_save, \
Scott Bakerc2a633d2019-04-01 19:27:41 -0700245 patch.object(RCORDIpAddress.objects, "get_items") as get_ips, \
246 patch.object(ip, "save_changed_fields") as ip_mock:
Matteo Scandolo74f63302018-11-01 14:05:01 -0700247
248 get_ips.return_value = [ip]
249 ip_mock.return_value = []
250
251 self.policy.update_subscriber(sub, self.si)
252 sub_save.assert_called()
253 self.assertEqual(sub.mac_address, self.si.mac_address)
254
255 ip_mock.assert_called_with()
256
257 def test_update_subscriber_dhcp_with_new_ip(self):
258 sub = RCORDSubscriber(
259 id=10,
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700260 onu_device="BRCM1234"
261 )
262
Andy Bavier8ed30c92018-12-11 13:46:25 -0700263 self.si.authentication_state = "APPROVED"
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700264 self.si.dhcp_state = "DHCPACK"
Matteo Scandolo74f63302018-11-01 14:05:01 -0700265 self.si.ip_address = "10.11.2.23"
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700266 self.si.mac_address = "4321"
267
Matteo Scandolo74f63302018-11-01 14:05:01 -0700268 with patch.object(sub, "save") as sub_save, \
Scott Bakerc2a633d2019-04-01 19:27:41 -0700269 patch.object(RCORDIpAddress, "save", autospec=True) as ip_mock:
Matteo Scandolo74f63302018-11-01 14:05:01 -0700270
271 ip_mock.return_value = []
272
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700273 self.policy.update_subscriber(sub, self.si)
274 sub_save.assert_called()
275 self.assertEqual(sub.mac_address, self.si.mac_address)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700276
Matteo Scandolo74f63302018-11-01 14:05:01 -0700277 saved_ip = ip_mock.call_args[0][0]
278 self.assertEqual(saved_ip.ip, self.si.ip_address)
279 self.assertEqual(saved_ip.subscriber_id, sub.id)
280 self.assertEqual(saved_ip.description, "DHCP Assigned IP Address")
Matteo Scandoloea529092018-09-11 16:36:39 -0700281
282 def test_handle_update_subscriber(self):
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -0700283 self.si.admin_onu_state = "DISABLED"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700284
Matteo Scandoloea529092018-09-11 16:36:39 -0700285 sub = RCORDSubscriber(
286 onu_device="BRCM1234"
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700287 )
288
Matteo Scandoloea529092018-09-11 16:36:39 -0700289 with patch.object(self.policy, "get_subscriber") as get_subscriber, \
Scott Bakerc2a633d2019-04-01 19:27:41 -0700290 patch.object(self.policy, "update_onu") as update_onu, \
291 patch.object(self.policy, "update_subscriber") as update_subscriber:
Matteo Scandoload0c1752018-08-09 15:47:16 -0700292
Matteo Scandoloea529092018-09-11 16:36:39 -0700293 get_subscriber.return_value = None
Matteo Scandoload0c1752018-08-09 15:47:16 -0700294 self.policy.handle_update(self.si)
Scott Bakerc2a633d2019-04-01 19:27:41 -0700295 update_onu.assert_called_with(sub.onu_device, "DISABLED")
Matteo Scandoloea529092018-09-11 16:36:39 -0700296 self.assertEqual(update_subscriber.call_count, 0)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700297
Matteo Scandoloea529092018-09-11 16:36:39 -0700298 get_subscriber.return_value = sub
Matteo Scandoload0c1752018-08-09 15:47:16 -0700299 self.policy.handle_update(self.si)
Matteo Scandoloea529092018-09-11 16:36:39 -0700300 update_subscriber.assert_called_with(sub, self.si)
301
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -0700302 def test_process_auth_state(self):
303 # testing change in admin_onu_state
304 self.si.admin_onu_state = "DISABLED"
305 self.si.oper_onu_status = "ENABLED"
306 self.si.authentication_state, "APPROVED"
307
308 self.policy.process_auth_state(self.si)
309 self.assertEqual(self.si.authentication_state, "AWAITING")
310
311 # testing change in oper_onu_status
312 self.si.admin_onu_state = "ENABLED"
313 self.si.oper_onu_status = "DISABLED"
314 self.si.authentication_state, "APPROVED"
315
316 self.policy.process_auth_state(self.si)
317 self.assertEqual(self.si.authentication_state, "AWAITING")
318
Matteo Scandoload0c1752018-08-09 15:47:16 -0700319
Matteo Scandoload0c1752018-08-09 15:47:16 -0700320if __name__ == '__main__':
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -0700321 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
Matteo Scandoload0c1752018-08-09 15:47:16 -0700322 unittest.main()