996adab2a5c296286f0bdef30eee3763ab51d9d2
[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.sfatablesRuntime import run_sfatables
6 from sfa.util.faults import SfaInvalidArgument
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         # client must specify a version
30         if not options.get('geni_rspec_version'):
31             if options.get('rspec_version'):
32                 options['geni_rspec_version'] = options['rspec_version']
33             else:
34                 raise SfaInvalidArgument('Must specify an rspec version option. geni_rspec_version cannot be null')
35  
36         # get slice's hrn from options    
37         xrn = options.get('geni_slice_urn', '')
38         (hrn, _) = urn_to_hrn(xrn)
39         print >>sys.stderr, " \r\n \r\n \t Lsitresources.pyeuuuuuu call : hrn %s options %s" %( hrn,options ) 
40         # Find the valid credentials
41         valid_creds = self.api.auth.checkCredentials(creds, 'listnodes', hrn)
42
43         # get hrn of the original caller 
44         origin_hrn = options.get('origin_hrn', None)
45         print >>sys.stderr, " \r\n \r\n \t Lsitresources  :origin_hrn %s sansvqalid credss %s " %(origin_hrn, Credential(string=creds[0]).get_gid_caller().get_hrn()) 
46         if not origin_hrn:
47             origin_hrn = Credential(string=valid_creds[0]).get_gid_caller().get_hrn()
48         print >>sys.stderr, " \r\n \r\n \t Lsitresources.py000 call : hrn %s self.api.interface %s  origin_hrn %s   \r\n \r\n \r\n " %(hrn ,self.api.interface,origin_hrn)          
49         rspec = self.api.manager.ListResources(self.api, creds, options)
50
51         # filter rspec through sfatables 
52         if self.api.interface in ['aggregate']:
53             chain_name = 'OUTGOING'
54         elif self.api.interface in ['slicemgr']: 
55             chain_name = 'FORWARD-OUTGOING'
56         self.api.logger.debug("ListResources: sfatables on chain %s"%chain_name)  
57         print >>sys.stderr, " \r\n \r\n \t Listresources.py001 call : chain_name %s hrn %s origine_hrn %s " %(chain_name, hrn, origin_hrn)
58         filtered_rspec = run_sfatables(chain_name, hrn, origin_hrn, rspec) 
59  
60         if options.has_key('geni_compressed') and options['geni_compressed'] == True:
61             filtered_rspec = zlib.compress(filtered_rspec).encode('base64')
62
63         return filtered_rspec  
64     
65