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

implement get_vlans() #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 50 additions & 2 deletions napalm_huawei_vrp/huawei_vrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}"
)
INTERFACE_REGEX = (
r"(?:[0-9]*GE[0-9\/\.]+)|(?:LoopBack\d+)|(?:Eth-Trunk[0-9\.]+)|(?:Vlanif[0-9\.]+)"
r"(?:[0-9]*X?GE[0-9\/\.]+)|(?:LoopBack\d+)|(?:Eth-Trunk[0-9\.]+)|(?:Vlanif[0-9\.]+)"
)

# Should validate IPv6 address using an IP address library after matching with this regex
Expand Down Expand Up @@ -1905,7 +1905,6 @@ def get_users(self):

# developing
def get_vlans(self):
pass
"""
{
"1": {
Expand All @@ -1932,6 +1931,51 @@ def get_vlans(self):
}
}
"""
vlans = {}
re_vlans_with_name = r"^vlan\s(?P<number>\d+)\n\s+(?P<nametype>name|description)\s+(?P<name>.*)$"
re_vlan_interfaces = r"^(?P<number>\d+).*(UT:|TG:)((\w+/\d+/\d+)\([UD]\)\s+|(Eth-Trunk\d+)\([UD]\)\s+)+"
cmd_vlan_names = "display cur conf vlan"
cmd_vlan_interfaces = "display vlan"

output_names = self.device.send_command(cmd_vlan_names)
output_interfaces = self.device.send_command(cmd_vlan_interfaces)


if not output_names:
return vlans


#Get vlan names from config and add to vlans dic.
#If name not set use description for name. See regex 're_vlans_with_name'
matches = re.finditer(re_vlans_with_name, output_names, re.MULTILINE)

for m in matches:
vlan = {}
number = m.group('number')
name = m.group('name').replace('"',"").rstrip()
#Include id as interger in vlan dictionary to turn interactions easier
#vlan['id'] = int(number)
vlan['name'] = name
vlans[number] = vlan

#Get interfaces what vlan is configured. Ignore tag or untag config
match_interfaces = re.compile(INTERFACE_REGEX)
matches = re.finditer(re_vlan_interfaces, output_interfaces, re.MULTILINE)

for m in matches:
interfaces = match_interfaces.findall(m.group(0))
interfaces = [self.interface_format_conversion(iface) for iface in interfaces]
n = m.group('number')

if vlans.get(n) is None:
#Add vlans without name and description
vlans[n] = {'name': 'VLAN ' + n, 'interfaces': interfaces}
else:
vlans[n]['interfaces'] = interfaces

return vlans



@staticmethod
def _separate_section(separator, content):
Expand Down Expand Up @@ -2214,14 +2258,18 @@ def interface_format_conversion(interface):

re_giga = r"^GE(?P<port>[A-Za-z0-9\/\.]+)"
re_ether = r"Ether(?P<port>\d[A-Za-z0-9\/\.]+)"
re_tengiga = r"^XGE(?P<port>[A-Za-z0-9\/\.]+)"

match_giga = re.search(re_giga, interface, flags=re.M)
match_ether = re.search(re_ether, interface, flags=re.M)
match_tengiga = re.search(re_tengiga,interface,flags=re.M)

if match_giga:
return "GigabitEthernet" + match_giga.group("port")
elif match_ether:
return "Ethernet" + match_ether.group("port")
elif match_tengiga:
return "XGigabitEthernet" + match_tengiga.group("port")
else:
return interface

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vlan 32
name CLIENT_1
vlan 45
description ACCESS_TO_BORDER_ROUTER


16 changes: 16 additions & 0 deletions test/unit/mocked_data/test_get_vlans/vrp5/display_vlan.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
The total number of VLANs is: 2
--------------------------------------------------------------------------------
U: Up; D: Down; TG: Tagged; UT: Untagged;
MP: Vlan-mapping; ST: Vlan-stacking;
#: ProtocolTransparent-vlan; *: Management-vlan;
--------------------------------------------------------------------------------

