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

[IOS][get_vlans()] Parse VLAN IDs when private VLANs are implemented #1994

Draft
wants to merge 47 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
a395045
Parse Private VLANs
vvas1lev Aug 24, 2023
8525937
add tests
vvas1lev Aug 24, 2023
590e3ce
Update mock data
vvas1lev Aug 24, 2023
bff8d13
run black
vvas1lev Aug 24, 2023
35817fe
fix typo
vvas1lev Aug 24, 2023
c747d1c
fix more typos
vvas1lev Aug 24, 2023
90a7b96
s/name/'name'/
vvas1lev Aug 24, 2023
d51968c
fix json encoding
vvas1lev Aug 24, 2023
6e54189
refactor VLAN match logic
vvas1lev Aug 25, 2023
9980dc6
fix tests
vvas1lev Aug 25, 2023
399670f
fixing a typo
vvas1lev Aug 25, 2023
0ee9432
Preserve private VLAN names
vvas1lev Aug 28, 2023
a0f8f56
fix a typo
vvas1lev Aug 28, 2023
42454d2
fix ident
vvas1lev Aug 28, 2023
7094e96
fix typo
vvas1lev Aug 28, 2023
ae45820
Merge branch 'develop' into develop
vvas1lev Sep 4, 2023
a8f97f1
Merge branch 'develop' into develop
vvas1lev Sep 11, 2023
9a30c62
Merge branch 'develop' into develop
vvas1lev Mar 23, 2024
808eca9
black
vvas1lev Mar 23, 2024
ca3acb0
rebase
vvas1lev Mar 23, 2024
f1054af
empty commit / re-run tests
vvas1lev Mar 23, 2024
07c6ae4
black 24.3.0
vvas1lev Mar 23, 2024
d41df1d
Merge branch 'develop' into develop
mirceaulinic May 21, 2024
09d96ab
Merge branch 'develop' into develop
vvas1lev Aug 10, 2024
4e373e9
remove ws
vvas1lev Aug 10, 2024
2a42429
refactor _get_vlan_all_ports()
vvas1lev Aug 10, 2024
f33e1cf
fix typo
vvas1lev Aug 10, 2024
870434d
update unittest
vvas1lev Aug 10, 2024
3df3890
remove wrongly entered PVLANs in mocked data
vvas1lev Aug 10, 2024
cf4c517
update mocked data
vvas1lev Aug 10, 2024
1fa6d76
update mocked data
vvas1lev Aug 10, 2024
f45c248
update tests
vvas1lev Aug 10, 2024
c3597ca
update tests
vvas1lev Aug 10, 2024
d1f7e1e
update tests
vvas1lev Aug 10, 2024
5053858
add new mock data
vvas1lev Aug 12, 2024
e1598b4
rename mock file
vvas1lev Aug 12, 2024
25dad20
fix logic
vvas1lev Aug 12, 2024
20e94b1
fixes
vvas1lev Aug 12, 2024
cb52b09
fixes
vvas1lev Aug 12, 2024
923035d
black
vvas1lev Aug 12, 2024
9b4023d
fixes
vvas1lev Aug 12, 2024
3740b64
add mock data
vvas1lev Aug 12, 2024
f546560
fixes
vvas1lev Aug 12, 2024
423e623
fixes
vvas1lev Aug 12, 2024
34e90aa
black
vvas1lev Aug 12, 2024
a850cd5
fixes
vvas1lev Aug 12, 2024
7680d42
fixes
vvas1lev Aug 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions napalm/ios/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -3741,15 +3741,18 @@ def get_vlans(self):
if output.find("Invalid input detected") >= 0:
return self._get_vlan_from_id()
else:
return self._get_vlan_all_ports(output)
return self._get_vlan_all_ports(output, _vlan_id=None, _vlan_name=None)

def _get_vlan_all_ports(self, output):
def _get_vlan_all_ports(self, output, _vlan_id, _vlan_name):
find_regexp = re.compile(
r"^(\d+)\s+" # vlan id
r"(.*?(?=active|act\/[isl]{1}shut|act\/unsup))" # vlan name
r"\w+(?:\/\w+)?\S+(\s+[A-Z][a-z].*)?$" # ports
)
continuation_regexp = re.compile(r"^\s+([A-Z][a-z].*)$")
match_pvlan_regexp = re.compile(
r"^(\d+)\s+(\S+)\s+(community|isolated)?\s+\S+(\s+[A-Z][a-z].*)?$"
)
output = output.splitlines()
vlans = {}

Expand All @@ -3760,11 +3763,32 @@ def _get_vlan_all_ports(self, output):
for line in output:
vlan_m = find_regexp.match(line)
if vlan_m:
was_vlan_or_cont = True
vlan_id = vlan_m.group(1)
vlan_name = vlan_m.group(2).strip()
interfaces = vlan_m.group(3) or ""
vlans[vlan_id] = {"name": vlan_name, "interfaces": []}
p_vlan_m = match_pvlan_regexp.match(line)
if p_vlan_m:
was_vlan_or_cont = True
vlan_name = vlan_m.group(2)
if (
vlan_name == p_vlan_m.group(2)
and p_vlan_m.group(3) in ["community", "isolated"]
and vlan_name == _vlan_id
):
vlan_id = p_vlan_m.group(2)
elif vlan_m.group(1) != p_vlan_m.group(2) and p_vlan_m.group(3) in [
"community",
"isolated",
]:
continue
else:
vlan_id = vlan_m.group(1)
vlan_name = _vlan_name if _vlan_name else vlan_name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably need to add a strip call on this line somewhere given the failing test output

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave this a try, and even with a strip() the tests would need the following in order to pass with this code:

