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