Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verification IPv4 address on LLDP conf Jinja2 Template #5699

Merged
merged 9 commits into from
Nov 7, 2020
19 changes: 13 additions & 6 deletions dockers/docker-lldp/lldpd.conf.j2
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
{% set mgmt_if = {} %}
{% if MGMT_INTERFACE %}
{% for (mgmt_name, mgmt_prefix) in MGMT_INTERFACE|pfx_filter %}
{% if mgmt_prefix|ipv4 %}
{% if mgmt_if.update({'port_name' : mgmt_name}) %} {% endif %}
{% if mgmt_if.update({'ipv4' : mgmt_prefix|ip}) %} {% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% if mgmt_if %}
{# If MGMT port alias is available, use it for port ID subtype, otherwise use port name #}
{% set mgmt_port_name = (MGMT_INTERFACE.keys()|list)[0][0] %}
{% set ipv4 = (MGMT_INTERFACE.keys()|list)[0][1].split('/')[0] %}
{% if MGMT_PORT and MGMT_PORT[mgmt_port_name] and MGMT_PORT[mgmt_port_name].alias %}
configure ports eth0 lldp portidsubtype local {{ MGMT_PORT[mgmt_port_name].alias }}
{% if MGMT_PORT and MGMT_PORT[mgmt_if.port_name] and MGMT_PORT[mgmt_if.port_name].alias %}
configure ports eth0 lldp portidsubtype local {{ MGMT_PORT[mgmt_if.port_name].alias }}
{% else %}
configure ports eth0 lldp portidsubtype local {{ mgmt_port_name }}
configure ports eth0 lldp portidsubtype local {{ mgmt_if.port_name }}
{% endif %}
configure system ip management pattern {{ ipv4 }}
configure system ip management pattern {{ mgmt_if.ipv4 }}
{% endif %}
configure system hostname {{ DEVICE_METADATA['localhost']['hostname'] }}
{# pause lldpd operations until all interfaces are well configured, resume command will run in lldpmgrd #}
Expand Down
12 changes: 12 additions & 0 deletions src/sonic-config-engine/tests/data/lldp/mgmt_iface_ipv4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"DEVICE_METADATA": {
"localhost": {
"hostname": "switch-t0"
}
},
"MGMT_INTERFACE": {
"eth0|10.0.0.100/24": {
"gwaddr": "10.0.0.100"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"DEVICE_METADATA": {
"localhost": {
"hostname": "switch-t0"
}
},
"MGMT_INTERFACE": {
"eth0|10.0.0.100/24": {
"gwaddr": "10.0.0.100"
},
"eth0|2603:10e2:0:2902::8/64": {
"gwaddr": "2603:10e2:0:2902::8"
}
}
}
12 changes: 12 additions & 0 deletions src/sonic-config-engine/tests/data/lldp/mgmt_iface_ipv6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"DEVICE_METADATA": {
"localhost": {
"hostname": "switch-t0"
}
},
"MGMT_INTERFACE": {
"eth0|2603:10e2:0:2902::8/64": {
"gwaddr": "2603:10e2:0:2902::8"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
configure system hostname switch-t0
pause
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
configure system hostname switch-t0
pause
23 changes: 21 additions & 2 deletions src/sonic-config-engine/tests/test_j2files.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,28 @@ def test_dhcp_relay(self):

def test_lldp(self):
lldpd_conf_template = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-lldp', 'lldpd.conf.j2')
argument = '-m ' + self.t0_minigraph + ' -p ' + self.t0_port_config + ' -t ' + lldpd_conf_template + ' > ' + self.output_file

expected_mgmt_ipv4 = os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'lldp_conf', 'lldpd-ipv4-iface.conf')
expected_mgmt_ipv6 = os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'lldp_conf', 'lldpd-ipv6-iface.conf')
expected_mgmt_ipv4_and_ipv6 = expected_mgmt_ipv4

# Test generation of lldpd.conf if IPv4 and IPv6 management interfaces exist
mgmt_iface_ipv4_and_ipv6_json = os.path.join(self.test_dir, "data", "lldp", "mgmt_iface_ipv4_and_ipv6.json")
argument = '-j {} -t {} > {}'.format(mgmt_iface_ipv4_and_ipv6_json, lldpd_conf_template, self.output_file)
self.run_script(argument)
self.assertTrue(filecmp.cmp(expected_mgmt_ipv4_and_ipv6, self.output_file))

# Test generation of lldpd.conf if management interface IPv4 only exist
mgmt_iface_ipv4_json = os.path.join(self.test_dir, "data", "lldp", "mgmt_iface_ipv4.json")
argument = '-j {} -t {} > {}'.format(mgmt_iface_ipv4_json, lldpd_conf_template, self.output_file)
self.run_script(argument)
self.assertTrue(filecmp.cmp(expected_mgmt_ipv4, self.output_file))

# Test generation of lldpd.conf if Management interface IPv6 only exist
mgmt_iface_ipv6_json = os.path.join(self.test_dir, "data", "lldp", "mgmt_iface_ipv6.json")
argument = '-j {} -t {} > {}'.format(mgmt_iface_ipv6_json, lldpd_conf_template, self.output_file)
self.run_script(argument)
self.assertTrue(filecmp.cmp(os.path.join(self.test_dir, 'sample_output', utils.PYvX_DIR, 'lldpd.conf'), self.output_file))
self.assertTrue(filecmp.cmp(expected_mgmt_ipv6, self.output_file))

def test_bgpd_quagga(self):
conf_template = os.path.join(self.test_dir, '..', '..', '..', 'dockers', 'docker-fpm-quagga', 'bgpd.conf.j2')
Expand Down