diff --git a/test/ios/mocked_data/test_get_vlans/no_show_vlan_all_ports/expected_result.json b/test/ios/mocked_data/test_get_vlans/no_show_vlan_all_ports/expected_result.json
index da9cd8e0..ec258ac5 100644
--- a/test/ios/mocked_data/test_get_vlans/no_show_vlan_all_ports/expected_result.json
+++ b/test/ios/mocked_data/test_get_vlans/no_show_vlan_all_ports/expected_result.json
@@ -227,15 +227,13 @@
   "2432": {
     "name": "Vlan2432",
     "interfaces": [
-      "GigabitEthernet0/1",
-      "GigabitEthernet0/2"
+      "Port-channel1"
     ]
   },
   "2433": {
     "name": "Vlan2433",
     "interfaces": [
-      "GigabitEthernet0/3",
-      "GigabitEthernet0/4"
+      "Port-channel1"
     ]
   }
 }

So I believe there's more work required in this PR, re-scheduling for the next release.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mirceaulinic @bewing I figured it'd need more work, which is why I changed it back to draft. I'm just back from PTO, will take a look at this coming week.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool - thanks for the heads up @vvas1lev!

interfaces = vlan_m.group(3) or ""
vlans[vlan_id] = {"name": vlan_name, "interfaces": []}
else:
was_vlan_or_cont = True
vlan_id = vlan_m.group(1)
vlan_name = vlan_m.group(2).strip()
interfaces = vlan_m.group(3) or ""
vlans[vlan_id] = {"name": vlan_name, "interfaces": []}

cont_m = None
if was_vlan_or_cont:
Expand Down Expand Up @@ -3794,7 +3818,7 @@ def _get_vlan_from_id(self):
vlans = {}
for vlan_id, vlan_name in find_vlan:
output = self._send_command("show vlan id {}".format(vlan_id))
_vlans = self._get_vlan_all_ports(output)
_vlans = self._get_vlan_all_ports(output, vlan_id, vlan_name)
if len(_vlans) == 0:
vlans[vlan_id] = {"name": vlan_name, "interfaces": []}
elif len(_vlans) == 1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,25 @@
"1005": {
"name": "Vlan1005",
"interfaces": []
},
"2400": {
"name": "Vlan2400",
"interfaces": [
"Port-channel1"
]
},
"2432": {
"name": "Vlan2432",
"interfaces": [
"GigabitEthernet0/1",
"GigabitEthernet0/2"
]
},
"2433": {
"name": "Vlan2433",
"interfaces": [
"GigabitEthernet0/3",
"GigabitEthernet0/4"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ VLAN Name Status Ports
1002 Vlan1002 act/unsup
1003 Vlan1003 act/unsup
1004 Vlan1004 act/unsup
1005 Vlan1005 act/unsup
1005 Vlan1005 act/unsup
2400 Vlan2400 active
2432 Vlan2432 active
2433 Vlan2433 active
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
VLAN Name Status Ports
---- -------------------------------- --------- -------------------------------
2400 Vlan2400 active Po1

VLAN Type SAID MTU Parent RingNo BridgeNo Stp BrdgMode Trans1 Trans2
---- ----- ---------- ----- ------ ------ -------- ---- -------- ------ ------
2400 enet 102400 1500 - - - - - 0 0

Remote SPAN VLAN
----------------
Disabled

Primary Secondary Type Ports
------- --------- ----------------- ------------------------------------------
2400 2432 community Gi0/1, Gi0/2
2400 2433 community Gi0/3, Gi0/4
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
VLAN Name Status Ports
---- -------------------------------- --------- -------------------------------
2432 Vlan2432 active Po1

VLAN Type SAID MTU Parent RingNo BridgeNo Stp BrdgMode Trans1 Trans2
---- ----- ---------- ----- ------ ------ -------- ---- -------- ------ ------
2432 enet 102432 1500 - - - - - 0 0

Remote SPAN VLAN
----------------
Disabled

Primary Secondary Type Ports
------- --------- ----------------- ------------------------------------------
2400 2432 community Gi0/1, Gi0/2
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
VLAN Name Status Ports
---- -------------------------------- --------- -------------------------------
2433 Vlan2433 active Po1

VLAN Type SAID MTU Parent RingNo BridgeNo Stp BrdgMode Trans1 Trans2
---- ----- ---------- ----- ------ ------ -------- ---- -------- ------ ------
2433 enet 102433 1500 - - - - - 0 0

Remote SPAN VLAN
----------------
Disabled

Primary Secondary Type Ports
------- --------- ----------------- ------------------------------------------
2400 2433 community Gi0/3, Gi0/4
Loading