group db-related stuff in sfa/storage
[sfa.git] / sfa / methods / ListResources.py
1 import zlib
2
3 from sfa.util.xrn import urn_to_hrn
4 from sfa.util.method import Method
5 from sfa.util.sfatablesRuntime import run_sfatables
6
7 from sfa.trust.credential import Credential
8
9 from sfa.storage.parameter import Parameter, Mixed
10
11 class ListResources(Method):
12     """
13     Returns information about available resources or resources allocated to this slice
14     @param credential list
15     @param options dictionary
16     @return string
17     """
18     interfaces = ['aggregate', 'slicemgr']
19     accepts = [
20         Mixed(Parameter(str, "Credential string"), 
21               Parameter(type([str]), "List of credentials")),
22         Parameter(dict, "Options")
23         ]
24     returns = Parameter(str, "List of resources")
25
26     def call(self, creds, options={}):
27         self.api.logger.info("interface: %s\tmethod-name: %s" % (self.api.interface, self.name))
28         
29         # get slice's hrn from options    
30         xrn = options.get('geni_slice_urn', '')
31         (hrn, _) = urn_to_hrn(xrn)
32
33         # Find the valid credentials
34         valid_creds = self.api.auth.checkCredentials(creds, 'listnodes', hrn)
35
36         # get hrn of the original caller 
37         origin_hrn = options.get('origin_hrn', None)
38         if not origin_hrn:
39             origin_hrn = Credential(string=valid_creds[0]).get_gid_caller().get_hrn()
40         rspec = self.api.manager.ListResources(self.api, creds, options)
41
42         # filter rspec through sfatables 
43         if self.api.interface in ['aggregate']:
44             chain_name = 'OUTGOING'
45         elif self.api.interface in ['slicemgr']: 
46             chain_name = 'FORWARD-OUTGOING'
47         self.api.logger.debug("ListResources: sfatables on chain %s"%chain_name)
48         filtered_rspec = run_sfatables(chain_name, hrn, origin_hrn, rspec) 
49  
50         if options.has_key('geni_compressed') and options['geni_compressed'] == True:
51             filtered_rspec = zlib.compress(filtered_rspec).encode('base64')
52
53         return filtered_rspec  
54     
55