sfadump more usable
[sfa.git] / sfa / server / sfa-clean-peer-records.py
1 #!/usr/bin/python
2
3 import sys
4 import os
5 import traceback
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 
14 import socket
15
16 def main():
17     config = Config()
18     if not config.SFA_REGISTRY_ENABLED:
19         sys.exit(0)
20
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) 
28
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()
36
37     # get peer registries
38     registries = Registries(sfa_api)
39     tree = prefixTree()
40     tree.load(registries.keys())
41     
42     # get local peer records
43     table = SfaTable()
44     peer_records = table.find({'~peer_authority': None})
45     found_records = []
46     hrn_dict = {}
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'])
52
53     # attempt to resolve the record at the authoritative interface 
54     for registry_hrn in hrn_dict:
55         if registry_hrn in registries:
56             records = []
57             target_hrns = hrn_dict[registry_hrn]    
58             try:
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
64                 continue
65             except:
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)
69                 traceback.print_exc()
70
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'])
75                 
76 if __name__ == '__main__':
77     main()