blob: 1abc7c890349d3f1d3c0fcdab5ba9a600a47b86c [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
18from mock import patch, call, Mock, PropertyMock
19
20import os, sys
21
22test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
23service_dir=os.path.join(test_path, "../../../..")
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")
28
29def get_models_fn(service_name, xproto_name):
30 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
31 if os.path.exists(os.path.join(services_dir, name)):
32 return name
33 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
34
35class TestModelPolicyAttWorkflowDriverServiceInstance(unittest.TestCase):
36 def setUp(self):
37
38 self.sys_path_save = sys.path
39 sys.path.append(xos_dir)
40 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
41
42 config = os.path.join(test_path, "../test_config.yaml")
43 from xosconfig import Config
44 Config.clear()
45 Config.init(config, 'synchronizer-config-schema.yaml')
46
47 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
48 build_mock_modelaccessor(xos_dir, services_dir, [
49 get_models_fn("att-workflow-driver", "att-workflow-driver.xproto"),
50 get_models_fn("olt-service", "volt.xproto"),
51 get_models_fn("../profiles/rcord", "rcord.xproto")
52 ])
53
54 import synchronizers.new_base.modelaccessor
55 from model_policy_att_workflow_driver_serviceinstance import AttWorkflowDriverServiceInstancePolicy, model_accessor
56
57 from mock_modelaccessor import MockObjectList
58
59 # import all class names to globals
60 for (k, v) in model_accessor.all_model_classes.items():
61 globals()[k] = v
62
63 # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of
64 # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
65 model_accessor.reset_all_object_stores()
66
Matteo Scandoloea529092018-09-11 16:36:39 -070067
Matteo Scandoload0c1752018-08-09 15:47:16 -070068 self.policy = AttWorkflowDriverServiceInstancePolicy()
69 self.si = AttWorkflowDriverServiceInstance()
70 self.si.owner = AttWorkflowDriverService()
Matteo Scandoloea529092018-09-11 16:36:39 -070071 self.si.serial_number = "BRCM1234"
Matteo Scandoload0c1752018-08-09 15:47:16 -070072
73 def tearDown(self):
74 sys.path = self.sys_path_save
Matteo Scandoload0c1752018-08-09 15:47:16 -070075
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -070076 def test_update_onu(self):
77
78 onu = ONUDevice(
79 serial_number="BRCM1234",
80 admin_state="ENABLED"
81 )
82 with patch.object(ONUDevice.objects, "get_items") as get_onu, \
83 patch.object(onu, "save") as onu_save:
84 get_onu.return_value = [onu]
85
86 self.policy.update_onu("brcm1234", "ENABLED")
87 onu_save.assert_not_called()
88
89 self.policy.update_onu("brcm1234", "DISABLED")
90 self.assertEqual(onu.admin_state, "DISABLED")
Andy Bavier0d631eb2018-10-17 18:05:04 -070091 onu_save.assert_called_with(always_update_timestamp=True, update_fields=['admin_state', 'serial_number', 'updated'])
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -070092
93
Matteo Scandoload0c1752018-08-09 15:47:16 -070094 def test_enable_onu(self):
Matteo Scandoloea529092018-09-11 16:36:39 -070095 from helpers import AttHelpers
96 with patch.object(AttHelpers, "validate_onu") as validate_onu, \
97 patch.object(self.policy, "update_onu") as update_onu, \
98 patch.object(self.si, "save") as save_si:
99 validate_onu.return_value = [True, "valid onu"]
Matteo Scandoload0c1752018-08-09 15:47:16 -0700100
Andy Bavierafaf1762019-01-16 09:41:43 -0700101 self.policy.process_onu_state(self.si)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700102
Matteo Scandoloea529092018-09-11 16:36:39 -0700103 update_onu.assert_called_once()
104 update_onu.assert_called_with("BRCM1234", "ENABLED")
Matteo Scandoload0c1752018-08-09 15:47:16 -0700105
Matteo Scandoloea529092018-09-11 16:36:39 -0700106 self.assertIn("valid onu", self.si.status_message)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700107
Matteo Scandoloea529092018-09-11 16:36:39 -0700108 def test_disable_onu(self):
109 from helpers import AttHelpers
110 with patch.object(AttHelpers, "validate_onu") as validate_onu, \
111 patch.object(self.policy, "update_onu") as update_onu, \
112 patch.object(self.si, "save") as save_si:
113 validate_onu.return_value = [False, "invalid onu"]
Matteo Scandoload0c1752018-08-09 15:47:16 -0700114
Andy Bavierafaf1762019-01-16 09:41:43 -0700115 self.policy.process_onu_state(self.si)
Matteo Scandoloea529092018-09-11 16:36:39 -0700116
117 update_onu.assert_called_once()
118 update_onu.assert_called_with("BRCM1234", "DISABLED")
119
120 self.assertIn("invalid onu", self.si.status_message)
121
122 def test_handle_update_validate_onu(self):
123 """
124 Testing that handle_update calls validate_onu with the correct parameters
125 when necessary
126 """
Andy Bavierafaf1762019-01-16 09:41:43 -0700127 with patch.object(self.policy, "process_onu_state") as process_onu_state, \
Matteo Scandoloea529092018-09-11 16:36:39 -0700128 patch.object(self.policy, "update_onu") as update_onu, \
129 patch.object(self.policy, "get_subscriber") as get_subscriber:
130 update_onu.return_value = None
131 get_subscriber.return_value = None
132
133 self.si.onu_state = "AWAITING"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700134 self.policy.handle_update(self.si)
Andy Bavierafaf1762019-01-16 09:41:43 -0700135 process_onu_state.assert_called_with(self.si)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700136
Matteo Scandoloea529092018-09-11 16:36:39 -0700137 self.si.onu_state = "ENABLED"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700138 self.policy.handle_update(self.si)
Andy Bavierafaf1762019-01-16 09:41:43 -0700139 process_onu_state.assert_called_with(self.si)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700140
Matteo Scandoloea529092018-09-11 16:36:39 -0700141 self.si.onu_state = "DISABLED"
142 self.policy.handle_update(self.si)
Andy Bavierafaf1762019-01-16 09:41:43 -0700143 process_onu_state.assert_called_with(self.si)
144
Matteo Scandoload0c1752018-08-09 15:47:16 -0700145
Matteo Scandoloea529092018-09-11 16:36:39 -0700146 def test_get_subscriber(self):
147
148 sub = RCORDSubscriber(
149 onu_device="BRCM1234"
150 )
151
152 with patch.object(RCORDSubscriber.objects, "get_items") as get_subscribers:
153 get_subscribers.return_value = [sub]
154
155 res = self.policy.get_subscriber("BRCM1234")
156 self.assertEqual(res, sub)
157
158 res = self.policy.get_subscriber("brcm1234")
159 self.assertEqual(res, sub)
160
161 res = self.policy.get_subscriber("foo")
162 self.assertEqual(res, None)
163
164 def test_update_subscriber(self):
165
166 sub = RCORDSubscriber(
167 onu_device="BRCM1234"
168 )
169
170 self.si.status_message = "some content"
171
172 with patch.object(sub, "save") as sub_save:
173 self.si.authentication_state = "AWAITING"
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 = "REQUESTED"
181 self.policy.update_subscriber(sub, self.si)
182 self.assertEqual(sub.status, "awaiting-auth")
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 = "STARTED"
188 self.policy.update_subscriber(sub, self.si)
189 self.assertEqual(sub.status, "awaiting-auth")
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
Matteo Scandoloea529092018-09-11 16:36:39 -0700193
194 self.si.authentication_state = "APPROVED"
195 self.policy.update_subscriber(sub, self.si)
196 self.assertEqual(sub.status, "enabled")
Matteo Scandoloea529092018-09-11 16:36:39 -0700197 sub_save.assert_called()
198 sub_save.reset_mock()
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700199 sub.status = None
Matteo Scandoloea529092018-09-11 16:36:39 -0700200
201 self.si.authentication_state = "DENIED"
202 self.policy.update_subscriber(sub, self.si)
203 self.assertEqual(sub.status, "auth-failed")
Matteo Scandoloea529092018-09-11 16:36:39 -0700204 sub_save.assert_called()
205 sub_save.reset_mock()
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700206 sub.status = None
207
208 def test_update_subscriber_not(self):
209 sub = RCORDSubscriber(
210 onu_device="BRCM1234"
211 )
212
213 with patch.object(sub, "save") as sub_save:
214 sub.status = "awaiting-auth"
215 self.si.authentication_state = "AWAITING"
216 self.policy.update_subscriber(sub, self.si)
217 sub_save.assert_not_called()
218
219 sub.status = "awaiting-auth"
220 self.si.authentication_state = "REQUESTED"
221 self.policy.update_subscriber(sub, self.si)
222 sub_save.assert_not_called()
223
224 sub.status = "awaiting-auth"
225 self.si.authentication_state = "STARTED"
226 self.policy.update_subscriber(sub, self.si)
227 sub_save.assert_not_called()
228
229 sub.status = "enabled"
230 self.si.authentication_state = "APPROVED"
231 self.policy.update_subscriber(sub, self.si)
232 sub_save.assert_not_called()
233
234 sub.status = "auth-failed"
235 self.si.authentication_state = "DENIED"
236 self.policy.update_subscriber(sub, self.si)
237 sub_save.assert_not_called()
238
Matteo Scandolo74f63302018-11-01 14:05:01 -0700239 def test_update_subscriber_dhcp_with_exiting_ip(self):
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700240 sub = RCORDSubscriber(
Matteo Scandolo74f63302018-11-01 14:05:01 -0700241 id=10,
242 onu_device="BRCM1234"
243 )
244
245 ip = RCORDIpAddress(
246 subscriber_id=sub.id,
247 ip='10.11.2.23'
248 )
249
Andy Bavier8ed30c92018-12-11 13:46:25 -0700250 self.si.authentication_state = "APPROVED"
Matteo Scandolo74f63302018-11-01 14:05:01 -0700251 self.si.dhcp_state = "DHCPACK"
252 self.si.ip_address = "10.11.2.23"
253 self.si.mac_address = "4321"
254
255 with patch.object(sub, "save") as sub_save, \
256 patch.object(RCORDIpAddress.objects, "get_items") as get_ips, \
257 patch.object(ip, "save_changed_fields") as ip_mock:
258
259 get_ips.return_value = [ip]
260 ip_mock.return_value = []
261
262 self.policy.update_subscriber(sub, self.si)
263 sub_save.assert_called()
264 self.assertEqual(sub.mac_address, self.si.mac_address)
265
266 ip_mock.assert_called_with()
267
268 def test_update_subscriber_dhcp_with_new_ip(self):
269 sub = RCORDSubscriber(
270 id=10,
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700271 onu_device="BRCM1234"
272 )
273
Andy Bavier8ed30c92018-12-11 13:46:25 -0700274 self.si.authentication_state = "APPROVED"
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700275 self.si.dhcp_state = "DHCPACK"
Matteo Scandolo74f63302018-11-01 14:05:01 -0700276 self.si.ip_address = "10.11.2.23"
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700277 self.si.mac_address = "4321"
278
Matteo Scandolo74f63302018-11-01 14:05:01 -0700279 with patch.object(sub, "save") as sub_save, \
280 patch.object(RCORDIpAddress, "save", autospec=True) as ip_mock:
281
282 ip_mock.return_value = []
283
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700284 self.policy.update_subscriber(sub, self.si)
285 sub_save.assert_called()
286 self.assertEqual(sub.mac_address, self.si.mac_address)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700287
Matteo Scandolo74f63302018-11-01 14:05:01 -0700288 saved_ip = ip_mock.call_args[0][0]
289 self.assertEqual(saved_ip.ip, self.si.ip_address)
290 self.assertEqual(saved_ip.subscriber_id, sub.id)
291 self.assertEqual(saved_ip.description, "DHCP Assigned IP Address")
Matteo Scandoloea529092018-09-11 16:36:39 -0700292
293 def test_handle_update_subscriber(self):
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700294 self.si.onu_state = "DISABLED"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700295
Matteo Scandoloea529092018-09-11 16:36:39 -0700296 sub = RCORDSubscriber(
297 onu_device="BRCM1234"
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700298 )
299
Matteo Scandoloea529092018-09-11 16:36:39 -0700300 with patch.object(self.policy, "get_subscriber") as get_subscriber, \
Matteo Scandoloc5c558f2018-11-02 10:33:40 -0700301 patch.object(self.policy, "update_onu") as update_onu, \
Matteo Scandoloea529092018-09-11 16:36:39 -0700302 patch.object(self.policy, "update_subscriber") as update_subscriber:
Matteo Scandoload0c1752018-08-09 15:47:16 -0700303
Matteo Scandoloea529092018-09-11 16:36:39 -0700304 get_subscriber.return_value = None
Matteo Scandoload0c1752018-08-09 15:47:16 -0700305 self.policy.handle_update(self.si)
Matteo Scandoloc5c558f2018-11-02 10:33:40 -0700306 update_onu.assert_called_with(sub.onu_device, "DISABLED");
Matteo Scandoloea529092018-09-11 16:36:39 -0700307 self.assertEqual(update_subscriber.call_count, 0)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700308
Matteo Scandoloea529092018-09-11 16:36:39 -0700309 get_subscriber.return_value = sub
Matteo Scandoload0c1752018-08-09 15:47:16 -0700310 self.policy.handle_update(self.si)
Matteo Scandoloea529092018-09-11 16:36:39 -0700311 update_subscriber.assert_called_with(sub, self.si)
312
Matteo Scandoload0c1752018-08-09 15:47:16 -0700313
Matteo Scandoload0c1752018-08-09 15:47:16 -0700314if __name__ == '__main__':
315 unittest.main()
316