Skip to content

Commit

Permalink
[dhcp_server] Remove dependency in port-name-alias-map.txt.j2 (#17858)
Browse files Browse the repository at this point in the history
* [dhcp_server] Remove dependency in port-name-alias-map.txt.j2
  • Loading branch information
yaqiangz authored and rajib-dutta1 committed Feb 9, 2024
1 parent 5a7facb commit c74baf9
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dockers/docker-dhcp-server/Dockerfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ RUN apt-get clean -y && \
COPY ["docker_init.sh", "start.sh", "/usr/bin/"]
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
COPY ["files/supervisor-proc-exit-listener", "/usr/bin"]
COPY ["port-name-alias-map.txt.j2", "rsyslog/rsyslog.conf.j2", "kea-dhcp4.conf.j2", "/usr/share/sonic/templates/"]
COPY ["rsyslog/rsyslog.conf.j2", "kea-dhcp4.conf.j2", "/usr/share/sonic/templates/"]
COPY ["critical_processes", "/etc/supervisor/"]
COPY ["lease_update.sh", "/etc/kea/"]
COPY ["kea-dhcp4-init.conf", "/etc/kea/kea-dhcp4.conf"]
Expand Down
1 change: 0 additions & 1 deletion dockers/docker-dhcp-server/docker_init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ hostname=$(hostname)
sonic-cfggen -d -t /usr/share/sonic/templates/rsyslog.conf.j2 \
-a "{\"udp_server_ip\": \"$udp_server_ip\", \"hostname\": \"$hostname\"}" \
> /etc/rsyslog.conf
sonic-cfggen -d -t /usr/share/sonic/templates/port-name-alias-map.txt.j2,/tmp/port-name-alias-map.txt

# Make the script that waits for all interfaces to come up executable
chmod +x /etc/kea/lease_update.sh /usr/bin/start.sh
Expand Down
8 changes: 0 additions & 8 deletions dockers/docker-dhcp-server/port-name-alias-map.txt.j2

This file was deleted.

20 changes: 9 additions & 11 deletions src/sonic-dhcp-utilities/dhcp_utilities/dhcpservd/dhcp_cfggen.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from jinja2 import Environment, FileSystemLoader
from dhcp_utilities.common.utils import merge_intervals, validate_str_type, is_smart_switch

PORT_MAP_PATH = "/tmp/port-name-alias-map.txt"
UNICODE_TYPE = str
DHCP_SERVER_IPV4 = "DHCP_SERVER_IPV4"
DHCP_SERVER_IPV4_CUSTOMIZED_OPTIONS = "DHCP_SERVER_IPV4_CUSTOMIZED_OPTIONS"
Expand Down Expand Up @@ -35,14 +34,14 @@ class DhcpServCfgGenerator(object):
lease_update_script_path = ""
lease_path = ""

def __init__(self, dhcp_db_connector, lease_path=DEFAULT_LEASE_PATH, port_map_path=PORT_MAP_PATH,
def __init__(self, dhcp_db_connector, lease_path=DEFAULT_LEASE_PATH,
lease_update_script_path=LEASE_UPDATE_SCRIPT_PATH, dhcp_option_path=DHCP_OPTION_FILE,
kea_conf_template_path=KEA_DHCP4_CONF_TEMPLATE_PATH):
self.db_connector = dhcp_db_connector
self.lease_path = lease_path
self.lease_update_script_path = lease_update_script_path
# Read port alias map file, this file is render after container start, so it would not change any more
self._parse_port_map_alias(port_map_path)
self._parse_port_map_alias()
# Get kea config template
self._get_render_template(kea_conf_template_path)
self._read_dhcp_option(dhcp_option_path)
Expand Down Expand Up @@ -166,14 +165,13 @@ def _get_render_template(self, kea_conf_template_path):
env = Environment(loader=FileSystemLoader(os.path.dirname(kea_conf_template_path))) # nosemgrep
self.kea_template = env.get_template(os.path.basename(kea_conf_template_path))

def _parse_port_map_alias(self, port_map_path):
with open(port_map_path, "r") as file:
lines = file.readlines()
for line in lines:
splits = line.strip().split(" ")
if len(splits) != 2:
continue
self.port_alias_map[splits[0]] = splits[1]
def _parse_port_map_alias(self):
port_table = self.db_connector.get_config_db_table("PORT")
pc_table = self.db_connector.get_config_db_table("PORTCHANNEL")
for port_name, item in port_table.items():
self.port_alias_map[port_name] = item.get("alias", port_name)
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):
subnets = []
Expand Down
33 changes: 33 additions & 0 deletions src/sonic-dhcp-utilities/tests/test_data/mock_config_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,38 @@
"192.168.0.10"
]
}
},
"PORT": {
"Ethernet0": {
"admin_status": "up",
"alias": "etp1",
"description": "Servers0:eth0",
"index": "1",
"lanes": "25",
"mtu": "9100",
"pfc_asym": "off",
"speed": "1000",
"tpid": "0x8100"
},
"Ethernet1": {
"admin_status": "up",
"alias": "etp2",
"description": "Servers1:eth0",
"index": "2",
"lanes": "26",
"mtu": "9100",
"pfc_asym": "off",
"speed": "1000",
"tpid": "0x8100"
}
},
"PORTCHANNEL": {
"PortChannel101": {
"admin_status": "up",
"lacp_key": "auto",
"min_links": "1",
"mtu": "9100",
"tpid": "0x8100"
}
}
}

This file was deleted.

9 changes: 5 additions & 4 deletions src/sonic-dhcp-utilities/tests/test_dhcp_cfggen.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,11 @@


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


@pytest.mark.parametrize("is_success", [True, False])
Expand Down
4 changes: 2 additions & 2 deletions src/sonic-dhcp-utilities/tests/test_dhcpservd.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def test_dump_dhcp4_config(mock_swsscommon_dbconnector_init, enabled_checker):
new_callable=PropertyMock), \
patch.object(DhcpServdDbMonitor, "disable_checkers") as mock_unsubscribe, \
patch.object(DhcpServdDbMonitor, "enable_checkers") as mock_subscribe, \
patch.object(DhcpServd, "enabled_checker", return_value=enabled_checker, new_callable=PropertyMock):
patch.object(DhcpServd, "enabled_checker", return_value=enabled_checker, new_callable=PropertyMock), \
patch.object(DhcpServCfgGenerator, "_parse_port_map_alias"):
dhcp_db_connector = DhcpDbConnector()
dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector,
port_map_path="tests/test_data/port-name-alias-map.txt",
kea_conf_template_path="tests/test_data/kea-dhcp4.conf.j2")
dhcpservd = DhcpServd(dhcp_cfg_generator, dhcp_db_connector, None,
kea_dhcp4_config_path="/tmp/kea-dhcp4.conf")
Expand Down

0 comments on commit c74baf9

Please sign in to comment.