Add readiness endpoint 54/119954/1
authorBogumil Zebek <bogumil.zebek@nokia.com>
Mon, 29 Mar 2021 09:49:13 +0000 (11:49 +0200)
committerZebek Bogumil <bogumil.zebek@nokia.com>
Mon, 29 Mar 2021 09:49:13 +0000 (11:49 +0200)
Signed-off-by: Bogumil Zebek <bogumil.zebek@nokia.com>
Issue-ID: INT-1869
Signed-off-by: Zebek Bogumil <bogumil.zebek@nokia.com>
Change-Id: I70323ddad5dcf4589789963afb1bb6831522a9a1

.gitignore
src/python/netconf_rest_application.py
src/python/netconf_server/netconf_app_configuration.py
src/python/netconf_server/netconf_rest_server.py

index 2072bde..9d1d337 100644 (file)
@@ -6,3 +6,6 @@
 **/__pycache__
 
 .tox/
+.classpath
+.project
+.settings
index 0d255bc..0a040c9 100644 (file)
@@ -33,9 +33,9 @@ logging.basicConfig(
 logger = logging.getLogger("netconf_rest_application")
 
 
-def start_rest_server(session, connection, server_rest: NetconfRestServer):
+def start_rest_server(session, connection, server_rest: NetconfRestServer, netconf_app_configuration: NetconfAppConfiguration):
     sysrepo_cfg_manager = create_conf_manager(session, connection)
-    server_rest.start(sysrepo_cfg_manager)
+    server_rest.start(sysrepo_cfg_manager, netconf_app_configuration)
 
 
 def create_rest_server() -> NetconfRestServer:
@@ -52,6 +52,6 @@ if __name__ == "__main__":
     if app_configuration:
         logger.info("Netconf rest application configuration: {}".format(app_configuration))
         rest_server = create_rest_server()
-        SysrepoClient().run_in_session(start_rest_server, rest_server)
+        SysrepoClient().run_in_session(start_rest_server, rest_server, app_configuration)
     else:
         logger.error(error)
index 190e113..0e25a41 100644 (file)
@@ -27,11 +27,11 @@ class NetconfAppConfiguration(object):
             kafka_port = args[3]
             kafka_topic = args[4]
 
-            return NetconfAppConfiguration(configuration_file, kafka_host_name, kafka_port, kafka_topic), None
+            return NetconfAppConfiguration(configuration_file, kafka_host_name, int(kafka_port), kafka_topic), None
         else:
             return None, "Invalid number of arguments. Please provide all required arguments."
 
-    def __init__(self, module_configuration_file_path: str, kafka_host_name: str, kafka_port: str, kafka_topic: str):
+    def __init__(self, module_configuration_file_path: str, kafka_host_name: str, kafka_port: int, kafka_topic: str):
         self.module_configuration_file_path = module_configuration_file_path
         self.kafka_host_name = kafka_host_name
         self.kafka_port = kafka_port
index dce4f82..e90c104 100644 (file)
 # ============LICENSE_END=========================================================
 ###
 
-import logging as sys_logging
 from flask import Flask, logging, make_response, Response, request
+
+from netconf_server.netconf_app_configuration import NetconfAppConfiguration
+from netconf_server.netconf_kafka_client import NetconfKafkaClient
 from netconf_server.sysrepo_configuration.sysrepo_configuration_manager import SysrepoConfigurationManager
 
 
@@ -27,13 +29,17 @@ class NetconfRestServer:
     _rest_server: Flask = Flask("server")
     logger = logging.create_logger(_rest_server)
     _configuration_manager: SysrepoConfigurationManager
+    _app_configuration: NetconfAppConfiguration
 
     def __init__(self, host='0.0.0.0', port=6555):
         self._host = host
         self._port = port
 
-    def start(self, configuration_manager: SysrepoConfigurationManager):
+    def start(self,
+              configuration_manager: SysrepoConfigurationManager,
+              netconf_app_configuration: NetconfAppConfiguration):
         NetconfRestServer._configuration_manager = configuration_manager
+        NetconfRestServer._app_configuration = netconf_app_configuration
         Flask.run(
             NetconfRestServer._rest_server,
             host=self._host,
@@ -42,12 +48,31 @@ class NetconfRestServer:
 
     @staticmethod
     @_rest_server.route("/healthcheck")
-    def __health_check():
+    def _health_check():
         return "UP"
 
+    @staticmethod
+    @_rest_server.route("/readiness")
+    def _readiness_check():
+        try:
+            NetconfRestServer.__try_connect_to_kafka()
+            return Response('Ready', status=200)
+        except Exception as e:
+            NetconfRestServer.logger.error("Unable to create a Kafka client", e)
+            return Response('Not Ready', status=503)
+
+    # if Kafka is up & running and hostname with port is proper, then client will be created; otherwise
+    # an error will be reported
+    @staticmethod
+    def __try_connect_to_kafka():
+        NetconfKafkaClient.create(
+            host=NetconfRestServer._app_configuration.kafka_host_name,
+            port=NetconfRestServer._app_configuration.kafka_port
+        )
+
     @staticmethod
     @_rest_server.route("/change_config/<path:module_name>", methods=['POST'])
-    def __change_config(module_name):
+    def _change_config(module_name):
         config_data = request.data.decode("utf-8")
         NetconfRestServer._configuration_manager.change_configuration(config_data, module_name)
         return NetconfRestServer.__create_http_response(202, "Accepted")