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