Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame^] | 1 | """ |
| 2 | Copyright 2020 The Magma Authors. |
| 3 | |
| 4 | This source code is licensed under the BSD-style license found in the |
| 5 | LICENSE file in the root directory of this source tree. |
| 6 | |
| 7 | Unless required by applicable law or agreed to in writing, software |
| 8 | distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | See the License for the specific language governing permissions and |
| 11 | limitations under the License. |
| 12 | """ |
| 13 | |
| 14 | import asyncio |
| 15 | from typing import Dict |
| 16 | from unittest import mock |
| 17 | |
| 18 | from lte.protos.mconfig import mconfigs_pb2 |
| 19 | from common.service import MagmaService |
| 20 | from devices.device_map import get_device_handler_from_name |
| 21 | from devices.device_utils import EnodebDeviceName |
| 22 | from state_machines.enb_acs import EnodebAcsStateMachine |
| 23 | from state_machines.enb_acs_manager import StateMachineManager |
| 24 | from tests.test_utils.config_builder import EnodebConfigBuilder |
| 25 | |
| 26 | |
| 27 | class EnodebAcsStateMachineBuilder: |
| 28 | @classmethod |
| 29 | def build_acs_manager( |
| 30 | cls, |
| 31 | device: EnodebDeviceName = EnodebDeviceName.BAICELLS, |
| 32 | ) -> StateMachineManager: |
| 33 | service = cls.build_magma_service(device) |
| 34 | return StateMachineManager(service) |
| 35 | |
| 36 | @classmethod |
| 37 | def build_multi_enb_acs_manager( |
| 38 | cls, |
| 39 | ) -> StateMachineManager: |
| 40 | service = cls.build_multi_enb_magma_service() |
| 41 | return StateMachineManager(service) |
| 42 | |
| 43 | @classmethod |
| 44 | def build_multi_enb_acs_state_machine( |
| 45 | cls, |
| 46 | device: EnodebDeviceName = EnodebDeviceName.BAICELLS, |
| 47 | ) -> EnodebAcsStateMachine: |
| 48 | # Build the state_machine |
| 49 | service = cls.build_multi_enb_magma_service() |
| 50 | handler_class = get_device_handler_from_name(device) |
| 51 | acs_state_machine = handler_class(service) |
| 52 | return acs_state_machine |
| 53 | |
| 54 | @classmethod |
| 55 | def build_acs_state_machine( |
| 56 | cls, |
| 57 | device: EnodebDeviceName = EnodebDeviceName.BAICELLS, |
| 58 | ) -> EnodebAcsStateMachine: |
| 59 | # Build the state_machine |
| 60 | service = cls.build_magma_service(device) |
| 61 | handler_class = get_device_handler_from_name(device) |
| 62 | acs_state_machine = handler_class(service) |
| 63 | return acs_state_machine |
| 64 | |
| 65 | @classmethod |
| 66 | def build_magma_service( |
| 67 | cls, |
| 68 | device: EnodebDeviceName = EnodebDeviceName.BAICELLS, |
| 69 | mconfig: mconfigs_pb2.EnodebD = None, |
| 70 | service_config: Dict = None, |
| 71 | ) -> MagmaService: |
| 72 | event_loop = asyncio.get_event_loop() |
| 73 | if not mconfig: |
| 74 | mconfig = EnodebConfigBuilder.get_mconfig(device) |
| 75 | if not service_config: |
| 76 | service_config = EnodebConfigBuilder.get_service_config() |
| 77 | with mock.patch('magma.common.service.MagmaService') as MockService: |
| 78 | MockService.config = service_config |
| 79 | MockService.mconfig = mconfig |
| 80 | MockService.loop = event_loop |
| 81 | return MockService |
| 82 | |
| 83 | @classmethod |
| 84 | def build_multi_enb_magma_service(cls) -> MagmaService: |
| 85 | event_loop = asyncio.get_event_loop() |
| 86 | mconfig = EnodebConfigBuilder.get_multi_enb_mconfig() |
| 87 | service_config = EnodebConfigBuilder.get_service_config() |
| 88 | with mock.patch('magma.common.service.MagmaService') as MockService: |
| 89 | MockService.config = service_config |
| 90 | MockService.mconfig = mconfig |
| 91 | MockService.loop = event_loop |
| 92 | return MockService |