-def get_slices(api):
- # XX just import the legacy module and excute that until
- # we transition the code to this module
- from sfa.plc.slices import Slices
- slices = Slices(api)
- slices.refresh()
- return [hrn_to_urn(slice_hrn, 'slice') for slice_hrn in slices['hrn']]
-
-def get_rspec(api, xrn=None, origin_hrn=None):
- hrn, type = urn_to_hrn(xrn)
- rspec = None
-
- aggs = api.aggregates
- cred = api.getCredential()
- for agg in aggs:
- if agg not in [api.auth.client_cred.get_gid_caller().get_hrn()]:
- try:
- # get the rspec from the aggregate
- agg_rspec = aggs[agg].get_resources(cred, xrn, origin_hrn)
- except:
- # XX print out to some error log
- print >> log, "Error getting resources at aggregate %s" % agg
- traceback.print_exc(log)
- print >> log, "%s" % (traceback.format_exc())
-
-
- try:
- tree = etree.parse(StringIO(agg_rspec))
- except etree.XMLSyntaxError:
- message = agg + ": " + str(sys.exc_info()[1])
- raise InvalidRSpec(message)
-
- root = tree.getroot()
- if root.get("type") in ["SFA"]:
- if rspec == None:
- rspec = root
- else:
- for network in root.iterfind("./network"):
- rspec.append(deepcopy(network))
- for request in root.iterfind("./request"):
- rspec.append(deepcopy(request))
-
- return etree.tostring(rspec, xml_declaration=True, pretty_print=True)
-
-"""
-Returns the request context required by sfatables. At some point, this
-mechanism should be changed to refer to "contexts", which is the
-information that sfatables is requesting. But for now, we just return
-the basic information needed in a dict.
-"""
-def fetch_context(slice_hrn, user_hrn, contexts):
- #slice_hrn = urn_to_hrn(slice_xrn)[0]
- #user_hrn = urn_to_hrn(user_xrn)[0]
- base_context = {'sfa':{'user':{'hrn':user_hrn}, 'slice':{'hrn':slice_hrn}}}
- return base_context
+def shutdown(api, xrn, creds):
+ """
+ Not implemented
+ """
+ return 1
+
+def status(api, xrn, creds):
+ """
+ Not implemented
+ """
+ return 1
+
+def get_slices(api, creds):
+
+ # look in cache first
+ if api.cache:
+ slices = api.cache.get('slices')
+ if slices:
+ return slices
+
+ # get the callers hrn
+ valid_cred = api.auth.checkCredentials(creds, 'listslices', None)[0]
+ caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
+
+ # attempt to use delegated credential first
+ credential = api.getDelegatedCredential(creds)
+ if not credential:
+ credential = api.getCredential()
+ threads = ThreadManager()
+ # fetch from aggregates
+ for aggregate in api.aggregates:
+ # prevent infinite loop. Dont send request back to caller
+ # unless the caller is the aggregate's SM
+ if caller_hrn == aggregate and aggregate != api.hrn:
+ continue
+ server = api.aggregates[aggregate]
+ threads.run(server.ListSlices, credential)
+
+ # combime results
+ results = threads.get_results()
+ slices = []
+ for result in results:
+ slices.extend(result)
+
+ # cache the result
+ if api.cache:
+ api.cache.add('slices', slices)
+
+ return slices
+
+
+# Thierry : caching at the slicemgr level makes sense to some extent
+caching=True
+#caching=False
+def ListResources(api, creds, options, call_id):
+
+ if Callids().already_handled(call_id):
+ api.logger.info("%d received ListResources with known call_id %s"%(api.interface,call_id))
+ return ""
+
+ # get slice's hrn from options
+ xrn = options.get('geni_slice_urn', '')
+ (hrn, type) = urn_to_hrn(xrn)
+
+ # get hrn of the original caller
+ origin_hrn = options.get('origin_hrn', None)
+ if not origin_hrn:
+ if isinstance(creds, list):
+ origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
+ else:
+ origin_hrn = Credential(string=creds).get_gid_caller().get_hrn()
+
+ # look in cache first
+ if caching and api.cache and not xrn:
+ rspec = api.cache.get('nodes')
+ if rspec:
+ return rspec
+
+ # get the callers hrn
+ valid_cred = api.auth.checkCredentials(creds, 'listnodes', hrn)[0]
+ caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
+
+ # attempt to use delegated credential first
+ credential = api.getDelegatedCredential(creds)
+ if not credential:
+ credential = api.getCredential()
+ threads = ThreadManager()
+ for aggregate in api.aggregates:
+ # prevent infinite loop. Dont send request back to caller
+ # unless the caller is the aggregate's SM
+ if caller_hrn == aggregate and aggregate != api.hrn:
+ continue
+ # get the rspec from the aggregate
+ server = api.aggregates[aggregate]
+ my_opts = copy(options)
+ my_opts['geni_compressed'] = False
+ threads.run(server.ListResources, credential, my_opts, call_id)
+ #threads.run(server.get_resources, cred, xrn, origin_hrn)
+
+ results = threads.get_results()
+ merged_rspec = merge_rspecs(results)
+
+ # cache the result
+ if caching and api.cache and not xrn:
+ api.cache.add('nodes', merged_rspec)
+
+ return merged_rspec