def list(api, xrn, origin_hrn=None): hrn, type = urn_to_hrn(xrn) # load all know registry names into a prefix tree and attempt to find # the longest matching prefix records = [] registries = api.registries registry_hrns = registries.keys() tree = prefixTree() tree.load(registry_hrns) registry_hrn = tree.best_match(hrn) #if there was no match then this record belongs to an unknow registry if not registry_hrn: raise MissingAuthority(xrn) # if the best match (longest matching hrn) is not the local registry, # forward the request records = [] if registry_hrn != api.hrn: credential = api.getCredential() record_list = registries[registry_hrn].List(xrn, credential) records = [SfaRecord(dict=record).as_dict() for record in record_list] # if we still have not found the record yet, try the local registry if not records: if not api.auth.hierarchy.auth_exists(hrn): raise MissingAuthority(hrn) table = SfaTable() records = table.find({'authority': hrn}) return records
def main(): config = Config() if not config.SFA_REGISTRY_ENABLED: sys.exit(0) # Get the path to the sfa server key/cert files from # the sfa hierarchy object sfa_hierarchy = Hierarchy() sfa_key_path = sfa_hierarchy.basedir key_file = os.path.join(sfa_key_path, "server.key") cert_file = os.path.join(sfa_key_path, "server.cert") key = Keypair(filename=key_file) # get a connection to our local sfa registry # and a valid credential authority = config.SFA_INTERFACE_HRN url = 'http://%s:%s/' %(config.SFA_REGISTRY_HOST, config.SFA_REGISTRY_PORT) registry = xmlrpcprotocol.get_server(url, key_file, cert_file) sfa_api = SfaAPI(key_file = key_file, cert_file = cert_file, interface='registry') credential = sfa_api.getCredential() # get peer registries registries = Registries(sfa_api) tree = prefixTree() tree.load(registries.keys()) # get local peer records table = SfaTable() peer_records = table.find({'~peer_authority': None}) found_records = [] hrn_dict = {} for record in peer_records: registry_hrn = tree.best_match(record['hrn']) if registry_hrn not in hrn_dict: hrn_dict[registry_hrn] = [] hrn_dict[registry_hrn].append(record['hrn']) # attempt to resolve the record at the authoritative interface for registry_hrn in hrn_dict: if registry_hrn in registries: records = [] target_hrns = hrn_dict[registry_hrn] try: records = registries[registry_hrn].Resolve(target_hrns, credential) found_records.extend([record['hrn'] for record in records]) except ServerException: # an exception will be thrown if the record doenst exist # if so remove the record from the local registry continue except: # this deosnt necessarily mean the records dont exist # lets give them the benefit of the doubt here (for now) found_records.extend(target_hrns) traceback.print_exc() # remove what wasnt found for peer_record in peer_records: if peer_record['hrn'] not in found_records: registries[sfa_api.hrn].Remove(peer_record['hrn'], credential, peer_record['type'])
def List(self, api, xrn, origin_hrn=None, options={}): dbsession = api.dbsession() # load all know registry names into a prefix tree and attempt to find # the longest matching prefix hrn, type = urn_to_hrn(xrn) registries = api.registries registry_hrns = registries.keys() tree = prefixTree() tree.load(registry_hrns) registry_hrn = tree.best_match(hrn) #if there was no match then this record belongs to an unknow registry if not registry_hrn: raise MissingAuthority(xrn) # if the best match (longest matching hrn) is not the local registry, # forward the request record_dicts = [] if registry_hrn != api.hrn: credential = api.getCredential() interface = api.registries[registry_hrn] server_proxy = api.server_proxy(interface, credential) record_list = server_proxy.List(xrn, credential, options) # same as above, no need to process what comes from through xmlrpc # pass foreign records as-is record_dicts = record_list # if we still have not found the record yet, try the local registry # logger.debug("before trying local records, %d foreign records"% len(record_dicts)) if not record_dicts: recursive = False if ('recursive' in options and options['recursive']): recursive = True elif hrn.endswith('*'): hrn = hrn[:-1] recursive = True if not api.auth.hierarchy.auth_exists(hrn): raise MissingAuthority(hrn) if recursive: records = dbsession.query(RegRecord).filter( RegRecord.hrn.startswith(hrn)).all() # logger.debug("recursive mode, found %d local records"%(len(records))) else: records = dbsession.query(RegRecord).filter_by( authority=hrn).all() # logger.debug("non recursive mode, found %d local records"%(len(records))) # so that sfi list can show more than plain names... for record in records: augment_with_sfa_builtins(record) record_dicts = [ record.todict(exclude_types=[InstrumentedList]) for record in records ] return record_dicts
def resolve(api, xrns, type=None, full=True): # load all known registry names into a prefix tree and attempt to find # the longest matching prefix if not isinstance(xrns, types.ListType): if not type: type = Xrn(xrns).get_type() xrns = [xrns] hrns = [urn_to_hrn(xrn)[0] for xrn in xrns] # create a dict where key is a registry hrn and its value is a # hrns at that registry (determined by the known prefix tree). xrn_dict = {} registries = api.registries tree = prefixTree() registry_hrns = registries.keys() tree.load(registry_hrns) for xrn in xrns: registry_hrn = tree.best_match(urn_to_hrn(xrn)[0]) if registry_hrn not in xrn_dict: xrn_dict[registry_hrn] = [] xrn_dict[registry_hrn].append(xrn) records = [] for registry_hrn in xrn_dict: # skip the hrn without a registry hrn # XX should we let the user know the authority is unknown? if not registry_hrn: continue # if the best match (longest matching hrn) is not the local registry, # forward the request xrns = xrn_dict[registry_hrn] if registry_hrn != api.hrn: credential = api.getCredential() peer_records = registries[registry_hrn].Resolve(xrns, credential) records.extend([SfaRecord(dict=record).as_dict() for record in peer_records]) # try resolving the remaining unfound records at the local registry remaining_hrns = set(hrns).difference([record['hrn'] for record in records]) # convert set to list remaining_hrns = [hrn for hrn in remaining_hrns] table = SfaTable() local_records = table.findObjects({'hrn': remaining_hrns}) if full: api.fill_record_info(local_records) # convert local record objects to dicts records.extend([dict(record) for record in local_records]) if not records: raise RecordNotFound(str(hrns)) if type: records = filter(lambda rec: rec['type'] in [type], records) return records
def List (self, api, xrn, origin_hrn=None, options=None): if options is None: options={} dbsession=api.dbsession() # load all know registry names into a prefix tree and attempt to find # the longest matching prefix hrn, type = urn_to_hrn(xrn) registries = api.registries registry_hrns = registries.keys() tree = prefixTree() tree.load(registry_hrns) registry_hrn = tree.best_match(hrn) #if there was no match then this record belongs to an unknow registry if not registry_hrn: raise MissingAuthority(xrn) # if the best match (longest matching hrn) is not the local registry, # forward the request record_dicts = [] if registry_hrn != api.hrn: credential = api.getCredential() interface = api.registries[registry_hrn] server_proxy = api.server_proxy(interface, credential) record_list = server_proxy.List(xrn, credential, options) # same as above, no need to process what comes from through xmlrpc # pass foreign records as-is record_dicts = record_list # if we still have not found the record yet, try the local registry # logger.debug("before trying local records, %d foreign records"% len(record_dicts)) if not record_dicts: recursive = False if ('recursive' in options and options['recursive']): recursive = True elif hrn.endswith('*'): hrn = hrn[:-1] recursive = True if not api.auth.hierarchy.auth_exists(hrn): raise MissingAuthority(hrn) if recursive: records = dbsession.query(RegRecord).filter(RegRecord.hrn.startswith(hrn)).all() # logger.debug("recursive mode, found %d local records"%(len(records))) else: records = dbsession.query(RegRecord).filter_by(authority=hrn).all() # logger.debug("non recursive mode, found %d local records"%(len(records))) # so that sfi list can show more than plain names... for record in records: augment_with_sfa_builtins (record) record_dicts=[ record.todict(exclude_types=[InstrumentedList]) for record in records ] return record_dicts
def Resolve(self, api, xrns, type=None, details=False): if not isinstance(xrns, types.ListType): # try to infer type if not set and we get a single input if not type: type = Xrn(xrns).get_type() xrns = [xrns] hrns = [urn_to_hrn(xrn)[0] for xrn in xrns] # load all known registry names into a prefix tree and attempt to find # the longest matching prefix # create a dict where key is a registry hrn and its value is a list # of hrns at that registry (determined by the known prefix tree). xrn_dict = {} registries = api.registries tree = prefixTree() registry_hrns = registries.keys() tree.load(registry_hrns) for xrn in xrns: registry_hrn = tree.best_match(urn_to_hrn(xrn)[0]) if registry_hrn not in xrn_dict: xrn_dict[registry_hrn] = [] xrn_dict[registry_hrn].append(xrn) records = [] for registry_hrn in xrn_dict: # skip the hrn without a registry hrn # XX should we let the user know the authority is unknown? if not registry_hrn: continue # if the best match (longest matching hrn) is not the local registry, # forward the request xrns = xrn_dict[registry_hrn] if registry_hrn != api.hrn: credential = api.getCredential() interface = api.registries[registry_hrn] server_proxy = api.server_proxy(interface, credential) # should propagate the details flag but that's not supported in the xmlrpc interface yet #peer_records = server_proxy.Resolve(xrns, credential,type, details=details) peer_records = server_proxy.Resolve(xrns, credential) # pass foreign records as-is # previous code used to read # records.extend([SfaRecord(dict=record).as_dict() for record in peer_records]) # not sure why the records coming through xmlrpc had to be processed at all records.extend(peer_records) # try resolving the remaining unfound records at the local registry local_hrns = list ( set(hrns).difference([record['hrn'] for record in records]) ) # local_records = dbsession.query(RegRecord).filter(RegRecord.hrn.in_(local_hrns)) if type: local_records = local_records.filter_by(type=type) local_records=local_records.all() for local_record in local_records: augment_with_sfa_builtins (local_record) logger.info("Resolve, (details=%s,type=%s) local_records=%s "%(details,type,local_records)) local_dicts = [ record.__dict__ for record in local_records ] if details: # in details mode we get as much info as we can, which involves contacting the # testbed for getting implementation details about the record self.driver.augment_records_with_testbed_info(local_dicts) # also we fill the 'url' field for known authorities # used to be in the driver code, sounds like a poorman thing though def solve_neighbour_url (record): if not record.type.startswith('authority'): return hrn=record.hrn for neighbour_dict in [ api.aggregates, api.registries ]: if hrn in neighbour_dict: record.url=neighbour_dict[hrn].get_url() return for record in local_records: solve_neighbour_url (record) # convert local record objects to dicts for xmlrpc # xxx somehow here calling dict(record) issues a weird error # however record.todict() seems to work fine # records.extend( [ dict(record) for record in local_records ] ) records.extend( [ record.todict(exclude_types=[InstrumentedList]) for record in local_records ] ) if not records: raise RecordNotFound(str(hrns)) return records