import logging
from netconf_server.netconf_app_configuration import NetconfAppConfiguration
-from netconf_server.netconf_kafka_client import provide_configured_kafka_client
from netconf_server.netconf_rest_server import NetconfRestServer
from netconf_server.sysrepo_configuration.sysrepo_configuration_manager import SysrepoConfigurationManager
def start_rest_server(session, connection, server_rest: NetconfRestServer, netconf_app_configuration: NetconfAppConfiguration):
sysrepo_cfg_manager = create_conf_manager(session, connection)
- kafka_client = provide_configured_kafka_client(
- netconf_app_configuration.kafka_host_name,
- netconf_app_configuration.kafka_port
- )
- server_rest.start(sysrepo_cfg_manager, kafka_client, netconf_app_configuration.kafka_topic)
+ server_rest.start(sysrepo_cfg_manager, netconf_app_configuration)
def create_rest_server() -> NetconfRestServer:
from flask import Flask, logging, make_response, Response, request, jsonify
-from netconf_server.netconf_kafka_client import NetconfKafkaClient
+from netconf_server.netconf_app_configuration import NetconfAppConfiguration
+from netconf_server.netconf_kafka_client import provide_configured_kafka_client
from netconf_server.sysrepo_configuration.sysrepo_configuration_manager import SysrepoConfigurationManager
_rest_server: Flask = Flask("server")
logger = logging.create_logger(_rest_server)
_configuration_manager: SysrepoConfigurationManager
- _kafka_topic: str
- _kafka_client: NetconfKafkaClient
+ _app_configuration: NetconfAppConfiguration
def __init__(self, host='0.0.0.0', port=6555):
self._host = host
def start(self,
configuration_manager: SysrepoConfigurationManager,
- kafka_client: NetconfKafkaClient,
- kafka_topic: str):
+ netconf_app_configuration: NetconfAppConfiguration):
NetconfRestServer._configuration_manager = configuration_manager
- NetconfRestServer._kafka_client = kafka_client
- NetconfRestServer._kafka_topic = kafka_topic
+ NetconfRestServer._app_configuration = netconf_app_configuration
Flask.run(
NetconfRestServer._rest_server,
host=self._host,
@staticmethod
@_rest_server.route("/readiness")
def _readiness_check():
- if NetconfRestServer._kafka_client:
+ try:
+ NetconfRestServer.__try_connect_to_kafka()
return Response('Ready', status=200)
- else:
+ 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():
+ return provide_configured_kafka_client(
+ NetconfRestServer._app_configuration.kafka_host_name,
+ NetconfRestServer._app_configuration.kafka_port
+ )
+
@staticmethod
@_rest_server.route("/change_config/<path:module_name>", methods=['POST'])
def _change_config(module_name):
@staticmethod
@_rest_server.route("/change_history")
def _change_history():
- history = NetconfRestServer._kafka_client.get_all_messages_from(NetconfRestServer._kafka_topic)
+ history = NetconfRestServer.__try_connect_to_kafka()\
+ .get_all_messages_from(NetconfRestServer._app_configuration.kafka_topic)
return jsonify(history), 200
@staticmethod