Example #1
0
 def parse_conf_result(gauge_file, gauge_dir):
     """Return True if config parses successfully."""
     try:
         cp.watcher_parser(gauge_file, gauge_dir, None)
     except InvalidConfigError:
         return False
     return True
Example #2
0
 def parse_conf_result(gauge_file, gauge_dir):
     """Return True if config parses successfully."""
     try:
         cp.watcher_parser(gauge_file, gauge_dir, None)
     except InvalidConfigError:
         return False
     return True
Example #3
0
    def test_no_faucet_config_file(self):
        """Test missing FAUCET config."""
        GAUGE_CONF = """
faucet:
    dps:
        dp1:
            dp_id: 1
            interfaces:
                1:
                    native_vlan: v1
    vlans:
        v1:
            vid: 1
watchers:
    port_stats_poller:
        type: 'port_stats'
        dps: ['dp1']
        db: 'prometheus'
dbs:
    prometheus:
        type: 'prometheus'
"""
        gauge_file, _ = self.create_config_files(GAUGE_CONF, '')
        watcher_conf = cp.watcher_parser(
            gauge_file, 'gauge_config_test', None)[0]
        msg = 'failed to create watcher correctly when dps configured in gauge.yaml'
        self.assertEqual(watcher_conf.dps[0], 'dp1', msg)
        self.assertEqual(watcher_conf.type, 'port_stats', msg)
        self.assertEqual(watcher_conf.db_type, 'prometheus', msg)
    def test_no_faucet_config_file(self):
        """Test missing FAUCET config."""
        GAUGE_CONF = """
faucet:
    dps:
        dp1:
            dp_id: 1
            interfaces:
                1:
                    native_vlan: v1
    vlans:
        v1:
            vid: 1
watchers:
    port_stats_poller:
        type: 'port_stats'
        dps: ['dp1']
        db: 'prometheus'
dbs:
    prometheus:
        type: 'prometheus'
"""
        gauge_file, _ = self.create_config_files(GAUGE_CONF, '')
        watcher_conf = cp.watcher_parser(gauge_file, 'gauge_config_test',
                                         None)[0]
        msg = 'failed to create watcher correctly when dps configured in gauge.yaml'
        self.assertEqual(watcher_conf.dps[0], 'dp1', msg)
        self.assertEqual(watcher_conf.type, 'port_stats', msg)
        self.assertEqual(watcher_conf.db_type, 'prometheus', msg)
Example #5
0
    def _load_config(self):
        """Load Gauge config."""
        for watcher_dpid, old_watchers in list(self.watchers.items()):
            self._stop_watchers(watcher_dpid, old_watchers)

        new_confs = watcher_parser(self.config_file, self.logname, self.prom_client)
        new_watchers = {}

        for conf in new_confs:
            watcher = watcher_factory(conf)(conf, self.logname, self.prom_client)
            watcher_dpid = watcher.dp.dp_id
            watcher_type = watcher.conf.type
            if watcher_dpid not in new_watchers:
                new_watchers[watcher_dpid] = {}
            if watcher_type not in new_watchers[watcher_dpid]:
                new_watchers[watcher_dpid][watcher_type] = []
            new_watchers[watcher_dpid][watcher_type].append(watcher)

        for watcher_dpid, watchers in list(new_watchers.items()):
            ryu_dp = self.dpset.get(watcher_dpid)
            if ryu_dp:
                self._start_watchers(ryu_dp, watcher_dpid, watchers)

        self.watchers = new_watchers
        self.config_watcher.update(self.config_file)
        self.logger.info('config complete')
Example #6
0
    def _load_config(self):
        """Load Gauge config."""
        try:
            new_confs = watcher_parser(self.config_file, self.logname,
                                       self.prom_client)
        except InvalidConfigError as err:
            self.logger.error('invalid config: %s', err)
            return

        for old_watchers in self.watchers.values():
            self._stop_watchers(old_watchers)

        new_watchers = {}

        for conf in new_confs:
            watcher = watcher_factory(conf)(conf, self.logname,
                                            self.prom_client)
            watcher_dpid = watcher.dp.dp_id
            watcher_type = watcher.conf.type
            if watcher_dpid not in new_watchers:
                new_watchers[watcher_dpid] = {}
            if watcher_type not in new_watchers[watcher_dpid]:
                new_watchers[watcher_dpid][watcher_type] = []
            new_watchers[watcher_dpid][watcher_type].append(watcher)

        timestamp = time.time()
        for watcher_dpid, watchers in new_watchers.items():
            ryu_dp = self.dpset.get(watcher_dpid)
            if ryu_dp:
                self._start_watchers(ryu_dp, watchers, timestamp)

        self.watchers = new_watchers
        self.config_watcher.update(self.config_file)
        self.logger.info('config complete')
