Wei-Yu Chen | ad55cb8 | 2022-02-15 20:07:01 +0800 | [diff] [blame] | 1 | # 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 Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 5 | |
| 6 | from threading import Thread |
| 7 | from typing import List |
| 8 | from unittest import mock |
| 9 | |
| 10 | from lte.protos.mconfig import mconfigs_pb2 |
Wei-Yu Chen | 678f0a5 | 2021-12-21 13:50:52 +0800 | [diff] [blame] | 11 | from configuration.service_configs import load_service_config |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 12 | from common.sentry import sentry_init |
| 13 | from common.service import MagmaService |
| 14 | from enodeb_status import ( |
| 15 | get_operational_states, |
| 16 | get_service_status_old, |
| 17 | ) |
| 18 | from logger import EnodebdLogger as logger |
| 19 | from state_machines.enb_acs_manager import StateMachineManager |
| 20 | from orc8r.protos.service303_pb2 import State |
| 21 | |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 22 | from rpc_servicer import EnodebdRpcServicer |
| 23 | from stats_manager import StatsManager |
| 24 | from tr069.server import tr069_server |
Wei-Yu Chen | 678f0a5 | 2021-12-21 13:50:52 +0800 | [diff] [blame] | 25 | from prometheus_client import start_http_server as prometheus_start_http_server |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def get_context(ip: str): |
| 29 | with mock.patch('spyne.server.wsgi.WsgiApplication') as MockTransport: |
| 30 | MockTransport.req_env = {"REMOTE_ADDR": ip} |
| 31 | with mock.patch('spyne.server.wsgi.WsgiMethodContext') as MockContext: |
| 32 | MockContext.transport = MockTransport |
| 33 | return MockContext |
| 34 | |
| 35 | |
| 36 | def main(): |
| 37 | """ |
| 38 | Top-level function for enodebd |
| 39 | """ |
| 40 | service = MagmaService('enodebd', mconfigs_pb2.EnodebD()) |
| 41 | logger.init() |
| 42 | |
Wei-Yu Chen | 678f0a5 | 2021-12-21 13:50:52 +0800 | [diff] [blame] | 43 | enodebd_cfg = load_service_config('enodebd') |
| 44 | prometheus_cfg = enodebd_cfg.get("prometheus", None) |
| 45 | |
| 46 | if not prometheus_cfg: |
| 47 | logger.warning("Prometheus configuration wasn't found in enodebd configuration.") |
| 48 | else: |
| 49 | prometheus_ip = prometheus_cfg.get("ip") |
| 50 | prometheus_port = prometheus_cfg.get("port") |
| 51 | prometheus_start_http_server(prometheus_port, addr=prometheus_ip) |
| 52 | logger.info( |
| 53 | "Starting Prometheus server on address %s:%d", |
| 54 | prometheus_ip, prometheus_port) |
| 55 | |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 56 | # Optionally pipe errors to Sentry |
| 57 | sentry_init(service_name=service.name) |
| 58 | |
| 59 | # State machine manager for tracking multiple connected eNB devices. |
| 60 | state_machine_manager = StateMachineManager(service) |
| 61 | |
| 62 | # Statistics manager |
| 63 | stats_mgr = StatsManager(state_machine_manager) |
| 64 | stats_mgr.run() |
| 65 | |
| 66 | # Start TR-069 thread |
| 67 | server_thread = Thread( |
| 68 | target=tr069_server, |
| 69 | args=(state_machine_manager,), |
| 70 | daemon=True, |
| 71 | ) |
| 72 | server_thread.start() |
| 73 | |
| 74 | # Add all servicers to the server |
| 75 | enodebd_servicer = EnodebdRpcServicer(state_machine_manager) |
| 76 | enodebd_servicer.add_to_server(service.rpc_server) |
| 77 | |
| 78 | # Register function to get service status |
| 79 | def get_enodebd_status(): |
| 80 | return get_service_status_old(state_machine_manager) |
| 81 | service.register_get_status_callback(get_enodebd_status) |
| 82 | |
| 83 | # Register a callback function for GetOperationalStates service303 function |
| 84 | def get_enodeb_operational_states() -> List[State]: |
| 85 | return get_operational_states(state_machine_manager, service.mconfig) |
| 86 | service.register_operational_states_callback(get_enodeb_operational_states) |
| 87 | |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 88 | # Run the service loop |
| 89 | service.run() |
| 90 | |
| 91 | # Cleanup the service |
| 92 | service.close() |
| 93 | |
| 94 | |
| 95 | def call_repeatedly(loop, interval, function, *args, **kwargs): |
| 96 | """ |
| 97 | Wrapper function to schedule function periodically |
| 98 | """ |
| 99 | # Schedule next call |
| 100 | loop.call_later( |
| 101 | interval, call_repeatedly, loop, interval, function, |
| 102 | *args, **kwargs, |
| 103 | ) |
| 104 | # Call function |
| 105 | function(*args, **kwargs) |
| 106 | |
| 107 | |
| 108 | if __name__ == "__main__": |
| 109 | main() |