fb831170f9a1c2ab8eb46105ee24cd9b75ad6519
[sfa.git] / sfa / methods / ListResources.py
1 import sys
2 import zlib
3
4 from sfa.util.faults import *
5 from sfa.util.xrn import urn_to_hrn
6 from sfa.util.method import Method
7 from sfa.util.parameter import Parameter, Mixed
8 from sfa.trust.credential import Credential
9 from sfa.util.sfatablesRuntime import run_sfatables
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         Parameter(str, "call_id"),
24         ]
25     returns = Parameter(str, "List of resources")
26
27     def call(self, creds, options, call_id=""):
28         self.api.logger.info("interface: %s\tmethod-name: %s" % (self.api.interface, self.name))
29         
30         # get slice's hrn from options    
31         xrn = options.get('geni_slice_urn', '')
32         (hrn, _) = urn_to_hrn(xrn)
33
34         # Find the valid credentials
35         valid_creds = self.api.auth.checkCredentials(creds, 'listnodes', hrn)
36
37         # get hrn of the original caller 
38         origin_hrn = options.get('origin_hrn', None)
39         if not origin_hrn:
40             origin_hrn = Credential(string=valid_creds[0]).get_gid_caller().get_hrn()
41         # get manager for this interface    
42         manager = self.api.get_interface_manager()
43         rspec = manager.ListResources(self.api, creds, options, call_id)
44
45         # filter rspec through sfatables 
46         if self.api.interface in ['aggregate']:
47             chain_name = 'OUTGOING'
48         elif self.api.interface in ['slicemgr']: 
49             chain_name = 'FORWARD-OUTGOING'
50         self.api.logger.debug("ListResources: sfatables on chain %s"%chain_name)
51         filtered_rspec = run_sfatables(chain_name, hrn, origin_hrn, rspec) 
52  
53         if options.has_key('geni_compressed') and options['geni_compressed'] == True:
54             filtered_rspec = zlib.compress(filtered_rspec).encode('base64')
55
56         return filtered_rspec  
57     
58