Exemple #1
0
    def execute(self):
        interfaces = []
        links = self.profile.parseBrokenJson(
            self.http.get("/link.b", cached=True, eof_mark="}"))
        vlans = self.profile.parseBrokenJson(
            self.http.get("/vlan.b", cached=True, eof_mark="}"))
        try:
            fwds = self.profile.parseBrokenJson(
                self.http.get("/fwd.b", cached=True, eof_mark="}"))
        except HTTPError:
            fwds = links
        sys_info = self.profile.parseBrokenJson(
            self.http.get("/sys.b", cached=True, eof_mark="}"))
        if links.get("prt"):
            prt = int(links["prt"], 16)
            sfp = int(links.get("sfp", "0x0"), 16)
            sfpo = int(links.get("sfpo", "0x0"), 16)
        else:
            if self.is_platform_6port1sfp:
                prt = 6
                sfp = 1
                sfpo = 5
        if sfpo + sfp != prt:
            raise self.UnexpectedResultError("prt=%d sfp=%d sfpo=%d" %
                                             (prt, sfp, sfpo))

        BITS = dict((i, 2**i) for i in range(self.PORT_RANGE))
        oper_statuses = dict((i, bool(int(links["lnk"], 16) & BITS[i]))
                             for i in range(self.PORT_RANGE))
        admin_statuses = dict((i, bool(int(links["an"], 16) & BITS[i]))
                              for i in range(self.PORT_RANGE))

        for port in range(1, prt + 1):
            if port <= sfpo:
                ifname = "Port%d" % int(port)
            else:
                if sfp > 1:
                    ifname = "SFP%d" % (int(port) - sfpo)
                else:
                    ifname = "SFP"
            if links.get("nm"):
                descr = links["nm"][port - 1].decode("hex")
            elif links.get("nm%d" % (port - 1)):
                descr = links["nm%d" % (port - 1)].decode("hex")
            else:
                descr = None
            iface = {
                "name": ifname,
                "type": "physical",
                "oper_status": oper_statuses[port - 1],
                "admin_status": admin_statuses[port - 1],
            }
            sub = {
                "name": ifname,
                "enabled_afi": ["BRIDGE"],
                "oper_status": oper_statuses[port - 1],
                "admin_status": admin_statuses[port - 1],
            }
            if descr:
                iface["description"] = descr
                sub["description"] = descr
            tagged_vlans = []
            for vlan in vlans:
                vid = int(vlan["vid"], 16)
                if vlan.get("mbr"):
                    ports = dict((i, bool(int(vlan["mbr"], 16) & BITS[i]))
                                 for i in range(self.PORT_RANGE))
                else:
                    ports = dict((i, bool(int(vlan["prt"][i], 16) & BITS[i]))
                                 for i in xrange(len(vlan["prt"])))
                if ports[port - 1]:
                    tagged_vlans += [vid]
            untagged = int(fwds["dvid"][port - 1], 16)
            if int(fwds["vlni"][port - 1], 16) != 1:  # only tagged
                sub["untagged_vlan"] = untagged
            if int(fwds["vlni"][port - 1], 16) != 2:  # only untagged
                sub["tagged_vlans"] = tagged_vlans
            iface["subinterfaces"] = [sub]
            interfaces += [iface]

        ip = self.profile.swap32(int(sys_info["ip"], 16))
        vlan_id = int(sys_info["avln"], 16)
        if vlan_id == 0:
            vlan_id = 1
        iface = {
            "name":
            "mgmt",
            "type":
            "SVI",
            "oper_status":
            True,
            "admin_status":
            True,
            "subinterfaces": [{
                "name": "mgmt",
                "enabled_afi": ["IPv4"],
                "ipv4_addresses": [IPv4._to_prefix(ip, 32)],
                "oper_status": True,
                "admin_status": True,
                "vlan_ids": vlan_id,
            }],
        }
        interfaces += [iface]

        return [{"interfaces": interfaces}]
