1 ###########################################################################
2 # Copyright (C) 2011 by root
5 # Copyright: See COPYING file that comes with this distribution
7 ###########################################################################
11 from sfa.senslab.OARrestapi import OARapi
12 from sfa.senslab.LDAPapi import LDAPapi
13 from sfa.senslab.slabdriver import SlabDriver
14 from sfa.util.config import Config
15 from sfa.util.xrn import hrn_to_urn, get_authority,Xrn,get_leaf
16 from sfa.util.table import SfaTable
17 from sfa.util.record import SfaRecord
18 from sfa.trust.hierarchy import Hierarchy
19 from sfa.trust.certificate import Keypair
20 from sfa.trust.gid import create_uuid
23 AuthHierarchy = Hierarchy()
25 if not table.exists():
28 def create_top_level_auth_records(hrn):
30 Create top level records (includes root and sub authorities (local/remote)
32 print>>sys.stderr, "\r\n =========SenslabImport create_top_level_auth_records\r\n"
33 urn = hrn_to_urn(hrn, 'authority')
34 # make sure parent exists
35 parent_hrn = get_authority(hrn)
38 if not parent_hrn == hrn:
39 create_top_level_auth_records(parent_hrn)
42 # create the authority if it doesnt already exist
43 if not AuthHierarchy.auth_exists(urn):
44 AuthHierarchy.create_auth(urn)
46 # create the db record if it doesnt already exist
47 auth_info = AuthHierarchy.get_auth_info(hrn)
49 auth_record = table.find({'type': 'authority', 'hrn': hrn})
52 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
53 auth_record['authority'] = get_authority(auth_record['hrn'])
54 print sys.stderr, " \r\n \t slab-import : auth record %s inserted" %(auth_record['hrn'])
55 table.insert(auth_record)
56 print>>sys.stderr, "\r\n ========= \t\t SenslabImport NO AUTH RECORD \r\n" ,auth_record['authority']
59 def import_node(hrn, node):
61 # ASN.1 will have problems with hrn's longer than 64 characters
65 node_record = table.find({'type': 'node', 'hrn': hrn})
66 pkey = Keypair(create=True)
67 urn = hrn_to_urn(hrn, 'node')
68 node_gid = AuthHierarchy.create_gid(urn, create_uuid(), pkey)
69 node_record = SfaRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
70 node_record['authority'] = get_authority(node_record['hrn'])
71 extime = datetime.datetime.utcnow()
72 node_record['date_created'] = int(time.mktime(extime.timetuple()))
73 existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']})
74 if not existing_records:
75 print>>sys.stderr, " \r\n \t slab-import : node record %s inserted" %(node_record['hrn'])
76 table.insert(node_record)
78 existing_record = existing_records[0]
79 node_record['record_id'] = existing_record['record_id']
80 table.update(node_record)
82 # person is already a sfa record
83 def import_person(person):
84 existing_records = table.find({'hrn': person['hrn'], 'type': 'user'})
85 extime = datetime.datetime.utcnow()
86 person['date_created'] = int(time.mktime(extime.timetuple()))
87 if not existing_records:
88 print>>sys.stderr, " \r\n \t slab-import : person record %s inserted" %(person['hrn'])
91 existing_record = existing_records[0]
92 person['record_id'] = existing_record['record_id']
95 def import_slice(person):
97 hrn = person['hrn']+'_slice'
98 pkey = Keypair(create=True)
99 urn = hrn_to_urn(hrn, 'slice')
100 gid = AuthHierarchy.create_gid(urn, create_uuid(), pkey)
101 slice_record= SfaRecord(hrn=hrn, gid=gid, type="slice", pointer=-1)
102 slice_record['authority'] = get_authority(slice_record['hrn'])
104 extime = datetime.datetime.utcnow()
105 slice_record['date_created'] = int(time.mktime(extime.timetuple()))
107 print>>sys.stderr, " \r\n \t slab-import : slice record %s " %(slice_record['hrn'])
108 existing_records = table.find({'hrn': slice_record['hrn'], 'type': 'slice'})
109 if not existing_records:
110 print>>sys.stderr, " \r\n \t slab-import : slice record %s inserted" %(slice_record['hrn'])
111 table.insert(slice_record)
113 print>>sys.stderr, " \r\n \t slab-import : slice record %s updated" %(slice_record['hrn'])
114 existing_record = existing_records[0]
115 slice_record['record_id'] = existing_record['record_id']
116 table.update(slice_record)
118 def delete_record( hrn, type):
120 record_list = table.find({'type': type, 'hrn': hrn})
121 for record in record_list:
122 print>>sys.stderr, " \r\n \t slab-import : record %s deleted" %(record['hrn'])
125 def hostname_to_hrn(root_auth,hostname):
126 # keep only the first part of the DNS name
127 #hrn='.'.join( [auth,hostname.split(".")[0] ] )
128 # escape the '.' in the hostname
129 hrn='.'.join( [root_auth,Xrn.escape(hostname)] )
130 return hrn_to_urn(hrn,'node')
135 if not config.SFA_REGISTRY_ENABLED:
137 root_auth = config.SFA_REGISTRY_ROOT_AUTH
138 interface_hrn = config.SFA_INTERFACE_HRN
139 print interface_hrn, root_auth
141 # initialize registry db table
143 #if not table.exists():
146 # create root authority
147 create_top_level_auth_records(root_auth)
149 # create s user record for the slice manager
151 #SenslabImporter.create_sm_client_record()
153 # create interface records
155 #SenslabImporter.logger.info("Import: creating interface records")
156 #SenslabImporter.create_interface_records()
157 # create dict of all existing sfa records
159 existing_records = {}
163 results = table.find()
164 for result in results:
165 existing_records[(result['hrn'], result['type'])] = result
166 existing_hrns.append(result['hrn'])
170 Driver = SlabDriver(config)
171 nodes_dict = Driver.GetNodes()
172 #print "\r\n NODES8DICT ",nodes_dict
174 ldap_person_list = Driver.GetPersons()
177 #slices_list = SenslabUsers.GetSlices()
178 #print "\r\n SLICES_LIST ",slices_list
180 # Get all Senslab sites
181 #sites_dict = OARImporter.GetSites()
182 #print "\r\n sSITES_DICT" , sites_dict
185 #for site in sites_dict:
186 #site_hrn = interface_hrn + "." + site['login_base']
187 ##sfa_logger().info("Importing site: %s" % site_hrn)
188 #print "HRN %s %s site existing in hrn ? %s" %( site['login_base'],site_hrn, site_hrn in existing_hrns)
189 ## import if hrn is not in list of existing hrns or if the hrn exists
190 ## but its not a site record
191 #if site_hrn not in existing_hrns or \
192 #(site_hrn, 'authority') not in existing_records:
193 #print "SITE HRN UNKNOWN" , site, site_hrn
194 #site_hrn = SenslabImporter.import_site(interface_hrn, site)
196 # import node records
197 for node in nodes_dict:
198 hrn = hostname_to_hrn( root_auth, node['hostname'])
199 if hrn not in existing_hrns or \
200 (hrn, 'node') not in existing_records:
201 import_node(hrn, node)
204 for person in ldap_person_list:
205 if person['hrn'] not in existing_hrns or \
206 (person['hrn'], 'user') not in existing_records :
207 import_person(person)
211 #for slice_id in site['slice_ids']:
212 #print >>sys.stderr, "\r\n\r\n \t ^^^^^^^\\\\\\\\\\\\\\\^^^^^^ slice_id %s " %(slice_id)
213 #for sl in slices_list:
214 #if slice_id is sl['slice_id']:
215 ##hrn = slicename_to_hrn(interface_hrn, sl['name'])
216 #hrn = email_to_hrn(site_hrn, sl['name'])
217 #print >>sys.stderr, "\r\n\r\n^^^^^^^^^^^^^SLICE ID hrn %s site_hrn %s" %(hrn,site_hrn)
218 #if hrn not in existing_hrns or \
219 #(hrn, 'slice') not in existing_records:
220 #SenslabImporter.import_slice(site_hrn, sl)
223 # remove stale records
224 system_records = [interface_hrn, root_auth, interface_hrn + '.slicemanager']
226 for (record_hrn, type) in existing_records.keys():
227 if record_hrn in system_records:
230 record = existing_records[(record_hrn, type)]
231 if record['peer_authority']:
238 if type == 'authority':
239 #for site in sites_dict:
240 #print "\t type : authority : ", site
241 #site_hrn = interface_hrn + "." + site['login_base']
242 #if site_hrn == record_hrn and site['site_id'] == record['pointer']:
244 print "\t \t Found :", found
248 for person in ldap_person_list:
249 if person['hrn'] == record_hrn:
254 login_base = get_leaf(get_authority(record_hrn))
255 nodename = Xrn.unescape(get_leaf(record_hrn))
256 for node in nodes_dict:
257 if node['hostname'] == nodename :
261 elif type == 'slice':
262 for person in ldap_person_list:
263 if person['hrn']+'_slice' == record_hrn:
270 record_object = existing_records[(record_hrn, type)]
271 print "\t\t NOT FOUND ! ", record_hrn
272 delete_record(record_hrn, type)
274 if __name__ == "__main__":