Added prints for debugging purposes.
[sfa.git] / sfa / methods / ListResources.py
1 import zlib
2 import sys
3 from sfa.util.xrn import urn_to_hrn
4 from sfa.util.method import Method
5 from sfa.util.parameter import Parameter, Mixed
6 from sfa.trust.credential import Credential
7 from sfa.util.sfatablesRuntime import run_sfatables
8
9 class ListResources(Method):
10     """
11     Returns information about available resources or resources allocated to this slice
12     @param credential list
13     @param options dictionary
14     @return string
15     """
16     interfaces = ['aggregate', 'slicemgr']
17     accepts = [
18         Mixed(Parameter(str, "Credential string"), 
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         # get slice's hrn from options    
28         xrn = options.get('geni_slice_urn', '')
29         (hrn, _) = urn_to_hrn(xrn)
30
31         # Find the valid credentials
32         valid_creds = self.api.auth.checkCredentials(creds, 'listnodes', hrn)
33
34         # get hrn of the original caller 
35         origin_hrn = options.get('origin_hrn', None)
36
37         if not origin_hrn:
38             origin_hrn = Credential(string=valid_creds[0]).get_gid_caller().get_hrn()
39         print >>sys.stderr, " \r\n \r\n \t Lsitresources.py call :self.api.interface %s  origin_hrn %s options %s \r\n \t creds %s " %(self.api.interface,origin_hrn,options, creds)          
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         print >>sys.stderr, " \r\n \r\n \t Listresources.py call : chain_name %s hrn %s origine_hrn %s " %(chain_name, hrn, origin_hrn)
49         filtered_rspec = run_sfatables(chain_name, hrn, origin_hrn, rspec) 
50  
51         if options.has_key('geni_compressed') and options['geni_compressed'] == True:
52             filtered_rspec = zlib.compress(filtered_rspec).encode('base64')
53
54         return filtered_rspec  
55     
56