Exemple #2
0
 def execute(self):
     self.portchannel = self.scripts.get_portchannel()
     self.if_stp = []
     self.interfaces = []
     self.iface = {}
     self.subiface = {}
     self.parent = ""
     self.snmp_ifindex = 0
     for s in self.cli("ifconfig -v", cached=True).splitlines():
         match = self.rx_if_name.search(s)
         if match:
             self.snmp_ifindex += 1
             self.add_iface()
             flags = match.group("flags")
             self.iface["name"] = match.group("ifname")
             self.subiface["name"] = match.group("ifname")
             self.iface["admin_status"] = flags.startswith("UP,")
             self.subiface["admin_status"] = flags.startswith("UP,")
             self.subiface["enabled_afi"] = []
             self.subiface["mtu"] = int(match.group("mtu"))
             self.iface["snmp_ifindex"] = self.snmp_ifindex
             self.iface["enabled_protocols"] = []
             if "LOOPBACK" in flags:
                 self.iface["type"] = "loopback"
                 self.iface["oper_status"] = flags.startswith("UP,")
                 self.subiface["oper_status"] = flags.startswith("UP,")
             if "POINTOPOINT" in flags:
                 self.iface["type"] = "tunnel"
             continue
         match = self.rx_if_descr.search(s)
         if match:
             self.iface["descriptions"] = match.group("descr")
             self.subiface["descriptions"] = match.group("descr")
             continue
         match = self.rx_if_mac.search(s)
         if match:
             self.iface["mac"] = match.group("mac")
             self.subiface["mac"] = match.group("mac")
             self.iface["type"] = "physical"
             continue
         match = self.rx_if_inet.search(s)
         if match:
             ip = match.group("inet")
             netmask = match.group("netmask")
             mask = IPv4._to_prefix(int(netmask, 16), 32).address
             mask = IPv4.netmask_to_len(mask)
             ipv4_addr = "%s/%s" % (ip, mask)
             if "ipv4_addresses" in self.subiface:
                 self.subiface["ipv4_addresses"] += [ipv4_addr]
             else:
                 self.subiface["ipv4_addresses"] = [ipv4_addr]
                 self.subiface["enabled_afi"] += ["IPv4"]
             continue
         match = self.rx_if_inet6.search(s)
         if match:
             ipv6 = match.group("inet6")
             if ipv6.find("%") >= 0:
                 continue
             prefixlen = match.group("prefixlen")
             ipv6_addr = "%s/%s" % (ipv6, prefixlen)
             if "ipv6_addresses" in self.subiface:
                 self.subiface["ipv6_addresses"] += [ipv6_addr]
             else:
                 self.subiface["ipv6_addresses"] = [ipv6_addr]
                 self.subiface["enabled_afi"] += ["IPv6"]
             continue
         match = self.rx_if_status.search(s)
         if match:
             self.iface["oper_status"] = True
             self.subiface["oper_status"] = True
             continue
         match = self.rx_if_vlan.search(s)
         if match:
             self.subiface.update({"vlan_ids": [int(match.group("vlan"))]})
             self.parent = match.group("parent")
             continue
         for i in self.portchannel:
             if self.iface["name"] == i["interface"]:
                 self.iface["type"] = "aggregated"
                 # self.subiface["enabled_afi"] = ["BRIDGE"]
             if self.iface["name"] in i["members"]:
                 if i["type"] == "L" and "LACP" not in self.iface[
                         "enabled_protocols"]:
                     self.iface["enabled_protocols"] += ["LACP"]
                 self.iface["aggregated_interface"] = i["interface"]
         match = self.rx_if_wlan.search(s)
         if match:
             self.parent = "IEEE 802.11"
             continue
         match = self.rx_if_bridge.search(s)
         if match:
             self.iface["type"] = "SVI"
             if "BRIDGE" not in self.subiface["enabled_afi"]:
                 self.subiface["enabled_afi"] += ["BRIDGE"]
             continue
         match = self.rx_if_bridge_m.search(s)
         if match:
             ifname = match.group("ifname")
             continue
         match = self.rx_if_bridge_i.search(s)
         if match:
             caps = {
                 "name": ifname,
                 "ifindex": match.group("ifindex"),
                 "parent": self.iface["name"],
             }
             match = self.rx_if_bridge_s.search(s)
             if match:
                 caps["STP"] = True
             self.if_stp += [caps]
     self.add_iface()
     if len(self.if_stp) > 0:
         for i in self.interfaces:
             for s in self.if_stp:
                 if i["name"] == s["name"]:
                     # For verify
                     i["snmp_ifindex"] = int(s["ifindex"])
                     i["aggregated_interface"] = s["parent"]
                 if "STP" in s:
                     i["enabled_protocols"] += ["STP"]
     return [{"interfaces": self.interfaces}]