diff options
author | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2021-03-17 10:40:23 +0100 |
---|---|---|
committer | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2021-03-23 12:23:43 +0100 |
commit | 3b73097920be148bc82ce4a1c719cc42da8fc721 (patch) | |
tree | e535ef5119dfe6f9a55824560fe7a676f22f6459 /src/python/netconf_server | |
parent | 29c2b0edfb72940a8617207e3ee2aaeb9ac115ab (diff) |
Create python application for config change subscription.
Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Change-Id: I690a188d155bed70f799ef1c6b947c9ecb1a5f47
Issue-ID: INT-1869
Diffstat (limited to 'src/python/netconf_server')
10 files changed, 324 insertions, 0 deletions
diff --git a/src/python/netconf_server/__init__.py b/src/python/netconf_server/__init__.py new file mode 100644 index 0000000..eeb06d5 --- /dev/null +++ b/src/python/netconf_server/__init__.py @@ -0,0 +1,19 @@ +### +# ============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========================================================= +### diff --git a/src/python/netconf_server/netconf_server.py b/src/python/netconf_server/netconf_server.py new file mode 100644 index 0000000..b790604 --- /dev/null +++ b/src/python/netconf_server/netconf_server.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 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 new file mode 100644 index 0000000..28297ad --- /dev/null +++ b/src/python/netconf_server/netconf_server_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_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) + diff --git a/src/python/netconf_server/sysrepo_configuration/__init__.py b/src/python/netconf_server/sysrepo_configuration/__init__.py new file mode 100644 index 0000000..eeb06d5 --- /dev/null +++ b/src/python/netconf_server/sysrepo_configuration/__init__.py @@ -0,0 +1,19 @@ +### +# ============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========================================================= +### diff --git a/src/python/netconf_server/sysrepo_configuration/sysrepo_configuration.py b/src/python/netconf_server/sysrepo_configuration/sysrepo_configuration.py new file mode 100644 index 0000000..fa48098 --- /dev/null +++ b/src/python/netconf_server/sysrepo_configuration/sysrepo_configuration.py @@ -0,0 +1,24 @@ +### +# ============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========================================================= +### + +class SysrepoConfiguration(object): + + def __init__(self, models_to_subscribe_to: list): + self.models_to_subscribe_to = models_to_subscribe_to diff --git a/src/python/netconf_server/sysrepo_configuration/sysrepo_configuration_loader.py b/src/python/netconf_server/sysrepo_configuration/sysrepo_configuration_loader.py new file mode 100644 index 0000000..dc7ac90 --- /dev/null +++ b/src/python/netconf_server/sysrepo_configuration/sysrepo_configuration_loader.py @@ -0,0 +1,58 @@ +### +# ============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 +import os +from configparser import ConfigParser + +from netconf_server.sysrepo_configuration.sysrepo_configuration import SysrepoConfiguration + +MODELS_LIST_TAG = "models" +SUBSCRIPTION_TAG = "SUBSCRIPTION" + +logger = logging.getLogger("sysrep_configuration_loader") + + +class SysrepoConfigurationLoader(object): + + # configuration_file must be in .ini format + @staticmethod + def load_configuration(configuration_file: str) -> SysrepoConfiguration: + if os.path.isfile(configuration_file): + config_object = ConfigParser() + config_object.read(configuration_file) + if SUBSCRIPTION_TAG in config_object and MODELS_LIST_TAG in config_object[SUBSCRIPTION_TAG]: + logger.info("Loading configuration from file %s" % configuration_file) + models_to_subscribe_to = config_object[SUBSCRIPTION_TAG][MODELS_LIST_TAG].split(",") + return SysrepoConfiguration(models_to_subscribe_to) + else: + logger.warning("Loading configuration failed, %s is not valid configuration file" % configuration_file) + raise ConfigLoadingException( + "Loading sysrepo configuration have failed, %s is not correct config file" % configuration_file + ) + else: + logger.warning("Loading configuration failed, %s does not exist or is not a file" % configuration_file) + raise ConfigLoadingException( + "Loading sysrepo configuration have failed, %s is not valid file" % configuration_file + ) + + +class ConfigLoadingException(Exception): + pass diff --git a/src/python/netconf_server/sysrepo_interface/__init__.py b/src/python/netconf_server/sysrepo_interface/__init__.py new file mode 100644 index 0000000..eeb06d5 --- /dev/null +++ b/src/python/netconf_server/sysrepo_interface/__init__.py @@ -0,0 +1,19 @@ +### +# ============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========================================================= +### diff --git a/src/python/netconf_server/sysrepo_interface/config_change_data.py b/src/python/netconf_server/sysrepo_interface/config_change_data.py new file mode 100644 index 0000000..8e329b5 --- /dev/null +++ b/src/python/netconf_server/sysrepo_interface/config_change_data.py @@ -0,0 +1,28 @@ +### +# ============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========================================================= +### + +class ConfigChangeData(object): + + def __init__(self, event: str, req_id: int, changes: list): + self.event = event + self.req_id = req_id + self.changes = changes + + diff --git a/src/python/netconf_server/sysrepo_interface/config_change_subscriber.py b/src/python/netconf_server/sysrepo_interface/config_change_subscriber.py new file mode 100644 index 0000000..faa8254 --- /dev/null +++ b/src/python/netconf_server/sysrepo_interface/config_change_subscriber.py @@ -0,0 +1,49 @@ +### +# ============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("sysrep_config_change_subscriber") + + +class ConfigChangeSubscriber(object): + + def __init__(self, module_name: str, callback_function: callable = None): + self.module_name = module_name + if callback_function is None: + self.callback_function = self.default_callback + else: + self.callback_function = callback_function + + def subscribe_on_model_change(self, session): + logger.info("Subscribing on config change for module %s" % self.module_name) + session.subscribe_module_change( + self.module_name, None, self.on_module_have_changed, asyncio_register=True + ) + + async def on_module_have_changed(self, event: str, req_id: int, changes: list, private_data: any): + logger.debug("Module changed: %s (request ID %s)" % (event, req_id)) + self.callback_function(ConfigChangeData(event, req_id, changes)) + + @staticmethod + def default_callback(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/sysrepo_interface/sysrepo_client.py b/src/python/netconf_server/sysrepo_interface/sysrepo_client.py new file mode 100644 index 0000000..fcd29e2 --- /dev/null +++ b/src/python/netconf_server/sysrepo_interface/sysrepo_client.py @@ -0,0 +1,29 @@ +### +# ============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 sysrepo + + +class SysrepoClient(object): + + @staticmethod + def run_in_session(method_to_run: callable, *extra_args): + with sysrepo.SysrepoConnection() as connection: + with connection.start_session() as session: + method_to_run(session, *extra_args) |