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.senslab.slabdriver import SlabDriver
12 from sfa.util.config import Config
13 from sfa.util.xrn import hrn_to_urn, get_authority,Xrn,get_leaf
14 from sfa.util.table import SfaTable
15 from sfa.util.record import SfaRecord
16 from sfa.trust.hierarchy import Hierarchy
17 from sfa.trust.certificate import Keypair
18 from sfa.trust.gid import create_uuid
21 AuthHierarchy = Hierarchy()
23 if not table.exists():
26 def create_top_level_auth_records(hrn):
28 Create top level records (includes root and sub authorities (local/remote)
30 print>>sys.stderr, "\r\n =========SenslabImport create_top_level_auth_records\r\n"
31 urn = hrn_to_urn(hrn, 'authority')
32 # make sure parent exists
33 parent_hrn = get_authority(hrn)
36 if not parent_hrn == hrn:
37 create_top_level_auth_records(parent_hrn)
40 # create the authority if it doesnt already exist
41 if not AuthHierarchy.auth_exists(urn):
42 AuthHierarchy.create_auth(urn)
44 # create the db record if it doesnt already exist
45 auth_info = AuthHierarchy.get_auth_info(hrn)
47 auth_record = table.find({'type': 'authority', 'hrn': hrn})
50 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
51 auth_record['authority'] = get_authority(auth_record['hrn'])
52 print sys.stderr, " \r\n \t slab-import : auth record %s inserted" %(auth_record['hrn'])
53 table.insert(auth_record)
54 print>>sys.stderr, "\r\n ========= \t\t SenslabImport NO AUTH RECORD \r\n" ,auth_record['authority']
57 def import_node(hrn, node):
59 # ASN.1 will have problems with hrn's longer than 64 characters
63 node_record = table.find({'type': 'node', 'hrn': hrn})
64 pkey = Keypair(create=True)
65 urn = hrn_to_urn(hrn, 'node')
66 node_gid = AuthHierarchy.create_gid(urn, create_uuid(), pkey)
67 node_record = SfaRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
68 node_record['authority'] = get_authority(node_record['hrn'])
69 existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']})
70 if not existing_records:
71 print>>sys.stderr, " \r\n \t slab-import : node record %s inserted" %(node_record['hrn'])
72 table.insert(node_record)
74 existing_record = existing_records[0]
75 node_record['record_id'] = existing_record['record_id']
76 table.update(node_record)
78 # person is already a sfa record
79 def import_person(person):
80 existing_records = table.find({'hrn': person['hrn'], 'type': 'user'})
81 if not existing_records:
82 print>>sys.stderr, " \r\n \t slab-import : person record %s inserted" %(person['hrn'])
85 existing_record = existing_records[0]
86 person['record_id'] = existing_record['record_id']
89 def import_slice(person):
90 slice = { 'peer_authority': None,'type':'slice','pointer':-1, 'date_created': None, 'last_updated': None }
91 slice['hrn'] = person['hrn']+'_slice'
92 pkey = Keypair(create=True)
93 urn = hrn_to_urn(slice['hrn'], 'slice')
94 slice['gid'] = AuthHierarchy.create_gid(urn, create_uuid(), pkey)
95 slice_record= SfaRecord(hrn=slice['hrn'], gid=slice['gid'], type="slice", pointer=slice['pointer'])
96 slice_record['authority'] = get_authority(slice_record['hrn'])
97 print>>sys.stderr, " \r\n \t slab-import : slice record %s " %(slice_record['hrn'])
98 existing_records = table.find({'hrn': slice_record['hrn'], 'type': 'slice'})
99 if not existing_records:
100 print>>sys.stderr, " \r\n \t slab-import : slice record %s inserted" %(slice_record['hrn'])
101 table.insert(slice_record)
103 print>>sys.stderr, " \r\n \t slab-import : slice record %s updated" %(slice_record['hrn'])
104 existing_record = existing_records[0]
105 slice_record['record_id'] = existing_record['record_id']
106 table.update(slice_record)
108 def delete_record( hrn, type):
110 record_list = table.find({'type': type, 'hrn': hrn})
111 for record in record_list:
112 print>>sys.stderr, " \r\n \t slab-import : record %s deleted" %(record['hrn'])
115 def hostname_to_hrn(root_auth,hostname):
116 # keep only the first part of the DNS name
117 #hrn='.'.join( [auth,hostname.split(".")[0] ] )
118 # escape the '.' in the hostname
119 hrn='.'.join( [root_auth,Xrn.escape(hostname)] )
120 return hrn_to_urn(hrn,'node')
125 if not config.SFA_REGISTRY_ENABLED:
127 root_auth = config.SFA_REGISTRY_ROOT_AUTH
128 interface_hrn = config.SFA_INTERFACE_HRN
129 print interface_hrn, root_auth
131 # initialize registry db table
133 #if not table.exists():
136 # create root authority
137 create_top_level_auth_records(root_auth)
139 # create s user record for the slice manager
141 #SenslabImporter.create_sm_client_record()
143 # create interface records
145 #SenslabImporter.logger.info("Import: creating interface records")
146 #SenslabImporter.create_interface_records()
147 # create dict of all existing sfa records
149 existing_records = {}
153 results = table.find()
154 for result in results:
155 existing_records[(result['hrn'], result['type'])] = result
156 existing_hrns.append(result['hrn'])
160 Driver = SlabDriver(config)
161 nodes_dict = Driver.GetNodes()
162 #print "\r\n NODES8DICT ",nodes_dict
164 ldap_person_list = Driver.GetPersons()
167 #slices_list = SenslabUsers.GetSlices()
168 #print "\r\n SLICES_LIST ",slices_list
170 # Get all Senslab sites
171 #sites_dict = OARImporter.GetSites()
172 #print "\r\n sSITES_DICT" , sites_dict
175 #for site in sites_dict:
176 #site_hrn = interface_hrn + "." + site['login_base']
177 ##sfa_logger().info("Importing site: %s" % site_hrn)
178 #print "HRN %s %s site existing in hrn ? %s" %( site['login_base'],site_hrn, site_hrn in existing_hrns)
179 ## import if hrn is not in list of existing hrns or if the hrn exists
180 ## but its not a site record
181 #if site_hrn not in existing_hrns or \
182 #(site_hrn, 'authority') not in existing_records:
183 #print "SITE HRN UNKNOWN" , site, site_hrn
184 #site_hrn = SenslabImporter.import_site(interface_hrn, site)
186 # import node records
187 for node in nodes_dict:
188 hrn = hostname_to_hrn( root_auth, node['hostname'])
189 if hrn not in existing_hrns or \
190 (hrn, 'node') not in existing_records:
191 import_node(hrn, node)
194 for person in ldap_person_list:
195 if person['hrn'] not in existing_hrns or \
196 (person['hrn'], 'user') not in existing_records :
200 #for slice_id in site['slice_ids']:
201 #print >>sys.stderr, "\r\n\r\n \t ^^^^^^^\\\\\\\\\\\\\\\^^^^^^ slice_id %s " %(slice_id)
202 #for sl in slices_list:
203 #if slice_id is sl['slice_id']:
204 ##hrn = slicename_to_hrn(interface_hrn, sl['name'])
205 #hrn = email_to_hrn(site_hrn, sl['name'])
206 #print >>sys.stderr, "\r\n\r\n^^^^^^^^^^^^^SLICE ID hrn %s site_hrn %s" %(hrn,site_hrn)
207 #if hrn not in existing_hrns or \
208 #(hrn, 'slice') not in existing_records:
209 #SenslabImporter.import_slice(site_hrn, sl)
212 # remove stale records
213 system_records = [interface_hrn, root_auth, interface_hrn + '.slicemanager']
215 for (record_hrn, type) in existing_records.keys():
216 if record_hrn in system_records:
219 record = existing_records[(record_hrn, type)]
220 if record['peer_authority']:
227 if type == 'authority':
228 #for site in sites_dict:
229 #print "\t type : authority : ", site
230 #site_hrn = interface_hrn + "." + site['login_base']
231 #if site_hrn == record_hrn and site['site_id'] == record['pointer']:
233 print "\t \t Found :", found
237 for person in ldap_person_list:
238 if person['hrn'] == record_hrn:
243 login_base = get_leaf(get_authority(record_hrn))
244 nodename = Xrn.unescape(get_leaf(record_hrn))
245 for node in nodes_dict:
246 if node['hostname'] == nodename :
250 elif type == 'slice':
251 for person in ldap_person_list:
252 if person['hrn']+'_slice' == record_hrn:
259 record_object = existing_records[(record_hrn, type)]
260 print "\t\t NOT FOUND ! ", record_hrn
261 delete_record(record_hrn, type)
263 if __name__ == "__main__":