diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..59bb9ba7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +**/__pycache__/ +*.cpython* diff --git a/src/nethsec/mwan/__init__.py b/src/nethsec/mwan/__init__.py new file mode 100644 index 00000000..22e6eeab --- /dev/null +++ b/src/nethsec/mwan/__init__.py @@ -0,0 +1,81 @@ +#!/usr/bin/python3 + +# +# Copyright (C) 2023 Nethesis S.r.l. +# SPDX-License-Identifier: GPL-2.0-only +# + +from euci import EUci + +from nethsec import utils + + +def __mwan_keys(e_uci: EUci) -> list[str]: + return utils.get_all_by_type(e_uci, 'mwan3', 'policy').keys() + + +def index(e_uci: EUci) -> dict: + interfaces = utils.get_all_by_type(e_uci, 'mwan', 'policy') + if interfaces is None: + interfaces = [] + return { + 'values': interfaces + } + + +def __store_interface(e_uci: EUci, name: str) -> str: + default_interface_config = utils.get_all_by_type(e_uci, 'ns-api', 'defaults_mwan').get('defaults_mwan') + e_uci.set('mwan3', name, 'interface') + e_uci.set('mwan3', name, 'enabled', '1') + e_uci.set('mwan3', name, 'initial_state', default_interface_config['initial_state']) + e_uci.set('mwan3', name, 'family', default_interface_config['protocol']) + e_uci.set('mwan3', name, 'track_ip', default_interface_config['track_ip']) + e_uci.set('mwan3', name, 'track_method', default_interface_config['tracking_method']) + e_uci.set('mwan3', name, 'reliability', default_interface_config['tracking_reliability']) + e_uci.set('mwan3', name, 'count', default_interface_config['ping_count']) + e_uci.set('mwan3', name, 'size', default_interface_config['ping_size']) + e_uci.set('mwan3', name, 'max_ttl', default_interface_config['ping_max_ttl']) + e_uci.set('mwan3', name, 'timeout', default_interface_config['ping_timeout']) + e_uci.set('mwan3', name, 'interval', default_interface_config['ping_interval']) + e_uci.set('mwan3', name, 'failure_interval', default_interface_config['ping_failure_interval']) + e_uci.set('mwan3', name, 'recovery_interval', default_interface_config['ping_recovery_interval']) + e_uci.set('mwan3', name, 'down', default_interface_config['interface_down_threshold']) + e_uci.set('mwan3', name, 'up', default_interface_config['interface_up_threshold']) + return name + + +def __store_member(e_uci: EUci, interface_name: str, metric: int, weight: int) -> str: + member_config_name = utils.get_id(f'{interface_name}_M{metric}_W{weight}') + e_uci.set('mwan3', member_config_name, 'member') + e_uci.set('mwan3', member_config_name, 'interface', interface_name) + e_uci.set('mwan3', member_config_name, 'metric', metric) + e_uci.set('mwan3', member_config_name, 'weight', weight) + return member_config_name + + +def store_policy(e_uci: EUci, name: str, interfaces: list[dict]) -> list[str]: + # set default name if no policy is defined + available_policies = utils.get_all_by_type(e_uci, 'mwan3', 'policy') + if len(available_policies) < 1: + name = 'default' + + changed_config = [] + # generate policy name + policy_config_name = utils.get_id(name) + # generate policy config with corresponding name + e_uci.set('mwan3', policy_config_name, 'policy') + e_uci.set('mwan3', policy_config_name, 'name', name) + + changed_config.append(policy_config_name) + + member_names: list[str] = [] + for interface in interfaces: + changed_interface_name = __store_interface(e_uci, interface['name']) + changed_member_name = __store_member(e_uci, changed_interface_name, interface['metric'], interface['weight']) + member_names.append(changed_member_name) + changed_config.extend([changed_interface_name, changed_member_name]) + + e_uci.set('mwan3', policy_config_name, 'use_member', member_names) + + e_uci.save('mwan3') + return [f'mwan3.{config_name}' for config_name in changed_config]