Example #7
0
    def _load_config(self):
        """Load Gauge config."""
        try:
            new_confs = watcher_parser(self.config_file, self.logname, self.prom_client)
        except InvalidConfigError as err:
            self.logger.error('invalid config: %s', err)
            return

        for old_watchers in self.watchers.values():
            self._stop_watchers(old_watchers)

        new_watchers = {}

        for conf in new_confs:
            watcher = watcher_factory(conf)(conf, self.logname, self.prom_client)
            watcher_dpid = watcher.dp.dp_id
            watcher_type = watcher.conf.type
            if watcher_dpid not in new_watchers:
                new_watchers[watcher_dpid] = {}
            if watcher_type not in new_watchers[watcher_dpid]:
                new_watchers[watcher_dpid][watcher_type] = []
            new_watchers[watcher_dpid][watcher_type].append(watcher)

        timestamp = time.time()
        for watcher_dpid, watchers in new_watchers.items():
            ryu_dp = self.dpset.get(watcher_dpid)
            if ryu_dp:
                self._start_watchers(ryu_dp, watchers, timestamp)

        self.watchers = new_watchers
        self.config_watcher.update(self.config_file)
        self.logger.info('config complete')
Example #8
0
    def __init__(self, *args, **kwargs):
        super(Gauge, self).__init__(*args, **kwargs)
        sysprefix = get_sys_prefix()
        self.config_file = os.getenv('GAUGE_CONFIG',
                                     sysprefix + '/etc/ryu/faucet/gauge.yaml')
        self.exc_logfile = os.getenv(
            'GAUGE_EXCEPTION_LOG',
            sysprefix + '/var/log/ryu/faucet/gauge_exception.log')
        self.logfile = os.getenv('GAUGE_LOG',
                                 sysprefix + '/var/log/ryu/faucet/gauge.log')

        # Setup logging
        self.logger = get_logger(self.logname, self.logfile, logging.DEBUG, 0)
        # Set up separate logging for exceptions
        self.exc_logger = get_logger(self.exc_logname, self.exc_logfile,
                                     logging.DEBUG, 1)

        # Set the signal handler for reloading config file
        signal.signal(signal.SIGHUP, self.signal_handler)

        # dict of watchers/handlers:
        # indexed by dp_id and then by name
        self.watchers = {}
        confs = watcher_parser(self.config_file, self.logname)
        for conf in confs:
            watcher = watcher_factory(conf)(conf, self.logname)
            self.watchers.setdefault(watcher.dp.dp_id, {})
            self.watchers[watcher.dp.dp_id][watcher.conf.type] = watcher
        # Create dpset object for querying Ryu's DPSet application
        self.dpset = kwargs['dpset']
