blob: d7842589ad80d2f6cb971d0fdb146cc3ac1ce98d [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
120 self.si.onu_state = "AWAITING"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700121 self.policy.handle_update(self.si)
Andy Bavierafaf1762019-01-16 09:41:43 -0700122 process_onu_state.assert_called_with(self.si)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700123
Matteo Scandoloea529092018-09-11 16:36:39 -0700124 self.si.onu_state = "ENABLED"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700125 self.policy.handle_update(self.si)
Andy Bavierafaf1762019-01-16 09:41:43 -0700126 process_onu_state.assert_called_with(self.si)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700127
Matteo Scandoloea529092018-09-11 16:36:39 -0700128 self.si.onu_state = "DISABLED"
129 self.policy.handle_update(self.si)
Andy Bavierafaf1762019-01-16 09:41:43 -0700130 process_onu_state.assert_called_with(self.si)
131
Matteo Scandoloea529092018-09-11 16:36:39 -0700132 def test_get_subscriber(self):
133
134 sub = RCORDSubscriber(
135 onu_device="BRCM1234"
136 )
137
138 with patch.object(RCORDSubscriber.objects, "get_items") as get_subscribers:
139 get_subscribers.return_value = [sub]
140
141 res = self.policy.get_subscriber("BRCM1234")
142 self.assertEqual(res, sub)
143
144 res = self.policy.get_subscriber("brcm1234")
145 self.assertEqual(res, sub)
146
147 res = self.policy.get_subscriber("foo")
148 self.assertEqual(res, None)
149
150 def test_update_subscriber(self):
151
152 sub = RCORDSubscriber(
153 onu_device="BRCM1234"
154 )
155
156 self.si.status_message = "some content"
157
158 with patch.object(sub, "save") as sub_save:
159 self.si.authentication_state = "AWAITING"
160 self.policy.update_subscriber(sub, self.si)
161 self.assertEqual(sub.status, "awaiting-auth")
Matteo Scandoloea529092018-09-11 16:36:39 -0700162 sub_save.assert_called()
163 sub_save.reset_mock()
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700164 sub.status = None
Matteo Scandoloea529092018-09-11 16:36:39 -0700165
166 self.si.authentication_state = "REQUESTED"
167 self.policy.update_subscriber(sub, self.si)
168 self.assertEqual(sub.status, "awaiting-auth")
Matteo Scandoloea529092018-09-11 16:36:39 -0700169 sub_save.assert_called()
170 sub_save.reset_mock()
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700171 sub.status = None
Matteo Scandoloea529092018-09-11 16:36:39 -0700172
173 self.si.authentication_state = "STARTED"
174 self.policy.update_subscriber(sub, self.si)
175 self.assertEqual(sub.status, "awaiting-auth")
Matteo Scandoloea529092018-09-11 16:36:39 -0700176 sub_save.assert_called()
177 sub_save.reset_mock()
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700178 sub.status = None
Matteo Scandoloea529092018-09-11 16:36:39 -0700179
180 self.si.authentication_state = "APPROVED"
181 self.policy.update_subscriber(sub, self.si)
182 self.assertEqual(sub.status, "enabled")
Matteo Scandoloea529092018-09-11 16:36:39 -0700183 sub_save.assert_called()
184 sub_save.reset_mock()
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700185 sub.status = None
Matteo Scandoloea529092018-09-11 16:36:39 -0700186
187 self.si.authentication_state = "DENIED"
188 self.policy.update_subscriber(sub, self.si)
189 self.assertEqual(sub.status, "auth-failed")
Matteo Scandoloea529092018-09-11 16:36:39 -0700190 sub_save.assert_called()
191 sub_save.reset_mock()
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700192 sub.status = None
193
194 def test_update_subscriber_not(self):
195 sub = RCORDSubscriber(
196 onu_device="BRCM1234"
197 )
198
199 with patch.object(sub, "save") as sub_save:
200 sub.status = "awaiting-auth"
201 self.si.authentication_state = "AWAITING"
202 self.policy.update_subscriber(sub, self.si)
203 sub_save.assert_not_called()
204
205 sub.status = "awaiting-auth"
206 self.si.authentication_state = "REQUESTED"
207 self.policy.update_subscriber(sub, self.si)
208 sub_save.assert_not_called()
209
210 sub.status = "awaiting-auth"
211 self.si.authentication_state = "STARTED"
212 self.policy.update_subscriber(sub, self.si)
213 sub_save.assert_not_called()
214
215 sub.status = "enabled"
216 self.si.authentication_state = "APPROVED"
217 self.policy.update_subscriber(sub, self.si)
218 sub_save.assert_not_called()
219
220 sub.status = "auth-failed"
221 self.si.authentication_state = "DENIED"
222 self.policy.update_subscriber(sub, self.si)
223 sub_save.assert_not_called()
224
Matteo Scandolo74f63302018-11-01 14:05:01 -0700225 def test_update_subscriber_dhcp_with_exiting_ip(self):
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700226 sub = RCORDSubscriber(
Matteo Scandolo74f63302018-11-01 14:05:01 -0700227 id=10,
228 onu_device="BRCM1234"
229 )
230
231 ip = RCORDIpAddress(
232 subscriber_id=sub.id,
233 ip='10.11.2.23'
234 )
235
Andy Bavier8ed30c92018-12-11 13:46:25 -0700236 self.si.authentication_state = "APPROVED"
Matteo Scandolo74f63302018-11-01 14:05:01 -0700237 self.si.dhcp_state = "DHCPACK"
238 self.si.ip_address = "10.11.2.23"
239 self.si.mac_address = "4321"
240
241 with patch.object(sub, "save") as sub_save, \
Scott Bakerc2a633d2019-04-01 19:27:41 -0700242 patch.object(RCORDIpAddress.objects, "get_items") as get_ips, \
243 patch.object(ip, "save_changed_fields") as ip_mock:
Matteo Scandolo74f63302018-11-01 14:05:01 -0700244
245 get_ips.return_value = [ip]
246 ip_mock.return_value = []
247
248 self.policy.update_subscriber(sub, self.si)
249 sub_save.assert_called()
250 self.assertEqual(sub.mac_address, self.si.mac_address)
251
252 ip_mock.assert_called_with()
253
254 def test_update_subscriber_dhcp_with_new_ip(self):
255 sub = RCORDSubscriber(
256 id=10,
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700257 onu_device="BRCM1234"
258 )
259
Andy Bavier8ed30c92018-12-11 13:46:25 -0700260 self.si.authentication_state = "APPROVED"
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700261 self.si.dhcp_state = "DHCPACK"
Matteo Scandolo74f63302018-11-01 14:05:01 -0700262 self.si.ip_address = "10.11.2.23"
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700263 self.si.mac_address = "4321"
264
Matteo Scandolo74f63302018-11-01 14:05:01 -0700265 with patch.object(sub, "save") as sub_save, \
Scott Bakerc2a633d2019-04-01 19:27:41 -0700266 patch.object(RCORDIpAddress, "save", autospec=True) as ip_mock:
Matteo Scandolo74f63302018-11-01 14:05:01 -0700267
268 ip_mock.return_value = []
269
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700270 self.policy.update_subscriber(sub, self.si)
271 sub_save.assert_called()
272 self.assertEqual(sub.mac_address, self.si.mac_address)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700273
Matteo Scandolo74f63302018-11-01 14:05:01 -0700274 saved_ip = ip_mock.call_args[0][0]
275 self.assertEqual(saved_ip.ip, self.si.ip_address)
276 self.assertEqual(saved_ip.subscriber_id, sub.id)
277 self.assertEqual(saved_ip.description, "DHCP Assigned IP Address")
Matteo Scandoloea529092018-09-11 16:36:39 -0700278
279 def test_handle_update_subscriber(self):
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700280 self.si.onu_state = "DISABLED"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700281
Matteo Scandoloea529092018-09-11 16:36:39 -0700282 sub = RCORDSubscriber(
283 onu_device="BRCM1234"
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700284 )
285
Matteo Scandoloea529092018-09-11 16:36:39 -0700286 with patch.object(self.policy, "get_subscriber") as get_subscriber, \
Scott Bakerc2a633d2019-04-01 19:27:41 -0700287 patch.object(self.policy, "update_onu") as update_onu, \
288 patch.object(self.policy, "update_subscriber") as update_subscriber:
Matteo Scandoload0c1752018-08-09 15:47:16 -0700289
Matteo Scandoloea529092018-09-11 16:36:39 -0700290 get_subscriber.return_value = None
Matteo Scandoload0c1752018-08-09 15:47:16 -0700291 self.policy.handle_update(self.si)
Scott Bakerc2a633d2019-04-01 19:27:41 -0700292 update_onu.assert_called_with(sub.onu_device, "DISABLED")
Matteo Scandoloea529092018-09-11 16:36:39 -0700293 self.assertEqual(update_subscriber.call_count, 0)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700294
Matteo Scandoloea529092018-09-11 16:36:39 -0700295 get_subscriber.return_value = sub
Matteo Scandoload0c1752018-08-09 15:47:16 -0700296 self.policy.handle_update(self.si)
Matteo Scandoloea529092018-09-11 16:36:39 -0700297 update_subscriber.assert_called_with(sub, self.si)
298
Matteo Scandoload0c1752018-08-09 15:47:16 -0700299
Matteo Scandoload0c1752018-08-09 15:47:16 -0700300if __name__ == '__main__':
301 unittest.main()