Handler and Manager and dependency
[ric-app/hw-python.git] / src / hwxapp.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
19 from os import getenv
20 from ricxappframe.xapp_frame import RMRXapp, rmr
21
22
23 from .utils.constants import Constants
24 from .manager import *
25
26 from .handler import *
27 from mdclogpy import Logger
28
29
30 class HWXapp:
31
32     def __init__(self):
33         fake_sdl = getenv("USE_FAKE_SDL", False)
34         self._rmr_xapp = RMRXapp(self._default_handler,
35                                  config_handler=self._handle_config_change,
36                                  rmr_port=4560,
37                                  post_init=self._post_init,
38                                  use_fake_sdl=bool(fake_sdl))
39
40     def _post_init(self, rmr_xapp):
41         """
42         Function that runs when xapp initialization is complete
43         """
44         rmr_xapp.logger.info("HWXapp.post_init :: post_init called")
45         # self.sdl_alarm_mgr = SdlAlarmManager()
46         sdl_mgr = SdlManager(rmr_xapp)
47         sdl_mgr.sdlGetGnbList()
48         a1_mgr = A1PolicyManager(rmr_xapp)
49         a1_mgr.startup()
50         sub_mgr = SubscriptionManager(rmr_xapp)
51         enb_list = sub_mgr.get_enb_list()
52         for enb in enb_list:
53             sub_mgr.send_subscription_request(enb)
54         gnb_list = sub_mgr.get_gnb_list()
55         for gnb in gnb_list:
56             sub_mgr.send_subscription_request(gnb)
57         metric_mgr = MetricManager(rmr_xapp)
58         metric_mgr.send_metric()
59
60     def _handle_config_change(self, rmr_xapp, config):
61         """
62         Function that runs at start and on every configuration file change.
63         """
64         rmr_xapp.logger.info("HWXapp.handle_config_change:: config: {}".format(config))
65         rmr_xapp.config = config  # No mutex required due to GIL
66
67     def _default_handler(self, rmr_xapp, summary, sbuf):
68         """
69         Function that processes messages for which no handler is defined
70         """
71         rmr_xapp.logger.info("HWXapp.default_handler called for msg type = " +
72                                    str(summary[rmr.RMR_MS_MSG_TYPE]))
73         rmr_xapp.rmr_free(sbuf)
74
75     def createHandlers(self):
76         """
77         Function that creates all the handlers for RMR Messages
78         """
79         HealthCheckHandler(self._rmr_xapp, Constants.RIC_HEALTH_CHECK_REQ)
80         A1PolicyHandler(self._rmr_xapp, Constants.A1_POLICY_REQ)
81         SubscriptionHandler(self._rmr_xapp,Constants.SUBSCRIPTION_REQ)
82
83     def start(self, thread=False):
84         """
85         This is a convenience function that allows this xapp to run in Docker
86         for "real" (no thread, real SDL), but also easily modified for unit testing
87         (e.g., use_fake_sdl). The defaults for this function are for the Dockerized xapp.
88         """
89         self.createHandlers()
90         self._rmr_xapp.run(thread)
91
92     def stop(self):
93         """
94         can only be called if thread=True when started
95         TODO: could we register a signal handler for Docker SIGTERM that calls this?
96         """
97         self._rmr_xapp.stop()