7 from sfa.trust.sfaticket import SfaTicket
8 from sfa.trust.credential import Credential
10 from sfa.util.sfalogging import logger
11 from sfa.util.xrn import Xrn, urn_to_hrn
12 from sfa.util.version import version_core
13 from sfa.util.callids import Callids
14 from sfa.util.cache import Cache
16 from sfa.client.multiclient import MultiClient
18 from sfa.rspecs.rspec_converter import RSpecConverter
19 from sfa.rspecs.version_manager import VersionManager
20 from sfa.rspecs.rspec import RSpec
22 from sfa.client.client_helper import sfa_to_pg_users_arg
23 from sfa.client.return_value import ReturnValue
28 # the cache instance is a class member so it survives across incoming
32 def __init__(self, config):
34 if config.SFA_SM_CACHING:
35 if SliceManager.cache is None:
36 SliceManager.cache = Cache()
37 self.cache = SliceManager.cache
39 def GetVersion(self, api, options):
40 # peers explicitly in aggregates.xml
41 peers = dict([(peername, interface.get_url()) for (peername, interface) in api.aggregates.iteritems()
42 if peername != api.hrn])
43 version_manager = VersionManager()
44 ad_rspec_versions = []
45 request_rspec_versions = []
46 cred_types = [{'geni_type': 'geni_sfa',
47 'geni_version': str(i)} for i in range(4)[-2:]]
48 for rspec_version in version_manager.versions:
49 if rspec_version.content_type in ['*', 'ad']:
50 ad_rspec_versions.append(rspec_version.to_dict())
51 if rspec_version.content_type in ['*', 'request']:
52 request_rspec_versions.append(rspec_version.to_dict())
53 xrn = Xrn(api.hrn, 'authority+sm')
55 'interface': 'slicemgr',
58 'geni_api_versions': {'3': 'https://%s:%s' % (api.config.SFA_SM_HOST, api.config.SFA_SM_PORT)},
62 # Accept operations that act on as subset of slivers in a given
64 'geni_single_allocation': 0,
65 # Multiple slivers can exist and be incrementally added, including
66 # those which connect or overlap in some way.
67 'geni_allocate': 'geni_many',
68 'geni_credential_types': cred_types,
70 sm_version = version_core(version_more)
71 # local aggregate if present needs to have localhost resolved
72 if api.hrn in api.aggregates:
73 local_am_url = api.aggregates[api.hrn].get_url()
74 sm_version['peers'][api.hrn] = local_am_url.replace(
75 'localhost', sm_version['hostname'])
78 def drop_slicemgr_stats(self, rspec):
80 stats_elements = rspec.xml.xpath('//statistics')
81 for node in stats_elements:
82 node.getparent().remove(node)
83 except Exception as e:
84 logger.warn("drop_slicemgr_stats failed: %s " % (str(e)))
86 def add_slicemgr_stat(self, rspec, callname, aggname, elapsed, status, exc_info=None):
88 stats_tags = rspec.xml.xpath('//statistics[@call="%s"]' % callname)
90 stats_tag = stats_tags[0]
92 stats_tag = rspec.xml.root.add_element(
93 "statistics", call=callname)
95 stat_tag = stats_tag.add_element("aggregate", name=str(aggname),
96 elapsed=str(elapsed), status=str(status))
99 exc_tag = stat_tag.add_element(
100 "exc_info", name=str(exc_info[1]))
102 # formats the traceback as one big text blob
103 #exc_tag.text = "\n".join(traceback.format_exception(exc_info[0], exc_info[1], exc_info[2]))
105 # formats the traceback as a set of xml elements
106 tb = traceback.extract_tb(exc_info[2])
108 exc_frame = exc_tag.add_element("tb_frame", filename=str(item[0]),
109 line=str(item[1]), func=str(item[2]), code=str(item[3]))
111 except Exception as e:
112 logger.warn("add_slicemgr_stat failed on %s: %s" %
115 def ListResources(self, api, creds, options):
116 call_id = options.get('call_id')
117 if Callids().already_handled(call_id):
120 version_manager = VersionManager()
122 def _ListResources(aggregate, server, credential, options):
123 forward_options = copy(options)
126 version = api.get_cached_server_version(server)
127 # force ProtoGENI aggregates to give us a v2 RSpec
128 forward_options['geni_rspec_version'] = options.get(
129 'geni_rspec_version')
130 result = server.ListResources(credential, forward_options)
131 return {"aggregate": aggregate, "result": result, "elapsed": time.time() - tStart, "status": "success"}
132 except Exception as e:
133 api.logger.log_exc("ListResources failed at %s" % (server.url))
134 return {"aggregate": aggregate, "elapsed": time.time() - tStart, "status": "exception", "exc_info": sys.exc_info()}
136 # get slice's hrn from options
137 xrn = options.get('geni_slice_urn', '')
138 (hrn, type) = urn_to_hrn(xrn)
139 if 'geni_compressed' in options:
140 del(options['geni_compressed'])
142 # get the rspec's return format from options
143 rspec_version = version_manager.get_version(
144 options.get('geni_rspec_version'))
145 version_string = "rspec_%s" % (rspec_version)
147 # look in cache first
148 cached_requested = options.get('cached', True)
149 if not xrn and self.cache and cached_requested:
150 rspec = self.cache.get(version_string)
153 "SliceManager.ListResources returns cached advertisement")
156 # get the callers hrn
157 valid_cred = api.auth.checkCredentials(creds, 'listnodes', hrn)[0]
158 caller_hrn = Credential(cred=valid_cred).get_gid_caller().get_hrn()
160 # attempt to use delegated credential first
161 cred = api.getDelegatedCredential(creds)
163 cred = api.getCredential()
164 multiclient = MultiClient()
165 for aggregate in api.aggregates:
166 # prevent infinite loop. Dont send request back to caller
167 # unless the caller is the aggregate's SM
168 if caller_hrn == aggregate and aggregate != api.hrn:
171 # get the rspec from the aggregate
172 interface = api.aggregates[aggregate]
173 server = api.server_proxy(interface, cred)
174 multiclient.run(_ListResources, aggregate, server, [cred], options)
176 results = multiclient.get_results()
177 rspec_version = version_manager.get_version(
178 options.get('geni_rspec_version'))
180 result_version = version_manager._get_version(
181 rspec_version.type, rspec_version.version, 'manifest')
183 result_version = version_manager._get_version(
184 rspec_version.type, rspec_version.version, 'ad')
185 rspec = RSpec(version=result_version)
186 for result in results:
187 self.add_slicemgr_stat(rspec, "ListResources", result["aggregate"], result["elapsed"],
188 result["status"], result.get("exc_info", None))
189 if result["status"] == "success":
190 res = result['result']['value']
192 rspec.version.merge(ReturnValue.get_value(res))
195 "SM.ListResources: Failed to merge aggregate rspec")
198 if self.cache and not xrn:
199 api.logger.debug("SliceManager.ListResources caches advertisement")
200 self.cache.add(version_string, rspec.toxml())
204 def Allocate(self, api, xrn, creds, rspec_str, expiration, options):
205 call_id = options.get('call_id')
206 if Callids().already_handled(call_id):
209 version_manager = VersionManager()
211 def _Allocate(aggregate, server, xrn, credential, rspec, options):
214 # Need to call GetVersion at an aggregate to determine the supported
215 # rspec type/format beofre calling CreateSliver at an Aggregate.
216 #server_version = api.get_cached_server_version(server)
217 # if 'sfa' not in server_version and 'geni_api' in server_version:
218 # sfa aggregtes support both sfa and pg rspecs, no need to convert
219 # if aggregate supports sfa rspecs. otherwise convert to pg rspec
220 #rspec = RSpec(RSpecConverter.to_pg_rspec(rspec, 'request'))
221 #filter = {'component_manager_id': server_version['urn']}
222 # rspec.filter(filter)
223 #rspec = rspec.toxml()
224 result = server.Allocate(xrn, credential, rspec, options)
225 return {"aggregate": aggregate, "result": result, "elapsed": time.time() - tStart, "status": "success"}
228 'Something wrong in _Allocate with URL %s' % server.url)
229 return {"aggregate": aggregate, "elapsed": time.time() - tStart, "status": "exception", "exc_info": sys.exc_info()}
231 # Validate the RSpec against PlanetLab's schema --disabled for now
232 # The schema used here needs to aggregate the PL and VINI schemas
233 # schema = "/var/www/html/schemas/pl.rng"
234 rspec = RSpec(rspec_str)
237 # rspec.validate(schema)
239 # if there is a <statistics> section, the aggregates don't care about it,
241 self.drop_slicemgr_stats(rspec)
243 # attempt to use delegated credential first
244 cred = api.getDelegatedCredential(creds)
246 cred = api.getCredential()
248 # get the callers hrn
249 hrn, type = urn_to_hrn(xrn)
250 valid_cred = api.auth.checkCredentials(creds, 'createsliver', hrn)[0]
251 caller_hrn = Credential(cred=valid_cred).get_gid_caller().get_hrn()
252 multiclient = MultiClient()
253 for aggregate in api.aggregates:
254 # prevent infinite loop. Dont send request back to caller
255 # unless the caller is the aggregate's SM
256 if caller_hrn == aggregate and aggregate != api.hrn:
258 interface = api.aggregates[aggregate]
259 server = api.server_proxy(interface, cred)
260 # Just send entire RSpec to each aggregate
261 multiclient.run(_Allocate, aggregate, server, xrn,
262 [cred], rspec.toxml(), options)
264 results = multiclient.get_results()
265 manifest_version = version_manager._get_version(
266 rspec.version.type, rspec.version.version, 'manifest')
267 result_rspec = RSpec(version=manifest_version)
271 for result in results:
272 self.add_slicemgr_stat(result_rspec, "Allocate", result["aggregate"], result["elapsed"],
273 result["status"], result.get("exc_info", None))
274 if result["status"] == "success":
276 res = result['result']['value']
277 geni_urn = res['geni_urn']
278 result_rspec.version.merge(
279 ReturnValue.get_value(res['geni_rspec']))
280 geni_slivers.extend(res['geni_slivers'])
283 "SM.Allocate: Failed to merge aggregate rspec")
285 'geni_urn': geni_urn,
286 'geni_rspec': result_rspec.toxml(),
287 'geni_slivers': geni_slivers
290 def Provision(self, api, xrn, creds, options):
291 call_id = options.get('call_id')
292 if Callids().already_handled(call_id):
295 version_manager = VersionManager()
297 def _Provision(aggregate, server, xrn, credential, options):
300 # Need to call GetVersion at an aggregate to determine the supported
301 # rspec type/format beofre calling CreateSliver at an
303 server_version = api.get_cached_server_version(server)
304 result = server.Provision(xrn, credential, options)
305 return {"aggregate": aggregate, "result": result, "elapsed": time.time() - tStart, "status": "success"}
308 'Something wrong in _Allocate with URL %s' % server.url)
309 return {"aggregate": aggregate, "elapsed": time.time() - tStart, "status": "exception", "exc_info": sys.exc_info()}
311 # attempt to use delegated credential first
312 cred = api.getDelegatedCredential(creds)
314 cred = api.getCredential()
316 # get the callers hrn
317 valid_cred = api.auth.checkCredentials(creds, 'createsliver', xrn)[0]
318 caller_hrn = Credential(cred=valid_cred).get_gid_caller().get_hrn()
319 multiclient = MultiClient()
320 for aggregate in api.aggregates:
321 # prevent infinite loop. Dont send request back to caller
322 # unless the caller is the aggregate's SM
323 if caller_hrn == aggregate and aggregate != api.hrn:
325 interface = api.aggregates[aggregate]
326 server = api.server_proxy(interface, cred)
327 # Just send entire RSpec to each aggregate
328 multiclient.run(_Provision, aggregate,
329 server, xrn, [cred], options)
331 results = multiclient.get_results()
332 manifest_version = version_manager._get_version(
333 'GENI', '3', 'manifest')
334 result_rspec = RSpec(version=manifest_version)
337 for result in results:
338 self.add_slicemgr_stat(result_rspec, "Provision", result["aggregate"], result["elapsed"],
339 result["status"], result.get("exc_info", None))
340 if result["status"] == "success":
342 res = result['result']['value']
343 geni_urn = res['geni_urn']
344 result_rspec.version.merge(
345 ReturnValue.get_value(res['geni_rspec']))
346 geni_slivers.extend(res['geni_slivers'])
349 "SM.Provision: Failed to merge aggregate rspec")
351 'geni_urn': geni_urn,
352 'geni_rspec': result_rspec.toxml(),
353 'geni_slivers': geni_slivers
356 def Renew(self, api, xrn, creds, expiration_time, options):
357 call_id = options.get('call_id')
358 if Callids().already_handled(call_id):
361 def _Renew(aggregate, server, xrn, creds, expiration_time, options):
363 result = server.Renew(xrn, creds, expiration_time, options)
364 if type(result) != dict:
365 result = {'code': {'geni_code': 0}, 'value': result}
366 result['aggregate'] = aggregate
370 'Something wrong in _Renew with URL %s' % server.url)
371 return {'aggregate': aggregate, 'exc_info': traceback.format_exc(),
372 'code': {'geni_code': -1},
373 'value': False, 'output': ""}
375 # get the callers hrn
376 valid_cred = api.auth.checkCredentials(creds, 'renewsliver', xrn)[0]
377 caller_hrn = Credential(cred=valid_cred).get_gid_caller().get_hrn()
379 # attempt to use delegated credential first
380 cred = api.getDelegatedCredential(creds)
382 cred = api.getCredential(minimumExpiration=31 * 86400)
383 multiclient = MultiClient()
384 for aggregate in api.aggregates:
385 # prevent infinite loop. Dont send request back to caller
386 # unless the caller is the aggregate's SM
387 if caller_hrn == aggregate and aggregate != api.hrn:
389 interface = api.aggregates[aggregate]
390 server = api.server_proxy(interface, cred)
391 multiclient.run(_Renew, aggregate, server, xrn, [
392 cred], expiration_time, options)
394 results = multiclient.get_results()
397 geni_output = ",".join([x.get('output', "") for x in results])
398 geni_value = reduce(lambda x, y: x and y, [result.get(
399 'value', False) for result in results], True)
400 for agg_result in results:
401 agg_geni_code = agg_result['code'].get('geni_code', 0)
403 geni_code = agg_geni_code
405 results = {'aggregates': results, 'code': {
406 'geni_code': geni_code}, 'value': geni_value, 'output': geni_output}
410 def Delete(self, api, xrn, creds, options):
411 call_id = options.get('call_id')
412 if Callids().already_handled(call_id):
415 def _Delete(server, xrn, creds, options):
416 return server.Delete(xrn, creds, options)
418 (hrn, type) = urn_to_hrn(xrn[0])
419 # get the callers hrn
420 valid_cred = api.auth.checkCredentials(creds, 'deletesliver', hrn)[0]
421 caller_hrn = Credential(cred=valid_cred).get_gid_caller().get_hrn()
423 # attempt to use delegated credential first
424 cred = api.getDelegatedCredential(creds)
426 cred = api.getCredential()
427 multiclient = MultiClient()
428 for aggregate in api.aggregates:
429 # prevent infinite loop. Dont send request back to caller
430 # unless the caller is the aggregate's SM
431 if caller_hrn == aggregate and aggregate != api.hrn:
433 interface = api.aggregates[aggregate]
434 server = api.server_proxy(interface, cred)
435 multiclient.run(_Delete, server, xrn, [cred], options)
438 for result in multiclient.get_results():
439 results += ReturnValue.get_value(result)
442 # first draft at a merging SliverStatus
443 def Status(self, api, slice_xrn, creds, options):
444 def _Status(server, xrn, creds, options):
445 return server.Status(xrn, creds, options)
447 call_id = options.get('call_id')
448 if Callids().already_handled(call_id):
450 # attempt to use delegated credential first
451 cred = api.getDelegatedCredential(creds)
453 cred = api.getCredential()
454 multiclient = MultiClient()
455 for aggregate in api.aggregates:
456 interface = api.aggregates[aggregate]
457 server = api.server_proxy(interface, cred)
458 multiclient.run(_Status, server, slice_xrn, [cred], options)
459 results = [ReturnValue.get_value(result)
460 for result in multiclient.get_results()]
462 # get rid of any void result - e.g. when call_id was hit, where by
463 # convention we return {}
465 result for result in results if result and result['geni_slivers']]
467 # do not try to combine if there's no result
471 # otherwise let's merge stuff
474 for result in results:
476 geni_urn = result['geni_urn']
477 geni_slivers.extend(result['geni_slivers'])
480 "SM.Provision: Failed to merge aggregate rspec")
482 'geni_urn': geni_urn,
483 'geni_slivers': geni_slivers
486 def Describe(self, api, creds, xrns, options):
487 def _Describe(server, xrn, creds, options):
488 return server.Describe(xrn, creds, options)
490 call_id = options.get('call_id')
491 if Callids().already_handled(call_id):
493 # attempt to use delegated credential first
494 cred = api.getDelegatedCredential(creds)
496 cred = api.getCredential()
497 multiclient = MultiClient()
498 for aggregate in api.aggregates:
499 interface = api.aggregates[aggregate]
500 server = api.server_proxy(interface, cred)
501 multiclient.run(_Describe, server, xrns, [cred], options)
502 results = [ReturnValue.get_value(result)
503 for result in multiclient.get_results()]
505 # get rid of any void result - e.g. when call_id was hit, where by
506 # convention we return {}
508 result for result in results if result and result.get('geni_urn')]
510 # do not try to combine if there's no result
514 # otherwise let's merge stuff
515 version_manager = VersionManager()
516 manifest_version = version_manager._get_version(
517 'GENI', '3', 'manifest')
518 result_rspec = RSpec(version=manifest_version)
521 for result in results:
523 geni_urn = result['geni_urn']
524 result_rspec.version.merge(
525 ReturnValue.get_value(result['geni_rspec']))
526 geni_slivers.extend(result['geni_slivers'])
529 "SM.Provision: Failed to merge aggregate rspec")
531 'geni_urn': geni_urn,
532 'geni_rspec': result_rspec.toxml(),
533 'geni_slivers': geni_slivers
536 def PerformOperationalAction(self, api, xrn, creds, action, options):
537 # get the callers hrn
538 valid_cred = api.auth.checkCredentials(creds, 'createsliver', xrn)[0]
539 caller_hrn = Credential(cred=valid_cred).get_gid_caller().get_hrn()
541 # attempt to use delegated credential first
542 cred = api.getDelegatedCredential(creds)
544 cred = api.getCredential()
545 multiclient = MultiClient()
546 for aggregate in api.aggregates:
547 # prevent infinite loop. Dont send request back to caller
548 # unless the caller is the aggregate's SM
549 if caller_hrn == aggregate and aggregate != api.hrn:
551 interface = api.aggregates[aggregate]
552 server = api.server_proxy(interface, cred)
553 multiclient.run(server.PerformOperationalAction,
554 xrn, [cred], action, options)
555 multiclient.get_results()
558 def Shutdown(self, api, xrn, creds, options=None):
562 # get the callers hrn
563 valid_cred = api.auth.checkCredentials(creds, 'stopslice', xrn.hrn)[0]
564 caller_hrn = Credential(cred=valid_cred).get_gid_caller().get_hrn()
566 # attempt to use delegated credential first
567 cred = api.getDelegatedCredential(creds)
569 cred = api.getCredential()
570 multiclient = MultiClient()
571 for aggregate in api.aggregates:
572 # prevent infinite loop. Dont send request back to caller
573 # unless the caller is the aggregate's SM
574 if caller_hrn == aggregate and aggregate != api.hrn:
576 interface = api.aggregates[aggregate]
577 server = api.server_proxy(interface, cred)
578 multiclient.run(server.Shutdown, xrn.urn, cred)
579 multiclient.get_results()