2074c19d0314c16300481ccab8c2f7b0f8c4fdef
[ric-app/qp.git] / qp / main.py
1 # ==================================================================================
2 #       Copyright (c) 2020 AT&T Intellectual Property.
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #          http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 # ==================================================================================
16 """
17 mock qp module
18
19 RMR Messages:
20  #define TS_QOE_PRED_REQ 30001
21  #define TS_QOE_PREDICTION 30002
22 30001 is the message type QP receives from the driver;
23 sends out type 30002 which should be routed to TS.
24
25 """
26
27
28 import os
29 from mdclogpy import Logger
30 from ricxappframe.xapp_frame import RMRXapp, rmr
31
32
33 qp_xapp = None
34 logger = Logger(name=__name__)
35
36
37 def post_init(self):
38     self.predict_requests = 0
39     logger.debug("QP xApp started")
40
41
42 def qp_default_handler(self, summary, sbuf):
43     logger.debug("default handler received message type {}".format(summary[rmr.RMR_MS_MSG_TYPE]))
44     # we don't use rts here; free this
45     self.rmr_free(sbuf)
46
47
48 def qp_predict_handler(self, summary, sbuf):
49     logger.debug("predict handler received message type {}".format(summary[rmr.RMR_MS_MSG_TYPE]))
50     self.predict_requests += 1
51     # we don't use rts here; free this
52     self.rmr_free(sbuf)
53     # send a mock message
54     mock_msg = '{ "12345" : { "310-680-200-555001" : [ 2000000 , 1200000 ] , "310-680-200-555002" : [ 800000 , 400000 ] , "310-680-200-555003" : [ 800000 , 400000 ] } }'
55     ok = self.rmr_send(mock_msg.encode(), 30002)
56     if ok:
57         logger.debug("predict handler: sent message successfully")
58     else:
59         logger.warn("predict handler: failed to send message")
60
61
62 def start(thread=False):
63     """
64     This is a convenience function that allows this xapp to run in Docker
65     for "real" (no thread, real SDL), but also easily modified for unit testing
66     (e.g., use_fake_sdl). The defaults for this function are for the Dockerized xapp.
67     """
68     logger.debug("QP xApp starting")
69     global qp_xapp
70     fake_sdl = os.environ.get("USE_FAKE_SDL", None)
71     qp_xapp = RMRXapp(qp_default_handler, post_init=post_init, use_fake_sdl=True if fake_sdl else False)
72     qp_xapp.register_callback(qp_predict_handler, 30001)
73     qp_xapp.run(thread)
74
75
76 def stop():
77     """
78     can only be called if thread=True when started
79     TODO: could we register a signal handler for Docker SIGTERM that calls this?
80     """
81     qp_xapp.stop()
82
83
84 def get_stats():
85     # hacky for now, will evolve
86     return {"PredictRequests": qp_xapp.predict_requests}