blob: 80637c459397004e460388f11e4c9ef513516efe [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 threading import Thread
7from typing import List
8from unittest import mock
9
10from lte.protos.mconfig import mconfigs_pb2
Wei-Yu Chen678f0a52021-12-21 13:50:52 +080011from configuration.service_configs import load_service_config
Wei-Yu Chen49950b92021-11-08 19:19:18 +080012from common.sentry import sentry_init
13from common.service import MagmaService
14from enodeb_status import (
15 get_operational_states,
16 get_service_status_old,
17)
18from logger import EnodebdLogger as logger
19from state_machines.enb_acs_manager import StateMachineManager
20from orc8r.protos.service303_pb2 import State
21
Wei-Yu Chen49950b92021-11-08 19:19:18 +080022from rpc_servicer import EnodebdRpcServicer
23from stats_manager import StatsManager
24from tr069.server import tr069_server
Wei-Yu Chen678f0a52021-12-21 13:50:52 +080025from prometheus_client import start_http_server as prometheus_start_http_server
Wei-Yu Chen49950b92021-11-08 19:19:18 +080026
27
28def 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
36def main():
37 """
38 Top-level function for enodebd
39 """
40 service = MagmaService('enodebd', mconfigs_pb2.EnodebD())
41 logger.init()
42
Wei-Yu Chen678f0a52021-12-21 13:50:52 +080043 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 Chen49950b92021-11-08 19:19:18 +080056 # 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 Chen49950b92021-11-08 19:19:18 +080088 # Run the service loop
89 service.run()
90
91 # Cleanup the service
92 service.close()
93
94
95def 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
108if __name__ == "__main__":
109 main()