def _dissociate_mac_from_port_profile(self, vfab_id, vlanid, mac_address,
                                          config=None, do_not_commit=False):
        """Dissociates a MAC address from a port profile."""

        config = config if config else self.mgr.get_running_config()
        pprofile = self._get_pprofile(vlanid, mac_address, config)
        if pprofile is None:
            return
        index = _get_vfab_pprofile_index(
            vfab_id, pprofile, mac_address, config)
        if index is not None:
            delete_port_profile = True
            for m in _VFAB_PPROFILE_RE.finditer(config):
                if m.group(3) == pprofile and not (
                        m.group(1) == vfab_id and m.group(2) == index):
                    delete_port_profile = False
                    break
            commit = False if do_not_commit else (not delete_port_profile)
            self.mgr.configure(
                ["no vfab {vfab_id} pprofile {index}".format(
                    vfab_id=vfab_id, index=index)], commit=commit)
            if delete_port_profile:
                commit = False if do_not_commit else delete_port_profile
                self.mgr.configure(["no pprofile {pid}".format(pid=pprofile)],
                    commit=commit)
        else:
            LOG.warning(
                _LW("No corresponding vfab pprofile for %(vid)s, %(mac)s"),
                dict(vid=vlanid, mac=mac_address))
    def _associate_mac_to_port_profile(self, vfab_id, vlanid, mac_address):
        """Associates a MAC address to a port profile."""

        running_config = self.mgr.get_running_config()
        pprofile = self._create_port_profile(
            vlanid, mac_address, running_config, commit=False)
        index, profile_name = _search_vfab_pprofile(
            vfab_id, mac_address, running_config)
        if index is None:
            index = _get_available_vfab_pprofile_index(
                vfab_id, running_config)
            if index is None:
                LOG.error(_LE("No unused vfab pprofile index"))
                raise ml2_exc.MechanismDriverError(
                    method="_associate_mac_to_port_profile")
        else:
            if pprofile == profile_name:
                return
            else:
                LOG.warning(
                    _LW('Override "vfab %(vfab_id)s pprofile %(index)d vsiid '
                        'mac %(mac)s %(profile_name)s" to "vsiid mac %(mac)s '
                        '%(pprofile)s"'),
                    dict(vfab_id=vfab_id, index=index, mac=mac_address,
                         profile_name=profile_name, pprofile=pprofile))
        self.mgr.configure(
            ["vfab {vfab_id} pprofile {index} vsiid mac {mac} "
             "{pid}".format(
                 vfab_id=vfab_id, index=index, mac=mac_address, pid=pprofile)])
    def _create_port_profile(self, vlanid, mac_address, running_config=None,
                             commit=True):
        """Creates a port profile."""

        if running_config is None:
            running_config = self.mgr.get_running_config()
        if self._share_pprofile:
            pprofile = self._get_pprofile(vlanid, mac_address, running_config)
            if pprofile is None:
                pprofile = self._get_new_pprofile(
                    vlanid, mac_address, running_config)
                self._configure_pprofile(pprofile, vlanid, commit)
        else:
            pprofile = self._get_new_pprofile(
                vlanid, mac_address, running_config)
            match = re.search(
                r"^pprofile\s+{pid}\s+vlan\s+tag\s+([0-9,-]+)".format(
                    pid=re.escape(pprofile)), running_config, re.MULTILINE)
            if match:
                if match.group(1) != str(vlanid):
                    LOG.warning(
                        _LW('Override "pprofile %(pid)s vlan tag %(vids)s" '
                            'to "vlan tag %(vlanid)s"'),
                        dict(pid=pprofile, vids=match.group(1),
                             vlanid=vlanid))
                    self._configure_pprofile(pprofile, vlanid, commit)
            else:
                self._configure_pprofile(pprofile, vlanid, commit)
        return pprofile
    def validate_network(network):
        """Validate network parameter(network_type and segmentation_id).

        @param a network object
        @return True if network_type is 'VLAN' and segmentation_id is included
                otherwise False
        """

        segment = network.network_segments[0]
        seg_id = segment[driver_api.SEGMENTATION_ID]
        net_type = segment[driver_api.NETWORK_TYPE]
        if (net_type in _SUPPORTED_NET_TYPES and seg_id):
            return True
        LOG.warning(_LW("Only network type vlan is supported."))
        return False
    def _reconnect(self):
        """Re-connect and initialize the CLI session."""

        # Close the old connection
        self._close_session()
        # Open new TELNET connection
        try:
            self._telnet = telnetlib.Telnet(
                host=self._address, port=TELNET_PORT, timeout=self._timeout)
        except EnvironmentError:
            with excutils.save_and_reraise_exception():
                LOG.exception(_LE("Connect failed to switch"))
        try:
            prompt = ""
            prompt = self._telnet.read_until(_PROMPT_LOGIN, _TIMEOUT_LOGIN)
            prompt.index(_PROMPT_LOGIN)
            self._telnet.write(self._username + "\n")
            prompt = self._telnet.read_until(_PROMPT_PASS, _TIMEOUT_LOGIN)
            prompt.index(_PROMPT_PASS)
            self._telnet.write(self._password + "\n")
            prompt = self._telnet.read_until(_PROMPT_ADMIN, _TIMEOUT_LOGIN)
            prompt.index(_PROMPT_ADMIN)
        except (EOFError, EnvironmentError, ValueError):
            if _PROMPT_BUSY in prompt:
                # Wait 3 seconds
                if self._retry_count < self._max_retry:
                    LOG.warning(_LW("Switch is busy. Wait(%ssec) and retry."),
                        _WAIT_FOR_BUSY)
                    self._retry_count += 1
                    time.sleep(_WAIT_FOR_BUSY)
                    self._reconnect()
                    return
                with excutils.save_and_reraise_exception():
                    self._retry_count = 0
                    LOG.exception(_LE("Number of retry times has reached."))
            else:
                self._telnet.close()
                self._telnet = None
                self._retry_count = 0
                with excutils.save_and_reraise_exception():
                    LOG.exception(_LE("Login failed to switch.(%s)"), prompt)

        self._retry_count = 0
        LOG.debug("Connect success to address %(address)s:%(telnet_port)s",
                  dict(address=self._address, telnet_port=TELNET_PORT))
