6 from sfa.util.table import SfaTable
7 from sfa.util.prefixTree import prefixTree
8 from sfa.plc.api import SfaAPI
9 from sfa.util.config import Config
10 from sfa.trust.certificate import Keypair
11 from sfa.trust.hierarchy import Hierarchy
12 from sfa.server.registry import Registries
13 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
18 if not config.SFA_REGISTRY_ENABLED:
21 # Get the path to the sfa server key/cert files from
22 # the sfa hierarchy object
23 sfa_hierarchy = Hierarchy()
24 sfa_key_path = sfa_hierarchy.basedir
25 key_file = os.path.join(sfa_key_path, "server.key")
26 cert_file = os.path.join(sfa_key_path, "server.cert")
27 key = Keypair(filename=key_file)
29 # get a connection to our local sfa registry
30 # and a valid credential
31 authority = config.SFA_INTERFACE_HRN
32 url = 'http://%s:%s/' %(config.SFA_REGISTRY_HOST, config.SFA_REGISTRY_PORT)
33 registry = xmlrpcprotocol.get_server(url, key_file, cert_file)
34 sfa_api = SfaAPI(key_file = key_file, cert_file = cert_file, interface='registry')
35 credential = sfa_api.getCredential()
38 registries = Registries(sfa_api)
40 tree.load(registries.keys())
42 # get local peer records
44 peer_records = table.find({'~peer_authority': None})
47 for record in peer_records:
48 registry_hrn = tree.best_match(record['hrn'])
49 if registry_hrn not in hrn_dict:
50 hrn_dict[registry_hrn] = []
51 hrn_dict[registry_hrn].append(record['hrn'])
53 # attempt to resolve the record at the authoritative interface
54 for registry_hrn in hrn_dict:
55 if registry_hrn in registries:
57 target_hrns = hrn_dict[registry_hrn]
59 records = registries[registry_hrn].Resolve(target_hrns, credential)
60 found_records.extend([record['hrn'] for record in records])
61 except ServerException:
62 # an exception will be thrown if the record doenst exist
63 # if so remove the record from the local registry
66 # this deosnt necessarily mean the records dont exist
67 # lets give them the benefit of the doubt here (for now)
68 found_records.extend(target_hrns)
71 # remove what wasnt found
72 for peer_record in peer_records:
73 if peer_record['hrn'] not in found_records:
74 registries[sfa_api.hrn].Remove(peer_record['hrn'], credential, peer_record['type'])
76 if __name__ == '__main__':