From 0f1d2dbff0258128556fd6785e396b92e4b618b4 Mon Sep 17 00:00:00 2001 From: Andrey Vertiprahov Date: Sun, 18 Aug 2019 19:12:31 +0500 Subject: [PATCH] Backport. NSN.TIMOS.get_lldp_neighbors port descripton regex. --- sa/profiles/NSN/TIMOS/get_interfaces.py | 539 ++++++++++---------- sa/profiles/NSN/TIMOS/get_lldp_neighbors.py | 49 +- 2 files changed, 311 insertions(+), 277 deletions(-) diff --git a/sa/profiles/NSN/TIMOS/get_interfaces.py b/sa/profiles/NSN/TIMOS/get_interfaces.py index f087d91b65..b995e48b90 100644 --- a/sa/profiles/NSN/TIMOS/get_interfaces.py +++ b/sa/profiles/NSN/TIMOS/get_interfaces.py @@ -9,6 +9,7 @@ # Python modules import re + # NOC modules from noc.core.script.base import BaseScript from noc.sa.interfaces.igetinterfaces import IGetInterfaces @@ -19,8 +20,9 @@ class Script(BaseScript): name = "NSN.TIMOS.get_interfaces" interface = IGetInterfaces - re_int = re.compile(r'-{79}\nInterface\n-{79}', re.MULTILINE) - re_int_desc_vprn = re.compile(r""" + re_int = re.compile(r"-{79}\nInterface\n-{79}", re.MULTILINE) + re_int_desc_vprn = re.compile( + r""" If\sName\s*?:\s(?P.*?)\n .*? Admin\sState\s*?:\s(?P.*?)\s+? @@ -37,8 +39,11 @@ class Script(BaseScript): MAC\sAddress\s*?:\s(?P.*?)\s .*? IP\sOper\sMTU\s*?:\s(?P.*?)\s - .*?""", re.VERBOSE | re.MULTILINE | re.DOTALL) - re_int_desc_subs = re.compile(r""" + .*?""", + re.VERBOSE | re.MULTILINE | re.DOTALL, + ) + re_int_desc_subs = re.compile( + r""" ^If\sName\s*?:\s(?P.*?)\n .*? Admin\sState\s*?:\s(?P.*?)\s+? @@ -50,8 +55,11 @@ class Script(BaseScript): Details\n -{79}\n Description\s*?:\s(?P.*?)\n - """, re.VERBOSE | re.MULTILINE | re.DOTALL) - re_int_desc_group = re.compile(r""" + """, + re.VERBOSE | re.MULTILINE | re.DOTALL, + ) + re_int_desc_group = re.compile( + r""" ^If\sName\s*?:\s(?P.*?)\n Sub\sIf\sName\s*?:\s(?P.+?)\s+?\n .*? @@ -69,12 +77,18 @@ class Script(BaseScript): .*? IP\sOper\sMTU\s*?:\s(?P.*?)\s .*? - """, re.VERBOSE | re.MULTILINE | re.DOTALL) + """, + re.VERBOSE | re.MULTILINE | re.DOTALL, + ) re_iface = re.compile(r"^(?P\d+/\d+/\d+|lag-\d+)") - re_ipaddr = re.compile(r""" + re_ipaddr = re.compile( + r""" (IPv6\sAddr|IP\sAddr/mask)\s.*?:\s(?P.+?)(\s|$) - """, re.VERBOSE | re.MULTILINE | re.DOTALL) - re_saps = re.compile(r""" + """, + re.VERBOSE | re.MULTILINE | re.DOTALL, + ) + re_saps = re.compile( + r""" sap:(?P.+?): (?P(\d+?|\*))\. (?P(\d+?|\*))\s+ @@ -83,8 +97,11 @@ class Script(BaseScript): (?P.+?)\s+ (?P.+?)\s+ (?P.+) - """, re.MULTILINE | re.DOTALL | re.VERBOSE) - re_vpls = re.compile(r""" + """, + re.MULTILINE | re.DOTALL | re.VERBOSE, + ) + re_vpls = re.compile( + r""" Service\sId\s+:\s(?P.+?)\s .*? Name\s+?:\s(?P.+?)\n @@ -95,20 +112,27 @@ class Script(BaseScript): MTU\s+?:\s(?P.+?)\s .+? Identifier.+?-{79}\n(?P.+?)={79} - """, re.MULTILINE | re.DOTALL | re.VERBOSE) + """, + re.MULTILINE | re.DOTALL | re.VERBOSE, + ) re_forwarding_instance = re.compile( - r'^(?P\d+)\s+(?P\S+)\s+(?P\S+)\s+(?P\S+)', - re.MULTILINE) - re_rd = re.compile(r'^Route Dist.\s+:\s(?P.+?)\s', re.MULTILINE) - re_cards_detail = re.compile(r""" + r"^(?P\d+)\s+(?P\S+)\s+(?P\S+)\s+(?P\S+)", + re.MULTILINE, + ) + re_rd = re.compile(r"^Route Dist.\s+:\s(?P.+?)\s", re.MULTILINE) + re_cards_detail = re.compile( + r""" -{79}\n (?P[A-B])\s+?sfm\d*-\d*\s+ (?P.*?)\s+ (?P.*?/.*?)\s .+? Base\sMAC\saddress\s*?:\s(?P.*?)\n - """, re.VERBOSE | re.MULTILINE | re.DOTALL) - re_port_info = re.compile(""" + """, + re.VERBOSE | re.MULTILINE | re.DOTALL, + ) + re_port_info = re.compile( + """ ^(?P\d+/\d+/\d+)\s+ (?P\S*)\s+ (?P\S*)\s+ @@ -116,8 +140,11 @@ class Script(BaseScript): (?P\d*)\s+ (?P\d*)\s+ (?P\d*)\s - """, re.VERBOSE | re.MULTILINE | re.DOTALL) - re_port_detail_info = re.compile(""" + """, + re.VERBOSE | re.MULTILINE | re.DOTALL, + ) + re_port_detail_info = re.compile( + """ Description\s*?:\s(?P.*?)\n Interface\s*?:\s(?P\d*/\d*/\S*)\s* .*? @@ -130,8 +157,11 @@ class Script(BaseScript): IfIndex\s*?:\s(?P\d*)\s .*? Configured\sAddress\s*?:\s(?P.*?)\s - """, re.VERBOSE | re.MULTILINE | re.DOTALL) - re_lag_detail = re.compile(""" + """, + re.VERBOSE | re.MULTILINE | re.DOTALL, + ) + re_lag_detail = re.compile( + """ Description\s*?:\s(?P.+?)\n-{79} .*? Detail @@ -146,13 +176,19 @@ class Script(BaseScript): Lag-IfIndex\s+:\s(?P.*?)\s .*? LACP\s*?:\s(?P.*?)\s - """, re.VERBOSE | re.MULTILINE | re.DOTALL) - re_lag_split = re.compile(r""" + """, + re.VERBOSE | re.MULTILINE | re.DOTALL, + ) + re_lag_split = re.compile( + r""" -{79}\n (?PLAG\s\d+.+?) Port-id\s+Adm - """, re.VERBOSE | re.MULTILINE | re.DOTALL) - re_lag_subs = re.compile(r"""(?P.+?): + """, + re.VERBOSE | re.MULTILINE | re.DOTALL, + ) + re_lag_subs = re.compile( + r"""(?P.+?): (?P.+?)\s+ (?P.+?)\s+ (?P.+?)\s+ @@ -161,7 +197,9 @@ class Script(BaseScript): (?P.+?)\s+ (?P.+?)\s+ (?P.+?)\s+ - """, re.VERBOSE | re.MULTILINE | re.DOTALL) + """, + re.VERBOSE | re.MULTILINE | re.DOTALL, + ) @staticmethod def fix_protocols(protocols): @@ -169,17 +207,17 @@ class Script(BaseScript): :rtype : list """ proto = [] - if 'None' in protocols: + if "None" in protocols: return [] - if 'OSPFv2' in protocols: + if "OSPFv2" in protocols: proto += ["OSPF"] - if 'OSPFv3' in protocols: + if "OSPFv3" in protocols: proto += ["OSPFv3"] - if 'PIM' in protocols: + if "PIM" in protocols: proto += ["PIM"] - if 'IGMP' in protocols: + if "IGMP" in protocols: proto += ["IGMP"] - if 'RSVP' in protocols: + if "RSVP" in protocols: proto += ["RSVP"] return proto @@ -191,11 +229,7 @@ class Script(BaseScript): """ :rtype : dict """ - result = { - 'ipv4_addresses': [], - 'ipv6_addresses': [], - 'enabled_afi': [], - } + result = {"ipv4_addresses": [], "ipv6_addresses": [], "enabled_afi": []} if "Unnumbered If" in ipaddr_section: return result for line in ipaddr_section.splitlines(): @@ -203,166 +237,147 @@ class Script(BaseScript): if match_obj: afi = match_obj.group(1) ip = match_obj.group(2) - if afi == 'IP Addr/mask' and 'Not' not in ip: - result['ipv4_addresses'] += [ip] - elif afi == 'IPv6 Addr' and is_ipv6(ip): - result['ipv6_addresses'] += [ip] - if result['ipv4_addresses']: - result['enabled_afi'] += ['IPv4'] - if result['ipv6_addresses']: - result['enabled_afi'] += ['IPv6'] + if afi == "IP Addr/mask" and "Not" not in ip: + result["ipv4_addresses"] += [ip] + elif afi == "IPv6 Addr" and is_ipv6(ip): + result["ipv6_addresses"] += [ip] + if result["ipv4_addresses"]: + result["enabled_afi"] += ["IPv4"] + if result["ipv6_addresses"]: + result["enabled_afi"] += ["IPv6"] return result def parse_interfaces(self, data, vrf): ifaces = self.re_int.split(data) result = [] - iftypeVPRN = ': VPRN' - iftypeNetwork = ': Network' - iftypeSubsc = ': VPRN Sub' - iftypeGroup = ': VPRN Grp' - iftypeRed = ': VPRN Red' - iftypeIES = ': IES' + iftypeVPRN = ": VPRN" + iftypeNetwork = ": Network" + iftypeSubsc = ": VPRN Sub" + iftypeGroup = ": VPRN Grp" + iftypeRed = ": VPRN Red" + iftypeIES = ": IES" for iface in ifaces[1:]: - parent_iface = '' + parent_iface = "" my_dict = {} if iftypeGroup in iface: match_obj = self.re_int_desc_group.search(iface) if match_obj: my_dict = match_obj.groupdict() - my_dict['type'] = 'other' - my_dict['subinterfaces'] = [] - + my_dict["type"] = "other" + my_dict["subinterfaces"] = [] elif iftypeSubsc in iface: match_obj = self.re_int_desc_subs.search(iface) my_dict = match_obj.groupdict() - my_dict['subinterfaces'] = [{}] - my_dict['type'] = 'loopback' + my_dict["subinterfaces"] = [{}] + my_dict["type"] = "loopback" my_sub = { - 'oper_status': my_dict['oper_status'], - 'admin_status': my_dict['admin_status'], - 'name': my_dict['name'], + "oper_status": my_dict["oper_status"], + "admin_status": my_dict["admin_status"], + "name": my_dict["name"], } - if 'enabled_afi' in my_dict: - my_sub['enabled_afi'] = my_dict['enabled_afi'] - my_dict.pop('enabled_afi') - if 'ipv4_addresses' in my_dict: - my_sub['ipv4_addresses'] = my_dict['ipv4_addresses'] - my_dict.pop('ipv4_addresses') - if 'ipv6_addresses' in my_dict: - my_sub['ipv6_addresses'] = my_dict['ipv6_addresses'] - my_dict.pop('ipv6_addresses') - - my_dict['subinterfaces'][0].update(my_sub) - + if "enabled_afi" in my_dict: + my_sub["enabled_afi"] = my_dict["enabled_afi"] + my_dict.pop("enabled_afi") + if "ipv4_addresses" in my_dict: + my_sub["ipv4_addresses"] = my_dict["ipv4_addresses"] + my_dict.pop("ipv4_addresses") + if "ipv6_addresses" in my_dict: + my_sub["ipv6_addresses"] = my_dict["ipv6_addresses"] + my_dict.pop("ipv6_addresses") + my_dict["subinterfaces"][0].update(my_sub) elif iftypeRed in iface: match_obj = self.re_int_desc_vprn.search(iface) my_dict = match_obj.groupdict() - if 'subinterfaces' in my_dict: - my_dict['subinterfaces'] = [ - { - 'name': my_dict['subinterfaces'], - 'type': 'tunnel' - } + if "subinterfaces" in my_dict: + my_dict["subinterfaces"] = [ + {"name": my_dict["subinterfaces"], "type": "tunnel"} ] - my_dict['type'] = 'tunnel' - - elif ( - iftypeNetwork in iface or - iftypeVPRN in iface or - iftypeIES in iface - ): + my_dict["type"] = "tunnel" + elif iftypeNetwork in iface or iftypeVPRN in iface or iftypeIES in iface: match_obj = self.re_int_desc_vprn.search(iface) if match_obj: my_dict = match_obj.groupdict() - if 'subinterfaces' in my_dict: - if my_dict['subinterfaces'].startswith('sdp'): - my_dict['type'] = 'tunnel' - elif my_dict['subinterfaces'].startswith('loopback'): - my_dict['type'] = 'loopback' - match = self.re_iface.search(my_dict['subinterfaces']) + if "subinterfaces" in my_dict: + if my_dict["subinterfaces"].startswith("sdp"): + my_dict["type"] = "tunnel" + elif my_dict["subinterfaces"].startswith("loopback"): + my_dict["type"] = "loopback" + match = self.re_iface.search(my_dict["subinterfaces"]) if match: parent_iface = match.group("iface") - if ":" in my_dict['subinterfaces']: - vlans = my_dict['subinterfaces'].split(":")[1] + if ":" in my_dict["subinterfaces"]: + vlans = my_dict["subinterfaces"].split(":")[1] if "." in vlans and "*" not in vlans: up_tag, down_tag = vlans.split(".") if is_vlan(down_tag): - my_dict['vlan_ids'] = [int(up_tag), int(down_tag)] + my_dict["vlan_ids"] = [int(up_tag), int(down_tag)] else: - my_dict['vlan_ids'] = [int(up_tag)] + my_dict["vlan_ids"] = [int(up_tag)] elif "*" in vlans: - my_dict['vlan_ids'] = [] + my_dict["vlan_ids"] = [] else: - my_dict['vlan_ids'] = [int(vlans)] - - my_dict['subinterfaces'] = [ - { - 'name': my_dict['name'] - } - ] + my_dict["vlan_ids"] = [int(vlans)] + my_dict["subinterfaces"] = [{"name": my_dict["name"]}] else: continue - if my_dict['description'] == '(Not Specified)': - my_dict.pop('description') - proto = my_dict['protocols'] - my_dict['protocols'] = self.fix_protocols(my_dict['protocols']) - if 'srrp' in my_dict: - my_dict['protocols'] += ['SRRP'] - my_dict.pop('srrp') - - my_dict['oper_status'] = self.fix_status(my_dict['oper_status']) - my_dict['admin_status'] = self.fix_status(my_dict['admin_status']) - if 'ipaddr_section' in my_dict: - my_dict.update(self.fix_ip_addr(my_dict['ipaddr_section'])) - my_dict.pop('ipaddr_section') - if 'subinterfaces' in my_dict: + if my_dict["description"] == "(Not Specified)": + my_dict.pop("description") + proto = my_dict["protocols"] + my_dict["protocols"] = self.fix_protocols(my_dict["protocols"]) + if "srrp" in my_dict: + my_dict["protocols"] += ["SRRP"] + my_dict.pop("srrp") + my_dict["oper_status"] = self.fix_status(my_dict["oper_status"]) + my_dict["admin_status"] = self.fix_status(my_dict["admin_status"]) + if "ipaddr_section" in my_dict: + my_dict.update(self.fix_ip_addr(my_dict["ipaddr_section"])) + my_dict.pop("ipaddr_section") + if "subinterfaces" in my_dict: if not isinstance(my_dict["subinterfaces"], (list, dict)): - my_dict['subinterfaces'] = [my_dict['subinterfaces']] - if len(my_dict['subinterfaces']) == 1: + my_dict["subinterfaces"] = [my_dict["subinterfaces"]] + if len(my_dict["subinterfaces"]) == 1: my_sub = { - 'oper_status': my_dict['oper_status'], - 'admin_status': my_dict['admin_status'], - 'protocols': my_dict['protocols'] + "oper_status": my_dict["oper_status"], + "admin_status": my_dict["admin_status"], + "protocols": my_dict["protocols"], } - my_dict.pop('protocols') - if 'enabled_afi' in my_dict: - my_sub['enabled_afi'] = my_dict['enabled_afi'] - my_dict.pop('enabled_afi') - if 'ipv4_addresses' in my_dict: - my_sub['ipv4_addresses'] = my_dict['ipv4_addresses'] - my_dict.pop('ipv4_addresses') - if 'ipv6_addresses' in my_dict: - my_sub['ipv6_addresses'] = my_dict['ipv6_addresses'] - my_dict.pop('ipv6_addresses') - if 'vlan_ids' in my_dict: - my_sub['vlan_ids'] = my_dict['vlan_ids'] - my_dict.pop('vlan_ids') - if 'MPLS' in proto: - if 'enabled_afi' in my_sub: - my_sub['enabled_afi'] += ['MPLS'] + my_dict.pop("protocols") + if "enabled_afi" in my_dict: + my_sub["enabled_afi"] = my_dict["enabled_afi"] + my_dict.pop("enabled_afi") + if "ipv4_addresses" in my_dict: + my_sub["ipv4_addresses"] = my_dict["ipv4_addresses"] + my_dict.pop("ipv4_addresses") + if "ipv6_addresses" in my_dict: + my_sub["ipv6_addresses"] = my_dict["ipv6_addresses"] + my_dict.pop("ipv6_addresses") + if "vlan_ids" in my_dict: + my_sub["vlan_ids"] = my_dict["vlan_ids"] + my_dict.pop("vlan_ids") + if "MPLS" in proto: + if "enabled_afi" in my_sub: + my_sub["enabled_afi"] += ["MPLS"] else: - my_sub['enabled_afi'] = ['MPLS'] - if 'mac' in my_dict: - my_sub['mac'] = my_dict['mac'] - if 'mtu' in my_dict: - my_sub['mtu'] = my_dict['mtu'] - my_dict.pop('mtu') - my_dict['subinterfaces'][0].update(my_sub) + my_sub["enabled_afi"] = ["MPLS"] + if "mac" in my_dict: + my_sub["mac"] = my_dict["mac"] + if "mtu" in my_dict: + my_sub["mtu"] = my_dict["mtu"] + my_dict.pop("mtu") + my_dict["subinterfaces"][0].update(my_sub) if vrf: found = False for i in vrf: - if i['name'] == parent_iface: - my_sub['name'] = my_dict['name'] - i['subinterfaces'] += [my_sub] + if i["name"] == parent_iface: + my_sub["name"] = my_dict["name"] + i["subinterfaces"] += [my_sub] found = True break if found: continue - - if 'type' not in my_dict: - my_dict['type'] = 'unknown' - + if "type" not in my_dict: + my_dict["type"] = "unknown" result += [my_dict] return result @@ -377,59 +392,58 @@ class Script(BaseScript): return fitype def fix_vpls_saps(self, sap_section): - result = {'interfaces': []} + result = {"interfaces": []} for line in sap_section.splitlines(): match_obj = self.re_saps.match(line) if match_obj: raw_sap = match_obj.groupdict() sap = { - 'name': raw_sap['interface'], - 'subinterfaces': [{ - 'enabled_afi': ['BRIDGE'], - 'name': str(raw_sap['interface'] + ":" + raw_sap['uptag'] + "." + raw_sap['downtag']), - 'oper_status': self.fix_status(raw_sap['oper_status']), - 'admin_status': self.fix_status(raw_sap['admin_status']), - 'mtu': raw_sap['mtu'], - 'vlan_ids': [raw_sap['uptag'], raw_sap['downtag']], - }] + "name": raw_sap["interface"], + "subinterfaces": [ + { + "enabled_afi": ["BRIDGE"], + "name": str( + raw_sap["interface"] + + ":" + + raw_sap["uptag"] + + "." + + raw_sap["downtag"] + ), + "oper_status": self.fix_status(raw_sap["oper_status"]), + "admin_status": self.fix_status(raw_sap["admin_status"]), + "mtu": raw_sap["mtu"], + "vlan_ids": [raw_sap["uptag"], raw_sap["downtag"]], + } + ], } - if "*" in sap['subinterfaces'][0]['name']: - sap['subinterfaces'][0].pop('vlan_ids') - if 'lag' in sap['name']: - sap['type'] = 'aggregated' + if "*" in sap["subinterfaces"][0]["name"]: + sap["subinterfaces"][0].pop("vlan_ids") + if "lag" in sap["name"]: + sap["type"] = "aggregated" else: - sap['type'] = 'physical' - - result['interfaces'] += [sap] - + sap["type"] = "physical" + result["interfaces"] += [sap] return result def get_vpls(self, vpls_id): - result = { - 'forwarding_instance': vpls_id, - 'type': 'VPLS' - } - vpls = self.cli('show service id %s base' % vpls_id) + result = {"forwarding_instance": vpls_id, "type": "VPLS"} + vpls = self.cli("show service id %s base" % vpls_id) match_obj = self.re_vpls.search(vpls) if match_obj: result = match_obj.groupdict() - result['oper_status'] = self.fix_status(result['oper_status']) - result['admin_status'] = self.fix_status(result['admin_status']) + result["oper_status"] = self.fix_status(result["oper_status"]) + result["admin_status"] = self.fix_status(result["admin_status"]) - result['type'] = 'bridge' + result["type"] = "bridge" - if result['forwarding_instance'] == '(Not Specified)': - result['forwarding_instance'] = result['id'] - if result['sap_section']: - result.update(self.fix_vpls_saps(result['sap_section'])) - result.pop('sap_section') + if result["forwarding_instance"] == "(Not Specified)": + result["forwarding_instance"] = result["id"] + if result["sap_section"]: + result.update(self.fix_vpls_saps(result["sap_section"])) + result.pop("sap_section") else: - result['interfaces'] = { - 'type': 'unknown', - 'subinterfaces': {'name': 'empty_vpls'}, - } - + result["interfaces"] = {"type": "unknown", "subinterfaces": {"name": "empty_vpls"}} return result def get_forwarding_instance(self): @@ -439,77 +453,74 @@ class Script(BaseScript): mo1 = self.re_forwarding_instance.search(line) if mo1: fi = mo1.groupdict() - fi['type'] = self.fix_fi_type(fi['type']) - fi['oper_status'] = self.fix_status(fi['oper_status']) - fi['admin_status'] = self.fix_status(fi['admin_status']) - - if fi['type'] == 'ip' or fi['type'] == 'VRF': - r = self.cli('show service id %s base | match invert-match "sap:"' % - fi["forwarding_instance"]) + fi["type"] = self.fix_fi_type(fi["type"]) + fi["oper_status"] = self.fix_status(fi["oper_status"]) + fi["admin_status"] = self.fix_status(fi["admin_status"]) + + if fi["type"] == "ip" or fi["type"] == "VRF": + r = self.cli( + 'show service id %s base | match invert-match "sap:"' + % fi["forwarding_instance"] + ) mo2 = self.re_rd.search(r) - fi["rd"] = mo2.group('rd') - if fi["rd"] == 'None': - fi.pop('rd') - - intf = self.cli('show router %s interface detail' % fi["forwarding_instance"]) - fi['interfaces'] = self.parse_interfaces(intf, '') - - elif fi["type"] == 'bridge': - fi.update(self.get_vpls(fi['forwarding_instance'])) - fi.pop('id') + fi["rd"] = mo2.group("rd") + if fi["rd"] == "None": + fi.pop("rd") + intf = self.cli("show router %s interface detail" % fi["forwarding_instance"]) + fi["interfaces"] = self.parse_interfaces(intf, "") + elif fi["type"] == "bridge": + fi.update(self.get_vpls(fi["forwarding_instance"])) + fi.pop("id") elif fi["type"] == "Unsupported": continue result.append(fi) - return result def get_managment_router(self): - fi = {'forwarding_instance': 'management', 'type': 'ip', 'interfaces': []} - card_detail = self.cli('show card detail') + fi = {"forwarding_instance": "management", "type": "ip", "interfaces": []} + card_detail = self.cli("show card detail") cards = self.re_cards_detail.findall(card_detail) for card in cards: sub_iface = self.cli('show router "management" interface detail') - fi['interfaces'].append({ - 'name': "/".join([card[0], "1"]), - 'admin_status': self.fix_status(card[1]), - 'oper_status': self.fix_status(card[2]), - 'protocols': [], - 'mac': card[3], - 'type': 'physical', - 'subinterfaces': self.parse_interfaces(sub_iface, ''), - }) - + fi["interfaces"].append( + { + "name": "/".join([card[0], "1"]), + "admin_status": self.fix_status(card[1]), + "oper_status": self.fix_status(card[2]), + "protocols": [], + "mac": card[3], + "type": "physical", + "subinterfaces": self.parse_interfaces(sub_iface, ""), + } + ) return fi def get_base_router(self): - fi = { - 'forwarding_instance': 'default', - 'type': 'ip', - 'interfaces': [] - } + fi = {"forwarding_instance": "default", "type": "ip", "interfaces": []} - port_info = self.cli('show port') + port_info = self.cli("show port") for line in port_info.splitlines(): match = self.re_port_info.search(line) if match: - port_detail = self.cli('show port %s detail' % match.group('name')) + port_detail = self.cli("show port %s detail" % match.group("name")) match_detail = self.re_port_detail_info.search(port_detail) my_dict = match.groupdict() my_dict.update(match_detail.groupdict()) - if 'aggregated_interface' in my_dict: - if is_int(my_dict['aggregated_interface']): - my_dict['aggregated_interface'] = "-".join(["lag", my_dict['aggregated_interface']]) + if "aggregated_interface" in my_dict: + if is_int(my_dict["aggregated_interface"]): + my_dict["aggregated_interface"] = "-".join( + ["lag", my_dict["aggregated_interface"]] + ) else: - del my_dict['aggregated_interface'] - my_dict['type'] = 'physical' - my_dict['subinterfaces'] = [] - my_dict.pop('bad_stat') - my_dict['description'] = my_dict['description'].replace("\n", "") - fi['interfaces'].append(my_dict) - - lag_info = self.cli('show lag detail') + del my_dict["aggregated_interface"] + my_dict["type"] = "physical" + my_dict["subinterfaces"] = [] + my_dict.pop("bad_stat") + my_dict["description"] = my_dict["description"].replace("\n", "") + fi["interfaces"].append(my_dict) + lag_info = self.cli("show lag detail") lags = self.re_lag_split.split(lag_info) @@ -517,43 +528,46 @@ class Script(BaseScript): match = self.re_lag_detail.search(lag) if match: my_dict = match.groupdict() - my_dict['type'] = 'aggregated' - if my_dict['name']: - my_dict['name'] = "-".join(['lag', my_dict['name']]) - my_dict['subinterfaces'] = [] - saps = self.cli('show service sap-using sap %s | match invert-match [' % my_dict['name']) + my_dict["type"] = "aggregated" + if my_dict["name"]: + my_dict["name"] = "-".join(["lag", my_dict["name"]]) + my_dict["subinterfaces"] = [] + saps = self.cli( + "show service sap-using sap %s | match invert-match [" % my_dict["name"] + ) for sapline in saps.splitlines(): sap = self.re_lag_subs.match(sapline) if sap: - if sap.group('physname'): - vlans = sap.group('sapname') + if sap.group("physname"): + vlans = sap.group("sapname") s = { - 'name': ":".join([sap.group('physname'), vlans]), - 'admin_status': self.fix_status(sap.group('admin_status')), - 'oper_status': self.fix_status(sap.group('admin_status')), + "name": ":".join([sap.group("physname"), vlans]), + "admin_status": self.fix_status(sap.group("admin_status")), + "oper_status": self.fix_status(sap.group("admin_status")), } if "." in vlans and "*" not in vlans: up_tag, down_tag = vlans.split(".") if is_vlan(down_tag): - s['vlan_ids'] = [int(up_tag), int(down_tag)] + s["vlan_ids"] = [int(up_tag), int(down_tag)] else: - s['vlan_ids'] = [int(up_tag)] + s["vlan_ids"] = [int(up_tag)] elif "*" in vlans: - s['vlan_ids'] = [] + s["vlan_ids"] = [] + elif int(vlans) == 0: + s["vlan_ids"] = [] else: - s['vlan_ids'] = [int(vlans)] - my_dict['subinterfaces'].append(s) - my_dict['oper_status'] = self.fix_status(my_dict['oper_status']) - my_dict['admin_status'] = self.fix_status(my_dict['admin_status']) - if my_dict['protocols'].lower() == 'enabled': - my_dict['protocols'] = ['LACP'] + s["vlan_ids"] = [int(vlans)] + my_dict["subinterfaces"].append(s) + my_dict["oper_status"] = self.fix_status(my_dict["oper_status"]) + my_dict["admin_status"] = self.fix_status(my_dict["admin_status"]) + if my_dict["protocols"].lower() == "enabled": + my_dict["protocols"] = ["LACP"] else: - my_dict['protocols'] = [] - my_dict['description'] = my_dict['description'].replace("\n", "") - fi['interfaces'].append(my_dict) - + my_dict["protocols"] = [] + my_dict["description"] = my_dict["description"].replace("\n", "") + fi["interfaces"].append(my_dict) intf = self.cli('show router "Base" interface detail') - fi['interfaces'] += self.parse_interfaces(intf, fi['interfaces']) + fi["interfaces"] += self.parse_interfaces(intf, fi["interfaces"]) return fi @@ -563,7 +577,6 @@ class Script(BaseScript): fi = self.get_forwarding_instance() for forw_instance in fi: result += [forw_instance] - fi = self.get_managment_router() result += [fi] diff --git a/sa/profiles/NSN/TIMOS/get_lldp_neighbors.py b/sa/profiles/NSN/TIMOS/get_lldp_neighbors.py index 87c57e649c..ab0d4d8d43 100644 --- a/sa/profiles/NSN/TIMOS/get_lldp_neighbors.py +++ b/sa/profiles/NSN/TIMOS/get_lldp_neighbors.py @@ -8,6 +8,7 @@ # Python modules import re + # NOC modules from noc.core.script.base import BaseScript from noc.sa.interfaces.igetlldpneighbors import IGetLLDPNeighbors @@ -25,10 +26,17 @@ class Script(BaseScript): r"^Chassis Id Subtype\s+:\s(?P\d)\s.*\n" r"^Chassis Id\s+:\s(?P\S+)\s*\n" r"^PortId Subtype\s+:\s(?P\d)\s.*\n" - r"^Port\sId\s+:\s(?P.+?)\n" - r"^Port\sDescription\s+:\s(?P.+?)\n" + r"^Port\sId\s+:\s(?P(.+\n?){1,3}?)\n" # Port Id : 70:6F:72:74:20:32:37\n"port 27" + r"^Port\sDescription\s+:\s(?P.+\n?.+?)\n" r"^System\sName\s+:\s(?P\S+)\n", - re.MULTILINE) + re.MULTILINE, + ) + rx_port_descr = re.compile( + r"(?P\S+)," + r"\s*(?P.+),\n?" + r"\s*\"(?P.+)" + ) + rx_port_id = re.compile(r"^\s*(?P.+)\s+\"(?P.+)\"", re.DOTALL) CAPS_MAP = { "other": 1, @@ -39,7 +47,7 @@ class Script(BaseScript): "telephone": 32, "docsis": 64, "station": 128, - "cvlan": 0 + "cvlan": 0, } NOT_SPECIFIED = "(not specified)" @@ -65,8 +73,19 @@ class Script(BaseScript): match_obj = self.rx_remote_info.search(v) pri = match_obj.groupdict() pri["remote_capabilities"] = self.fixcaps(pri["remote_capabilities"]) - if 'n/a' in pri['remote_system_name']: - del pri['remote_system_name'] + if "n/a" in pri["remote_system_name"]: + del pri["remote_system_name"] + # print("Match Port Description", self.rx_port_descr.match(pri["remote_port_description"])) + if self.rx_port_descr.match(pri["remote_port_description"]): + pri["remote_port_description"] = ( + self.rx_port_descr.match(pri["remote_port_description"]) + .group("remote_port_description") + .strip(' "') + ) + elif "n/a" in pri["remote_port"]: + del pri["remote_port_description"] + if self.rx_port_id.match(pri["remote_port"]): + pri["remote_port"] = self.rx_port_id.match(pri["remote_port"]).group("port_name") return pri def execute_cli(self): @@ -79,16 +98,18 @@ class Script(BaseScript): match = self.rx_some.search(line) if not match: continue - port = match.group('port') - local_lldp = self.cli('show port %s ethernet detail | match IfIndex' % port) + port = match.group("port") + local_lldp = self.cli("show port %s ethernet detail | match IfIndex" % port) lldp_match = self.rx_local_lldp.search(local_lldp) if not lldp_match: continue - local_interface_id = str(lldp_match.group('local_interface_id')) + local_interface_id = str(lldp_match.group("local_interface_id")) pri = self.get_port_info(port) - r += [{ - "local_interface": port, - "local_interface_id": local_interface_id, - "neighbors": [pri] - }] + r += [ + { + "local_interface": port, + "local_interface_id": local_interface_id, + "neighbors": [pri], + } + ] return r -- GitLab