示例#1
0
 def sanitize_policy_profile_table(self):
     """Clear policy profiles from stale VSM."""
     db_session = db.get_session()
     hosts = config.get_vsm_hosts()
     vsm_info = db_session.query(
         n1kv_models.PolicyProfile.vsm_ip).distinct()
     if vsm_info is None or hosts is None:
         return
     vsm_ips = [vsm_ip[0] for vsm_ip in vsm_info if vsm_ip[0] not in hosts]
     for vsm_ip in vsm_ips:
         pprofiles = n1kv_db.get_policy_profiles_by_host(vsm_ip, db_session)
         for pprofile in pprofiles:
             # Do not delete profile if it is in use and if it
             # is the only VSM to have it configured
             pp_in_use = n1kv_db.policy_profile_in_use(
                 pprofile['id'], db_session)
             num_vsm_using_pp = db_session.query(
                 n1kv_models.PolicyProfile).filter_by(
                     id=pprofile['id']).count()
             if (not pp_in_use) or (num_vsm_using_pp > 1):
                 db_session.delete(pprofile)
                 db_session.flush()
             else:
                 LOG.warning(
                     _LW('Cannot delete policy profile %s '
                         'as it is in use.'), pprofile['id'])
 def sanitize_policy_profile_table(self):
     """Clear policy profiles from stale VSM."""
     db_session = db.get_session()
     hosts = config.get_vsm_hosts()
     vsm_info = db_session.query(
         n1kv_models.PolicyProfile.vsm_ip).distinct()
     if vsm_info is None or hosts is None:
         return
     vsm_ips = [vsm_ip[0] for vsm_ip in vsm_info if vsm_ip[0] not in hosts]
     for vsm_ip in vsm_ips:
         pprofiles = n1kv_db.get_policy_profiles_by_host(vsm_ip, db_session)
         for pprofile in pprofiles:
             # Do not delete profile if it is in use and if it
             # is the only VSM to have it configured
             pp_in_use = n1kv_db.policy_profile_in_use(pprofile['id'],
                                                       db_session)
             num_vsm_using_pp = db_session.query(
                 n1kv_models.PolicyProfile).filter_by(
                 id=pprofile['id']).count()
             if (not pp_in_use) or (num_vsm_using_pp > 1):
                 db_session.delete(pprofile)
                 db_session.flush()
             else:
                 LOG.warning(_LW('Cannot delete policy profile %s '
                                 'as it is in use.'), pprofile['id'])
 def _populate_policy_profiles(self):
     """Populate all the policy profiles from VSM."""
     hosts = config.get_vsm_hosts()
     for vsm_ip in hosts:
         try:
             policy_profiles = self.n1kvclient.list_port_profiles(vsm_ip)
             vsm_profiles = {}
             plugin_profiles_set = set()
             # Fetch policy profiles from VSM
             for profile_name in policy_profiles:
                 profile_id = (policy_profiles[profile_name]
                               [n1kv_const.PROPERTIES][n1kv_const.ID])
                 vsm_profiles[profile_id] = profile_name
             # Fetch policy profiles previously populated
             for profile in self._get_policy_profiles_by_host(vsm_ip):
                 plugin_profiles_set.add(profile.id)
             vsm_profiles_set = set(vsm_profiles)
             # Update database if the profile sets differ.
             if vsm_profiles_set.symmetric_difference(plugin_profiles_set):
                 # Add new profiles to database if they were created in VSM
                 for pid in vsm_profiles_set.difference(
                                             plugin_profiles_set):
                     self._add_policy_profile(pid, vsm_profiles[pid],
                                              vsm_ip)
                 # Delete profiles from database if they were deleted in VSM
                 for pid in plugin_profiles_set.difference(
                                                vsm_profiles_set):
                     if not n1kv_db.policy_profile_in_use(pid):
                         self._remove_policy_profile(pid, vsm_ip)
                     else:
                         LOG.warning(_LW('Policy profile %s in use'), pid)
         except (n1kv_exc.VSMError, n1kv_exc.VSMConnectionFailed):
             with excutils.save_and_reraise_exception(reraise=False):
                 LOG.warning(_LW('No policy profile populated from VSM'))
     self.sanitize_policy_profile_table()
