1 ###########################################################################
2 # Copyright (C) 2011 by root
5 # Copyright: See COPYING file that comes with this distribution
7 ###########################################################################
9 from sfa.senslab.OARrestapi import OARapi
10 from sfa.senslab.LDAPapi import LDAPapi
11 from sfa.util.config import Config
12 from sfa.util.xrn import hrn_to_urn, get_authority
13 from sfa.util.table import SfaTable
14 from sfa.trust.hierarchy import Hierarchy
16 AuthHierarchy = Hierarchy()
19 def create_top_level_auth_records(hrn):
21 Create top level records (includes root and sub authorities (local/remote)
23 print>>sys.stderr, "\r\n =========SenslabImport create_top_level_auth_records\r\n"
24 urn = hrn_to_urn(hrn, 'authority')
25 # make sure parent exists
26 parent_hrn = get_authority(hrn)
29 if not parent_hrn == hrn:
30 create_top_level_auth_records(parent_hrn)
33 # create the authority if it doesnt already exist
34 if not AuthHierarchy.auth_exists(urn):
35 AuthHierarchy.create_auth(urn)
37 # create the db record if it doesnt already exist
38 auth_info = AuthHierarchy.get_auth_info(hrn)
40 auth_record = table.find({'type': 'authority', 'hrn': hrn})
43 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
44 auth_record['authority'] = get_authority(auth_record['hrn'])
45 table.insert(auth_record)
46 print>>sys.stderr, "\r\n ========= \t\t SenslabImport NO AUTH RECORD \r\n" ,auth_record['authority']
49 def import_node(hrn, node):
51 # ASN.1 will have problems with hrn's longer than 64 characters
55 node_record = table.find({'type': 'node', 'hrn': hrn})
56 pkey = Keypair(create=True)
57 urn = hrn_to_urn(hrn, 'node')
58 node_gid = AuthHierarchy.create_gid(urn, create_uuid(), pkey)
59 node_record = SfaRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
60 node_record['authority'] = get_authority(node_record['hrn'])
61 existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']})
62 if not existing_records:
63 table.insert(node_record)
65 existing_record = existing_records[0]
66 node_record['record_id'] = existing_record['record_id']
67 table.update(node_record)
70 def import_person(person):
71 existing_records = table.find({'hrn': person['hrn'], 'type': 'user'})
72 if not existing_records:
73 table.insert(person_record)
75 existing_record = existing_records[0]
76 person['record_id'] = existing_record['record_id']
77 table.update(person_record)
81 def delete_record( hrn, type):
83 record_list = table.find({'type': type, 'hrn': hrn})
84 for record in record_list:
91 if not config.SFA_REGISTRY_ENABLED:
93 root_auth = config.SFA_REGISTRY_ROOT_AUTH
94 interface_hrn = config.SFA_INTERFACE_HRN
95 print interface_hrn, root_auth
97 # initialize registry db table
99 if not table.exists():
102 # create root authority
103 create_top_level_auth_records(root_auth)
105 # create s user record for the slice manager
107 #SenslabImporter.create_sm_client_record()
109 # create interface records
111 #SenslabImporter.logger.info("Import: creating interface records")
112 #SenslabImporter.create_interface_records()
113 # create dict of all existing sfa records
115 existing_records = {}
119 results = table.find()
120 for result in results:
121 existing_records[(result['hrn'], result['type'])] = result
122 existing_hrns.append(result['hrn'])
125 Driver = SlabDriver(OARapi(),SenslabUsers())
126 nodes_dict = Driver.GetNodes()
127 print "\r\n NODES8DICT ",nodes_dict
129 persons_list = Driver.GetPersons()
130 print "\r\n PERSONS_LIST ",persons_list
132 keys_list = Driver.GetKeys()
133 print "\r\n KEYSS_LIST ",keys_list
135 #slices_list = SenslabUsers.GetSlices()
136 #print "\r\n SLICES_LIST ",slices_list
138 # Get all Senslab sites
139 #sites_dict = OARImporter.GetSites()
140 #print "\r\n sSITES_DICT" , sites_dict
143 #for site in sites_dict:
144 #site_hrn = interface_hrn + "." + site['login_base']
145 ##sfa_logger().info("Importing site: %s" % site_hrn)
146 #print "HRN %s %s site existing in hrn ? %s" %( site['login_base'],site_hrn, site_hrn in existing_hrns)
147 ## import if hrn is not in list of existing hrns or if the hrn exists
148 ## but its not a site record
149 #if site_hrn not in existing_hrns or \
150 #(site_hrn, 'authority') not in existing_records:
151 #print "SITE HRN UNKNOWN" , site, site_hrn
152 #site_hrn = SenslabImporter.import_site(interface_hrn, site)
154 print "\r\n \r\n ===========IMPORT NODE_RECORDS ==========\r\n site %s \r\n \t nodes_dict %s" %(site,nodes_dict)
155 # import node records
156 #for node_id in site['node_ids']:
157 #for[node['node_id'] for node in nodes_dict]:
158 #print '\r\n \t **NODE_ID %s node %s '%( node_id, node)
160 for node in nodes_dict:
161 #if node_id is node['node_id']:
162 #node = nodes_dict[node_id]
163 print '\r\n \t NODE_ID %s node %s '%( node_id, node)
164 hrn = hostname_to_hrn(interface_hrn, root_auth, node['hostname'])
167 if hrn not in existing_hrns or \
168 (hrn, 'node') not in existing_records:
169 print "\t\t NODE HRN NOT in existing records!" ,hrn
170 import_node(hrn, node)
173 for person in persons_list:
174 print >>sys.stderr, "\r\n\r\n^^^^^^^^^^^^^PERSON hrn %s person %s site hrn %s" %(hrn,person)
175 import_person( site_hrn, person,keys_list)
176 if hrn not in existing_hrns or \
177 (hrn, 'user') not in existing_records or update_record:
178 import_person(site_hrn, person)
180 #for slice_id in site['slice_ids']:
181 #print >>sys.stderr, "\r\n\r\n \t ^^^^^^^\\\\\\\\\\\\\\\^^^^^^ slice_id %s " %(slice_id)
182 #for sl in slices_list:
183 #if slice_id is sl['slice_id']:
184 ##hrn = slicename_to_hrn(interface_hrn, sl['name'])
185 #hrn = email_to_hrn(site_hrn, sl['name'])
186 #print >>sys.stderr, "\r\n\r\n^^^^^^^^^^^^^SLICE ID hrn %s site_hrn %s" %(hrn,site_hrn)
187 #if hrn not in existing_hrns or \
188 #(hrn, 'slice') not in existing_records:
189 #SenslabImporter.import_slice(site_hrn, sl)
192 # remove stale records
193 system_records = [interface_hrn, root_auth, interface_hrn + '.slicemanager']
194 for (record_hrn, type) in existing_records.keys():
195 if record_hrn in system_records:
198 record = existing_records[(record_hrn, type)]
199 if record['peer_authority']:
206 if type == 'authority':
207 #for site in sites_dict:
208 #print "\t type : authority : ", site
209 #site_hrn = interface_hrn + "." + site['login_base']
210 #if site_hrn == record_hrn and site['site_id'] == record['pointer']:
212 print "\t \t Found :", found
216 for person in persons:
217 if person['hrn'] == record_hrn:
222 login_base = get_leaf(get_authority(record_hrn))
223 nodename = Xrn.unescape(get_leaf(record_hrn))
224 print "type: node : nodename %s" %(nodename)
225 for node in nodes_dict.values():
226 if node['hostname'] == nodename :
234 record_object = existing_records[(record_hrn, type)]
235 print "\t\t NOT FOUND ! "
236 delete_record(record_hrn, type)
238 if __name__ == "__main__":