| 1 |
from __future__ import absolute_import, division, print_function |
| 2 |
|
| 3 |
|
| 4 |
__metaclass__ = type |
| 5 |
import socket |
| 6 |
|
| 7 |
from functools import total_ordering |
| 8 |
from itertools import count, groupby |
| 9 |
|
| 10 |
|
| 11 |
LOGGING_SEVMAP = { |
| 12 |
0: "emergency", |
| 13 |
1: "alert", |
| 14 |
2: "critical", |
| 15 |
3: "error", |
| 16 |
4: "warning", |
| 17 |
5: "notification", |
| 18 |
6: "informational", |
| 19 |
7: "debugging", |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
def search_obj_in_list(name, lst, identifier): |
| 24 |
for o in lst: |
| 25 |
if o[identifier] == name: |
| 26 |
return o |
| 27 |
return None |
| 28 |
|
| 29 |
|
| 30 |
def flatten_dict(x): |
| 31 |
result = {} |
| 32 |
if not isinstance(x, dict): |
| 33 |
return result |
| 34 |
|
| 35 |
for key, value in x.items(): |
| 36 |
if isinstance(value, dict): |
| 37 |
result.update(flatten_dict(value)) |
| 38 |
else: |
| 39 |
result[key] = value |
| 40 |
|
| 41 |
return result |
| 42 |
|
| 43 |
|
| 44 |
def validate_ipv4_addr(address): |
| 45 |
address = address.split("/")[0] |
| 46 |
try: |
| 47 |
socket.inet_aton(address) |
| 48 |
except socket.error: |
| 49 |
return False |
| 50 |
return address.count(".") == 3 |
| 51 |
|
| 52 |
|
| 53 |
def validate_ipv6_addr(address): |
| 54 |
address = address.split("/")[0] |
| 55 |
try: |
| 56 |
socket.inet_pton(socket.AF_INET6, address) |
| 57 |
except socket.error: |
| 58 |
return False |
| 59 |
return True |
| 60 |
|
| 61 |
|
| 62 |
def normalize_interface(name): |
| 63 |
"""Return the normalized interface name""" |
| 64 |
if not name: |
| 65 |
return |
| 66 |
|
| 67 |
def _get_number(name): |
| 68 |
digits = "" |
| 69 |
for char in name: |
| 70 |
if char.isdigit() or char in "/.": |
| 71 |
digits += char |
| 72 |
return digits |
| 73 |
|
| 74 |
if name.lower().startswith("et"): |
| 75 |
if_type = "Ethernet" |
| 76 |
elif name.lower().startswith("vl"): |
| 77 |
if_type = "Vlan" |
| 78 |
elif name.lower().startswith("lo"): |
| 79 |
if_type = "loopback" |
| 80 |
elif name.lower().startswith("po"): |
| 81 |
if_type = "port-channel" |
| 82 |
elif name.lower().startswith("nv"): |
| 83 |
if_type = "nve" |
| 84 |
else: |
| 85 |
if_type = None |
| 86 |
|
| 87 |
number_list = name.split(" ") |
| 88 |
if len(number_list) == 2: |
| 89 |
number = number_list[-1].strip() |
| 90 |
else: |
| 91 |
number = _get_number(name) |
| 92 |
|
| 93 |
if if_type: |
| 94 |
proper_interface = if_type + number |
| 95 |
else: |
| 96 |
proper_interface = name |
| 97 |
|
| 98 |
return proper_interface |
| 99 |
|
| 100 |
|
| 101 |
def get_interface_type(interface): |
| 102 |
"""Gets the type of interface""" |
| 103 |
if interface.upper().startswith("ET"): |
| 104 |
return "ethernet" |
| 105 |
elif interface.upper().startswith("VL"): |
| 106 |
return "svi" |
| 107 |
elif interface.upper().startswith("LO"): |
| 108 |
return "loopback" |
| 109 |
elif interface.upper().startswith("MG"): |
| 110 |
return "management" |
| 111 |
elif interface.upper().startswith("MA"): |
| 112 |
return "management" |
| 113 |
elif interface.upper().startswith("PO"): |
| 114 |
return "portchannel" |
| 115 |
elif interface.upper().startswith("NV"): |
| 116 |
return "nve" |
| 117 |
else: |
| 118 |
return "unknown" |
| 119 |
|
| 120 |
|
| 121 |
def remove_rsvd_interfaces(interfaces): |
| 122 |
"""Exclude reserved interfaces from user management""" |
| 123 |
if not interfaces: |
| 124 |
return [] |
| 125 |
return [i for i in interfaces if get_interface_type(i["name"]) != "management"] |
| 126 |
|
| 127 |
|
| 128 |
def vlan_range_to_dict(vlans): |
| 129 |
result = {} |
| 130 |
if vlans: |
| 131 |
for part in vlans.split(","): |
| 132 |
if part == "none": |
| 133 |
break |
| 134 |
if "-" in part: |
| 135 |
a, b = part.split("-") |
| 136 |
a, b = int(a), int(b) |
| 137 |
for vlan in range(a, b + 1): |
| 138 |
result[str(vlan)] = vlan |
| 139 |
else: |
| 140 |
a = int(part) |
| 141 |
result[str(a)] = a |
| 142 |
return result |
| 143 |
|
| 144 |
|
| 145 |
def numerical_sort(string_int_list): |
| 146 |
"""Sorts list of integers that are digits in numerical order.""" |
| 147 |
|
| 148 |
as_int_list = [] |
| 149 |
|
| 150 |
for vlan in string_int_list: |
| 151 |
as_int_list.append(int(vlan)) |
| 152 |
as_int_list.sort() |
| 153 |
return as_int_list |
| 154 |
|
| 155 |
|
| 156 |
def get_logging_sevmap(invert=False): |
| 157 |
x = LOGGING_SEVMAP |
| 158 |
if invert: |
| 159 |
# cannot use dict comprehension yet |
| 160 |
# since we still test with Python 2.6 |
| 161 |
x = dict(map(reversed, x.items())) |
| 162 |
return x |
| 163 |
|
| 164 |
|
| 165 |
def get_ranges(data): |
| 166 |
""" |
| 167 |
Returns a generator object that yields lists of |
| 168 |
consequtive integers from a list of integers. |
| 169 |
""" |
| 170 |
for _k, group in groupby(data, lambda t, c=count(): int(t) - next(c)): |
| 171 |
yield list(group) |
| 172 |
|
| 173 |
|
| 174 |
def vlan_list_to_range(cmd): |
| 175 |
""" |
| 176 |
Converts a comma separated list of vlan IDs |
| 177 |
into ranges. |
| 178 |
""" |
| 179 |
ranges = [] |
| 180 |
for v in get_ranges(cmd): |
| 181 |
ranges.append("-".join(map(str, (v[0], v[-1])[: len(v)]))) |
| 182 |
return ",".join(ranges) |
| 183 |
|
| 184 |
|
| 185 |
def generate_switchport_trunk(type, add, vlans_range): |
| 186 |
""" |
| 187 |
Generates a list of switchport commands based on the trunk type and VLANs range. |
| 188 |
Ensures that the length of VLANs lexeme in a command does not exceed 220 characters. |
| 189 |
""" |
| 190 |
|
| 191 |
def append_command(): |
| 192 |
command_prefix = f"switchport trunk {type} vlan " |
| 193 |
if add or commands: |
| 194 |
command_prefix += "add " |
| 195 |
commands.append(command_prefix + ",".join(current_chunk)) |
| 196 |
|
| 197 |
commands = [] |
| 198 |
current_chunk = [] |
| 199 |
current_length = 0 |
| 200 |
|
| 201 |
for vrange in vlans_range.split(","): |
| 202 |
next_addition = vrange if not current_chunk else "," + vrange |
| 203 |
if current_length + len(next_addition) <= 220: |
| 204 |
current_chunk.append(vrange) |
| 205 |
current_length += len(next_addition) |
| 206 |
else: |
| 207 |
append_command() |
| 208 |
current_chunk = [vrange] |
| 209 |
current_length = len(vrange) |
| 210 |
|
| 211 |
if current_chunk: |
| 212 |
append_command() |
| 213 |
|
| 214 |
return commands |
| 215 |
|
| 216 |
|
| 217 |
@total_ordering |
| 218 |
class Version: |
| 219 |
"""Simple class to compare arbitrary versions""" |
| 220 |
|
| 221 |
def __init__(self, version_string): |
| 222 |
self.components = version_string.split(".") |
| 223 |
|
| 224 |
def __eq__(self, other): |
| 225 |
other = _coerce(other) |
| 226 |
if not isinstance(other, Version): |
| 227 |
return NotImplemented |
| 228 |
|
| 229 |
return self.components == other.components |
| 230 |
|
| 231 |
def __lt__(self, other): |
| 232 |
other = _coerce(other) |
| 233 |
if not isinstance(other, Version): |
| 234 |
return NotImplemented |
| 235 |
|
| 236 |
return self.components < other.components |
| 237 |
|
| 238 |
|
| 239 |
def _coerce(other): |
| 240 |
if isinstance(other, str): |
| 241 |
other = Version(other) |
| 242 |
if isinstance(other, (int, float)): |
| 243 |
other = Version(str(other)) |
| 244 |
return other |