示例#4
0
 def __init__(self, db_base_plugin_obj):
     self.n1kvclient = n1kv_client.Client()
     self.db_base_plugin = db_base_plugin_obj
     self.sync_resource = {n1kv_const.NETWORK_PROFILES: False,
                           n1kv_const.NETWORKS: False,
                           n1kv_const.SUBNETS: False,
                           n1kv_const.PORTS: False}
     self.sync_sleep_duration = cfg.CONF.ml2_cisco_n1kv.sync_interval
     # default to True so that BDs for all VSMs are synced at a neutron
     # restart
     self.sync_bds = {vsm_ip: True for vsm_ip in
                      config.get_vsm_hosts()}
     self.bd_names = set()
示例#5
0
 def __init__(self, db_base_plugin_obj):
     self.n1kvclient = n1kv_client.Client()
     self.db_base_plugin = db_base_plugin_obj
     self.sync_resource = {n1kv_const.NETWORK_PROFILES: False,
                           n1kv_const.NETWORKS: False,
                           n1kv_const.SUBNETS: False,
                           n1kv_const.PORTS: False}
     self.sync_sleep_duration = cfg.CONF.ml2_cisco_n1kv.sync_interval
     # default to True so that BDs for all VSMs are synced at a neutron
     # restart
     self.sync_bds = {vsm_ip: True for vsm_ip in
                      config.get_vsm_hosts()}
     self.bd_names = set()
示例#6
0
def get_policy_profile_by_uuid(db_session, pprofile_id):
    """
    Retrieve policy profile by its UUID.

    :param db_session: database session
    :param profile_id: string representing the UUID of the policy profile
    :returns: policy profile object
    """
    db_session = db_session or db.get_session()
    vsm_hosts = config.get_vsm_hosts()
    pp = n1kv_models.PolicyProfile
    pprofiles = (db_session.query(pp).filter(
        sql.and_(pp.id == pprofile_id, pp.vsm_ip.in_(vsm_hosts))).all())
    if pprofiles and check_policy_profile_exists_on_all_vsm(
            pprofiles, vsm_hosts):
        return pprofiles[0]
    else:
        raise n1kv_exc.PolicyProfileNotFound(profile=pprofile_id)
示例#7
0
def get_policy_profile_by_name(name, db_session=None):
    """
    Retrieve policy profile by name.

    :param name: string representing the name of the policy profile
    :param db_session: database session
    :returns: policy profile object
    """
    db_session = db_session or db.get_session()
    vsm_hosts = config.get_vsm_hosts()
    pp = n1kv_models.PolicyProfile
    pprofiles = db_session.query(pp).filter(
        sql.and_(pp.name == name, pp.vsm_ip.in_(vsm_hosts))).all()
    if pprofiles and check_policy_profile_exists_on_all_vsm(
            pprofiles, vsm_hosts):
        return pprofiles[0]
    else:
        raise n1kv_exc.PolicyProfileNotFound(profile=name)
示例#8
0
def get_policy_profile_by_name(name, db_session=None):
    """
    Retrieve policy profile by name.

    :param name: string representing the name of the policy profile
    :param db_session: database session
    :returns: policy profile object
    """
    db_session = db_session or db.get_session()
    vsm_hosts = config.get_vsm_hosts()
    pp = n1kv_models.PolicyProfile
    pprofiles = db_session.query(pp).filter(
        sql.and_(pp.name == name, pp.vsm_ip.in_(vsm_hosts))).all()
    if pprofiles and check_policy_profile_exists_on_all_vsm(pprofiles,
                                                            vsm_hosts):
        return pprofiles[0]
    else:
        raise n1kv_exc.PolicyProfileNotFound(profile=name)
示例#9
0
def get_policy_profile_by_uuid(db_session, pprofile_id):
    """
    Retrieve policy profile by its UUID.

    :param db_session: database session
    :param profile_id: string representing the UUID of the policy profile
    :returns: policy profile object
    """
    db_session = db_session or db.get_session()
    vsm_hosts = config.get_vsm_hosts()
    pp = n1kv_models.PolicyProfile
    pprofiles = (db_session.query(pp).
                 filter(sql.and_(pp.id == pprofile_id,
                 pp.vsm_ip.in_(vsm_hosts))).all())
    if pprofiles and check_policy_profile_exists_on_all_vsm(pprofiles,
                                                            vsm_hosts):
        return pprofiles[0]
    else:
        raise n1kv_exc.PolicyProfileNotFound(profile=pprofile_id)