VID Type Ports
--------------------------------------------------------------------------------
1 common UT:100GE0/0/3(D) 100GE0/0/4(D) XGE0/0/5(U) XGE0/0/6(U)
XGE0/0/11(D) XGE0/0/12(D) XGE0/0/13(D) XGE0/0/14(D)
XGE0/0/15(D) XGE0/0/16(D) XGE0/0/17(D) XGE0/0/18(D)
XGE0/0/19(D) XGE0/0/20(D) XGE0/0/21(D) XGE0/0/22(D)
XGE0/0/23(D) XGE0/0/24(U)
32 common TG:100GE0/0/5(U) XGE0/0/10(U)
45 common TG:100GE0/0/5(U) XGE0/0/6(U)
211 changes: 36 additions & 175 deletions test/unit/mocked_data/test_get_vlans/vrp5/expected_result.json
Original file line number Diff line number Diff line change
@@ -1,177 +1,38 @@
{
"global": {
"router_id": "10.0.1.1",
"peers": {
"10.0.0.2": {
"is_enabled": true,
"uptime": 4838400,
"remote_as": 65000,
"description": "internal-2",
"remote_id": "10.0.1.2",
"local_as": 65000,
"is_up": true,
"address_family": {
"ipv4 unicast": {
"sent_prefixes": 637213,
"accepted_prefixes": 3142,
"received_prefixes": 3142
},
"ipv6 unicast": {
"sent_prefixes": 36714,
"accepted_prefixes": 148,
"received_prefixes": 148
}
}
},
"10.0.0.3": {
"is_enabled": true,
"uptime": 133200,
"remote_as": 65000,
"description": "internal-3",
"remote_id": "10.0.1.3",
"local_as": 65000,
"is_up": true,
"address_family": {
"ipv4 unicast": {
"sent_prefixes": 966,
"accepted_prefixes": 1,
"received_prefixes": 1
},
"ipv6 unicast": {
"sent_prefixes": 105,
"accepted_prefixes": 2,
"received_prefixes": 2
}
}
},
"192.168.0.1": {
"is_enabled": true,
"uptime": 3888000,
"remote_as": 65001,
"description": "external-1",
"remote_id": "192.168.1.1",
"local_as": 65000,
"is_up": true,
"address_family": {
"ipv4 unicast": {
"sent_prefixes": 709,
"accepted_prefixes": 0,
"received_prefixes": 0
}
}
},
"192.168.0.2": {
"is_enabled": true,
"uptime": -1,
"remote_as": 65002,
"description": "external-2",
"remote_id": "0.0.0.0",
"local_as": 65000,
"is_up": false,
"address_family": {
"ipv4 unicast": {
"sent_prefixes": -1,
"accepted_prefixes": -1,
"received_prefixes": -1
}
}
},
"192.168.0.3": {
"is_enabled": false,
"uptime": -1,
"remote_as": 65003,
"description": "external-3",
"remote_id": "0.0.0.0",
"local_as": 65000,
"is_up": false,
"address_family": {
"ipv4 unicast": {
"sent_prefixes": -1,
"accepted_prefixes": -1,
"received_prefixes": -1
}
}
},
"192.168.0.4": {
"is_enabled": true,
"uptime": 16243200,
"remote_as": 65004,
"description": "external-4",
"remote_id": "192.168.1.4",
"local_as": 65000,
"is_up": true,
"address_family": {
"ipv4 unicast": {
"sent_prefixes": 213,
"accepted_prefixes": 629700,
"received_prefixes": 629700
}
}
},
"2001:db8::1": {
"is_enabled": true,
"uptime": 3456000,
"remote_as": 65001,
"description": "external-1",
"remote_id": "192.168.1.1",
"local_as": 65000,
"is_up": true,
"address_family": {
"ipv6 unicast": {
"sent_prefixes": 35,
"accepted_prefixes": 0,
"received_prefixes": 0
}
}
},
"2001:db8::2": {
"is_enabled": true,
"uptime": 10713600,
"remote_as": 65002,
"description": "external-2",
"remote_id": "192.168.1.2",
"local_as": 65000,
"is_up": true,
"address_family": {
"ipv6 unicast": {
"sent_prefixes": 36,
"accepted_prefixes": 1,
"received_prefixes": 1
}
}
},
"2001:db8::3": {
"is_enabled": false,
"uptime": -1,
"remote_as": 65003,
"description": "external-3",
"remote_id": "0.0.0.0",
"local_as": 65000,
"is_up": false,
"address_family": {
"ipv6 unicast": {
"sent_prefixes": -1,
"accepted_prefixes": -1,
"received_prefixes": -1
}
}
},
"2001:db8::4": {
"is_enabled": true,
"uptime": 16243200,
"remote_as": 65004,
"description": "external-4",
"remote_id": "192.168.1.4",
"local_as": 65000,
"is_up": true,
"address_family": {
"ipv6 unicast": {
"sent_prefixes": 29,
"accepted_prefixes": 36387,
"received_prefixes": 36387
}
}
}
}
"32": {
"name": "CLIENT_1",
"interfaces": [
"100GE0/0/5",
"XGigabitEthernet0/0/10"
]
},
"45": {
"name": "ACCESS_TO_BORDER_ROUTER",
"interfaces": [
"100GE0/0/5"
]
},
"1": {
"name": "VLAN 1",
"interfaces": [
"100GE0/0/3",
"100GE0/0/4",
"XGigabitEthernet0/0/5",
"XGigabitEthernet0/0/6",
"XGigabitEthernet0/0/11",
"XGigabitEthernet0/0/12",
"XGigabitEthernet0/0/13",
"XGigabitEthernet0/0/14",
"XGigabitEthernet0/0/15",
"XGigabitEthernet0/0/16",
"XGigabitEthernet0/0/17",
"XGigabitEthernet0/0/18",
"XGigabitEthernet0/0/19",
"XGigabitEthernet0/0/20",
"XGigabitEthernet0/0/21",
"XGigabitEthernet0/0/22",
"XGigabitEthernet0/0/23",
"XGigabitEthernet0/0/24"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vlan 32
name CLIENT_1
vlan 45
description ACCESS_TO_BORDER_ROUTER


16 changes: 16 additions & 0 deletions test/unit/mocked_data/test_get_vlans/vrp8/display_vlan.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
The total number of VLANs is: 2
--------------------------------------------------------------------------------
U: Up; D: Down; TG: Tagged; UT: Untagged;
MP: Vlan-mapping; ST: Vlan-stacking;
#: ProtocolTransparent-vlan; *: Management-vlan;
--------------------------------------------------------------------------------

VID Type Ports
--------------------------------------------------------------------------------
1 common UT:100GE0/0/3(D) 100GE0/0/4(D) XGE0/0/5(U) XGE0/0/6(U)
XGE0/0/11(D) XGE0/0/12(D) XGE0/0/13(D) XGE0/0/14(D)
XGE0/0/15(D) XGE0/0/16(D) XGE0/0/17(D) XGE0/0/18(D)
XGE0/0/19(D) XGE0/0/20(D) XGE0/0/21(D) XGE0/0/22(D)
XGE0/0/23(D) XGE0/0/24(U)
32 common TG:100GE0/0/5(U) XGE0/0/10(U)
45 common TG:100GE0/0/5(U) XGE0/0/6(U)
Loading