merging with geni-api branch
[sfa.git] / sfa / methods / ListResources.py
1 from sfa.util.faults import *
2 from sfa.util.namespace import *
3 from sfa.util.method import Method
4 from sfa.util.parameter import Parameter, Mixed
5 from sfa.trust.credential import Credential
6 from sfatables.runtime import SFATablesRules
7 import sys
8 import zlib
9
10 class ListResources(Method):
11     """
12     Returns information about available resources or resources allocated to this slice
13     @param credential list
14     @param options dictionary
15     @return string
16     """
17     interfaces = ['geni_am']
18     accepts = [
19         Parameter(type([str]), "List of credentials"),
20         Parameter(dict, "Options")
21         ]
22     returns = Parameter(str, "List of resources")
23
24     def call(self, creds, options):
25         self.api.logger.info("interface: %s\tmethod-name: %s" % (self.api.interface, self.name))
26             
27         # Find the valid credentials
28         hrn = None
29         if options.has_key('geni_slice_urn'):
30             xrn = options['geni_slice_urn']
31             hrn, _ = urn_to_hrn(xrn)        
32             
33         ValidCreds = self.api.auth.checkCredentials(creds, '', hrn)
34         origin_hrn = Credential(string=ValidCreds[0]).get_gid_caller().get_hrn()
35             
36                     
37         manager_base = 'sfa.managers'
38
39         if self.api.interface in ['geni_am']:
40             mgr_type = self.api.config.SFA_GENI_AGGREGATE_TYPE
41             manager_module = manager_base + ".geni_am_%s" % mgr_type
42             manager = __import__(manager_module, fromlist=[manager_base])
43             rspec = manager.ListResources(self.api, ValidCreds, options)
44             outgoing_rules = SFATablesRules('OUTGOING')
45             
46         
47         filtered_rspec = rspec
48         if outgoing_rules.sorted_rule_list:
49             context = {'sfa':{'user':{'hrn':origin_hrn}, 'slice':{'hrn':None}}}
50             outgoing_rules.set_context(context)
51             filtered_rspec = outgoing_rules.apply(rspec)      
52  
53         if options.has_key('geni_compressed') and options['geni_compressed'] == True:
54             filtered_rspec = zlib.compress(rspec).encode('base64')
55     
56
57         return filtered_rspec  
58     
59