From cbc25c0dad7058d30e7d4e11b21fb632f7131ffe Mon Sep 17 00:00:00 2001 From: Oleksandr Ivantsiv Date: Fri, 31 May 2024 19:54:52 +0000 Subject: [PATCH] [dhcp_server] Fix the issue with "kea-dhcp4.conf" file generation for Smart Switch. --- .../dhcp_utilities/dhcpservd/dhcp_cfggen.py | 7 ++++--- src/sonic-dhcp-utilities/tests/test_data/kea-dhcp4.conf.j2 | 1 + src/sonic-dhcp-utilities/tests/test_dhcp_cfggen.py | 1 + src/sonic-dhcp-utilities/tests/test_smart_switch.py | 1 + 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/sonic-dhcp-utilities/dhcp_utilities/dhcpservd/dhcp_cfggen.py b/src/sonic-dhcp-utilities/dhcp_utilities/dhcpservd/dhcp_cfggen.py index eb7ae34d852f..c79d6225d04f 100755 --- a/src/sonic-dhcp-utilities/dhcp_utilities/dhcpservd/dhcp_cfggen.py +++ b/src/sonic-dhcp-utilities/dhcp_utilities/dhcpservd/dhcp_cfggen.py @@ -90,7 +90,7 @@ def generate(self): port_ips, used_ranges = self._parse_port(port_ipv4, dhcp_interfaces, dhcp_members, ranges) customized_options = self._parse_customized_options(customized_options_ipv4) render_obj, enabled_dhcp_interfaces, used_options, subscribe_table = \ - self._construct_obj_for_template(dhcp_server_ipv4, port_ips, hostname, customized_options) + self._construct_obj_for_template(dhcp_server_ipv4, port_ips, hostname, customized_options, smart_switch) if smart_switch: subscribe_table |= set(SMART_SWITCH_CHECKER) @@ -175,7 +175,7 @@ def _parse_port_map_alias(self): for pc_name in pc_table.keys(): self.port_alias_map[pc_name] = pc_name - def _construct_obj_for_template(self, dhcp_server_ipv4, port_ips, hostname, customized_options): + def _construct_obj_for_template(self, dhcp_server_ipv4, port_ips, hostname, customized_options, smart_switch=False): subnets = [] client_classes = [] enabled_dhcp_interfaces = set() @@ -223,8 +223,9 @@ def _construct_obj_for_template(self, dhcp_server_ipv4, port_ips, hostname, cust "condition": "substring(relay4[1].hex, -{}, {}) == '{}'".format(class_len, class_len, client_class) }) + subnet_obj = { - "id": dhcp_interface_name.replace("Vlan", ""), + "id": 0 if smart_switch else dhcp_interface_name.replace("Vlan", ""), "subnet": str(ipaddress.ip_network(dhcp_interface_ip, strict=False)), "pools": pools, "gateway": dhcp_config["gateway"], diff --git a/src/sonic-dhcp-utilities/tests/test_data/kea-dhcp4.conf.j2 b/src/sonic-dhcp-utilities/tests/test_data/kea-dhcp4.conf.j2 index 082cbca14dd5..f5f7a9b054f0 100644 --- a/src/sonic-dhcp-utilities/tests/test_data/kea-dhcp4.conf.j2 +++ b/src/sonic-dhcp-utilities/tests/test_data/kea-dhcp4.conf.j2 @@ -42,6 +42,7 @@ {%- if add_subnet_preceding_comma.flag -%},{%- endif -%} {%- set _dummy = add_subnet_preceding_comma.update({'flag': True}) %} { + "id": {{ subnet_info["id"] }}, "subnet": "{{ subnet_info["subnet"] }}", "pools": [ {%- set add_pool_preceding_comma = { 'flag': False } %} diff --git a/src/sonic-dhcp-utilities/tests/test_dhcp_cfggen.py b/src/sonic-dhcp-utilities/tests/test_dhcp_cfggen.py index 723731839649..3548e39c1435 100644 --- a/src/sonic-dhcp-utilities/tests/test_dhcp_cfggen.py +++ b/src/sonic-dhcp-utilities/tests/test_dhcp_cfggen.py @@ -42,6 +42,7 @@ }, "subnet4": [ { + "id": 1000, "subnet": "192.168.0.0/21", "pools": [ { diff --git a/src/sonic-dhcp-utilities/tests/test_smart_switch.py b/src/sonic-dhcp-utilities/tests/test_smart_switch.py index 0347d5df2f6d..f8de9868b3d0 100644 --- a/src/sonic-dhcp-utilities/tests/test_smart_switch.py +++ b/src/sonic-dhcp-utilities/tests/test_smart_switch.py @@ -35,6 +35,7 @@ }, "subnet4": [ { + "id": 0, "subnet": "169.254.200.0/24", "pools": [ {