refactored sfalogging to be less awkward and more reliable
[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 from sfa.util.faults import SfaInvalidArgument
7 from sfa.trust.credential import Credential
8
9 from sfa.storage.parameter import Parameter, Mixed
10
11
12 class ListResources(Method):
13     """
14     Returns information about available resources
15     @param credential list
16     @param options dictionary
17     @return string
18     """
19     interfaces = ['aggregate', 'slicemgr']
20     accepts = [
21         Mixed(Parameter(str, "Credential string"),
22               Parameter(type([str]), "List of credentials")),
23         Parameter(dict, "Options")
24     ]
25     returns = Parameter(str, "List of resources")
26
27     def call(self, creds, options):
28         self.api.logger.info("interface: %s\tmethod-name: %s" %
29                              (self.api.interface, self.name))
30
31         # client must specify a version
32         if not options.get('geni_rspec_version'):
33             if options.get('rspec_version'):
34                 options['geni_rspec_version'] = options['rspec_version']
35             else:
36                 raise SfaInvalidArgument(
37                     'Must specify an rspec version option. geni_rspec_version cannot be null')
38
39         # Find the valid credentials
40         valid_creds = self.api.auth.checkCredentialsSpeaksFor(
41             creds, 'listnodes', options=options)
42
43         # get hrn of the original caller
44         origin_hrn = options.get('origin_hrn', None)
45         if not origin_hrn:
46             origin_hrn = Credential(
47                 cred=valid_creds[0]).get_gid_caller().get_hrn()
48         rspec = self.api.manager.ListResources(self.api, creds, options)
49
50         # filter rspec through sfatables
51         if self.api.interface in ['aggregate']:
52             chain_name = 'OUTGOING'
53         elif self.api.interface in ['slicemgr']:
54             chain_name = 'FORWARD-OUTGOING'
55         self.api.logger.debug(
56             "ListResources: sfatables on chain %s" % chain_name)
57         filtered_rspec = run_sfatables(chain_name, '', origin_hrn, rspec)
58
59         if 'geni_compressed' in options and options['geni_compressed'] == True:
60             filtered_rspec = zlib.compress(filtered_rspec).encode('base64')
61
62         return filtered_rspec