def gather_oids(self, target, model): if config.incarnation() != self.model_oid_cache_incarnation: self.model_oid_cache_incarnation = config.incarnation() self.model_oid_cache = {} cache_key = (target.layer, model) if cache_key in self.model_oid_cache: return self.model_oid_cache[cache_key] oids = set() vlan_aware_oids = set() for collection_name, collection in config.get('collection').iteritems(): for regexp in collection['models']: layers = collection.get('layers', None) if layers and target.layer not in layers: continue if 'oids' in collection and re.match(regexp, model): logging.debug( 'Model %s matches collection %s', model, collection_name) # VLAN aware collections are run against every VLAN. # We don't want to run all the other OIDs (there can be a *lot* of # VLANs). vlan_aware = collection.get('vlan_aware', False) if vlan_aware: vlan_aware_oids.update(set(collection['oids'])) else: oids.update(set(collection['oids'])) self.model_oid_cache[cache_key] = (list(oids), list(vlan_aware_oids)) return self.model_oid_cache[cache_key]
def testConfig(self, mock_config, mock_time): mock_time.return_value = 1234 mock_config.return_value = yaml.load(""" ipplan: /etc/ipplan.db domain: event snmp: access: version: 2 community: REMOVED port: 161 """) # First access should load the cache but only then self.assertEqual(config.get('ipplan'), '/etc/ipplan.db') self.assertEqual(config.get('snmp', 'access', 'version'), 2) self.assertEqual(config.incarnation(), 1) self.assertEqual(mock_config.call_count, 1) # Advance the clock to have the cache refresh mock_time.return_value = 1235 + config.CONFIG_CACHE self.assertEqual(config.get('snmp', 'access', 'version'), 2) self.assertEqual(config.incarnation(), 1) self.assertEqual(mock_config.call_count, 2) # Try a different config mock_config.return_value = yaml.load(""" snmp: access: version: 3 """) # See so the config updated and that we got a new incarnation number mock_time.return_value = 1236 + config.CONFIG_CACHE*2 self.assertEqual(config.get('snmp', 'access', 'version'), 3) self.assertEqual(config.incarnation(), 2) self.assertEqual(mock_config.call_count, 3) # Try to refresh the config self.assertEqual(config.get('snmp', 'access', 'version'), 3) config.refresh() self.assertEqual(config.get('snmp', 'access', 'version'), 3) # We should keep the incarnation self.assertEqual(config.incarnation(), 2) self.assertEqual(mock_config.call_count, 4)