1aa104dee7365a0340f22d1a0ba196ca1f5b3ebf
[sfa.git] / sfa / methods / get_resources.py
1 ### $Id$
2 ### $URL$
3
4 from sfa.util.faults import *
5 from sfa.util.method import Method
6 from sfa.util.parameter import Parameter, Mixed
7 from sfa.trust.auth import Auth
8 from sfa.util.config import Config
9 from sfa.plc.nodes import Nodes
10 # RSpecManager_pl is not used. This line is a check that ensures that everything is in place for the import to work.
11 import sfa.rspecs.aggregates.rspec_manager_pl
12 from sfa.trust.credential import Credential
13 from sfatables.runtime import SFATablesRules
14
15 class get_resources(Method):
16     """
17     Get an resource specification (rspec). The rspec may describe the resources
18     available at an authority or the resources being used by a slice.      
19
20     @param cred credential string specifying the rights of the caller
21     @param hrn human readable name of the slice we are interesed in or None 
22            for an authority.  
23     """
24
25     interfaces = ['aggregate', 'slicemgr']
26     
27     accepts = [
28         Parameter(str, "Credential string"),
29         Mixed(Parameter(str, "Human readable name (hrn)"),
30               Parameter(None, "hrn not specified")),
31         Mixed(Parameter(str, "Request hash"),
32               Parameter(None, "Request hash not specified")),
33         Parameter(str, "Callers credential string")
34         ]
35
36     returns = Parameter(str, "String representatin of an rspec")
37     
38     def call(self, cred, hrn=None, request_hash = None, caller_cred=None):
39         sfa_aggregate_type = Config().get_aggregate_rspec_type()
40
41         # This cred will be an authority cred, not a user, so we cant use it to 
42         # authenticate the caller's request_hash. Let just get the caller's gid
43         # from the cred and authenticate using that 
44         client_gid = Credential(string=cred).get_gid_caller()
45         client_gid_str = client_gid.save_to_string(save_parents=True)
46         self.api.auth.authenticateGid(client_gid_str, [cred,hrn], request_hash)
47         self.api.auth.check(cred, 'listnodes')
48         if caller_cred==None:
49             caller_cred=cred
50
51         #log the call
52         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, Credential(string=caller_cred).get_gid_caller().get_hrn(), hrn, self.name))
53
54
55
56         # send the call to the right manager
57         manager_base = 'sfa.managers'
58         if self.api.interface in ['aggregate']:
59             mgr_type = self.api.config.SFA_AGGREGATE_TYPE
60             manager_module = manager_base + ".aggregate_manager_%s" % mgr_type
61             manager = __import__(manager_module, fromlist=[manager_base])
62             rspec = manager.get_rspec(self.api, hrn, caller_cred)
63             outgoing_rules = SFATablesRules('OUTGOING')
64         elif self.api.interface in ['slicemgr']:
65             mgr_type = self.api.config.SFA_SM_TYPE
66             manager_module = manager_base + ".slice_manager_%s" % mgr_type
67             manager = __import__(manager_module, fromlist=[manager_base])
68             rspec = manager.get_rspec(self.api, hrn, caller_cred)
69             outgoing_rules = SFATablesRules('FORWARD-OUTGOING')
70
71         filtered_rspec = rspec
72         if outgoing_rules.sorted_rule_list:
73            request_context = manager.fetch_context(
74                hrn,
75                Credential(string=caller_cred).get_gid_caller().get_hrn(),
76                outgoing_rules.contexts)
77            outgoing_rules.set_context(request_context)
78            filtered_rspec = outgoing_rules.apply(rspec)
79
80         return filtered_rspec