From d8facaa17d35af37291192d09a783c620bc379c5 Mon Sep 17 00:00:00 2001 From: Edyta Krukowska Date: Thu, 25 Mar 2021 08:26:18 +0100 Subject: Run flask at image start Issue-ID: INT-1869 Signed-off-by: Edyta Krukowska Change-Id: I4fa3e16e5d4520b449274b67d8f7bca3c16f57a6 --- .../netconf_server/netconf_change_listener.py | 39 +++++++++++++++++ .../netconf_change_listener_factory.py | 40 +++++++++++++++++ src/python/netconf_server/netconf_rest_server.py | 50 ++++++++++++++++++++++ src/python/netconf_server/netconf_server.py | 39 ----------------- .../netconf_server/netconf_server_factory.py | 40 ----------------- 5 files changed, 129 insertions(+), 79 deletions(-) create mode 100644 src/python/netconf_server/netconf_change_listener.py create mode 100644 src/python/netconf_server/netconf_change_listener_factory.py create mode 100644 src/python/netconf_server/netconf_rest_server.py delete mode 100644 src/python/netconf_server/netconf_server.py delete mode 100644 src/python/netconf_server/netconf_server_factory.py (limited to 'src/python/netconf_server') diff --git a/src/python/netconf_server/netconf_change_listener.py b/src/python/netconf_server/netconf_change_listener.py new file mode 100644 index 0000000..4bb748c --- /dev/null +++ b/src/python/netconf_server/netconf_change_listener.py @@ -0,0 +1,39 @@ +### +# ============LICENSE_START======================================================= +# Netconf Server +# ================================================================================ +# Copyright (C) 2021 Nokia. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### +import logging + +from netconf_server.sysrepo_interface.config_change_data import ConfigChangeData + +logger = logging.getLogger("netconf_saver") + + +class NetconfChangeListener(object): + + def __init__(self, subscriptions: list): + self.subscriptions = subscriptions + + def run(self, session): + for subscription in self.subscriptions: + subscription.callback_function = self.__on_module_configuration_change + subscription.subscribe_on_model_change(session) + + @staticmethod + def __on_module_configuration_change(config_change_data: ConfigChangeData): + logger.info("Received module changed: %s , %s " % (config_change_data.event, config_change_data.changes)) diff --git a/src/python/netconf_server/netconf_change_listener_factory.py b/src/python/netconf_server/netconf_change_listener_factory.py new file mode 100644 index 0000000..00725dc --- /dev/null +++ b/src/python/netconf_server/netconf_change_listener_factory.py @@ -0,0 +1,40 @@ +### +# ============LICENSE_START======================================================= +# Netconf Server +# ================================================================================ +# Copyright (C) 2021 Nokia. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### +import logging + +from netconf_server.netconf_change_listener import NetconfChangeListener +from netconf_server.sysrepo_interface.config_change_subscriber import ConfigChangeSubscriber + +logger = logging.getLogger("netconf_saver") + + +class NetconfChangeListenerFactory(object): + + def __init__(self, modules_to_subscribe_names: list): + self.modules_to_subscribe_names = modules_to_subscribe_names + + def create(self) -> NetconfChangeListener: + subscriptions = list() + for module_name in self.modules_to_subscribe_names: + subscriptions.append( + ConfigChangeSubscriber(module_name) + ) + return NetconfChangeListener(subscriptions) + diff --git a/src/python/netconf_server/netconf_rest_server.py b/src/python/netconf_server/netconf_rest_server.py new file mode 100644 index 0000000..2c44029 --- /dev/null +++ b/src/python/netconf_server/netconf_rest_server.py @@ -0,0 +1,50 @@ +### +# ============LICENSE_START======================================================= +# Netconf Server +# ================================================================================ +# Copyright (C) 2021 Nokia. All rights reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### + +import logging as sys_logging +from flask import Flask, logging, make_response, Response + + +class NetconfRestServer: + _rest_server: Flask = Flask("server") + sys_logging.basicConfig(level=sys_logging.DEBUG) + logger = logging.create_logger(_rest_server) + + def __init__(self, host='0.0.0.0', port=6555): + self._host = host + self._port = port + + def start(self): + Flask.run( + NetconfRestServer._rest_server, + host=self._host, + port=self._port + ) + + @staticmethod + @_rest_server.route("/healthcheck") + def __health_check(): + return "UP" + + @staticmethod + def __create_http_response(code, message): + return make_response( + Response(message, headers={'Content-Type': 'application/json'}), + code) diff --git a/src/python/netconf_server/netconf_server.py b/src/python/netconf_server/netconf_server.py deleted file mode 100644 index b790604..0000000 --- a/src/python/netconf_server/netconf_server.py +++ /dev/null @@ -1,39 +0,0 @@ -### -# ============LICENSE_START======================================================= -# Netconf Server -# ================================================================================ -# Copyright (C) 2021 Nokia. All rights reserved. -# ================================================================================ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============LICENSE_END========================================================= -### -import logging - -from netconf_server.sysrepo_interface.config_change_data import ConfigChangeData - -logger = logging.getLogger("netconf_saver") - - -class NetconfServer(object): - - def __init__(self, subscriptions: list): - self.subscriptions = subscriptions - - def run(self, session): - for subscription in self.subscriptions: - subscription.callback_function = self.__on_module_configuration_change - subscription.subscribe_on_model_change(session) - - @staticmethod - def __on_module_configuration_change(config_change_data: ConfigChangeData): - logger.info("Received module changed: %s , %s " % (config_change_data.event, config_change_data.changes)) diff --git a/src/python/netconf_server/netconf_server_factory.py b/src/python/netconf_server/netconf_server_factory.py deleted file mode 100644 index 28297ad..0000000 --- a/src/python/netconf_server/netconf_server_factory.py +++ /dev/null @@ -1,40 +0,0 @@ -### -# ============LICENSE_START======================================================= -# Netconf Server -# ================================================================================ -# Copyright (C) 2021 Nokia. All rights reserved. -# ================================================================================ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============LICENSE_END========================================================= -### -import logging - -from netconf_server.netconf_server import NetconfServer -from netconf_server.sysrepo_interface.config_change_subscriber import ConfigChangeSubscriber - -logger = logging.getLogger("netconf_saver") - - -class NetconfServerFactory(object): - - def __init__(self, modules_to_subscribe_names: list): - self.modules_to_subscribe_names = modules_to_subscribe_names - - def create(self) -> NetconfServer: - subscriptions = list() - for module_name in self.modules_to_subscribe_names: - subscriptions.append( - ConfigChangeSubscriber(module_name) - ) - return NetconfServer(subscriptions) - -- cgit 1.2.3-korg