6 from sfa.util.namespace import *
7 from sfa.util.table import SfaTable
8 from sfa.util.prefixTree import prefixTree
9 from sfa.plc.api import SfaAPI
10 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
14 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
19 if not config.SFA_REGISTRY_ENABLED:
22 # Get the path to the sfa server key/cert files from
23 # the sfa hierarchy object
24 sfa_hierarchy = Hierarchy()
25 sfa_key_path = sfa_hierarchy.basedir
26 key_file = os.path.join(sfa_key_path, "server.key")
27 cert_file = os.path.join(sfa_key_path, "server.cert")
28 key = Keypair(filename=key_file)
30 # get a connection to our local sfa registry
31 # and a valid credential
32 authority = config.SFA_INTERFACE_HRN
33 url = 'http://%s:%s/' %(config.SFA_REGISTRY_HOST, config.SFA_REGISTRY_PORT)
34 registry = xmlrpcprotocol.get_server(url, key_file, cert_file)
35 sfa_api = SfaAPI(key_file = key_file, cert_file = cert_file, interface='registry')
36 credential = sfa_api.getCredential()
39 registries = Registries(sfa_api)
41 tree.load(registries.keys())
43 # get local peer records
45 peer_records = table.find({'~peer_authority': None})
48 for record in peer_records:
49 registry_hrn = tree.best_match(record['hrn'])
50 if registry_hrn not in hrn_dict:
51 hrn_dict[registry_hrn] = []
52 hrn_dict[registry_hrn].append(record['hrn'])
54 # attempt to resolve the record at the authoritative interface
55 for registry_hrn in hrn_dict:
56 if registry_hrn in registries:
58 target_hrns = hrn_dict[registry_hrn]
60 records = registries[registry_hrn].Resolve(target_hrns, credential)
61 found_records.extend([record['hrn'] for record in records])
62 except ServerException:
63 # an exception will be thrown if the record doenst exist
64 # if so remove the record from the local registry
67 # this deosnt necessarily mean the records dont exist
68 # lets give them the benefit of the doubt here (for now)
69 found_records.extend(target_hrns)
72 # remove what wasnt found
73 for peer_record in peer_records:
74 if peer_record['hrn'] not in found_records:
75 registries[sfa_api.hrn].Remove(peer_record['hrn'], credential, peer_record['type'])
77 if __name__ == '__main__':