Implement healthcheck handler
[ric-plt/xapp-frame-py.git] / ricxappframe / xapp_frame.py
index f278cf1..180c3ae 100644 (file)
@@ -24,8 +24,9 @@ from ricxappframe.xapp_sdl import SDLWrapper
 from rmr import rmr
 from mdclogpy import Logger
 
-
-mdc_logger = Logger(name=__name__)
+# constants
+RIC_HEALTH_CHECK_REQ = 100
+RIC_HEALTH_CHECK_RESP = 101
 
 
 # Private base class; not for direct client use
@@ -57,6 +58,8 @@ class _BaseXapp:
             runs this user provided function after the base xapp is initialized
             it's signature should be post_init(self)
         """
+        # PUBLIC, can be used by xapps using self.(name):
+        self.logger = Logger(name=__name__)
 
         # Start rmr rcv thread
         self._rmr_loop = xapp_rmr.RmrLoop(port=rmr_port, wait_for_ready=rmr_wait_for_ready)
@@ -136,6 +139,7 @@ class _BaseXapp:
             if sbuf.contents.state == 0:
                 return True
 
+        self.logger.info("RTS Failed! Summary: {}".format(rmr.message_summary(sbuf)))
         return False
 
     def rmr_free(self, sbuf):
@@ -283,6 +287,17 @@ class RMRXapp(_BaseXapp):
         # used for thread control
         self._keep_going = True
 
+        # register a default healthcheck handler
+        # this default checks that rmr is working and SDL is working
+        # the user can override this and register their own handler if they wish since the "last registered callback wins".
+        def handle_healthcheck(self, summary, sbuf):
+            ok = self.healthcheck()
+            payload = b"OK\n" if ok else b"ERROR [RMR or SDL is unhealthy]\n"
+            self.rmr_rts(sbuf, new_payload=payload, new_mtype=RIC_HEALTH_CHECK_RESP)
+            self.rmr_free(sbuf)
+
+        self.register_callback(handle_healthcheck, RIC_HEALTH_CHECK_REQ)
+
     def register_callback(self, handler, message_type):
         """
         registers this xapp to call handler(summary, buf) when an rmr message is received of type message_type
@@ -335,7 +350,7 @@ class RMRXapp(_BaseXapp):
         stops the rmr xapp completely.
         """
         super().stop()
-        mdc_logger.debug("Stopping queue reading thread..")
+        self.logger.debug("Stopping queue reading thread..")
         self._keep_going = False