042585301717010c8e4c4b8f735a8b9db0c167da
[sfa.git] / sfa / methods / ListResources.py
1 from sfa.util.faults import *
2 from sfa.util.namespace import urn_to_hrn
3 from sfa.util.method import Method
4 from sfa.util.parameter import Parameter, Mixed
5 from sfa.trust.credential import Credential
6 from sfa.util.sfatablesRuntime import run_sfatables
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 = ['aggregate', 'slicemgr']
18     accepts = [
19         Mixed(Parameter(str, "Credential string"), 
20               Parameter(type([str]), "List of credentials")),
21         Parameter(dict, "Options")
22         ]
23     returns = Parameter(str, "List of resources")
24
25     def call(self, creds, options):
26         self.api.logger.info("interface: %s\tmethod-name: %s" % (self.api.interface, self.name))
27         
28         # get slice's hrn from options    
29         xrn = options.get('geni_slice_urn', None)
30         hrn, _ = urn_to_hrn(xrn)
31
32         # Find the valid credentials
33         valid_creds = self.api.auth.checkCredentials(creds, 'listnodes', hrn)
34
35         # get hrn of the original caller 
36         origin_hrn = options.get('origin_hrn', None)
37         if not origin_hrn:
38             origin_hrn = Credential(string=valid_creds[0]).get_gid_caller().get_hrn()
39         # get manager for this interface    
40         manager = self.api.get_interface_manager()
41         rspec = manager.get_rspec(self.api, creds, options)
42
43         # filter rspec through sfatables 
44         if self.api.interface in ['aggregate']:
45             chain_name = 'OUTGOING'
46         elif self.api.interface in ['slicemgr']: 
47             chain_name = 'FORWARD-OUTGOING'
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