Example #6
0
def get_physical_connectivity(port):
    """Get local_link_information from specified port.

    @param port a port object
    @return lli a list of following dict
                {"switch_id": "MAC_of_switch", "port_id": "1/1/0/1",
                 "switch_info": "switch_name"}
    """

    # TODO(yushiro) replace following characters to constant value
    binding_profile = port['binding:profile']
    lli = binding_profile.get("local_link_information", {})
    is_all_specified = True if lli else False
    for i in lli:
        if not (i.get('switch_id') and i.get('port_id') and
                i.get('switch_info')):
            is_all_specified = False
    if is_all_specified:
        return lli
    LOG.warning(_LW("Some physical network param is missing:%s"), lli)
    return {}
def _get_associated_lag_id(ports, config):
    """Get lag_id which is associated to the ports."""

    ids = []
    interfaces = ports.split(",")
    for port in interfaces:
        match = re.search(
            r'^interface\s+{port}$\n((?:\s+.+\n)'
            r'*(\s+type\s+{port_type}\s+(\d+))(?:\s+.+\n)*)\s+exit$'.format(
                port=port, port_type='linkaggregation'), config, re.MULTILINE)
        if match:
            ids.append(match.group(3))
    lag_ids = list(set(ids))
    if not lag_ids:
        return None
    if len(lag_ids) > 1:
        LOG.warning(
            _LW("Each port%(ports)s has different LAG ids(%(lag_ids)s)"),
            dict(ports=ports, lag_ids=lag_ids))
    LOG.debug(_("Associated LAG%(lag_ids)s with interfaces:%(ports)s"),
        dict(lag_ids=lag_ids, ports=ports))
    return sorted(lag_ids)[0]