Skip to content

Commit c74baf9

Browse files
yaqiangzrajib-dutta1
authored andcommitted
[dhcp_server] Remove dependency in port-name-alias-map.txt.j2 (#17858)
* [dhcp_server] Remove dependency in port-name-alias-map.txt.j2
1 parent 5a7facb commit c74baf9

File tree

8 files changed

+50
-30
lines changed

8 files changed

+50
-30
lines changed

dockers/docker-dhcp-server/Dockerfile.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ RUN apt-get clean -y && \
8686
COPY ["docker_init.sh", "start.sh", "/usr/bin/"]
8787
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
8888
COPY ["files/supervisor-proc-exit-listener", "/usr/bin"]
89-
COPY ["port-name-alias-map.txt.j2", "rsyslog/rsyslog.conf.j2", "kea-dhcp4.conf.j2", "/usr/share/sonic/templates/"]
89+
COPY ["rsyslog/rsyslog.conf.j2", "kea-dhcp4.conf.j2", "/usr/share/sonic/templates/"]
9090
COPY ["critical_processes", "/etc/supervisor/"]
9191
COPY ["lease_update.sh", "/etc/kea/"]
9292
COPY ["kea-dhcp4-init.conf", "/etc/kea/kea-dhcp4.conf"]

dockers/docker-dhcp-server/docker_init.sh

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ hostname=$(hostname)
1212
sonic-cfggen -d -t /usr/share/sonic/templates/rsyslog.conf.j2 \
1313
-a "{\"udp_server_ip\": \"$udp_server_ip\", \"hostname\": \"$hostname\"}" \
1414
> /etc/rsyslog.conf
15-
sonic-cfggen -d -t /usr/share/sonic/templates/port-name-alias-map.txt.j2,/tmp/port-name-alias-map.txt
1615

1716
# Make the script that waits for all interfaces to come up executable
1817
chmod +x /etc/kea/lease_update.sh /usr/bin/start.sh

dockers/docker-dhcp-server/port-name-alias-map.txt.j2

-8
This file was deleted.

src/sonic-dhcp-utilities/dhcp_utilities/dhcpservd/dhcp_cfggen.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from jinja2 import Environment, FileSystemLoader
88
from dhcp_utilities.common.utils import merge_intervals, validate_str_type, is_smart_switch
99

10-
PORT_MAP_PATH = "/tmp/port-name-alias-map.txt"
1110
UNICODE_TYPE = str
1211
DHCP_SERVER_IPV4 = "DHCP_SERVER_IPV4"
1312
DHCP_SERVER_IPV4_CUSTOMIZED_OPTIONS = "DHCP_SERVER_IPV4_CUSTOMIZED_OPTIONS"
@@ -35,14 +34,14 @@ class DhcpServCfgGenerator(object):
3534
lease_update_script_path = ""
3635
lease_path = ""
3736

38-
def __init__(self, dhcp_db_connector, lease_path=DEFAULT_LEASE_PATH, port_map_path=PORT_MAP_PATH,
37+
def __init__(self, dhcp_db_connector, lease_path=DEFAULT_LEASE_PATH,
3938
lease_update_script_path=LEASE_UPDATE_SCRIPT_PATH, dhcp_option_path=DHCP_OPTION_FILE,
4039
kea_conf_template_path=KEA_DHCP4_CONF_TEMPLATE_PATH):
4140
self.db_connector = dhcp_db_connector
4241
self.lease_path = lease_path
4342
self.lease_update_script_path = lease_update_script_path
4443
# Read port alias map file, this file is render after container start, so it would not change any more
45-
self._parse_port_map_alias(port_map_path)
44+
self._parse_port_map_alias()
4645
# Get kea config template
4746
self._get_render_template(kea_conf_template_path)
4847
self._read_dhcp_option(dhcp_option_path)
@@ -166,14 +165,13 @@ def _get_render_template(self, kea_conf_template_path):
166165
env = Environment(loader=FileSystemLoader(os.path.dirname(kea_conf_template_path))) # nosemgrep
167166
self.kea_template = env.get_template(os.path.basename(kea_conf_template_path))
168167

169-
def _parse_port_map_alias(self, port_map_path):
170-
with open(port_map_path, "r") as file:
171-
lines = file.readlines()
172-
for line in lines:
173-
splits = line.strip().split(" ")
174-
if len(splits) != 2:
175-
continue
176-
self.port_alias_map[splits[0]] = splits[1]
168+
def _parse_port_map_alias(self):
169+
port_table = self.db_connector.get_config_db_table("PORT")
170+
pc_table = self.db_connector.get_config_db_table("PORTCHANNEL")
171+
for port_name, item in port_table.items():
172+
self.port_alias_map[port_name] = item.get("alias", port_name)
173+
for pc_name in pc_table.keys():
174+
self.port_alias_map[pc_name] = pc_name
177175

178176
def _construct_obj_for_template(self, dhcp_server_ipv4, port_ips, hostname, customized_options):
179177
subnets = []

src/sonic-dhcp-utilities/tests/test_data/mock_config_db.json

+33
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,38 @@
180180
"192.168.0.10"
181181
]
182182
}
183+
},
184+
"PORT": {
185+
"Ethernet0": {
186+
"admin_status": "up",
187+
"alias": "etp1",
188+
"description": "Servers0:eth0",
189+
"index": "1",
190+
"lanes": "25",
191+
"mtu": "9100",
192+
"pfc_asym": "off",
193+
"speed": "1000",
194+
"tpid": "0x8100"
195+
},
196+
"Ethernet1": {
197+
"admin_status": "up",
198+
"alias": "etp2",
199+
"description": "Servers1:eth0",
200+
"index": "2",
201+
"lanes": "26",
202+
"mtu": "9100",
203+
"pfc_asym": "off",
204+
"speed": "1000",
205+
"tpid": "0x8100"
206+
}
207+
},
208+
"PORTCHANNEL": {
209+
"PortChannel101": {
210+
"admin_status": "up",
211+
"lacp_key": "auto",
212+
"min_links": "1",
213+
"mtu": "9100",
214+
"tpid": "0x8100"
215+
}
183216
}
184217
}

