blob: fecd885f9c3c06b9a7f294910871b14dd8076b82 [file] [log] [blame]
Wei-Yu Chenad55cb82022-02-15 20:07:01 +08001# SPDX-FileCopyrightText: 2020 The Magma Authors.
2# SPDX-FileCopyrightText: 2022 Open Networking Foundation <support@opennetworking.org>
3#
4# SPDX-License-Identifier: BSD-3-Clause
Wei-Yu Chen49950b92021-11-08 19:19:18 +08005
6from typing import Dict
7from unittest import TestCase
8from unittest.mock import patch
9
10from common.service import MagmaService
11from data_models.data_model import DataModel
12from devices.device_utils import EnodebDeviceName
13from exceptions import Tr069Error
14from state_machines.enb_acs_impl import BasicEnodebAcsStateMachine
15from state_machines.enb_acs_states import (
16 AcsMsgAndTransition,
17 AcsReadMsgResult,
18 EnodebAcsState,
19 WaitEmptyMessageState,
20 WaitInformState,
21 WaitSetParameterValuesState,
22)
23from tests.test_utils.enb_acs_builder import (
24 EnodebAcsStateMachineBuilder,
25)
26from tr069 import models
27
28
29class DummyDataModel(DataModel):
30 @classmethod
31 def get_parameter(cls, param_name):
32 return None
33
34 @classmethod
35 def _get_magma_transforms(cls):
36 return {}
37
38 @classmethod
39 def _get_enb_transforms(cls):
40 return {}
41
42 @classmethod
43 def get_load_parameters(cls):
44 return []
45
46 @classmethod
47 def get_num_plmns(cls) -> int:
48 return 1
49
50 @classmethod
51 def get_parameter_names(cls):
52 return []
53
54 @classmethod
55 def get_numbered_param_names(cls):
56 return {}
57
58
59class DummyHandler(BasicEnodebAcsStateMachine):
60
61 def __init__(
62 self,
63 service: MagmaService,
64 ) -> None:
65 self._state_map = {}
66 super().__init__(service=service, use_param_key=False)
67
68 def are_invasive_changes_applied(self) -> bool:
69 return False
70
71 def _init_state_map(self) -> None:
72 self._state_map = {
73 'wait_inform': WaitInformState(
74 self,
75 when_done='wait_empty',
76 when_boot='wait_rem',
77 ),
78 }
79
80 @property
81 def state_map(self) -> Dict[str, EnodebAcsState]:
82 return self._state_map
83
84 @property
85 def disconnected_state_name(self) -> str:
86 return 'wait_inform'
87
88 @property
89 def unexpected_fault_state_name(self) -> str:
90 """ State to handle unexpected Fault messages """
91 return ''
92
93 @property
94 def device_name(self) -> EnodebDeviceName:
95 return "dummy"
96
97 @property
98 def config_postprocessor(self):
99 pass
100
101 def reboot_asap(self) -> None:
102 """
103 Send a request to reboot the eNodeB ASAP
104 """
105 pass
106
107 def is_enodeb_connected(self) -> bool:
108 return True
109
110 @property
111 def data_model_class(self):
112 return DummyDataModel
113
114
115class EnodebStatusTests(TestCase):
116
117 def _get_acs(self):
118 """ Get a dummy ACS statemachine for tests"""
119 service = EnodebAcsStateMachineBuilder.build_magma_service()
120 return DummyHandler(service)
121
122 @patch(
123 'magma.enodebd.state_machines.enb_acs_states'
124 '.get_param_values_to_set',
125 )
126 @patch(
127 'magma.enodebd.state_machines.enb_acs_states.get_obj_param_values_to_set',
128 )
129 def test_wait_set_parameter_values_state(
130 self, mock_get_obj_param,
131 mock_get_param,
132 ):
133 """ Test SetParameter return values"""
134 mock_get_param.return_value = {}
135 mock_get_obj_param.return_value = {}
136 test_message_0 = models.SetParameterValuesResponse()
137 test_message_0.Status = 0
138 test_message_1 = models.SetParameterValuesResponse()
139 test_message_1.Status = 1
140 # TC-1: return value is 0. No fault
141 acs_state = WaitSetParameterValuesState(
142 self._get_acs(), 'done',
143 'invasive',
144 )
145
146 rc = acs_state.read_msg(test_message_0)
147 self.assertEqual(type(rc), AcsReadMsgResult)
148
149 # It raises exception if we return 1
150 self.assertRaises(
151 Tr069Error,
152 acs_state.read_msg, test_message_1,
153 )
154
155 # It passes if we return 1 and pass the non zero flag
156 acs_state = WaitSetParameterValuesState(
157 self._get_acs(), 'done',
158 'invasive',
159 status_non_zero_allowed=True,
160 )
161 rc = acs_state.read_msg(test_message_1)
162 self.assertEqual(type(rc), AcsReadMsgResult)
163 rc = acs_state.read_msg(test_message_0)
164 self.assertEqual(type(rc), AcsReadMsgResult)
165
166 @patch(
167 'magma.enodebd.state_machines.enb_acs_states.get_optional_param_to_check',
168 )
169 def test_wait_empty_message_state(
170 self,
171 mock_param_to_check,
172 ):
173 test_message_1 = models.DummyInput()
174 test_message_2 = models.SetParameterValuesResponse()
175 mock_param_to_check.return_value = True
176
177 # test 1: No missing_param_transition
178 # ensure we go to done state even when there are
179 # optional params to check
180 acs_state = WaitEmptyMessageState(
181 self._get_acs(),
182 when_done='done',
183 )
184 rc = acs_state.read_msg(test_message_1)
185 self.assertEqual(type(rc), AcsReadMsgResult)
186 self.assertEqual(rc.next_state, 'done')
187 self.assertEqual(rc.msg_handled, True)
188
189 # test 2: No unknown_param_transition
190 # ensure we go to missing state when there are
191 # optional params to check and missing state is specified
192 acs_state = WaitEmptyMessageState(
193 self._get_acs(),
194 when_done='done',
195 when_missing='missing',
196 )
197 rc = acs_state.read_msg(test_message_1)
198 self.assertEqual(type(rc), AcsReadMsgResult)
199 self.assertEqual(rc.next_state, 'missing')
200 self.assertEqual(rc.msg_handled, True)
201
202 # test 3: Negative test case send a message that is not empty
203 # ensure we return msg_handled is False
204 acs_state = WaitEmptyMessageState(
205 self._get_acs(),
206 when_done='done',
207 when_missing='missing',
208 )
209 rc = acs_state.read_msg(test_message_2)
210 self.assertEqual(type(rc), AcsReadMsgResult)
211 self.assertEqual(rc.next_state, None)
212 self.assertEqual(rc.msg_handled, False)
213
214 # test 4: Test get_msg
215 rc = acs_state.get_msg(test_message_1)
216 self.assertEqual(type(rc), AcsMsgAndTransition)
217 self.assertEqual(type(rc.msg), models.DummyInput)
218 self.assertEqual(rc.next_state, None)