10ad0f8a66e39a5c0caf22cdb9ad9f6ef41dc32d
[ric-app/hw-python.git] / src / main.py
1 # ==================================================================================
2 #
3 #       Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #          http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 # ==================================================================================
17
18 import json
19 from os import getenv
20 from ricxappframe.xapp_frame import RMRXapp, rmr
21 from ricxappframe.alarm import alarm
22
23
24 # pylint: disable=invalid-name
25 rmr_xapp = None
26
27
28 def post_init(self):
29     """
30     Function that runs when xapp initialization is complete
31     """
32     self.logger.info("post_init called")
33
34 def handle_config_change(self, config):
35     """
36     Function that runs at start and on every configuration file change.
37     """
38     self.logger.info("handle_config_change: config: {}".format(config))
39
40
41 def default_handler(self, summary, sbuf):
42     """
43     Function that processes messages for which no handler is defined
44     """
45     self.logger.info("default_handler called")
46     self.rmr_free(sbuf)
47
48
49 def start(thread=False):
50     """
51     This is a convenience function that allows this xapp to run in Docker
52     for "real" (no thread, real SDL), but also easily modified for unit testing
53     (e.g., use_fake_sdl). The defaults for this function are for the Dockerized xapp.
54     """
55     global rmr_xapp
56     fake_sdl = getenv("USE_FAKE_SDL", True)
57     config_file = getenv("CONFIG_FILE", None)
58     rmr_xapp = RMRXapp(default_handler,
59                        config_handler=handle_config_change,
60                        rmr_port=4560,
61                        post_init=post_init,
62                        use_fake_sdl=bool(fake_sdl))
63     rmr_xapp.run(thread)
64
65
66 def stop():
67     """
68     can only be called if thread=True when started
69     TODO: could we register a signal handler for Docker SIGTERM that calls this?
70     """
71     rmr_xapp.stop()
72
73 if __name__ == "__main__":
74     start()