示例#10
0
    def do_sync(self):
        """
        Entry point function for VSM-Neutron sync.

        Triggered on an eventlet from the N1kv mechanism driver.
        """
        while True:
            try:
                vsm_hosts = config.get_vsm_hosts()
                for vsm_ip in vsm_hosts:
                    try:
                        self._sync_vsm(vsm_ip=vsm_ip)
                    except n1kv_exc.VSMConnectionFailed:
                        LOG.warning('Sync thread exception: VSM '
                                    '%s unreachable.' % vsm_ip)
                    except n1kv_exc.VSMError:
                        LOG.warning('Sync thread exception: Internal '
                                    'server error on VSM %s.' % vsm_ip)
            except Exception as e:
                LOG.warning('Sync thread exception: %s' % e.message)
            eventlet.sleep(seconds=self.sync_sleep_duration)
示例#11
0
    def do_sync(self):
        """
        Entry point function for VSM-Neutron sync.

        Triggered on an eventlet from the N1kv mechanism driver.
        """
        while True:
            try:
                vsm_hosts = config.get_vsm_hosts()
                for vsm_ip in vsm_hosts:
                    try:
                        self._sync_vsm(vsm_ip=vsm_ip)
                    except n1kv_exc.VSMConnectionFailed:
                        LOG.warning(_LW('Sync thread exception: VSM '
                                        '%s unreachable.') % vsm_ip)
                    except n1kv_exc.VSMError:
                        LOG.warning(_LW('Sync thread exception: Internal '
                                        'server error on VSM %s.') % vsm_ip)
            except Exception as e:
                LOG.warning(_LW('Sync thread exception: %s') % e.message)
            eventlet.sleep(seconds=self.sync_sleep_duration)
示例#12
0
    def __init__(self, **kwargs):
        """Initialize a new client for the plugin."""
        self.format = 'json'

        # Extract configuration parameters from the configuration file.
        self.username = cfg.CONF.ml2_cisco_n1kv.username
        self.password = cfg.CONF.ml2_cisco_n1kv.password
        self.vsm_ips = config.get_vsm_hosts()
        self.action_prefix = 'http://%s/api/n1k'
        self.timeout = cfg.CONF.ml2_cisco_n1kv.http_timeout
        self.max_vsm_retries = cfg.CONF.ml2_cisco_n1kv.max_vsm_retries
        required_opts = ('vsm_ips', 'username', 'password')
        # Validate whether required options are configured
        for opt in required_opts:
            if not getattr(self, opt):
                raise cfg.RequiredOptError(opt, 'ml2_cisco_n1kv')
        # Validate the configured VSM IP addresses
        # Note: Currently only support IPv4
        for vsm_ip in self.vsm_ips:
            if not (netaddr.valid_ipv4(vsm_ip) or netaddr.valid_ipv6(vsm_ip)):
                raise cfg.Error(
                    _("Cisco Nexus1000V ML2 driver config: "
                      "Invalid format for VSM IP address: %s") % vsm_ip)
示例#13
0
    def __init__(self, **kwargs):
        """Initialize a new client for the plugin."""
        self.format = 'json'

        # Extract configuration parameters from the configuration file.
        self.username = cfg.CONF.ml2_cisco_n1kv.username
        self.password = cfg.CONF.ml2_cisco_n1kv.password
        self.vsm_ips = config.get_vsm_hosts()
        self.action_prefix = 'http://%s/api/n1k'
        self.timeout = cfg.CONF.ml2_cisco_n1kv.http_timeout
        self.max_vsm_retries = cfg.CONF.ml2_cisco_n1kv.max_vsm_retries
        required_opts = ('vsm_ips', 'username', 'password')
        # Validate whether required options are configured
        for opt in required_opts:
            if not getattr(self, opt):
                raise cfg.RequiredOptError(opt, 'ml2_cisco_n1kv')
        # Validate the configured VSM IP addresses
        # Note: Currently only support IPv4
        for vsm_ip in self.vsm_ips:
            if not (netaddr.valid_ipv4(vsm_ip) or netaddr.valid_ipv6(vsm_ip)):
                raise cfg.Error(_("Cisco Nexus1000V ML2 driver config: "
                                  "Invalid format for VSM IP address: %s") %
                                vsm_ip)