blob: af448672a51594762789998e25042df8f24fdffd [file] [log] [blame]
Daniele Moro134b8d62020-01-14 11:32:05 -08001# Copyright 2020-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
16from mock import patch, Mock
17import json
18
19import os
20import sys
21
22test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
23
24
25class TestSyncOLTDevice(unittest.TestCase):
26
27 def setUp(self):
28
29 self.sys_path_save = sys.path
30
31 # Setting up the config module
32 from xosconfig import Config
33 config = os.path.join(test_path, "../test_config.yaml")
34 Config.clear()
35 Config.init(config, "synchronizer-config-schema.yaml")
36 # END Setting up the config module
37
38 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
39 mock_modelaccessor_config(test_path, [("dt-workflow-driver", "dt-workflow-driver.xproto"),
40 ("olt-service", "volt.xproto"),
41 ("rcord", "rcord.xproto")])
42
43 import xossynchronizer.modelaccessor
44 import mock_modelaccessor
45 reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
46 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
47
48 from xossynchronizer.modelaccessor import model_accessor
49 from onu_event import ONUEventStep
50
51 # import all class names to globals
52 for (k, v) in model_accessor.all_model_classes.items():
53 globals()[k] = v
54
55 self.model_accessor = model_accessor
56 self.log = Mock()
57
58 self.event_step = ONUEventStep(model_accessor=self.model_accessor, log=self.log)
59
60 self.event = Mock()
61 self.event_dict = {
62 'status': 'activated',
63 'serialNumber': 'BRCM1234',
64 'deviceId': 'of:109299321',
65 'portNumber': '16'
66 }
67 self.event.value = json.dumps(self.event_dict)
68
69 self.pppoe = DtWorkflowDriverService(name="dt-workflow-driver")
70
71 def tearDown(self):
72 sys.path = self.sys_path_save
73
74 def test_create_instance(self):
75
76 with patch.object(DtWorkflowDriverServiceInstance.objects, "get_items") as dt_si_mock, \
77 patch.object(DtWorkflowDriverService.objects, "get_items") as service_mock, \
78 patch.object(DtWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
79
80 dt_si_mock.return_value = []
81 service_mock.return_value = [self.pppoe]
82
83 self.event_step.process_event(self.event)
84
85 dt_si = mock_save.call_args[0][0]
86
87 self.assertEqual(mock_save.call_count, 1)
88
89 self.assertEqual(dt_si.serial_number, self.event_dict['serialNumber'])
90 self.assertEqual(dt_si.of_dpid, self.event_dict['deviceId'])
91 self.assertEqual(dt_si.uni_port_id, long(self.event_dict['portNumber']))
92 # Receiving an ONU event doesn't change the admin_onu_state until the model policy runs
93 self.assertEqual(dt_si.admin_onu_state, "AWAITING")
94 self.assertEqual(dt_si.oper_onu_status, "ENABLED")
95
96 def test_reuse_instance(self):
97
98 si = DtWorkflowDriverServiceInstance(
99 serial_number=self.event_dict["serialNumber"],
100 of_dpid="foo",
101 uni_port_id="foo"
102 )
103
104 with patch.object(DtWorkflowDriverServiceInstance.objects, "get_items") as dt_si_mock, \
105 patch.object(DtWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
106
107 dt_si_mock.return_value = [si]
108
109 self.event_step.process_event(self.event)
110
111 dt_si = mock_save.call_args[0][0]
112
113 self.assertEqual(mock_save.call_count, 1)
114
115 self.assertEqual(dt_si.serial_number, self.event_dict['serialNumber'])
116 self.assertEqual(dt_si.of_dpid, self.event_dict['deviceId'])
117 self.assertEqual(dt_si.uni_port_id, long(self.event_dict['portNumber']))
118 # Receiving an ONU event doesn't change the admin_onu_state until the model policy runs
119 self.assertEqual(dt_si.admin_onu_state, "AWAITING")
120 self.assertEqual(dt_si.oper_onu_status, "ENABLED")
121
122 def test_disable_onu(self):
123 self.event_dict = {
124 'status': 'disabled',
125 'serialNumber': 'BRCM1234',
126 'deviceId': 'of:109299321',
127 'portNumber': '16',
128 }
129
130 si = DtWorkflowDriverServiceInstance(
131 serial_number=self.event_dict["serialNumber"],
132 of_dpid="foo",
133 uni_port_id="foo",
134 admin_onu_state="ENABLED",
135 oper_onu_status="ENABLED",
136 )
137
138 self.event.value = json.dumps(self.event_dict)
139
140 with patch.object(DtWorkflowDriverServiceInstance.objects, "get_items") as dt_si_mock, \
141 patch.object(DtWorkflowDriverServiceInstance, "save_changed_fields", autospec=True) as mock_save:
142 dt_si_mock.return_value = [si]
143
144 self.event_step.process_event(self.event)
145
146 dt_si = mock_save.call_args[0][0]
147
148 self.assertEqual(mock_save.call_count, 1)
149
150 # Receiving an ONU event doesn't change the admin_onu_state until the model policy runs
151 self.assertEqual(dt_si.admin_onu_state, 'ENABLED')
152 self.assertEqual(dt_si.oper_onu_status, 'DISABLED')
153
154 def test_enable_onu(self):
155 self.event_dict = {
156 'status': 'activated',
157 'serialNumber': 'BRCM1234',
158 'deviceId': 'of:109299321',
159 'portNumber': '16',
160 }
161
162 si = DtWorkflowDriverServiceInstance(
163 serial_number=self.event_dict["serialNumber"],
164 of_dpid="foo",
165 uni_port_id="foo",
166 admin_onu_state="DISABLED",
167 oper_onu_status="DISABLED",
168 )
169
170 self.event.value = json.dumps(self.event_dict)
171
172 with patch.object(DtWorkflowDriverServiceInstance.objects, "get_items") as dt_si_mock, \
173 patch.object(DtWorkflowDriverServiceInstance, "save_changed_fields", autospec=True) as mock_save:
174 dt_si_mock.return_value = [si]
175
176 self.event_step.process_event(self.event)
177
178 dt_si = mock_save.call_args[0][0]
179
180 self.assertEqual(mock_save.call_count, 1)
181
182 # Receiving an ONU event doesn't change the admin_onu_state until the model policy runs
183 self.assertEqual(dt_si.admin_onu_state, 'DISABLED')
184 self.assertEqual(dt_si.oper_onu_status, 'ENABLED')
185
186
187
188if __name__ == '__main__':
189 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) # for import of helpers.py
190 unittest.main()