Move rmr python api docs over.
[ric-plt/xapp-frame-py.git] / ricxappframe / xapp_frame.py
index 6b70bb6..95aa122 100644 (file)
@@ -21,11 +21,12 @@ Framework here means Xapp classes that can be subclassed
 from threading import Thread
 from ricxappframe import xapp_rmr
 from ricxappframe.xapp_sdl import SDLWrapper
-from rmr import rmr
+from ricxappframe.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
@@ -303,11 +318,16 @@ class RMRXapp(_BaseXapp):
         """
         self._dispatch[message_type] = handler
 
-    def run(self):
+    def run(self, thread=False):
         """
         This function should be called when the client xapp is ready to wait for their handlers to be called on received messages
 
-        execution is returned to caller
+        Parameters
+        ----------
+        thread: bool (optional)
+            if thread is True, execution is returned to caller and the queue read loop is executed in a thread.
+            The thread can be stopped using .stop()
+            if False, execution is not returned and the framework loops
         """
 
         def loop():
@@ -320,14 +340,17 @@ class RMRXapp(_BaseXapp):
                         func = self._default_handler
                     func(self, summary, sbuf)
 
-        Thread(target=loop).start()
+        if thread:
+            Thread(target=loop).start()
+        else:
+            loop()
 
     def stop(self):
         """
         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