-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from cherifimehdi/master
Add verify.py for iosxe eigrp folder containing 02 APIs for EIGRPv4 and EIGRPv6
- Loading branch information
Showing
6 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
pkgs/sdk-pkg/changelog/changelog_add_eigrp_verify.py_apis_202405031904.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-------------------------------------------------------------------------------- | ||
New | ||
-------------------------------------------------------------------------------- | ||
* IOSXE | ||
* Added verify.py in eigrp containing 02 APIs supporting IPv4 and IPv6: | ||
* verify_eigrp_interfaces | ||
* verify_eigrp_neighbors |
124 changes: 124 additions & 0 deletions
124
pkgs/sdk-pkg/src/genie/libs/sdk/apis/iosxe/eigrp/verify.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
from genie.utils.timeout import Timeout | ||
import logging | ||
import os | ||
from genie.metaparser.util.exceptions import SchemaEmptyParserError | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def verify_eigrp_interfaces( | ||
device, | ||
vrf="default", | ||
AS_N=None, | ||
interfaces_list=None, | ||
ip="ipv4", | ||
max_time=60, | ||
check_interval=10, | ||
): | ||
"""Verify active EIGRP interfaces for ipv4 (Default) or ipv6 for AS and VRF | ||
Args: | ||
device (obj): Device object | ||
vrf = "default" (str): Name of the vrf by default set to "default" | ||
AS_N = None (int): Autonomous System | ||
interfaces_list = None (list): List of active EIGRP interfaces to check | ||
ip = "ipv4" (str): Protocol ip set by default to "ipv4" to change to "ipv6" | ||
max_time (`int`): Max time, default: 30 | ||
check_interval (`int`): Check interval, default: 10 | ||
Returns: | ||
True | ||
False | ||
""" | ||
assert isinstance(AS_N, int), "AS_N must be int" | ||
assert isinstance(vrf, str), "vrf must be str" | ||
assert isinstance( | ||
interfaces_list, list | ||
), "interfaces_list must be list of interfaces" | ||
assert AS_N != 0, "AS_N must not be 0" | ||
assert ip in ["ipv4", "ipv6"], "ip must be ipv4 or ipv6" | ||
timeout = Timeout(max_time, check_interval) | ||
while timeout.iterate(): | ||
if AS_N and interfaces_list: | ||
try: | ||
response = device.parse( | ||
f"show {'ipv6' if ip!='ipv4' else 'ip'} eigrp interfaces" | ||
) | ||
except SchemaEmptyParserError: | ||
timeout.sleep() | ||
continue | ||
interfaces = [] | ||
if (vrf not in response.q.get_values("vrf")) or ( | ||
str(AS_N) not in response.q.get_values("eigrp_instance") | ||
): | ||
log.error( | ||
f"Sorry,no data for the provided 'AS = {AS_N}' and/or 'vrf = {vrf}' !" | ||
) | ||
else: | ||
interfaces = ( | ||
response.q.contains(vrf) | ||
.contains_key_value("eigrp_instance", str(AS_N)) | ||
.contains(ip) | ||
.get_values("interface") | ||
) | ||
return set(interfaces_list).issubset(interfaces) | ||
log.error( | ||
f"Please, provide a valid format for AS, interfaces_list, ip and/or vrf" | ||
) | ||
continue | ||
|
||
|
||
def verify_eigrp_neighbors( | ||
device, | ||
neighbors=None, | ||
vrf="default", | ||
AS_N=None, | ||
ip="ipv4", | ||
max_time=60, | ||
check_interval=10, | ||
): | ||
"""Verify active EIGRP neighbors for ipv4 (Default) and ipv6 for AS and VRF | ||
Args: | ||
device (obj): Device object | ||
neighbors = None (list): Active EIGRP neighbors to check | ||
vrf = "default" (str) : Name of the vrf | ||
AS_N = None (int) : Autonomous System | ||
ip = "ipv4" (str): Protocol ip set by default to "ipv4" to change to "ipv6" | ||
max_time (`int`): Max time, default: 30 | ||
check_interval (`int`): Check interval, default: 10 | ||
Returns: | ||
True | ||
False | ||
""" | ||
assert isinstance(AS_N, int), "AS_N must be int" | ||
assert isinstance(vrf, str), "vrf must be str" | ||
assert isinstance(neighbors, list), "neighbors must be list of interfaces" | ||
assert AS_N != 0, "AS_N must not be 0" | ||
assert ip in ["ipv4", "ipv6"], "ip must be ipv4 or ipv6" | ||
timeout = Timeout(max_time, check_interval) | ||
while timeout.iterate(): | ||
if AS_N and neighbors: | ||
try: | ||
response = device.parse( | ||
f"show {'ipv6' if ip!='ipv4' else 'ip'} eigrp neighbors" | ||
) | ||
except SchemaEmptyParserError: | ||
timeout.sleep() | ||
continue | ||
eigrp_neighbors = [] | ||
if (vrf not in response.q.get_values("vrf")) or ( | ||
str(AS_N) not in response.q.get_values("eigrp_instance") | ||
): | ||
log.error( | ||
f"Sorry, no data for the provided 'AS = {AS_N}' and/or 'vrf = {vrf}' !" | ||
) | ||
else: | ||
eigrp_neighbors = ( | ||
response.q.contains_key_value("eigrp_instance", str(AS_N)) | ||
.contains(vrf) | ||
.contains(ip) | ||
.get_values("eigrp_nbr") | ||
) | ||
return set(neighbors).issubset(eigrp_neighbors) | ||
log.error(f"Please, provide a valid format for AS, Neighbors, ip and/or vrf") | ||
continue |
46 changes: 46 additions & 0 deletions
46
.../sdk/apis/tests/iosxe/eigrp/verify/verify_eigrp_interfaces/mock_data/iosxe/mock_data.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
configure: | ||
commands: | ||
end: | ||
new_state: execute | ||
line console 0: | ||
new_state: configure_line | ||
no logging console: '' | ||
prompt: R1(config)# | ||
configure_line: | ||
commands: | ||
end: | ||
new_state: execute | ||
exec-timeout 0: '' | ||
prompt: R1(config-line)# | ||
connect: | ||
commands: | ||
? '' | ||
: new_state: execute | ||
preface: 'Trying mock_device ... | ||
Connected to mock_device. | ||
Escape character is ''^]''.' | ||
prompt: '' | ||
execute: | ||
commands: | ||
config term: | ||
new_state: configure | ||
config-transaction: | ||
new_state: configure | ||
show ip eigrp interfaces: | ||
response: | ||
- "EIGRP-IPv4 Interfaces for AS(1)\r\n Xmit Queue\ | ||
\ PeerQ Mean Pacing Time Multicast Pending\r\nInterface \ | ||
\ Peers Un/Reliable Un/Reliable SRTT Un/Reliable Flow Timer\ | ||
\ Routes\r\nFa0/0 1 0/0 0/0 44\ | ||
\ 0/0 50 0\r\nEIGRP-IPv4 Interfaces for AS(2)\r\n\ | ||
\ Xmit Queue PeerQ Mean Pacing Time\ | ||
\ Multicast Pending\r\nInterface Peers Un/Reliable Un/Reliable\ | ||
\ SRTT Un/Reliable Flow Timer Routes\r\nFa1/0 0\ | ||
\ 0/0 0/0 0 0/0 0 0" | ||
response_type: circular | ||
show version: '' | ||
term length 0: '' | ||
term width 0: '' | ||
prompt: R1# |
35 changes: 35 additions & 0 deletions
35
...apis/tests/iosxe/eigrp/verify/verify_eigrp_interfaces/test_api_verify_eigrp_interfaces.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import os | ||
import unittest | ||
from pyats.topology import loader | ||
from genie.libs.sdk.apis.iosxe.eigrp.verify import verify_eigrp_interfaces | ||
|
||
|
||
class TestVerifyEigrpInterfaces(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(self): | ||
testbed = f""" | ||
devices: | ||
R1: | ||
connections: | ||
defaults: | ||
class: unicon.Unicon | ||
a: | ||
command: mock_device_cli --os iosxe --mock_data_dir {os.path.dirname(__file__)}/mock_data --state connect | ||
protocol: unknown | ||
os: iosxe | ||
platform: iosxe | ||
type: iosxe | ||
""" | ||
self.testbed = loader.load(testbed) | ||
self.device = self.testbed.devices['R1'] | ||
self.device.connect( | ||
learn_hostname=True, | ||
init_config_commands=[], | ||
init_exec_commands=[] | ||
) | ||
|
||
def test_verify_eigrp_interfaces(self): | ||
result = verify_eigrp_interfaces(self.device, 'default', 1, ['FastEthernet0/0'], 'ipv4', 60, 10) | ||
expected_output = True | ||
self.assertEqual(result, expected_output) |
42 changes: 42 additions & 0 deletions
42
...s/sdk/apis/tests/iosxe/eigrp/verify/verify_eigrp_neighbors/mock_data/iosxe/mock_data.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
configure: | ||
commands: | ||
end: | ||
new_state: execute | ||
line console 0: | ||
new_state: configure_line | ||
no logging console: '' | ||
prompt: R1(config)# | ||
configure_line: | ||
commands: | ||
end: | ||
new_state: execute | ||
exec-timeout 0: '' | ||
prompt: R1(config-line)# | ||
connect: | ||
commands: | ||
? '' | ||
: new_state: execute | ||
preface: 'Trying mock_device ... | ||
Connected to mock_device. | ||
Escape character is ''^]''.' | ||
prompt: '' | ||
execute: | ||
commands: | ||
config term: | ||
new_state: configure | ||
config-transaction: | ||
new_state: configure | ||
show ip eigrp neighbors: | ||
response: | ||
- "EIGRP-IPv4 Neighbors for AS(1)\r\nH Address Interface \ | ||
\ Hold Uptime SRTT RTO Q Seq\r\n \ | ||
\ (sec) (ms) Cnt Num\r\n0 192.168.1.252\ | ||
\ Fa0/0 12 01:29:22 44 264 0 3\r\nEIGRP-IPv4\ | ||
\ Neighbors for AS(2)" | ||
response_type: circular | ||
show version: '' | ||
term length 0: '' | ||
term width 0: '' | ||
prompt: R1# |
35 changes: 35 additions & 0 deletions
35
...k/apis/tests/iosxe/eigrp/verify/verify_eigrp_neighbors/test_api_verify_eigrp_neighbors.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import os | ||
import unittest | ||
from pyats.topology import loader | ||
from genie.libs.sdk.apis.iosxe.eigrp.verify import verify_eigrp_neighbors | ||
|
||
|
||
class TestVerifyEigrpNeighbors(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(self): | ||
testbed = f""" | ||
devices: | ||
R1: | ||
connections: | ||
defaults: | ||
class: unicon.Unicon | ||
a: | ||
command: mock_device_cli --os iosxe --mock_data_dir {os.path.dirname(__file__)}/mock_data --state connect | ||
protocol: unknown | ||
os: iosxe | ||
platform: iosxe | ||
type: iosxe | ||
""" | ||
self.testbed = loader.load(testbed) | ||
self.device = self.testbed.devices['R1'] | ||
self.device.connect( | ||
learn_hostname=True, | ||
init_config_commands=[], | ||
init_exec_commands=[] | ||
) | ||
|
||
def test_verify_eigrp_neighbors(self): | ||
result = verify_eigrp_neighbors(self.device, ['192.168.1.252'], 'default', 1, 'ipv4', 60, 10) | ||
expected_output = True | ||
self.assertEqual(result, expected_output) |