示例#1
0
    def _get_netif_or_derivative(self, hostname, netif):
        """
        Get netif or derivative names of hostname

        netif can be a macaddr, but to be conservative, it will only work if
        only one interface has this macaddr.
        """
        interfaces = self._get_interfaces_for_device(hostname)

        if netif in interfaces:
            return interfaces[netif]

        if is_macaddr(netif):
            mac_addresses = defaultdict(list)
            for i in interfaces:
                mac_addresses[macaddr_to_int(
                    interfaces[i].mac_address)].append(interfaces[i])

            int_netif_mac = macaddr_to_int(netif)
            if len(mac_addresses.get(int_netif_mac, 0)) == 1:
                return mac_addresses[int_netif_mac][0]
        else:
            for netif_deriv in self._get_all_derivatives_for_netif(netif):
                for k in interfaces:
                    for i in self._get_all_derivatives_for_netif(k):
                        if i == netif_deriv:
                            return interfaces[k]

        raise ValueError("Interface {} not found".format(netif))
    def get_interfaces(self):
        assert self.device.device

        napalm_interfaces = self.device.get_interfaces()

        interfaces = {}
        trunks = []
        for ifname, napalm_ifprops in napalm_interfaces.items():
            is_subif, parent_if = self._is_subinterface(ifname)
            if is_subif:
                trunks.append(parent_if)
                continue

            if not is_macaddr(napalm_ifprops["mac_address"]):
                napalm_ifprops["mac_address"] = None

            interfaces[ifname] = {
                "enabled": napalm_ifprops["is_enabled"],
                # Netbox max descr size is 100 char
                "description": (napalm_ifprops["description"] or "")[:100],
                "mac_address": napalm_ifprops["mac_address"] or None,
                # wait for this pull request
                # https://github.com/napalm-automation/napalm/pull/531
                "mtu": napalm_ifprops.get("mtu", None),
                "type": self.specific_parser.get_interface_type(ifname),
                "mode": None,
            }

        for trunk in trunks:
            if trunk in interfaces:
                interfaces[trunk]["mode"] = "Tagged"

        interfaces_lag = self.specific_parser.get_interfaces_lag(interfaces)
        for ifname, lag in interfaces_lag.items():
            try:
                real_lag_name = (self._search_key_case_insensitive(
                    interfaces, lag))
            except KeyError:
                logger.error("%s not exist in polled interfaces", lag)
                continue

            interfaces[ifname]["lag"] = real_lag_name
            interfaces[real_lag_name]["type"] = (
                "Link Aggregation Group (LAG)")

        return interfaces
    def get_interfaces(self):
        assert self.device.device

        napalm_interfaces = self.device.get_interfaces()

        interfaces = {}
        trunks = []
        # sort to test it more easily
        for ifname, napalm_ifprops in sorted(napalm_interfaces.items()):
            is_subif, parent_if = self._is_subinterface(ifname)
            if is_subif:
                trunks.append(parent_if)
                continue

            if not is_macaddr(napalm_ifprops["mac_address"]):
                napalm_ifprops["mac_address"] = None

            _type = self.specific_parser.get_interface_type(ifname)
            if _type == "Other":
                logger.info(
                    "Switch %s iface type for the port %s not defined.",
                    self.hostname, ifname)
            try:
                mode = {
                    "access": "Access",
                    "trunk": "Tagged",
                    "static access": "Access",
                    None: None
                }[self.specific_parser.get_interface_mode(ifname)]
            except NotImplementedError:
                mode = None
            except KeyError as ex:
                logger.debug("Switch %s Interface %s unknown mode: %s.",
                             self.hostname, ifname, ex)
                mode = None

            interfaces[ifname] = {
                "enabled": napalm_ifprops["is_enabled"],
                # Netbox max descr size is 100 char
                "description": (napalm_ifprops["description"] or "")[:100],
                "mac_address": napalm_ifprops["mac_address"] or None,
                # wait for this pull request
                # https://github.com/napalm-automation/napalm/pull/531
                "mtu": napalm_ifprops.get("mtu", None),
                "type": _type,
                "mode": mode,
                "untagged_vlan": None,
                "tagged_vlans": [],
            }

            if mode == "Access":
                try:
                    interfaces[ifname]["untagged_vlan"] = \
                        self.specific_parser.get_interface_access_vlan(ifname)
                except NotImplementedError:
                    interfaces[ifname]["untagged_vlan"] = None

            try:
                vlans = self.specific_parser.get_interface_vlans(ifname)
            except NotImplementedError:
                vlans = None

            if vlans:
                interfaces[ifname]["tagged_vlans"] = vlans

        for ifname, data in interfaces.items():
            if data["mode"] == "Tagged":
                try:
                    navive = self.specific_parser.get_interface_netive_vlan(
                        ifname)
                except NotImplementedError:
                    navive = None
                if navive in data["tagged_vlans"]:
                    interfaces[ifname]["untagged_vlan"] = navive
                    interfaces[ifname]["tagged_vlans"].pop(navive)

        for trunk in trunks:
            if trunk in interfaces:
                interfaces[trunk]["mode"] = "Tagged"

        interfaces_lag = self.specific_parser.get_interfaces_lag(interfaces)
        for ifname, lag in interfaces_lag.items():
            try:
                real_lag_name = (self._search_key_case_insensitive(
                    interfaces, lag))
            except KeyError:
                logger.error("%s not exist in polled interfaces", lag)
                continue

            interfaces[ifname]["lag"] = real_lag_name
            interfaces[real_lag_name]["type"] = (
                "Link Aggregation Group (LAG)")

        return interfaces
示例#4
0
 def test_is_macaddr_false2(self):
     assert is_macaddr('00:11:22:AA:44:Gg') == False
示例#5
0
 def test_is_macaddr_true2(self):
     assert is_macaddr('001122AA44cc') == True
示例#6
0
 def test_is_macaddr_true1(self):
     assert is_macaddr('00.11:22:AA:44.ff') == True