From 02cc0fa95c01b3f7f4df4e15e9dc5b53dee913b9 Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Wed, 13 Mar 2024 01:50:16 +0000 Subject: [PATCH 01/11] Fix to use IPv6 linklocal address as snmp agent address Signed-off-by: Suvarna Meenakshi --- config/main.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/config/main.py b/config/main.py index cef96647e9..70a091097f 100644 --- a/config/main.py +++ b/config/main.py @@ -3250,7 +3250,13 @@ def add_snmp_agent_address(ctx, agentip, port, vrf): ipaddresses = netifaces.ifaddresses(intf) if ip_family[ip.version] in ipaddresses: for ipaddr in ipaddresses[ip_family[ip.version]]: - if agentip.lower() == ipaddr['addr'].lower(): + # link local address will have scope along with ip address + # for ex fe80::1%eth0 + if agentip.lower() == ipaddr['addr'].lower().split('%')[0]: + agent_address = ipaddress.ip_address(agentip) + if agent_address.version == 6 and agent_address.is_link_local: + # add scope id for link local ip address + agentip = agentip + '%' + intf found = 1 break if found == 1: From bbde4a4bfe1f76c61f64631af5bdae9afe5da836 Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Mon, 20 May 2024 15:59:34 +0000 Subject: [PATCH 02/11] Add unit-test Signed-off-by: Suvarna Meenakshi --- tests/config_snmp_test.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/config_snmp_test.py b/tests/config_snmp_test.py index 76f5675690..de3a091373 100644 --- a/tests/config_snmp_test.py +++ b/tests/config_snmp_test.py @@ -877,6 +877,19 @@ def test_config_snmp_community_add_new_community_with_invalid_type_yang_validati assert result.exit_code != 0 assert 'SNMP community configuration failed' in result.output + @patch('netifaces.interfaces', mock.Mock(return_value=['eth0'])) + @patch('netifaces.ifaddresses', mock.Mock(return_value={2: [{'addr': '10.1.0.32', 'netmask': '255.255.255.0', 'broadcast': '10.1.0.255'}], 10: [{'addr': 'fe80::1', 'netmask': 'ffff:ffff:ffff:ffff::/64'}]})) + @patch('os.system', mock.Mock(return_value=0)) + def test_config_snmpagentaddress_add_linklocal(self): + db = Db() + obj = {'db':db.cfgdb} + runner = CliRunner() + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["snmpagentaddress"].commands["add"], ["fe80::1"], obj=obj) + print(result.exit_code) + assert result.exit_code == 0 + assert db.cfgdb.get_entry("SNMP_AGENT_ADDRESS_CONFIG", "fe80::1%eth0|161|") == {} + @classmethod def teardown_class(cls): print("TEARDOWN") From fefb6421a7fca94f97818046a0c278ce87a9991b Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Wed, 22 May 2024 00:23:50 +0000 Subject: [PATCH 03/11] fix ut Signed-off-by: Suvarna Meenakshi --- tests/config_snmp_test.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/config_snmp_test.py b/tests/config_snmp_test.py index de3a091373..c949211b62 100644 --- a/tests/config_snmp_test.py +++ b/tests/config_snmp_test.py @@ -884,11 +884,9 @@ def test_config_snmpagentaddress_add_linklocal(self): db = Db() obj = {'db':db.cfgdb} runner = CliRunner() - with mock.patch('utilities_common.cli.run_command') as mock_run_command: - result = runner.invoke(config.config.commands["snmpagentaddress"].commands["add"], ["fe80::1"], obj=obj) - print(result.exit_code) - assert result.exit_code == 0 - assert db.cfgdb.get_entry("SNMP_AGENT_ADDRESS_CONFIG", "fe80::1%eth0|161|") == {} + runner.invoke(config.config.commands["snmpagentaddress"].commands["add"], ["fe80::1"], obj=obj) + assert ('fe80::1%eth0', '', '') in db.cfgdb.get_keys('SNMP_AGENT_ADDRESS_CONFIG') + assert db.cfgdb.get_entry("SNMP_AGENT_ADDRESS_CONFIG", "fe80::1%eth0||") == {} @classmethod def teardown_class(cls): From 4b52448cff6f35e7a3699c0be702556713df2a7f Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Wed, 22 May 2024 00:30:24 +0000 Subject: [PATCH 04/11] Fix flake8 Signed-off-by: Suvarna Meenakshi --- tests/config_snmp_test.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/config_snmp_test.py b/tests/config_snmp_test.py index c949211b62..c9706e00e8 100644 --- a/tests/config_snmp_test.py +++ b/tests/config_snmp_test.py @@ -878,11 +878,13 @@ def test_config_snmp_community_add_new_community_with_invalid_type_yang_validati assert 'SNMP community configuration failed' in result.output @patch('netifaces.interfaces', mock.Mock(return_value=['eth0'])) - @patch('netifaces.ifaddresses', mock.Mock(return_value={2: [{'addr': '10.1.0.32', 'netmask': '255.255.255.0', 'broadcast': '10.1.0.255'}], 10: [{'addr': 'fe80::1', 'netmask': 'ffff:ffff:ffff:ffff::/64'}]})) + @patch('netifaces.ifaddresses', mock.Mock(return_value={2:\ + [{'addr': '10.1.0.32', 'netmask': '255.255.255.0', 'broadcast': '10.1.0.255'}], \ + 10: [{'addr': 'fe80::1', 'netmask': 'ffff:ffff:ffff:ffff::/64'}]})) @patch('os.system', mock.Mock(return_value=0)) def test_config_snmpagentaddress_add_linklocal(self): db = Db() - obj = {'db':db.cfgdb} + obj = {'db': db.cfgdb} runner = CliRunner() runner.invoke(config.config.commands["snmpagentaddress"].commands["add"], ["fe80::1"], obj=obj) assert ('fe80::1%eth0', '', '') in db.cfgdb.get_keys('SNMP_AGENT_ADDRESS_CONFIG') From 33bc656963f5d25767f64b810d14d64fd2626ce1 Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Wed, 22 May 2024 00:57:34 +0000 Subject: [PATCH 05/11] fix flake8 Signed-off-by: Suvarna Meenakshi --- tests/config_snmp_test.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/config_snmp_test.py b/tests/config_snmp_test.py index c9706e00e8..f4ededd3ba 100644 --- a/tests/config_snmp_test.py +++ b/tests/config_snmp_test.py @@ -878,9 +878,10 @@ def test_config_snmp_community_add_new_community_with_invalid_type_yang_validati assert 'SNMP community configuration failed' in result.output @patch('netifaces.interfaces', mock.Mock(return_value=['eth0'])) - @patch('netifaces.ifaddresses', mock.Mock(return_value={2:\ - [{'addr': '10.1.0.32', 'netmask': '255.255.255.0', 'broadcast': '10.1.0.255'}], \ - 10: [{'addr': 'fe80::1', 'netmask': 'ffff:ffff:ffff:ffff::/64'}]})) + @patch('netifaces.ifaddresses', mock.Mock(return_value={2: + [{'addr': '10.1.0.32', 'netmask': '255.255.255.0', + 'broadcast': '10.1.0.255'}], + 10: [{'addr': 'fe80::1', 'netmask': 'ffff:ffff:ffff:ffff::/64'}]})) @patch('os.system', mock.Mock(return_value=0)) def test_config_snmpagentaddress_add_linklocal(self): db = Db() From f56f61ab0df6bea01f81d8e247b2e4162350ed10 Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Thu, 23 May 2024 17:00:20 +0000 Subject: [PATCH 06/11] Modify cli to add agent ip along with zone id for link local ip Signed-off-by: Suvarna Meenakshi --- config/main.py | 14 +++++--------- tests/config_snmp_test.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/config/main.py b/config/main.py index 70a091097f..5efc87a47d 100644 --- a/config/main.py +++ b/config/main.py @@ -3235,7 +3235,9 @@ def add_snmp_agent_address(ctx, agentip, port, vrf): """Add the SNMP agent listening IP:Port%Vrf configuration""" #Construct SNMP_AGENT_ADDRESS_CONFIG table key in the format ip|| - if not clicommon.is_ipaddress(agentip): + # Link local IP address should be provided along with zone id + # % for ex fe80::1%eth0 + if not clicommon.is_ipaddress(agentip.split('%')[0]): click.echo("Invalid IP address") return False config_db = ctx.obj['db'] @@ -3245,18 +3247,12 @@ def add_snmp_agent_address(ctx, agentip, port, vrf): click.echo("ManagementVRF is Enabled. Provide vrf.") return False found = 0 - ip = ipaddress.ip_address(agentip) + ip = ipaddress.ip_address(agentip.split('%')[0]) for intf in netifaces.interfaces(): ipaddresses = netifaces.ifaddresses(intf) if ip_family[ip.version] in ipaddresses: for ipaddr in ipaddresses[ip_family[ip.version]]: - # link local address will have scope along with ip address - # for ex fe80::1%eth0 - if agentip.lower() == ipaddr['addr'].lower().split('%')[0]: - agent_address = ipaddress.ip_address(agentip) - if agent_address.version == 6 and agent_address.is_link_local: - # add scope id for link local ip address - agentip = agentip + '%' + intf + if agentip.lower() == ipaddr['addr'].lower(): found = 1 break if found == 1: diff --git a/tests/config_snmp_test.py b/tests/config_snmp_test.py index f4ededd3ba..93d1a7cf6b 100644 --- a/tests/config_snmp_test.py +++ b/tests/config_snmp_test.py @@ -891,6 +891,20 @@ def test_config_snmpagentaddress_add_linklocal(self): assert ('fe80::1%eth0', '', '') in db.cfgdb.get_keys('SNMP_AGENT_ADDRESS_CONFIG') assert db.cfgdb.get_entry("SNMP_AGENT_ADDRESS_CONFIG", "fe80::1%eth0||") == {} + @patch('netifaces.interfaces', mock.Mock(return_value=['eth0'])) + @patch('netifaces.ifaddresses', mock.Mock(return_value={2: + [{'addr': '10.1.0.32', 'netmask': '255.255.255.0', + 'broadcast': '10.1.0.255'}], + 10: [{'addr': 'fe80::1', 'netmask': 'ffff:ffff:ffff:ffff::/64'}]})) + @patch('os.system', mock.Mock(return_value=0)) + def test_config_snmpagentaddress_add_ipv4(self): + db = Db() + obj = {'db': db.cfgdb} + runner = CliRunner() + runner.invoke(config.config.commands["snmpagentaddress"].commands["add"], ["10.1.0.32"], obj=obj) + assert ('10.1.0.32', '', '') in db.cfgdb.get_keys('SNMP_AGENT_ADDRESS_CONFIG') + assert db.cfgdb.get_entry("SNMP_AGENT_ADDRESS_CONFIG", "i10.1.0.32||") == {} + @classmethod def teardown_class(cls): print("TEARDOWN") From 6d7f5323eef2b2bf3ab5c11a5f3a819679ee3a31 Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Thu, 30 May 2024 19:47:19 +0000 Subject: [PATCH 07/11] Fix to add zone id in config command Signed-off-by: Suvarna Meenakshi --- tests/config_snmp_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/config_snmp_test.py b/tests/config_snmp_test.py index 93d1a7cf6b..379a533d4a 100644 --- a/tests/config_snmp_test.py +++ b/tests/config_snmp_test.py @@ -887,7 +887,7 @@ def test_config_snmpagentaddress_add_linklocal(self): db = Db() obj = {'db': db.cfgdb} runner = CliRunner() - runner.invoke(config.config.commands["snmpagentaddress"].commands["add"], ["fe80::1"], obj=obj) + runner.invoke(config.config.commands["snmpagentaddress"].commands["add"], ["fe80::1%eth0"], obj=obj) assert ('fe80::1%eth0', '', '') in db.cfgdb.get_keys('SNMP_AGENT_ADDRESS_CONFIG') assert db.cfgdb.get_entry("SNMP_AGENT_ADDRESS_CONFIG", "fe80::1%eth0||") == {} From 68fc8eaf3a290f47759f1cb3c23c2fc8670df2f4 Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Fri, 31 May 2024 23:11:00 +0000 Subject: [PATCH 08/11] Update as per review comment Signed-off-by: Suvarna Meenakshi --- config/main.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/config/main.py b/config/main.py index 5efc87a47d..31cacad5af 100644 --- a/config/main.py +++ b/config/main.py @@ -3237,7 +3237,8 @@ def add_snmp_agent_address(ctx, agentip, port, vrf): #Construct SNMP_AGENT_ADDRESS_CONFIG table key in the format ip|| # Link local IP address should be provided along with zone id # % for ex fe80::1%eth0 - if not clicommon.is_ipaddress(agentip.split('%')[0]): + ip_addr = agentip.split('%')[0] + if not clicommon.is_ipaddress(ip_addr): click.echo("Invalid IP address") return False config_db = ctx.obj['db'] @@ -3247,7 +3248,7 @@ def add_snmp_agent_address(ctx, agentip, port, vrf): click.echo("ManagementVRF is Enabled. Provide vrf.") return False found = 0 - ip = ipaddress.ip_address(agentip.split('%')[0]) + ip = ipaddress.ip_address(ip_addr) for intf in netifaces.interfaces(): ipaddresses = netifaces.ifaddresses(intf) if ip_family[ip.version] in ipaddresses: From f8db6d4f70e29aec69b3b7057c19cb1f6c212163 Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Mon, 29 Jul 2024 15:20:30 +0000 Subject: [PATCH 09/11] Fix unit test Signed-off-by: Suvarna Meenakshi --- tests/config_snmp_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/config_snmp_test.py b/tests/config_snmp_test.py index 379a533d4a..5058dfa84a 100644 --- a/tests/config_snmp_test.py +++ b/tests/config_snmp_test.py @@ -903,7 +903,7 @@ def test_config_snmpagentaddress_add_ipv4(self): runner = CliRunner() runner.invoke(config.config.commands["snmpagentaddress"].commands["add"], ["10.1.0.32"], obj=obj) assert ('10.1.0.32', '', '') in db.cfgdb.get_keys('SNMP_AGENT_ADDRESS_CONFIG') - assert db.cfgdb.get_entry("SNMP_AGENT_ADDRESS_CONFIG", "i10.1.0.32||") == {} + assert db.cfgdb.get_entry("SNMP_AGENT_ADDRESS_CONFIG", "10.1.0.32||") == {} @classmethod def teardown_class(cls): From 709b9d6537bad49bb43793a8e0424c4fcbc671e1 Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Mon, 29 Jul 2024 16:24:17 +0000 Subject: [PATCH 10/11] Fix check to match interface ip and agent ip being added using cli Signed-off-by: Suvarna Meenakshi --- config/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/main.py b/config/main.py index 31cacad5af..f5bd8b1786 100644 --- a/config/main.py +++ b/config/main.py @@ -3237,8 +3237,8 @@ def add_snmp_agent_address(ctx, agentip, port, vrf): #Construct SNMP_AGENT_ADDRESS_CONFIG table key in the format ip|| # Link local IP address should be provided along with zone id # % for ex fe80::1%eth0 - ip_addr = agentip.split('%')[0] - if not clicommon.is_ipaddress(ip_addr): + agent_ip_addr = agentip.split('%')[0] + if not clicommon.is_ipaddress(agent_ip_addr): click.echo("Invalid IP address") return False config_db = ctx.obj['db'] @@ -3248,12 +3248,12 @@ def add_snmp_agent_address(ctx, agentip, port, vrf): click.echo("ManagementVRF is Enabled. Provide vrf.") return False found = 0 - ip = ipaddress.ip_address(ip_addr) + ip = ipaddress.ip_address(agent_ip_addr) for intf in netifaces.interfaces(): ipaddresses = netifaces.ifaddresses(intf) if ip_family[ip.version] in ipaddresses: for ipaddr in ipaddresses[ip_family[ip.version]]: - if agentip.lower() == ipaddr['addr'].lower(): + if agent_ip_addr.lower() == ipaddr['addr'].lower(): found = 1 break if found == 1: From 214b222f66f51b6c4ea76197025156195dddb10a Mon Sep 17 00:00:00 2001 From: Suvarna Meenakshi Date: Fri, 2 Aug 2024 22:18:11 +0000 Subject: [PATCH 11/11] netifaces provides ipadress along with zoneid Signed-off-by: Suvarna Meenakshi --- config/main.py | 2 +- tests/config_snmp_test.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/main.py b/config/main.py index f5bd8b1786..bbf58214be 100644 --- a/config/main.py +++ b/config/main.py @@ -3253,7 +3253,7 @@ def add_snmp_agent_address(ctx, agentip, port, vrf): ipaddresses = netifaces.ifaddresses(intf) if ip_family[ip.version] in ipaddresses: for ipaddr in ipaddresses[ip_family[ip.version]]: - if agent_ip_addr.lower() == ipaddr['addr'].lower(): + if agentip.lower() == ipaddr['addr'].lower(): found = 1 break if found == 1: diff --git a/tests/config_snmp_test.py b/tests/config_snmp_test.py index 5058dfa84a..25c54d36ec 100644 --- a/tests/config_snmp_test.py +++ b/tests/config_snmp_test.py @@ -881,7 +881,7 @@ def test_config_snmp_community_add_new_community_with_invalid_type_yang_validati @patch('netifaces.ifaddresses', mock.Mock(return_value={2: [{'addr': '10.1.0.32', 'netmask': '255.255.255.0', 'broadcast': '10.1.0.255'}], - 10: [{'addr': 'fe80::1', 'netmask': 'ffff:ffff:ffff:ffff::/64'}]})) + 10: [{'addr': 'fe80::1%eth0', 'netmask': 'ffff:ffff:ffff:ffff::/64'}]})) @patch('os.system', mock.Mock(return_value=0)) def test_config_snmpagentaddress_add_linklocal(self): db = Db()