Ejemplo n.º 1
0
 def __init__(self, *args, **kwargs):
     super(ExtremeVlan, self).__init__(*args, **kwargs)
     self.extremevlan = ExtremeVlanMib(self.agent)
     self.bridge = BridgeMib(self.agent)
     self.baseport_ifindex = {}
Ejemplo n.º 2
0
class ExtremeVlan(Plugin):
    """Collects 802.1q info from EXTREME-VLAN-MIB and BRIDGE-MIB"""

    def __init__(self, *args, **kwargs):
        super(ExtremeVlan, self).__init__(*args, **kwargs)
        self.extremevlan = ExtremeVlanMib(self.agent)
        self.bridge = BridgeMib(self.agent)
        self.baseport_ifindex = {}

    @classmethod
    def can_handle(cls, netbox):
        daddy_says_ok = super(ExtremeVlan, cls).can_handle(netbox)
        if netbox.type:
            vendor_id = netbox.type.get_enterprise_id()
            if vendor_id != VENDORID_EXTREMENETWORKS:
                return False
        return daddy_says_ok

    def handle(self):
        self._logger.debug(
            "Collecting Extreme Networks proprietary 802.1q VLAN information")
        return self._get_vlan_data()

    @inlineCallbacks
    def _get_vlan_data(self):
        vlan_ports = yield self.extremevlan.get_vlan_ports()
        if not vlan_ports:
            # Doesn't appear to have VLAN data in EXTREME MIBs, get out now.
            returnValue(None)
        ifindex_vlan = yield self.extremevlan.get_ifindex_vlan_map()
        self.baseport_ifindex = yield self.bridge.get_baseport_ifindex_map()

        vlan_config = self._consolidate_vlan_config(vlan_ports, ifindex_vlan)

        self._store_access_ports(vlan_config)
        self._store_trunk_ports(vlan_config)

    def _consolidate_vlan_config(self, vlan_ports, ifindex_vlan):
        config = {}
        for vlan_ifindex, (tagged, untagged) in vlan_ports.items():
            vlanid = ifindex_vlan.get(vlan_ifindex, None)
            if not vlanid:
                continue
            tagged_indexes = self._portnums_to_ifindexes(tagged)
            untagged_indexes = self._portnums_to_ifindexes(untagged)
            config[vlanid] = (tagged_indexes, untagged_indexes)

        return config

    def _portnums_to_ifindexes(self, portnums):
        return [self.baseport_ifindex[portnum]
                for portnum in portnums
                if portnum in self.baseport_ifindex]

    def _store_access_ports(self, vlan_config):
        """Store vlan memberships for all ports."""
        for vlanid, (_tagged, untagged) in vlan_config.items():
            for ifindex in untagged:
                interface = self.containers.factory(ifindex, shadows.Interface)
                interface.trunk = False
                interface.vlan = vlanid

    def _store_trunk_ports(self, vlan_config):
        """Store the set of enabled vlans for each trunk port."""
        ifindex_vlans = {}
        for vlanid, (tagged, _untagged) in vlan_config.items():
            for ifindex in tagged:
                if ifindex not in ifindex_vlans:
                    ifindex_vlans[ifindex] = set()
                ifindex_vlans[ifindex].add(vlanid)

        for ifindex, vlans in ifindex_vlans.items():
            interface = self.containers.factory(ifindex, shadows.Interface)
            interface.trunk = True
            interface.vlan = None

            allowed = self.containers.factory(ifindex,
                                              shadows.SwPortAllowedVlan)
            allowed.interface = interface
            allowed.hex_string = vlan_list_to_hex(vlans)