Skip to content

Commit

Permalink
fix to get_interfaces_counters
Browse files Browse the repository at this point in the history
  • Loading branch information
codingnetworksb committed Nov 11, 2023
1 parent 47be1db commit 6aa1c36
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
41 changes: 20 additions & 21 deletions napalm_huawei_vrp/huawei_vrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
CommandErrorException,
CommitError,
)
from .utils.utils import pretty_mac
from .utils.utils import pretty_mac, SafeList

# Easier to store these as constants
HOUR_SECONDS = 3600
Expand Down Expand Up @@ -783,27 +783,26 @@ def get_interfaces_counters(self):
raise ValueError(msg)

intf_name = match_intf.group("intf_name")
match_errors = re.findall(re_errors, interface, flags=re.M)
match_unicast = re.findall(re_unicast, interface, flags=re.M)
match_multicast = re.findall(re_multicast, interface, flags=re.M)
match_broadcast = re.findall(re_broadcast, interface, flags=re.M)
match_discards = re.findall(re_dicards, interface, flags=re.M)
match_rx_octets = re.findall(re_rx_octets, interface, flags=re.M)
match_tx_octets = re.findall(re_tx_octets, interface, flags=re.M)

match_errors = SafeList(re.findall(re_errors, interface, flags=re.M))
match_unicast = SafeList(re.findall(re_unicast, interface, flags=re.M))
match_multicast = SafeList(re.findall(re_multicast, interface, flags=re.M))
match_broadcast = SafeList(re.findall(re_broadcast, interface, flags=re.M))
match_discards = SafeList(re.findall(re_dicards, interface, flags=re.M))
match_rx_octets = SafeList(re.findall(re_rx_octets, interface, flags=re.M))
match_tx_octets = SafeList(re.findall(re_tx_octets, interface, flags=re.M))
intf_counter = {
"tx_errors": match_errors.get(0, -1),
"rx_errors": match_errors.get(1, -1),
"tx_discards": match_discards.get(0, -1),
"rx_discards": match_discards.get(1, -1),
"tx_octets": match_tx_octets.get(0, -1),
"rx_octets": match_rx_octets.get(0, -1),
"tx_unicast_packets": match_unicast.get(0, -1),
"rx_unicast_packets": match_unicast.get(1, -1),
"tx_multicast_packets": match_multicast.get(0, -1),
"rx_multicast_packets": match_multicast.get(1, -1),
"tx_broadcast_packets": match_broadcast.get(0, -1),
"rx_broadcast_packets": match_broadcast.get(1, -1),
"tx_errors": int(match_errors.get_not_none(0, -1)),
"rx_errors": int(match_errors.get_not_none(1, -1)),
"tx_discards": int(match_discards.get_not_none(0, -1)),
"rx_discards": int(match_discards.get_not_none(1, -1)),
"tx_octets": int(match_tx_octets.get_not_none(0, -1)),
"rx_octets": int(match_rx_octets.get_not_none(0, -1)),
"tx_unicast_packets": int(match_unicast.get_not_none(0, -1)),
"rx_unicast_packets": int(match_unicast.get_not_none(1, -1)),
"tx_multicast_packets": int(match_multicast.get_not_none(0, -1)),
"rx_multicast_packets": int(match_multicast.get_not_none(1, -1)),
"tx_broadcast_packets": int(match_broadcast.get_not_none(0, -1)),
"rx_broadcast_packets": int(match_broadcast.get_not_none(1, -1)),
}

interfaces.update({intf_name: intf_counter})
Expand Down
17 changes: 17 additions & 0 deletions napalm_huawei_vrp/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ def pretty_mac(mac: str) -> str:
return ":".join(two_step_mac_list)


class SafeList(list):
def get(self, index, default=None):
try:
return self.__getitem__(index)
except IndexError:
return default

def get_not_none(self, index, default=None):
try:
value = self.__getitem__(index)[0]
if not value:
value = self.__getitem__(index)[1]
return value
except IndexError:
return default


if __name__ == "__main__":
res = pretty_mac("aaaa-bbbb-cccc")
print(res)

0 comments on commit 6aa1c36

Please sign in to comment.