8 from sfa.util.prefixTree import prefixTree
9 from sfa.util.config import Config
11 from sfa.trust.certificate import Keypair
12 from sfa.trust.hierarchy import Hierarchy
13 from sfa.server.registry import Registries
15 from sfa.storage.table import SfaTable
17 from sfa.client.sfaserverproxy import SfaServerProxy
19 from sfa.generic import Generic
23 if not config.SFA_REGISTRY_ENABLED:
26 # Get the path to the sfa server key/cert files from
27 # the sfa hierarchy object
28 sfa_hierarchy = Hierarchy()
29 sfa_key_path = sfa_hierarchy.basedir
30 key_file = os.path.join(sfa_key_path, "server.key")
31 cert_file = os.path.join(sfa_key_path, "server.cert")
32 key = Keypair(filename=key_file)
34 # get a connection to our local sfa registry
35 # and a valid credential
36 authority = config.SFA_INTERFACE_HRN
37 url = 'http://%s:%s/' %(config.SFA_REGISTRY_HOST, config.SFA_REGISTRY_PORT)
38 registry = SfaServerProxy(url, key_file, cert_file)
39 sfa_api = Generic.the_flavour()
40 credential = sfa_api.getCredential()
43 registries = Registries(sfa_api)
45 tree.load(registries.keys())
47 # get local peer records
49 peer_records = table.find({'~peer_authority': None})
52 for record in peer_records:
53 registry_hrn = tree.best_match(record['hrn'])
54 if registry_hrn not in hrn_dict:
55 hrn_dict[registry_hrn] = []
56 hrn_dict[registry_hrn].append(record['hrn'])
58 # attempt to resolve the record at the authoritative interface
59 for registry_hrn in hrn_dict:
60 if registry_hrn in registries:
62 target_hrns = hrn_dict[registry_hrn]
64 records = registries[registry_hrn].Resolve(target_hrns, credential)
65 found_records.extend([record['hrn'] for record in records])
66 except ServerException:
67 # an exception will be thrown if the record doenst exist
68 # if so remove the record from the local registry
71 # this deosnt necessarily mean the records dont exist
72 # lets give them the benefit of the doubt here (for now)
73 found_records.extend(target_hrns)
76 # remove what wasnt found
77 for peer_record in peer_records:
78 if peer_record['hrn'] not in found_records:
79 registries[sfa_api.hrn].Remove(peer_record['hrn'], credential, peer_record['type'])
81 if __name__ == '__main__':