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