def normalize_switchport_tagged(self, tokens): if_name = self.interface_name(tokens[1]) yield self.make_switchport_tagged( interface=if_name, unit=if_name, vlan_filter=",".join( str(x) for x in ranges_to_list(tokens[6], splitter=";")), )
def clean(self, value): if isinstance(value, six.string_types) and not value.strip(): return "" vp = VLANIDParameter() try: return list_to_ranges([vp.clean(v) for v in ranges_to_list(value)]) except SyntaxError: self.raise_error(value)
def parse_displ_port_allow_vlan(self): # Used on CX200 series try: v = self.cli("display port allow-vlan") except self.CLISyntaxError: v = "" for iface, vlan in self.rx_port_allow_vlan.findall(v): yield iface, ranges_to_list(vlan)
def on_interface_tagged(self, tokens): vlans = tokens[0].strip() if vlans.startswith("add"): vlans = vlans.split(" ", 1)[1].strip() si = self.get_current_subinterface() if vlans != "none": for v in ranges_to_list(vlans): si.tagged_vlans += [int(v)] si.add_afi("BRIDGE")
def parse_displ_port_vlan(self): # Used on Quidwai series switches try: v = self.cli("display port vlan") except self.CLISyntaxError: v = "" for iface, vlan in self.rx_port_port_vlan.findall(v): if not vlan.strip("- "): continue yield iface, ranges_to_list(vlan, None)
def on_vlan(self, tokens): """ if - vlan range database - :param tokens: :return: """ if "-" not in tokens[-1] and "database" not in tokens: self.get_vlan_fact(int(tokens[-1].strip())) elif "-" in tokens[-1]: for v in ranges_to_list(tokens[-1].strip()): self.get_vlan_fact(int(v))
def execute(self): interfaces = [] v = self.cli("show lldp local config") for port in self.rx_port.finditer(v): port_no = port.group("port") c = self.cli("show interface port-list %s switchport" % port_no) match = self.rx_vlan.search(c) iface = {"name": "P%s" % port_no, "type": "physical"} sub = {"name": "P%s" % port_no, "enabled_afi": ["BRIDGE"]} if match.group("op_mode") in ["trunk", "hybrid"]: sub["untagged_vlan"] = int(match.group("trunk_native_vlan")) sub["tagged_vlans"] = ranges_to_list(match.group("op_vlans")) else: sub["untagged_vlan"] = int(match.group("access_vlan")) iface["subinterfaces"] = [sub] interfaces += [iface] mac = self.scripts.get_chassis_id()[0]["first_chassis_mac"] v = self.cli("show interface ip") for match in self.rx_ip_iface.finditer(v): ifname = match.group("iface") i = { "name": "ip%s" % ifname, "type": "SVI", "mac": mac, "enabled_protocols": [], "subinterfaces": [{ "name": "ip%s" % ifname, "mac": mac, "enabled_afi": ["IPv4"] }], } addr = match.group("ip") mask = match.group("mask") ip_address = "%s/%s" % (addr, IPv4.netmask_to_len(mask)) i["subinterfaces"][0]["ipv4_addresses"] = [ip_address] interfaces += [i] v = self.cli("show interface ip vlan") for match in self.rx_vlans_ip.finditer(v): vlan_id = match.group("vlan_id") if vlan_id == "none": continue ifname = "ip%s" % match.group("iface") for i in interfaces: if i["name"] == ifname: i["subinterfaces"][0]["vlan_ids"] = vlan_id break return [{"interfaces": interfaces}]
def fn_MatchExactVLAN(self, _input, vlan_filter, vlans): """ Check all `vlans` is within `vlan_filter` expression :param _input: :param vlan_filter: :param vlans: :return: """ for ctx in _input: vf = self.resolve_var(ctx, vlan_filter) if not vf: continue vf = set(ranges_to_list(vf)) vs = self.resolve_var(ctx, vlans) if not vs: continue try: vs = set(ranges_to_list(str(vs))) except SyntaxError: return if not vf.symmetric_difference(vs): yield ctx
def on_interface_tagged(self, tokens): """ undo port trunk allow-pass vlan 1 port trunk allow-pass vlan 118 718 port trunk allow-pass vlan 2 to 4094 port trunk allow-pass vlan 76 90 118 123 126 165 to 169 175 to 179 621 2852 to 2854 port hybrid tagged vlan 51 90 to 91 110 118 319 333 402 419 433 524 to 527 """ si = self.get_current_subinterface() vlans = " ".join(tokens[4:]) if vlans != "none": for v in ranges_to_list(vlans, splitter=" "): si.tagged_vlans += [int(v)] si.add_afi("BRIDGE")
def get_switchport(self): r = {} v = self.cli("show interface * switchport") for block in v.split("\n\n"): if not block: continue b = parse_kv(self.switchport_map, block) if b["mode"] == "trunk": r[b["name"]] = { "tagged_vlans": ranges_to_list(b["tagged"]), "untagged_vlan": b["native"], } else: r[b["name"]] = { "tagged_vlans": [], "untagged_vlan": b["untagged"] } return r
def normalize_vlan_id(self, tokens): for vlan_id in ranges_to_list(tokens[3]): yield self.make_vlan_id(vlan_id=str(vlan_id))
def normalize_vlan(self, tokens): for vid in ranges_to_list(tokens[1]): yield self.make_vlan_id(vlan_id=vid)
def get_data_from_confdb(self): # Get interfaces and parse result interfaces = { d["if_name"]: d for d in self.confdb.query(self.IF_QUERY) } vrfs = {(d["vr"], d["instance"]): d for d in self.confdb.query(self.VRF_QUERY)} instances = defaultdict(dict) for d in self.confdb.query(self.UNIT_QUERY): r = instances[d["vr"], d["instance"]] if not r: r["virtual_router"] = d["vr"] r["forwarding_instance"] = d["instance"] if vrfs and (d["vr"], d["instance"]) in vrfs: try: vrf = vrfs[d["vr"], d["instance"]] r["type"] = vrf["type"] if vrf.get("rd"): r["rd"] = vrf["rd"] r["rt_export"] = vrf.get("rt_export", []) if vrf.get("rt_import"): r["rt_import"] = vrf["rt_import"] if "vpn_id" in vrf: r["vpn_id"] = vrf["vpn_id"] else: r["vpn_id"] = get_vpn_id({ "name": vrf["instance"], "rd": vrf.get("rd"), "rt_export": vrf.get("rt_export", []), "type": vrf["type"].upper() if vrf["type"] in ["vrf", "vpls", "vll"] else vrf["type"], }) except ValueError: pass if "interfaces" not in r: r["interfaces"] = {} if_name = d["if_name"] p_iface = interfaces.get(if_name) iface = r["interfaces"].get(if_name) if iface is None: iface = { "name": if_name, "type": p_iface.get("type", "unknown") if p_iface else "unknown", "admin_status": False, "subinterfaces": {}, } r["interfaces"][if_name] = iface if p_iface: if "description" in p_iface: iface["description"] = p_iface["description"] if "admin_status" in p_iface: iface["admin_status"] = p_iface["admin_status"] == "on" unit = iface["subinterfaces"].get(d["unit"]) if unit is None: unit = {"name": d["unit"], "enabled_afi": []} iface["subinterfaces"][d["unit"]] = unit unit = iface["subinterfaces"][d["unit"]] description = d.get("description") if description: unit["description"] = description elif p_iface and p_iface.get("description"): unit["description"] = p_iface["description"] if "ipv4_addresses" in d: unit["enabled_afi"] += ["IPv4"] unit["ipv4_addresses"] = d["ipv4_addresses"] if "ipv6_addresses" in d: unit["enabled_afi"] += ["IPv6"] unit["ipv6_addresses"] = d["ipv4_addresses"] if "tagged" in d or "untagged" in d: unit["enabled_afi"] += ["BRIDGE"] if "untagged" in d: unit["untagged_vlan"] = int(d["untagged"]) if "tagged" in d: unit["tagged_vlans"] = ranges_to_list(d["tagged"]) # Flatten units r = list(six.itervalues(instances)) for fi in r: # Flatten interfaces fi["interfaces"] = list(six.itervalues(fi["interfaces"])) # Flatten units for i in fi["interfaces"]: i["subinterfaces"] = list(six.itervalues(i["subinterfaces"])) return IGetInterfaces().clean_result(r)
def normalize_vlan_id_batch(self, tokens): for vlan in ranges_to_list(tokens[3]): yield self.make_vlan_id(vlan_id=vlan)
def normalize_switchport_tagged(self, tokens): if_name = self.interface_name(tokens[1]) yield self.make_switchport_tagged(interface=if_name, unit=if_name, vlan_filter=ranges_to_list( tokens[7]))
def on_vlan_range(self, tokens): for v in ranges_to_list(tokens[0].strip()): self.get_vlan_fact(v)
def normalize_vlan_id(self, tokens): splitter = ";" if "," in tokens[1]: splitter = "," for vlan in ranges_to_list(tokens[1], splitter=splitter): yield self.make_vlan_id(vlan_id=vlan)
def execute_cli(self): if self.is_iscom2624g: return self.execute_iscom2624g() lldp_ifaces = [] v = self.cli("show lldp local config") match = self.rx_lldp.search(v) if match: lldp_ifaces = self.expand_rangelist(match.group("ports")) ifaces = [] v = self.cli("show interface port description") for line in v.splitlines()[2:-1]: i = { "name": int(line[:8]), "type": "physical", "snmp_ifindex": int(line[:8]), "subinterfaces": [], } if str(line[8:]) != "-": i["description"] = str(line[8:]) if i["name"] in lldp_ifaces: i["enabled_protocols"] = ["LLDP"] ifaces.append(i) statuses = [] v = self.cli("show interface port") for line in v.splitlines()[5:]: i = { "name": int(line[:6]), "admin_status": "enable" in line[7:14], "oper_status": "up" in line[14:29], } statuses.append(i) vlans = [] v = self.cli("show interface port switchport") for section in v.split("Port"): if not section: continue vlans.append(self.parse_vlans(section)) d = defaultdict(dict) for l in (statuses, ifaces): for elem in l: d[elem["name"]].update(elem) l3 = list(six.itervalues(d)) for port in l3: name = port["name"] port["subinterfaces"] = [{ "name": str(name), "enabled_afi": ["BRIDGE"], "admin_status": port["admin_status"], "oper_status": port["oper_status"], "tagged_vlans": [], "untagged_vlan": [ int(vlan["untagged_vlan"]) for vlan in vlans if int(vlan["name"]) == name ][0], }] if "description" in port: port["subinterfaces"][0]["description"] = port["description"] tvl = [ vlan["op_trunk_allowed_vlan"] for vlan in vlans if int(vlan["name"]) == name ][0] if "n/a" not in tvl: port["subinterfaces"][0]["tagged_vlans"] = ranges_to_list(tvl) if_descr = [] v = self.cli("show interface ip description") for line in v.splitlines()[2:-1]: i = {"name": int(line[:9]), "description": str(line[9:])} if_descr.append(i) if not l3: v = self.cli("show interface description") for match in self.rx_descr.finditer(v): i = { "name": match.group("port"), "type": "physical", "description": match.group("descr").strip(), "enabled_protocols": [], "subinterfaces": [{ "name": match.group("port"), "description": match.group("descr").strip() }], } l3 += [i] v = self.cli("show vlan detail") for match in self.rx_vlan2.finditer(v): vlan_id = int(match.group("vlan_id")) ports = ranges_to_list(match.group("ports")) if match.group("untagged"): untagged = ranges_to_list(match.group("untagged")) else: untagged = [] for i in l3: for p in ports: if i["name"] == "port%s" % p: if p not in untagged: if "tagged_vlans" in i["subinterfaces"][0]: i["subinterfaces"][0]["tagged_vlans"] += [ vlan_id ] else: i["subinterfaces"][0]["tagged_vlans"] = [ vlan_id ] else: i["subinterfaces"][0][ "untagged_vlan"] = vlan_id v = self.profile.get_version(self) mac = v["mac"] # XXX: This is a dirty hack !!! # I do not know, how get ip interface MAC address try: v = self.cli("show interface ip") except self.CLISyntaxError: v = self.cli("show interface ip 0") for match in self.rx_iface.finditer(v): ifname = match.group("iface") i = { "name": "ip%s" % ifname, "type": "SVI", "oper_status": match.group("oper_status") == "active", "admin_status": match.group("oper_status") == "active", "mac": mac, "enabled_protocols": [], "subinterfaces": [{ "name": "ip%s" % ifname, "oper_status": match.group("oper_status") == "active", "admin_status": match.group("oper_status") == "active", "mac": mac, "vlan_ids": [int(match.group("vid"))], "enabled_afi": ["IPv4"], }], } addr = match.group("ip") mask = match.group("mask") ip_address = "%s/%s" % (addr, IPv4.netmask_to_len(mask)) i["subinterfaces"][0]["ipv4_addresses"] = [ip_address] for q in if_descr: if str(q["name"]).strip() == ifname: i["description"] = q["description"] i["subinterfaces"][0]["description"] = q["description"] l3 += [i] try: v = self.cli("show ip interface brief") except self.CLISyntaxError: return [{"interfaces": l3}] for match in self.rx_iface2.finditer(v): ifname = match.group("iface") i = { "name": "ip%s" % ifname, "type": "SVI", "mac": mac, "enabled_protocols": [], "subinterfaces": [{ "name": "ip%s" % ifname, "mac": mac, "enabled_afi": ["IPv4"] }], } addr = match.group("ip") mask = match.group("mask") ip_address = "%s/%s" % (addr, IPv4.netmask_to_len(mask)) i["subinterfaces"][0]["ipv4_addresses"] = [ip_address] l3 += [i] v = self.cli("show interface ip vlan") for match in self.rx_vlans_ip.finditer(v): vlan_id = match.group("vlan_id") if vlan_id == "none": continue ifname = "ip%s" % match.group("iface") for i in l3: if i["name"] == ifname: i["subinterfaces"][0]["vlan_ids"] = vlan_id break return [{"interfaces": l3}]
def execute_cli(self): if self.is_iscom2624g: return self.execute_iscom2624g() lldp_ifaces = self.get_lldp_config() interfaces = {} if not self.is_rotek: v = self.cli("show interface port description") for line in v.splitlines()[2:-1]: ifname = int(line[:8]) interfaces[ifname] = { "name": ifname, "type": "physical", "snmp_ifindex": int(line[:8]), "subinterfaces": [], } if str(line[8:]) != "-": interfaces[ifname]["description"] = str(line[8:]) if ifname in lldp_ifaces: interfaces[ifname]["enabled_protocols"] = ["LLDP"] for port in self.get_iface_statuses(): if port["name"] in interfaces: interfaces[port["name"]].update(port) else: interfaces[port["name"]] = port vlans = self.get_switchport_cli() for ifname in interfaces: port = interfaces[ifname] name = str(port["name"]) port["subinterfaces"] = [ { "name": name, "enabled_afi": ["BRIDGE"], "admin_status": port["admin_status"], "oper_status": port["oper_status"], "tagged_vlans": [], } ] if name in vlans: port["subinterfaces"][0]["untagged_vlan"] = int(vlans[name]["untagged_vlan"]) if "n/a" not in vlans[name]["op_trunk_allowed_vlan"]: port["subinterfaces"][0]["tagged_vlans"] = ranges_to_list( vlans[name]["op_trunk_allowed_vlan"] ) if "description" in port: port["subinterfaces"][0]["description"] = port["description"] if not interfaces: v = self.cli("show interface description") for match in self.rx_descr.finditer(v): i = { "name": match.group("port"), "type": "physical", "description": match.group("descr").strip(), "enabled_protocols": [], "subinterfaces": [ {"name": match.group("port"), "description": match.group("descr").strip()} ], } interfaces[i["name"]] = i v = self.cli("show vlan detail") for match in self.rx_vlan2.finditer(v): vlan_id = int(match.group("vlan_id")) ports = ranges_to_list(match.group("ports")) if match.group("untagged"): untagged = ranges_to_list(match.group("untagged")) else: untagged = [] for p in ports: p_name = "port%s" % p if p_name in interfaces: if p not in untagged: if "tagged_vlans" in interfaces[p_name]["subinterfaces"][0]: interfaces[p_name]["subinterfaces"][0]["tagged_vlans"] += [vlan_id] else: interfaces[p_name]["subinterfaces"][0]["tagged_vlans"] = [vlan_id] else: interfaces[p_name]["subinterfaces"][0]["untagged_vlan"] = vlan_id ifdescr = self.get_iface_ip_description() v = self.scripts.get_chassis_id() mac = v[0]["first_chassis_mac"] # XXX: This is a dirty hack !!! # I do not know, how get ip interface MAC address if not self.is_rotek: try: v = self.cli("show interface ip") except self.CLISyntaxError: v = self.cli("show interface ip 0") for match in self.rx_iface.finditer(v): ifname = match.group("iface") i = { "name": "ip%s" % ifname, "type": "SVI", "oper_status": match.group("oper_status") == "active", "admin_status": match.group("oper_status") == "active", "mac": mac, "enabled_protocols": [], "subinterfaces": [ { "name": "ip%s" % ifname, "oper_status": match.group("oper_status") == "active", "admin_status": match.group("oper_status") == "active", "mac": mac, "vlan_ids": [int(match.group("vid"))], "enabled_afi": ["IPv4"], } ], } addr = match.group("ip") mask = match.group("mask") ip_address = "%s/%s" % (addr, IPv4.netmask_to_len(mask)) i["subinterfaces"][0]["ipv4_addresses"] = [ip_address] if ifname in ifdescr: i["description"] = ifdescr[ifname]["description"] i["subinterfaces"][0]["description"] = ifdescr[ifname]["description"] interfaces[i["name"]] = i try: v = self.cli("show ip interface brief") except self.CLISyntaxError: return [{"interfaces": list(interfaces.values())}] for match in self.rx_iface2.finditer(v): ifname = match.group("iface") i = { "name": "ip%s" % ifname, "type": "SVI", "mac": mac, "enabled_protocols": [], "subinterfaces": [{"name": "ip%s" % ifname, "mac": mac, "enabled_afi": ["IPv4"]}], } addr = match.group("ip") mask = match.group("mask") ip_address = "%s/%s" % (addr, IPv4.netmask_to_len(mask)) i["subinterfaces"][0]["ipv4_addresses"] = [ip_address] interfaces[i["name"]] = i if not self.is_rotek: v = self.cli("show interface ip vlan") for match in self.rx_vlans_ip.finditer(v): vlan_id = match.group("vlan_id") if vlan_id == "none": continue ifname = "ip%s" % match.group("iface") for iname in interfaces: if iname == ifname: interfaces[ifname]["subinterfaces"][0]["vlan_ids"] = vlan_id break return [{"interfaces": list(interfaces.values())}]
def test_ranges_to_list(config, expected): assert ranges_to_list(config) == expected
def execute_cli(self): interfaces = [] v = self.cli("show interface port") for match in self.rx_port.finditer(v): i = { "name": match.group("port"), "type": "physical", "admin_status": match.group("admin_status") == "enable", "oper_status": match.group("admin_status") == "up", "snmp_ifindex": int(match.group("port")), "subinterfaces": [{ "name": match.group("port"), "admin_status": match.group("admin_status") == "enable", "oper_status": match.group("admin_status") == "up", "enabled_afi": ["BRIDGE"], }], } interfaces += [i] v = self.cli("show interface port switchport") for match in self.rx_vlan.finditer(v): port = match.group("port") for iface in interfaces: if iface["name"] == port: sub = iface["subinterfaces"][0] if match.group("op_mode") in ["trunk", "hybrid"]: sub["untagged_vlan"] = int( match.group("trunk_native_vlan")) sub["tagged_vlans"] = ranges_to_list( match.group("op_vlans")) else: sub["untagged_vlan"] = int(match.group("access_vlan")) break v = self.cli("show interface port description") for match in self.rx_descr.finditer(v): port = match.group("port") descr = match.group("descr").strip() if not descr: continue for iface in interfaces: if iface["name"] == port: iface["description"] = descr iface["subinterfaces"][0]["description"] = descr break mac = self.scripts.get_chassis_id()[0]["first_chassis_mac"] v = self.cli("show interface ip") for match in self.rx_ip_iface.finditer(v): ifname = str(int(match.group("iface")) - 1) addr = match.group("ip") mask = match.group("mask") ip_address = "%s/%s" % (addr, IPv4.netmask_to_len(mask)) i = { "name": "IP%s" % ifname, "type": "SVI", "mac": mac, "enabled_protocols": [], "subinterfaces": [{ "name": "ip%s" % ifname, "mac": mac, "enabled_afi": ["IPv4"], "ipv4_addresses": [ip_address], "vlan_ids": [match.group("vlan_id")], }], } interfaces += [i] return [{"interfaces": interfaces}]
def normalize_vlan_id(self, tokens): for vlan in ranges_to_list(tokens[1], splitter=";"): yield self.make_vlan_id(vlan_id=vlan)