3 from sfa.util.faults import MissingSfaInfo
4 from sfa.util.sfalogging import logger
5 from sfa.util.table import SfaTable
7 from sfa.util.xrn import hrn_to_urn
8 from sfa.util.plxrn import slicename_to_hrn, hostname_to_hrn, hrn_to_pl_slicename, hrn_to_pl_login_base
10 from sfa.server.sfaapi import SfaApi
12 #################### xxx should move into util/defaultdict
14 from collections import defaultdict
16 class defaultdict(dict):
17 def __init__(self, default_factory=None, *a, **kw):
18 if (default_factory is not None and
19 not hasattr(default_factory, '__call__')):
20 raise TypeError('first argument must be callable')
21 dict.__init__(self, *a, **kw)
22 self.default_factory = default_factory
23 def __getitem__(self, key):
25 return dict.__getitem__(self, key)
27 return self.__missing__(key)
28 def __missing__(self, key):
29 if self.default_factory is None:
31 self[key] = value = self.default_factory()
34 if self.default_factory is None:
37 args = self.default_factory,
38 return type(self), args, None, None, self.items()
40 return self.__copy__()
42 return type(self)(self.default_factory, self)
43 def __deepcopy__(self, memo):
45 return type(self)(self.default_factory,
46 copy.deepcopy(self.items()))
48 return 'defaultdict(%s, %s)' % (self.default_factory,
50 ## end of http://code.activestate.com/recipes/523034/ }}}
52 def list_to_dict(recs, key):
54 convert a list of dictionaries into a dictionary keyed on the
55 specified dictionary key
57 keys = [rec[key] for rec in recs]
58 return dict(zip(keys, recs))
60 class PlcSfaApi(SfaApi):
62 def __init__ (self, encoding="utf-8", methods='sfa.methods',
63 config = "/etc/sfa/sfa_config.py",
64 peer_cert = None, interface = None,
65 key_file = None, cert_file = None, cache = None):
66 SfaApi.__init__(self, encoding=encoding, methods=methods,
68 peer_cert=peer_cert, interface=interface,
70 cert_file=cert_file, cache=cache)
72 self.SfaTable = SfaTable
73 # Initialize the PLC shell only if SFA wraps a myPLC
74 rspec_type = self.config.get_aggregate_type()
75 if (rspec_type == 'pl' or rspec_type == 'vini' or \
76 rspec_type == 'eucalyptus' or rspec_type == 'max'):
77 self.plshell = self.getPLCShell()
78 self.plshell_version = "4.3"
80 def getPLCShell(self):
81 self.plauth = {'Username': self.config.SFA_PLC_USER,
82 'AuthMethod': 'password',
83 'AuthString': self.config.SFA_PLC_PASSWORD}
85 # The native shell (PLC.Shell.Shell) is more efficient than xmlrpc,
86 # but it leaves idle db connections open. use xmlrpc until we can figure
87 # out why PLC.Shell.Shell doesn't close db connection properly
89 # sys.path.append(os.path.dirname(os.path.realpath("/usr/bin/plcsh")))
90 # self.plshell_type = 'direct'
92 # shell = PLC.Shell.Shell(globals = globals())
95 self.plshell_type = 'xmlrpc'
96 url = self.config.SFA_PLC_URL
97 shell = xmlrpclib.Server(url, verbose = 0, allow_none = True)
101 # Convert SFA fields to PLC fields for use when registering up updating
102 # registry record in the PLC database
104 # @param type type of record (user, slice, ...)
105 # @param hrn human readable name
106 # @param sfa_fields dictionary of SFA fields
107 # @param pl_fields dictionary of PLC fields (output)
109 def sfa_fields_to_pl_fields(self, type, hrn, record):
111 def convert_ints(tmpdict, int_fields):
112 for field in int_fields:
114 tmpdict[field] = int(tmpdict[field])
117 #for field in record:
118 # pl_record[field] = record[field]
121 if not "instantiation" in pl_record:
122 pl_record["instantiation"] = "plc-instantiated"
123 pl_record["name"] = hrn_to_pl_slicename(hrn)
125 pl_record["url"] = record["url"]
126 if "description" in record:
127 pl_record["description"] = record["description"]
128 if "expires" in record:
129 pl_record["expires"] = int(record["expires"])
132 if not "hostname" in pl_record:
133 if not "hostname" in record:
134 raise MissingSfaInfo("hostname")
135 pl_record["hostname"] = record["hostname"]
136 if not "model" in pl_record:
137 pl_record["model"] = "geni"
139 elif type == "authority":
140 pl_record["login_base"] = hrn_to_pl_login_base(hrn)
142 if not "name" in pl_record:
143 pl_record["name"] = hrn
145 if not "abbreviated_name" in pl_record:
146 pl_record["abbreviated_name"] = hrn
148 if not "enabled" in pl_record:
149 pl_record["enabled"] = True
151 if not "is_public" in pl_record:
152 pl_record["is_public"] = True
156 def fill_record_pl_info(self, records):
158 Fill in the planetlab specific fields of a SFA record. This
159 involves calling the appropriate PLC method to retrieve the
160 database record for the object.
162 PLC data is filled into the pl_info field of the record.
164 @param record: record to fill in field (in/out param)
167 node_ids, site_ids, slice_ids = [], [], []
168 person_ids, key_ids = [], []
169 type_map = {'node': node_ids, 'authority': site_ids,
170 'slice': slice_ids, 'user': person_ids}
172 for record in records:
173 for type in type_map:
174 if type == record['type']:
175 type_map[type].append(record['pointer'])
178 nodes, sites, slices, persons, keys = {}, {}, {}, {}, {}
180 node_list = self.plshell.GetNodes(self.plauth, node_ids)
181 nodes = list_to_dict(node_list, 'node_id')
183 site_list = self.plshell.GetSites(self.plauth, site_ids)
184 sites = list_to_dict(site_list, 'site_id')
186 slice_list = self.plshell.GetSlices(self.plauth, slice_ids)
187 slices = list_to_dict(slice_list, 'slice_id')
189 person_list = self.plshell.GetPersons(self.plauth, person_ids)
190 persons = list_to_dict(person_list, 'person_id')
191 for person in persons:
192 key_ids.extend(persons[person]['key_ids'])
194 pl_records = {'node': nodes, 'authority': sites,
195 'slice': slices, 'user': persons}
198 key_list = self.plshell.GetKeys(self.plauth, key_ids)
199 keys = list_to_dict(key_list, 'key_id')
202 for record in records:
203 # records with pointer==-1 do not have plc info.
204 # for example, the top level authority records which are
205 # authorities, but not PL "sites"
206 if record['pointer'] == -1:
209 for type in pl_records:
210 if record['type'] == type:
211 if record['pointer'] in pl_records[type]:
212 record.update(pl_records[type][record['pointer']])
215 if record['type'] == 'user':
216 if 'key_ids' not in record:
217 logger.info("user record has no 'key_ids' - need to import from myplc ?")
219 pubkeys = [keys[key_id]['key'] for key_id in record['key_ids'] if key_id in keys]
220 record['keys'] = pubkeys
222 # fill in record hrns
223 records = self.fill_record_hrns(records)
227 def fill_record_hrns(self, records):
229 convert pl ids to hrns
233 slice_ids, person_ids, site_ids, node_ids = [], [], [], []
234 for record in records:
235 if 'site_id' in record:
236 site_ids.append(record['site_id'])
237 if 'site_ids' in records:
238 site_ids.extend(record['site_ids'])
239 if 'person_ids' in record:
240 person_ids.extend(record['person_ids'])
241 if 'slice_ids' in record:
242 slice_ids.extend(record['slice_ids'])
243 if 'node_ids' in record:
244 node_ids.extend(record['node_ids'])
247 slices, persons, sites, nodes = {}, {}, {}, {}
249 site_list = self.plshell.GetSites(self.plauth, site_ids, ['site_id', 'login_base'])
250 sites = list_to_dict(site_list, 'site_id')
252 person_list = self.plshell.GetPersons(self.plauth, person_ids, ['person_id', 'email'])
253 persons = list_to_dict(person_list, 'person_id')
255 slice_list = self.plshell.GetSlices(self.plauth, slice_ids, ['slice_id', 'name'])
256 slices = list_to_dict(slice_list, 'slice_id')
258 node_list = self.plshell.GetNodes(self.plauth, node_ids, ['node_id', 'hostname'])
259 nodes = list_to_dict(node_list, 'node_id')
261 # convert ids to hrns
262 for record in records:
263 # get all relevant data
264 type = record['type']
265 pointer = record['pointer']
271 if 'site_id' in record:
272 site = sites[record['site_id']]
273 login_base = site['login_base']
274 record['site'] = ".".join([auth_hrn, login_base])
275 if 'person_ids' in record:
276 emails = [persons[person_id]['email'] for person_id in record['person_ids'] \
277 if person_id in persons]
278 usernames = [email.split('@')[0] for email in emails]
279 person_hrns = [".".join([auth_hrn, login_base, username]) for username in usernames]
280 record['persons'] = person_hrns
281 if 'slice_ids' in record:
282 slicenames = [slices[slice_id]['name'] for slice_id in record['slice_ids'] \
283 if slice_id in slices]
284 slice_hrns = [slicename_to_hrn(auth_hrn, slicename) for slicename in slicenames]
285 record['slices'] = slice_hrns
286 if 'node_ids' in record:
287 hostnames = [nodes[node_id]['hostname'] for node_id in record['node_ids'] \
289 node_hrns = [hostname_to_hrn(auth_hrn, login_base, hostname) for hostname in hostnames]
290 record['nodes'] = node_hrns
291 if 'site_ids' in record:
292 login_bases = [sites[site_id]['login_base'] for site_id in record['site_ids'] \
294 site_hrns = [".".join([auth_hrn, lbase]) for lbase in login_bases]
295 record['sites'] = site_hrns
299 def fill_record_sfa_info(self, records):
301 def startswith(prefix, values):
302 return [value for value in values if value.startswith(prefix)]
307 for record in records:
308 person_ids.extend(record.get("person_ids", []))
309 site_ids.extend(record.get("site_ids", []))
310 if 'site_id' in record:
311 site_ids.append(record['site_id'])
313 # get all pis from the sites we've encountered
314 # and store them in a dictionary keyed on site_id
317 pi_filter = {'|roles': ['pi'], '|site_ids': site_ids}
318 pi_list = self.plshell.GetPersons(self.plauth, pi_filter, ['person_id', 'site_ids'])
320 # we will need the pi's hrns also
321 person_ids.append(pi['person_id'])
323 # we also need to keep track of the sites these pis
325 for site_id in pi['site_ids']:
326 if site_id in site_pis:
327 site_pis[site_id].append(pi)
329 site_pis[site_id] = [pi]
331 # get sfa records for all records associated with these records.
332 # we'll replace pl ids (person_ids) with hrns from the sfa records
335 # get the sfa records
336 table = self.SfaTable()
337 person_list, persons = [], {}
338 person_list = table.find({'type': 'user', 'pointer': person_ids})
339 # create a hrns keyed on the sfa record's pointer.
340 # Its possible for multiple records to have the same pointer so
341 # the dict's value will be a list of hrns.
342 persons = defaultdict(list)
343 for person in person_list:
344 persons[person['pointer']].append(person)
347 pl_person_list, pl_persons = [], {}
348 pl_person_list = self.plshell.GetPersons(self.plauth, person_ids, ['person_id', 'roles'])
349 pl_persons = list_to_dict(pl_person_list, 'person_id')
352 for record in records:
353 # skip records with no pl info (top level authorities)
354 #if record['pointer'] == -1:
357 type = record['type']
358 if (type == "slice"):
359 # all slice users are researchers
360 record['geni_urn'] = hrn_to_urn(record['hrn'], 'slice')
362 record['researcher'] = []
363 for person_id in record.get('person_ids', []):
364 hrns = [person['hrn'] for person in persons[person_id]]
365 record['researcher'].extend(hrns)
367 # pis at the slice's site
368 if 'site_id' in record and record['site_id'] in site_pis:
369 pl_pis = site_pis[record['site_id']]
370 pi_ids = [pi['person_id'] for pi in pl_pis]
371 for person_id in pi_ids:
372 hrns = [person['hrn'] for person in persons[person_id]]
373 record['PI'].extend(hrns)
374 record['geni_creator'] = record['PI']
376 elif (type.startswith("authority")):
378 if record['hrn'] in self.aggregates:
380 record['url'] = self.aggregates[record['hrn']].get_url()
382 if record['pointer'] != -1:
384 record['operator'] = []
386 for pointer in record.get('person_ids', []):
387 if pointer not in persons or pointer not in pl_persons:
388 # this means there is not sfa or pl record for this user
390 hrns = [person['hrn'] for person in persons[pointer]]
391 roles = pl_persons[pointer]['roles']
393 record['PI'].extend(hrns)
395 record['operator'].extend(hrns)
397 record['owner'].extend(hrns)
398 # xxx TODO: OrganizationName
399 elif (type == "node"):
400 sfa_info['dns'] = record.get("hostname", "")
401 # xxx TODO: URI, LatLong, IP, DNS
403 elif (type == "user"):
404 sfa_info['email'] = record.get("email", "")
405 sfa_info['geni_urn'] = hrn_to_urn(record['hrn'], 'user')
406 sfa_info['geni_certificate'] = record['gid']
407 # xxx TODO: PostalAddress, Phone
408 record.update(sfa_info)
410 def fill_record_info(self, records):
412 Given a SFA record, fill in the PLC specific and SFA specific
413 fields in the record.
415 if not isinstance(records, list):
418 self.fill_record_pl_info(records)
419 self.fill_record_sfa_info(records)
421 def update_membership_list(self, oldRecord, record, listName, addFunc, delFunc):
422 # get a list of the HRNs that are members of the old and new records
424 oldList = oldRecord.get(listName, [])
427 newList = record.get(listName, [])
428 # xxx ugly hack - somehow we receive here a list of {'text':value}
429 # instead of an expected list of strings
430 # please remove once this is issue is cleanly fixed
431 def normalize (value):
432 from types import StringTypes
433 if isinstance(value,StringTypes): return value
434 elif isinstance(value,dict):
435 newvalue=value['text']
436 logger.info("Normalizing %s=>%s"%(value,newvalue))
438 newList=[normalize(v) for v in newList]
440 # if the lists are the same, then we don't have to update anything
441 if (oldList == newList):
444 # build a list of the new person ids, by looking up each person to get
447 table = self.SfaTable()
448 records = table.find({'type': 'user', 'hrn': newList})
450 newIdList.append(rec['pointer'])
452 # build a list of the old person ids from the person_ids field
454 oldIdList = oldRecord.get("person_ids", [])
455 containerId = oldRecord.get_pointer()
457 # if oldRecord==None, then we are doing a Register, instead of an
460 containerId = record.get_pointer()
462 # add people who are in the new list, but not the oldList
463 for personId in newIdList:
464 if not (personId in oldIdList):
465 addFunc(self.plauth, personId, containerId)
467 # remove people who are in the old list, but not the new list
468 for personId in oldIdList:
469 if not (personId in newIdList):
470 delFunc(self.plauth, personId, containerId)
472 def update_membership(self, oldRecord, record):
473 if record.type == "slice":
474 self.update_membership_list(oldRecord, record, 'researcher',
475 self.plshell.AddPersonToSlice,
476 self.plshell.DeletePersonFromSlice)
477 elif record.type == "authority":