11'''
22Parsing Configuration utilities
33'''
4+ from typing import Dict
45from ciscoconfparse import CiscoConfParse
56
67
7- def get_access_port_interfaces (conf : CiscoConfParse , nos : str ) -> dict :
8+ def get_access_port_interfaces (conf : CiscoConfParse ,
9+ nos : str ) -> Dict [str , int ]:
810 '''For various NOS return the list of access port interfaces.
911
1012 This module uses the CiscoConfParse to extract the interface names.
@@ -19,7 +21,8 @@ def get_access_port_interfaces(conf: CiscoConfParse, nos: str) -> dict:
1921 return ifmap
2022
2123
22- def get_trunk_port_interfaces (conf : CiscoConfParse , nos : str ) -> dict :
24+ def get_trunk_port_interfaces (conf : CiscoConfParse ,
25+ nos : str ) -> Dict [str , int ]:
2326 '''For various NOS return the list of access port interfaces.
2427
2528 This module uses the CiscoConfParse to extract the interface names.
@@ -35,12 +38,13 @@ def get_trunk_port_interfaces(conf: CiscoConfParse, nos: str) -> dict:
3538
3639
3740# Do not invoke these functions directly, the implementation can change
38- def _get_swport_interfaces_junos (conf : CiscoConfParse , what : str ) -> dict :
41+ def _get_swport_interfaces_junos (conf : CiscoConfParse ,
42+ what : str ) -> Dict [str , int ]:
3943 '''Return interfaces that match the requested info for Junos.
4044
4145 This involves parsing the Junos config looking for the relevant info.
42- what can be access or trunk. For access ports, we return a dict of
43- interfaces that are access ports with the value that is the access vlan.
46+ what can be access or trunk. For access ports, we return a Dict[str, int]
47+ of interfaces that are access ports with the value that is the access vlan.
4448 '''
4549
4650 pm_dict = {}
@@ -58,12 +62,16 @@ def _get_swport_interfaces_junos(conf: CiscoConfParse, what: str) -> dict:
5862 if 'members' in line .text ]
5963 if vlan_ln :
6064 vlan = vlan_ln [0 ].text .split ('members' )[1 ].strip ()
61- pm_dict [ifname ] = vlan
65+ if vlan .isnumeric ():
66+ pm_dict [ifname ] = int (vlan )
67+ else :
68+ vlan = 0
6269
6370 return pm_dict
6471
6572
66- def _get_swport_interfaces_cls (conf : CiscoConfParse , what : str ) -> dict :
73+ def _get_swport_interfaces_cls (conf : CiscoConfParse ,
74+ what : str ) -> Dict [str , int ]:
6775 '''Return interfaces that match the requested info for Cumulus.
6876
6977 This involves parsing the ifupdown2 config looking for the relevant info.
@@ -76,7 +84,10 @@ def _get_swport_interfaces_cls(conf: CiscoConfParse, what: str) -> dict:
7684 '^iface ' , ['bridge-access' ]):
7785 ifname = intf .text .split ('iface' )[1 ].strip ()
7886 acc_vlan = intf .re_match_iter_typed (r'^.*bridge-access\s+(\d+)' )
79- pm_dict [ifname ] = acc_vlan
87+ if acc_vlan .isnumeric ():
88+ pm_dict [ifname ] = int (acc_vlan )
89+ else :
90+ pm_dict [ifname ] = 0
8091 if what == "trunk" :
8192 acc_ports = []
8293 for intf in conf .find_objects_w_all_children (
@@ -99,15 +110,18 @@ def _get_swport_interfaces_cls(conf: CiscoConfParse, what: str) -> dict:
99110 for intf in conf .find_objects_w_child ('iface ' , 'bridge-pvid' ):
100111 port = intf .text .split ('iface' )[1 ].strip ()
101112 if port not in pm_dict :
102- pm_dict [port ] = intf .re_match_iter_typed (
103- r'bridge-pvid\s+(\d+)' )
113+ vlan = intf .re_match_iter_typed (r'bridge-pvid\s+(\d+)' )
114+ else :
115+ vlan = intf .re_match_iter_typed (r'bridge-pvid\s+(\d+)' )
116+ if vlan .isnumeric ():
117+ pm_dict ['port' ] = int (vlan )
104118 else :
105- pm_dict ['port' ] = intf .re_match_iter_typed (
106- r'bridge-pvid\s+(\d+)' )
119+ pm_dict ['port' ] = 0
107120 return pm_dict
108121
109122
110- def _get_swport_interfaces_iosy (conf : CiscoConfParse , what : str ) -> dict :
123+ def _get_swport_interfaces_iosy (conf : CiscoConfParse ,
124+ what : str ) -> Dict [str , int ]:
111125 '''Return interfaces that match the requested info for IOSy NOS.
112126
113127 This involves parsing the config looking for the relevant info. This is
@@ -121,13 +135,19 @@ def _get_swport_interfaces_iosy(conf: CiscoConfParse, what: str) -> dict:
121135 for intf in conf .find_objects_w_child ('^interface ' , '.*access' ):
122136 ifname = intf .text .split ('interface' )[1 ].strip ()
123137 acc_vlan = intf .re_match_iter_typed (r'^.*vlan\s+(\d+)' )
124- pm_dict [ifname ] = acc_vlan
138+ if acc_vlan .isnumeric ():
139+ pm_dict [ifname ] = int (acc_vlan )
140+ else :
141+ pm_dict [ifname ] = 0
125142
126143 if what == 'trunk' :
127144 for intf in conf .find_objects_w_child ('^interface' , '.*trunk' ):
128145 ifname = intf .text .split ('interface' )[1 ].strip ()
129146 nvlan = intf .re_match_iter_typed (r'.*native vlan\s+(\d+)' ,
130147 default = '1' )
131- pm_dict [ifname ] = nvlan
148+ if nvlan .isnumeric ():
149+ pm_dict [ifname ] = int (nvlan )
150+ else :
151+ pm_dict [ifname ] = 0
132152
133153 return pm_dict
0 commit comments