From dbfaa47b0036339e28ae8fd57c8007612e1b38f3 Mon Sep 17 00:00:00 2001
From: sarthurdev <965089+sarthurdev@users.noreply.github.com>
Date: Tue, 13 Feb 2024 01:24:27 +0100
Subject: dhcp: dhcpv6: T3316: Add op-mode for showing DHCP(v6) static-mappings
---
op-mode-definitions/dhcp.xml.in | 52 +++++++++++++++++++++++++++++++++++++
src/op_mode/dhcp.py | 57 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 109 insertions(+)
diff --git a/op-mode-definitions/dhcp.xml.in b/op-mode-definitions/dhcp.xml.in
index ab305580b..0db7471e5 100644
--- a/op-mode-definitions/dhcp.xml.in
+++ b/op-mode-definitions/dhcp.xml.in
@@ -80,6 +80,32 @@
+
+
+ Show DHCP server static mappings
+
+ ${vyos_op_scripts_dir}/dhcp.py show_server_static_mappings --family inet
+
+
+
+ Show DHCP server static mappings for a specific pool
+
+ service dhcp-server shared-network-name
+
+
+ ${vyos_op_scripts_dir}/dhcp.py show_server_static_mappings --family inet --pool $6
+
+
+
+ Show DHCP server static mappings sorted by the specified key
+
+ ip mac duid pool
+
+
+ ${vyos_op_scripts_dir}/dhcp.py show_server_static_mappings --family inet --sort $6
+
+
+
Show DHCP server statistics
@@ -146,6 +172,32 @@
+
+
+ Show DHCPv6 server static mappings
+
+ ${vyos_op_scripts_dir}/dhcp.py show_server_static_mappings --family inet6
+
+
+
+ Show DHCPv6 server static mappings for a specific pool
+
+ service dhcp-server shared-network-name
+
+
+ ${vyos_op_scripts_dir}/dhcp.py show_server_static_mappings --family inet6 --pool $6
+
+
+
+ Show DHCPv6 server static mappings sorted by the specified key
+
+ ip mac duid pool
+
+
+ ${vyos_op_scripts_dir}/dhcp.py show_server_static_mappings --family inet6 --sort $6
+
+
+
diff --git a/src/op_mode/dhcp.py b/src/op_mode/dhcp.py
index 08e46880e..1d9ad0e76 100755
--- a/src/op_mode/dhcp.py
+++ b/src/op_mode/dhcp.py
@@ -39,6 +39,7 @@ config = ConfigTreeQuery()
lease_valid_states = ['all', 'active', 'free', 'expired', 'released', 'abandoned', 'reset', 'backup']
sort_valid_inet = ['end', 'mac', 'hostname', 'ip', 'pool', 'remaining', 'start', 'state']
sort_valid_inet6 = ['end', 'duid', 'ip', 'last_communication', 'pool', 'remaining', 'state', 'type']
+mapping_sort_valid = ['mac', 'ip', 'pool', 'duid']
ArgFamily = typing.Literal['inet', 'inet6']
ArgState = typing.Literal['all', 'active', 'free', 'expired', 'released', 'abandoned', 'reset', 'backup']
@@ -249,6 +250,47 @@ def _get_formatted_pool_statistics(pool_data, family='inet'):
output = tabulate(data_entries, headers, numalign='left')
return output
+def _get_raw_server_static_mappings(family='inet', pool=None, sorted=None):
+ if pool is None:
+ pool = _get_dhcp_pools(family=family)
+ else:
+ pool = [pool]
+
+ v = 'v6' if family == 'inet6' else ''
+ mappings = []
+ for p in pool:
+ pool_config = config.get_config_dict(['service', f'dhcp{v}-server', 'shared-network-name', p],
+ get_first_key=True)
+ if 'subnet' in pool_config:
+ for subnet, subnet_config in pool_config['subnet'].items():
+ if 'static-mapping' in subnet_config:
+ for name, mapping_config in subnet_config['static-mapping'].items():
+ mapping = {'pool': p, 'subnet': subnet, 'name': name}
+ mapping.update(mapping_config)
+ mappings.append(mapping)
+
+ if sorted:
+ if sorted == 'ip':
+ data.sort(key = lambda x:ip_address(x['ip-address']))
+ else:
+ data.sort(key = lambda x:x[sorted])
+ return mappings
+
+def _get_formatted_server_static_mappings(raw_data, family='inet'):
+ data_entries = []
+ for entry in raw_data:
+ pool = entry.get('pool')
+ subnet = entry.get('subnet')
+ name = entry.get('name')
+ ip_addr = entry.get('ip-address', 'N/A')
+ mac_addr = entry.get('mac', 'N/A')
+ duid = entry.get('duid', 'N/A')
+ description = entry.get('description', 'N/A')
+ data_entries.append([pool, subnet, name, ip_addr, mac_addr, duid, description])
+
+ headers = ['Pool', 'Subnet', 'Name', 'IP Address', 'MAC Address', 'DUID', 'Description']
+ output = tabulate(data_entries, headers, numalign='left')
+ return output
def _verify(func):
"""Decorator checks if DHCP(v6) config exists"""
@@ -302,6 +344,21 @@ def show_server_leases(raw: bool, family: ArgFamily, pool: typing.Optional[str],
else:
return _get_formatted_server_leases(lease_data, family=family)
+@_verify
+def show_server_static_mappings(raw: bool, family: ArgFamily, pool: typing.Optional[str],
+ sorted: typing.Optional[str]):
+ v = 'v6' if family == 'inet6' else ''
+ if pool and pool not in _get_dhcp_pools(family=family):
+ raise vyos.opmode.IncorrectValue(f'DHCP{v} pool "{pool}" does not exist!')
+
+ if sorted and sorted not in mapping_sort_valid:
+ raise vyos.opmode.IncorrectValue(f'DHCP{v} sort "{sorted}" is invalid!')
+
+ static_mappings = _get_raw_server_static_mappings(family=family, pool=pool, sorted=sorted)
+ if raw:
+ return static_mappings
+ else:
+ return _get_formatted_server_static_mappings(static_mappings, family=family)
def _get_raw_client_leases(family='inet', interface=None):
from time import mktime
--
cgit v1.2.3