blob: a4259081178cb3624febfea8f305c06ffe9eda7f [file] [log] [blame]
Matteo Scandoloccef5782018-08-13 13:25:17 -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
15import unittest
Scott Bakerc2a633d2019-04-01 19:27:41 -070016from mock import patch, Mock
Matteo Scandoloccef5782018-08-13 13:25:17 -070017import json
18
Scott Bakerc2a633d2019-04-01 19:27:41 -070019import os
20import sys
Matteo Scandoloccef5782018-08-13 13:25:17 -070021
Scott Bakerc2a633d2019-04-01 19:27:41 -070022test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
23
Matteo Scandoloccef5782018-08-13 13:25:17 -070024
25class TestSyncOLTDevice(unittest.TestCase):
26
27 def setUp(self):
28
29 self.sys_path_save = sys.path
Matteo Scandoloccef5782018-08-13 13:25:17 -070030
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
Scott Baker71d20472019-02-01 12:05:35 -080038 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
39 mock_modelaccessor_config(test_path, [("att-workflow-driver", "att-workflow-driver.xproto"),
40 ("olt-service", "volt.xproto"),
Matteo Scandoload6c8942019-02-14 13:02:08 -080041 ("rcord", "rcord.xproto")])
Matteo Scandoloccef5782018-08-13 13:25:17 -070042
Scott Baker71d20472019-02-01 12:05:35 -080043 import xossynchronizer.modelaccessor
44 import mock_modelaccessor
Scott Bakerc2a633d2019-04-01 19:27:41 -070045 reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
Scott Baker71d20472019-02-01 12:05:35 -080046 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
Matteo Scandoloccef5782018-08-13 13:25:17 -070050
51 # import all class names to globals
52 for (k, v) in model_accessor.all_model_classes.items():
53 globals()[k] = v
54
Scott Baker71d20472019-02-01 12:05:35 -080055 self.model_accessor = model_accessor
Matteo Scandoloccef5782018-08-13 13:25:17 -070056 self.log = Mock()
57
Scott Baker71d20472019-02-01 12:05:35 -080058 self.event_step = ONUEventStep(model_accessor=self.model_accessor, log=self.log)
Matteo Scandoloccef5782018-08-13 13:25:17 -070059
60 self.event = Mock()
61 self.event_dict = {
62 'status': 'activated',
Hardik Windlass7959e1f2019-03-30 20:38:02 +053063 'serialNumber': 'BRCM1234',
64 'deviceId': 'of:109299321',
65 'portNumber': '16'
Matteo Scandoloccef5782018-08-13 13:25:17 -070066 }
67 self.event.value = json.dumps(self.event_dict)
68
Matteo Scandolo242c2962018-08-28 16:50:47 -070069 self.att = AttWorkflowDriverService(name="att-workflow-driver")
70
Matteo Scandoloccef5782018-08-13 13:25:17 -070071 def tearDown(self):
72 sys.path = self.sys_path_save
73
Matteo Scandoloccef5782018-08-13 13:25:17 -070074 def test_create_instance(self):
75
Scott Bakerc2a633d2019-04-01 19:27:41 -070076 with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock, \
77 patch.object(AttWorkflowDriverService.objects, "get_items") as service_mock, \
78 patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
Matteo Scandoloccef5782018-08-13 13:25:17 -070079
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070080 att_si_mock.return_value = []
Matteo Scandolo242c2962018-08-28 16:50:47 -070081 service_mock.return_value = [self.att]
Matteo Scandoloccef5782018-08-13 13:25:17 -070082
83 self.event_step.process_event(self.event)
84
85 att_si = mock_save.call_args[0][0]
86
87 self.assertEqual(mock_save.call_count, 1)
88
Hardik Windlass7959e1f2019-03-30 20:38:02 +053089 self.assertEqual(att_si.serial_number, self.event_dict['serialNumber'])
90 self.assertEqual(att_si.of_dpid, self.event_dict['deviceId'])
91 self.assertEqual(att_si.uni_port_id, long(self.event_dict['portNumber']))
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -070092 # Receiving an ONU event doesn't change the admin_onu_state until the model policy runs
93 self.assertEqual(att_si.admin_onu_state, "AWAITING")
94 self.assertEqual(att_si.oper_onu_status, "ENABLED")
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070095
96 def test_reuse_instance(self):
97
98 si = AttWorkflowDriverServiceInstance(
Hardik Windlass7959e1f2019-03-30 20:38:02 +053099 serial_number=self.event_dict["serialNumber"],
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700100 of_dpid="foo",
101 uni_port_id="foo"
102 )
103
Scott Bakerc2a633d2019-04-01 19:27:41 -0700104 with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock, \
105 patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700106
107 att_si_mock.return_value = [si]
108
109 self.event_step.process_event(self.event)
110
111 att_si = mock_save.call_args[0][0]
112
113 self.assertEqual(mock_save.call_count, 1)
114
Hardik Windlass7959e1f2019-03-30 20:38:02 +0530115 self.assertEqual(att_si.serial_number, self.event_dict['serialNumber'])
116 self.assertEqual(att_si.of_dpid, self.event_dict['deviceId'])
117 self.assertEqual(att_si.uni_port_id, long(self.event_dict['portNumber']))
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -0700118 # Receiving an ONU event doesn't change the admin_onu_state until the model policy runs
119 self.assertEqual(att_si.admin_onu_state, "AWAITING")
120 self.assertEqual(att_si.oper_onu_status, "ENABLED")
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700121
122 def test_disable_onu(self):
123 self.event_dict = {
124 'status': 'disabled',
Hardik Windlass7959e1f2019-03-30 20:38:02 +0530125 'serialNumber': 'BRCM1234',
126 'deviceId': 'of:109299321',
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -0700127 'portNumber': '16',
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700128 }
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -0700129
130 si = AttWorkflowDriverServiceInstance(
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
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700138 self.event.value = json.dumps(self.event_dict)
139
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -0700140 with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock, \
141 patch.object(AttWorkflowDriverServiceInstance, "save_changed_fields", autospec=True) as mock_save:
142 att_si_mock.return_value = [si]
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700143
144 self.event_step.process_event(self.event)
145
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -0700146 att_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(att_si.admin_onu_state, 'ENABLED')
152 self.assertEqual(att_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 = AttWorkflowDriverServiceInstance(
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(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock, \
173 patch.object(AttWorkflowDriverServiceInstance, "save_changed_fields", autospec=True) as mock_save:
174 att_si_mock.return_value = [si]
175
176 self.event_step.process_event(self.event)
177
178 att_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(att_si.admin_onu_state, 'DISABLED')
184 self.assertEqual(att_si.oper_onu_status, 'ENABLED')
185
Scott Bakerc2a633d2019-04-01 19:27:41 -0700186
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700187
188if __name__ == '__main__':
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -0700189 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) # for import of helpers.py
Scott Bakerc2a633d2019-04-01 19:27:41 -0700190 unittest.main()