|
| 1 | +import random |
| 2 | +import re |
| 3 | +import logging |
| 4 | +from tests.common.portstat_utilities import parse_column_positions |
| 5 | + |
| 6 | +logger = logging.getLogger(__name__) |
| 7 | + |
| 8 | + |
| 9 | +def sum_ifaces_counts(counter_out, ifaces, column): |
| 10 | + if len(ifaces) == 0: |
| 11 | + return 0 |
| 12 | + if len(ifaces) == 1: |
| 13 | + return int(counter_out[ifaces[0]][column].replace(",", "")) |
| 14 | + return sum([int(counter_out[iface][column].replace(",", "")) for iface in ifaces]) |
| 15 | + |
| 16 | + |
| 17 | +def parse_interfaces(output_lines, pc_ports_map): |
| 18 | + """ |
| 19 | + Parse the interfaces from 'show ip route' into an array |
| 20 | + """ |
| 21 | + route_targets = [] |
| 22 | + ifaces = [] |
| 23 | + output_lines = output_lines[3:] |
| 24 | + |
| 25 | + for item in output_lines: |
| 26 | + match = re.search(r"(Ethernet\d+|PortChannel\d+)", item) |
| 27 | + if match: |
| 28 | + route_targets.append(match.group(0)) |
| 29 | + |
| 30 | + for route_target in route_targets: |
| 31 | + if route_target.startswith("Ethernet"): |
| 32 | + ifaces.append(route_target) |
| 33 | + elif route_target.startswith("PortChannel") and route_target in pc_ports_map: |
| 34 | + ifaces.extend(pc_ports_map[route_target]) |
| 35 | + |
| 36 | + return route_targets, ifaces |
| 37 | + |
| 38 | + |
| 39 | +def parse_rif_counters(output_lines): |
| 40 | + """Parse the output of "show interfaces counters rif" command |
| 41 | + Args: |
| 42 | + output_lines (list): The output lines of "show interfaces counters rif" command |
| 43 | + Returns: |
| 44 | + list: A dictionary, key is interface name, value is a dictionary of fields/values |
| 45 | + """ |
| 46 | + |
| 47 | + header_line = '' |
| 48 | + separation_line = '' |
| 49 | + separation_line_number = 0 |
| 50 | + for idx, line in enumerate(output_lines): |
| 51 | + if line.find('----') >= 0: |
| 52 | + header_line = output_lines[idx - 1] |
| 53 | + separation_line = output_lines[idx] |
| 54 | + separation_line_number = idx |
| 55 | + break |
| 56 | + |
| 57 | + try: |
| 58 | + positions = parse_column_positions(separation_line) |
| 59 | + except Exception: |
| 60 | + logger.error('Possibly bad command output') |
| 61 | + return {} |
| 62 | + |
| 63 | + headers = [] |
| 64 | + for pos in positions: |
| 65 | + header = header_line[pos[0]:pos[1]].strip().lower() |
| 66 | + headers.append(header) |
| 67 | + |
| 68 | + if not headers: |
| 69 | + return {} |
| 70 | + |
| 71 | + results = {} |
| 72 | + for line in output_lines[separation_line_number + 1:]: |
| 73 | + portstats = [] |
| 74 | + for pos in positions: |
| 75 | + portstat = line[pos[0]:pos[1]].strip() |
| 76 | + portstats.append(portstat) |
| 77 | + |
| 78 | + intf = portstats[0] |
| 79 | + results[intf] = {} |
| 80 | + for idx in range(1, len(portstats)): # Skip the first column interface name |
| 81 | + results[intf][headers[idx]] = portstats[idx] |
| 82 | + |
| 83 | + return results |
| 84 | + |
| 85 | + |
| 86 | +def random_mac(): |
| 87 | + return "02:00:00:%02x:%02x:%02x" % (random.randint(0, 255), |
| 88 | + random.randint(0, 255), |
| 89 | + random.randint(0, 255)) |
0 commit comments