src/sonic-dhcp-utilities/tests/test_data/port-name-alias-map.txt

-3
This file was deleted.

src/sonic-dhcp-utilities/tests/test_dhcp_cfggen.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,11 @@
268268

269269

270270
def test_parse_port_alias(mock_swsscommon_dbconnector_init, mock_get_render_template):
271-
dhcp_db_connector = DhcpDbConnector()
272-
dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector,
273-
port_map_path="tests/test_data/port-name-alias-map.txt")
274-
assert dhcp_cfg_generator.port_alias_map == {"Ethernet24": "etp7", "Ethernet28": "etp8"}
271+
with patch.object(DhcpDbConnector, "get_config_db_table", side_effect=mock_get_config_db_table):
272+
dhcp_db_connector = DhcpDbConnector()
273+
dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector)
274+
assert dhcp_cfg_generator.port_alias_map == {"Ethernet0": "etp1", "Ethernet1": "etp2",
275+
"PortChannel101": "PortChannel101"}
275276

276277

277278
@pytest.mark.parametrize("is_success", [True, False])

src/sonic-dhcp-utilities/tests/test_dhcpservd.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def test_dump_dhcp4_config(mock_swsscommon_dbconnector_init, enabled_checker):
2929
new_callable=PropertyMock), \
3030
patch.object(DhcpServdDbMonitor, "disable_checkers") as mock_unsubscribe, \
3131
patch.object(DhcpServdDbMonitor, "enable_checkers") as mock_subscribe, \
32-
patch.object(DhcpServd, "enabled_checker", return_value=enabled_checker, new_callable=PropertyMock):
32+
patch.object(DhcpServd, "enabled_checker", return_value=enabled_checker, new_callable=PropertyMock), \
33+
patch.object(DhcpServCfgGenerator, "_parse_port_map_alias"):
3334
dhcp_db_connector = DhcpDbConnector()
3435
dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector,
35-
port_map_path="tests/test_data/port-name-alias-map.txt",
3636
kea_conf_template_path="tests/test_data/kea-dhcp4.conf.j2")
3737
dhcpservd = DhcpServd(dhcp_cfg_generator, dhcp_db_connector, None,
3838
kea_dhcp4_config_path="/tmp/kea-dhcp4.conf")

0 commit comments

Comments
 (0)