Example #9
0
    def _load_config(self):
        """Load Gauge config."""
        new_confs = watcher_parser(self.config_file, self.logname, self.prom_client)
        new_watchers = {}

        for conf in new_confs:
            watcher = watcher_factory(conf)(conf, self.logname, self.prom_client)
            watcher_dpid = watcher.dp.dp_id
            ryu_dp = self.dpset.get(watcher_dpid)
            watcher_type = watcher.conf.type
            watcher_msg = '%s %s watcher' % (dpid_log(watcher_dpid), watcher_type)

            if watcher_dpid not in new_watchers:
                new_watchers[watcher_dpid] = {}

            if watcher_type not in new_watchers[watcher_dpid]:

                # remove old watchers for this stat
                if (watcher_dpid in self.watchers and
                        watcher_type in self.watchers[watcher_dpid]):
                    old_watchers = self.watchers[watcher_dpid][watcher_type]
                    for old_watcher in old_watchers:
                        if old_watcher.running():
                            self.logger.info('%s stopped', watcher_msg)
                            old_watcher.stop()
                    del self.watchers[watcher_dpid][watcher_type]

                # start new watcher
                new_watchers[watcher_dpid][watcher_type] = [watcher]
                if ryu_dp is None:
                    watcher.report_dp_status(0)
                    self.logger.info('%s added but DP currently down', watcher_msg)
                else:
                    watcher.report_dp_status(1)
                    watcher.start(ryu_dp, True)
                    self.logger.info('%s started', watcher_msg)
            else:
                new_watchers[watcher_dpid][watcher_type].append(watcher)
                watcher.start(ryu_dp, False)

        for watcher_dpid, leftover_watchers in list(self.watchers.items()):
            for watcher_type, watcher in list(leftover_watchers.items()):
                watcher.report_dp_status(0)
                if watcher.running():
                    self.logger.info(
                        '%s %s deconfigured', dpid_log(watcher_dpid), watcher_type)
                    watcher.stop()

        self.watchers = new_watchers
        self.logger.info('config complete')
Example #10
0
    def _load_config(self):
        """Load Gauge config."""
        try:
            conf_hash, _faucet_config_files, faucet_conf_hashes, new_confs = watcher_parser(
                self.config_file, self.logname, self.prom_client)
            watchers = [
                watcher_factory(watcher_conf)(watcher_conf, self.logname,
                                              self.prom_client)
                for watcher_conf in new_confs
            ]
            self.prom_client.reregister_nonflow_vars()
        except InvalidConfigError as err:
            self.config_watcher.update(self.config_file)
            self.logger.error('invalid config: %s', err)
            return

        for old_watchers in self.watchers.values():
            self._stop_watchers(old_watchers)
        new_watchers = {}
        for watcher in watchers:
            watcher_dpid = watcher.dp.dp_id
            watcher_type = watcher.conf.type
            if watcher_dpid not in new_watchers:
                new_watchers[watcher_dpid] = {}
            if watcher_type not in new_watchers[watcher_dpid]:
                new_watchers[watcher_dpid][watcher_type] = []
            new_watchers[watcher_dpid][watcher_type].append(watcher)

        timestamp = time.time()
        for watcher_dpid, watchers in new_watchers.items():
            ryu_dp = self.dpset.get(watcher_dpid)
            if ryu_dp:
                self._start_watchers(ryu_dp, watchers, timestamp)

        self.watchers = new_watchers
        self.config_watcher.update(self.config_file,
                                   {self.config_file: conf_hash})
        self.faucet_config_watchers = []
        for faucet_config_file, faucet_conf_hash in faucet_conf_hashes.items():
            faucet_config_watcher = ConfigWatcher()
            faucet_config_watcher.update(faucet_config_file, faucet_conf_hash)
            self.faucet_config_watchers.append(faucet_config_watcher)
            self.logger.info('watching FAUCET config %s', faucet_config_file)
        self.logger.info('config complete')
Example #11
0
    def setUp(self):
        logname = 'test_config'

        logger = logging.getLogger('%s.config' % logname)
        logger_handler = logging.StreamHandler(stream=sys.stderr)
        log_fmt = '%(asctime)s %(name)-6s %(levelname)-8s %(message)s'
        logger_handler.setFormatter(
            logging.Formatter(log_fmt, '%b %d %H:%M:%S'))
        logger.addHandler(logger_handler)
        logger.propagate = 0
        logger.setLevel(logging.CRITICAL)

        self.v2_config_hashes, v2_dps = dp_parser('config/testconfigv2.yaml', logname)
        self.v2_dps_by_id = {}
        for dp in v2_dps:
            self.v2_dps_by_id[dp.dp_id] = dp
        self.v2_dp = self.v2_dps_by_id[0xcafef00d]
        self.v2_watchers = watcher_parser(
            'config/testgaugeconfig.yaml', logname)
