blob: aae8e961b9fdb3e276971bf51ee92b8d5afdde69 [file] [log] [blame]
Wei-Yu Chen49950b92021-11-08 19:19:18 +08001"""
2Copyright 2020 The Magma Authors.
3
4This source code is licensed under the BSD-style license found in the
5LICENSE file in the root directory of this source tree.
6
7Unless required by applicable law or agreed to in writing, software
8distributed under the License is distributed on an "AS IS" BASIS,
9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10See the License for the specific language governing permissions and
11limitations under the License.
12"""
13
14from threading import Thread
15from typing import List
16from unittest import mock
17
18from lte.protos.mconfig import mconfigs_pb2
19from common.sentry import sentry_init
20from common.service import MagmaService
21from enodeb_status import (
22 get_operational_states,
23 get_service_status_old,
24)
25from logger import EnodebdLogger as logger
26from state_machines.enb_acs_manager import StateMachineManager
27from orc8r.protos.service303_pb2 import State
28
29from enodebd_iptables_rules import set_enodebd_iptables_rule
30from rpc_servicer import EnodebdRpcServicer
31from stats_manager import StatsManager
32from tr069.server import tr069_server
33
34
35def get_context(ip: str):
36 with mock.patch('spyne.server.wsgi.WsgiApplication') as MockTransport:
37 MockTransport.req_env = {"REMOTE_ADDR": ip}
38 with mock.patch('spyne.server.wsgi.WsgiMethodContext') as MockContext:
39 MockContext.transport = MockTransport
40 return MockContext
41
42
43def main():
44 """
45 Top-level function for enodebd
46 """
47 service = MagmaService('enodebd', mconfigs_pb2.EnodebD())
48 logger.init()
49
50 # Optionally pipe errors to Sentry
51 sentry_init(service_name=service.name)
52
53 # State machine manager for tracking multiple connected eNB devices.
54 state_machine_manager = StateMachineManager(service)
55
56 # Statistics manager
57 stats_mgr = StatsManager(state_machine_manager)
58 stats_mgr.run()
59
60 # Start TR-069 thread
61 server_thread = Thread(
62 target=tr069_server,
63 args=(state_machine_manager,),
64 daemon=True,
65 )
66 server_thread.start()
67
68 # Add all servicers to the server
69 enodebd_servicer = EnodebdRpcServicer(state_machine_manager)
70 enodebd_servicer.add_to_server(service.rpc_server)
71
72 # Register function to get service status
73 def get_enodebd_status():
74 return get_service_status_old(state_machine_manager)
75 service.register_get_status_callback(get_enodebd_status)
76
77 # Register a callback function for GetOperationalStates service303 function
78 def get_enodeb_operational_states() -> List[State]:
79 return get_operational_states(state_machine_manager, service.mconfig)
80 service.register_operational_states_callback(get_enodeb_operational_states)
81
82 # Set eNodeBD iptables rules due to exposing public IP to eNodeB
83 service.loop.create_task(set_enodebd_iptables_rule())
84
85 # Run the service loop
86 service.run()
87
88 # Cleanup the service
89 service.close()
90
91
92def call_repeatedly(loop, interval, function, *args, **kwargs):
93 """
94 Wrapper function to schedule function periodically
95 """
96 # Schedule next call
97 loop.call_later(
98 interval, call_repeatedly, loop, interval, function,
99 *args, **kwargs,
100 )
101 # Call function
102 function(*args, **kwargs)
103
104
105if __name__ == "__main__":
106 main()