check the specified hrn against the target hrn in the credential
[sfa.git] / sfa / methods / create_slice.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.plc.slices import Slices
10 from sfa.util.config import Config
11 from sfa.trust.credential import Credential
12 from sfatables.runtime import SFATablesRules
13
14 class create_slice(Method):
15     """
16     Instantiate the specified slice according to whats defined in the specified rspec      
17
18     @param cred credential string specifying the rights of the caller
19     @param hrn human readable name of slice to instantiate (hrn or xrn)
20     @param rspec resource specification
21     @return 1 is successful, faults otherwise  
22     """
23
24     interfaces = ['aggregate', 'slicemgr']
25     
26     accepts = [
27         Parameter(str, "Credential string"),
28         Parameter(str, "Human readable name of slice to instantiate (hrn or xrn)"),
29         Parameter(str, "Resource specification"),
30         Mixed(Parameter(str, "Human readable name of the original caller"),
31               Parameter(None, "Origin hrn not specified"))
32         ]
33
34     returns = Parameter(int, "1 if successful")
35
36     def __run_sfatables(self, manager, rules, hrn, origin_hrn, rspec):
37         if rules.sorted_rule_list:
38             contexts = rules.contexts
39             request_context = manager.fetch_context(hrn, origin_hrn, contexts)
40             rules.set_context(request_context)
41             newrspec = rules.apply(rspec)
42         else:   
43             newrspec = rspec
44         return newrspec
45
46         
47     def call(self, cred, xrn, requested_rspec, origin_hrn=None):
48         hrn, type = urn_to_hrn(xrn) 
49         user_cred = Credential(string=cred)
50         #log the call
51         if not origin_hrn:
52             origin_hrn = user_cred.get_gid_caller().get_hrn()
53         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, origin_hrn, hrn, self.name))
54          
55         # validate the credential
56         self.api.auth.check(cred, 'createslice', hrn)
57
58         manager_base = 'sfa.managers'
59         if self.api.interface in ['aggregate']:
60             mgr_type = self.api.config.SFA_AGGREGATE_TYPE
61             manager_module = manager_base + ".aggregate_manager_%s" % mgr_type
62             manager = __import__(manager_module, fromlist=[manager_base])
63             rspec = self.__run_sfatables(manager, 
64                                          SFATablesRules('INCOMING'),
65                                          hrn, origin_hrn, requested_rspec)
66             manager.create_slice(self.api, xrn, rspec)
67         elif self.api.interface in ['slicemgr']:
68             mgr_type = self.api.config.SFA_SM_TYPE
69             manager_module = manager_base + ".slice_manager_%s" % mgr_type
70             manager = __import__(manager_module, fromlist=[manager_base])
71             rspec = self.__run_sfatables(manager, 
72                                          SFATablesRules('FORWARD-INCOMING'),
73                                          hrn, origin_hrn, requested_rspec)
74             manager.create_slice(self.api, xrn, rspec, origin_hrn)
75
76         return 1