added support for urn name format. urn is the default name format used over the wire
[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 # RSpecManager_pl is not used. It's used to make sure the module is in place.
12 import sfa.rspecs.aggregates.rspec_manager_pl
13 from sfa.trust.credential import Credential
14 from sfatables.runtime import SFATablesRules
15
16
17 class create_slice(Method):
18     """
19     Instantiate the specified slice according to whats defined in the specified rspec      
20
21     @param cred credential string specifying the rights of the caller
22     @param hrn human readable name of slice to instantiate (hrn or xrn)
23     @param rspec resource specification
24     @return 1 is successful, faults otherwise  
25     """
26
27     interfaces = ['aggregate', 'slicemgr']
28     
29     accepts = [
30         Parameter(str, "Credential string"),
31         Parameter(str, "Human readable name of slice to instantiate (hrn or xrn)"),
32         Parameter(str, "Resource specification"),
33         Mixed(Parameter(str, "Human readable name of the original caller"),
34               Parameter(None, "Origin hrn not specified"))
35         ]
36
37     returns = Parameter(int, "1 if successful")
38     
39     def call(self, cred, xrn, requested_rspec, origin_hrn=None):
40         hrn, type = urn_to_hrn(xrn) 
41         user_cred = Credential(string=cred)
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 credential
48         self.api.auth.check(cred, 'createslice')
49
50         sfa_aggregate_type = Config().get_aggregate_rspec_type()
51         rspec_manager = __import__("sfa.rspecs.aggregates.rspec_manager_"+sfa_aggregate_type, fromlist = ["sfa.rspecs.aggregates"])
52         #Filter the incoming rspec using sfatables
53         if self.api.interface in ['slicemgr']:
54             incoming_rules = SFATablesRules('FORWARD-INCOMING')
55         elif self.api.interface in ['aggregate']:
56             incoming_rules = SFATablesRules('INCOMING')
57
58         if incoming_rules.sorted_rule_list:
59             #incoming_rules.set_slice(hrn) # This is a temporary kludge. Eventually, we'd like to fetch the context requested by the match/target
60
61             contexts = incoming_rules.contexts
62             request_context = rspec_manager.fetch_context(hrn, origin_hrn, contexts)
63             incoming_rules.set_context(request_context)
64             rspec = incoming_rules.apply(requested_rspec)
65         else:   
66             rspec = requested_rspec
67
68         # send the call to the right manager
69         if sfa_aggregate_type not in ['pl']:
70             # To clean up after July 21 - SB
71             rspec = rspec_manager.create_slice(self.api, hrn, rspec)
72             return 1
73
74         manager_base = 'sfa.managers'
75         if self.api.interface in ['aggregate']:
76             mgr_type = self.api.config.SFA_AGGREGATE_TYPE
77             manager_module = manager_base + ".aggregate_manager_%s" % mgr_type
78             manager = __import__(manager_module, fromlist=[manager_base])
79             manager.create_slice(self.api, xrn, rspec)
80         elif self.api.interface in ['slicemgr']:
81             mgr_type = self.api.config.SFA_SM_TYPE
82             manager_module = manager_base + ".slice_manager_%s" % mgr_type
83             manager = __import__(manager_module, fromlist=[manager_base])
84             manager.create_slice(self.api, xrn, rspec, origin_hrn)
85
86         return 1