Example #12
0
    def _load_config(self):
        """Load Gauge config."""
        self.config_file = os.getenv('GAUGE_CONFIG', self.config_file)
        new_confs = watcher_parser(self.config_file, self.logname,
                                   self.prom_client)
        new_watchers = {}

        for conf in new_confs:
            watcher = watcher_factory(conf)(conf, self.logname,
                                            self.prom_client)
            watcher_dpid = watcher.dp.dp_id
            ryu_dp = self.dpset.get(watcher_dpid)
            watcher_type = watcher.conf.type
            watcher_msg = '%s %s watcher' % (dpid_log(watcher_dpid),
                                             watcher_type)

            if watcher_dpid not in new_watchers:
                new_watchers[watcher_dpid] = {}

            if (watcher_dpid in self.watchers
                    and watcher_type in self.watchers[watcher_dpid]):
                old_watcher = self.watchers[watcher_dpid][watcher_type]
                if old_watcher.running():
                    self.logger.info('%s stopped', watcher_msg)
                    old_watcher.stop()
                del self.watchers[watcher_dpid][watcher_type]

            new_watchers[watcher_dpid][watcher_type] = watcher
            if ryu_dp is None:
                self.logger.info('%s added but DP currently down', watcher_msg)
            else:
                new_watchers[watcher_dpid][watcher_type].start(ryu_dp)
                self.logger.info('%s started', watcher_msg)

        for watcher_dpid, leftover_watchers in list(self.watchers.items()):
            for watcher_type, watcher in list(leftover_watchers.items()):
                if watcher.running():
                    self.logger.info('%s %s deconfigured',
                                     dpid_log(watcher_dpid), watcher_type)
                    watcher.stop()

        self.watchers = new_watchers
        self.logger.info('config complete')
Example #13
0
    def reload_config(self, ryu_event):
        self.config_file = os.getenv('GAUGE_CONFIG', self.config_file)

        new_confs = watcher_parser(self.config_file, self.logname)
        new_watchers = {}
        for conf in new_confs:
            watcher = watcher_factory(conf)(conf, self.logname)
            new_watchers.setdefault(watcher.dp.dp_id, {})
            new_watchers[watcher.dp.dp_id][watcher.conf.type] = watcher

        for dp_id, watchers in self.watchers:
            for watcher_type, watcher in watchers:
                try:
                    new_watcher = new_watchers[dp_id][watcher_type]
                    self.watchers[dp_id][watcher_type] = new_watcher
                except KeyError:
                    del self.watchers[dp_id][watcher_type]
                if watcher.running():
                    watcher.stop()
                    new_watcher.start(self.dpset.get(dp_id))
Example #14
0
    def test_all_dps(self):
        """Test config applies for all DPs."""
        GAUGE_CONF = """
watchers:
    port_stats_poller:
        type: 'port_stats'
        all_dps: True
        interval: 10
        db: 'prometheus'
dbs:
    prometheus:
        type: 'prometheus'
"""
        conf = self.get_config(GAUGE_CONF)
        gauge_file, _ = self.create_config_files(conf)
        watcher_confs = cp.watcher_parser(gauge_file, 'gauge_config_test', None)
        self.assertEqual(len(watcher_confs), 2, 'failed to create config for each dp')
        for watcher_conf in watcher_confs:
            msg = 'all_dps config not applied to each dp'
            self.assertEqual(watcher_conf.type, 'port_stats', msg)
            self.assertEqual(watcher_conf.interval, 10, msg)
            self.assertEqual(watcher_conf.db_type, 'prometheus', msg)
Example #15
0
    def test_all_dps(self):
        """Test config applies for all DPs."""
        GAUGE_CONF = """
watchers:
    port_stats_poller:
        type: 'port_stats'
        all_dps: True
        interval: 10
        db: 'prometheus'
dbs:
    prometheus:
        type: 'prometheus'
"""
        conf = self.get_config(GAUGE_CONF)
        gauge_file, _ = self.create_config_files(conf)
        watcher_confs = cp.watcher_parser(gauge_file, 'gauge_config_test', None)
        self.assertEqual(len(watcher_confs), 2, 'failed to create config for each dp')
        for watcher_conf in watcher_confs:
            msg = 'all_dps config not applied to each dp'
            self.assertEqual(watcher_conf.type, 'port_stats', msg)
            self.assertEqual(watcher_conf.interval, 10, msg)
            self.assertEqual(watcher_conf.db_type, 'prometheus', msg)