e87380187f25a79727720031e28bd9214d4b063e
[sfa.git] / sfa / methods / CreateSliver.py
1 from sfa.util.faults import *
2 from sfa.util.namespace import *
3 from sfa.util.method import Method
4 from sfa.util.parameter import Parameter
5 from sfatables.runtime import SFATablesRules
6 import sys
7 from sfa.trust.credential import Credential
8
9 class CreateSliver(Method):
10     """
11     Allocate resources to a slice.  This operation is expected to
12     start the allocated resources asynchornously after the operation
13     has successfully completed.  Callers can check on the status of
14     the resources using SliverStatus.
15
16     @param slice_urn (string) URN of slice to allocate to
17     @param credentials ([string]) of credentials
18     @param rspec (string) rspec to allocate
19     
20     """
21     interfaces = ['geni_am']
22     accepts = [
23         Parameter(str, "Slice URN"),
24         Parameter(type([str]), "List of credentials"),
25         Parameter(str, "RSpec")
26         ]
27     returns = Parameter(str, "Allocated RSpec")
28
29     def __run_sfatables(self, manager, rules, hrn, origin_hrn, rspec):
30         if rules.sorted_rule_list:
31             contexts = rules.contexts
32             request_context = manager.fetch_context(hrn, origin_hrn, contexts)
33             rules.set_context(request_context)
34             newrspec = rules.apply(rspec)
35         else:    
36             newrspec = rspec
37         return newrspec
38
39
40     def call(self, slice_xrn, creds, rspec):
41         hrn, type = urn_to_hrn(slice_xrn)
42
43         self.api.logger.info("interface: %s\ttarget-hrn: %s\tcaller-creds: %s\tmethod-name: %s"%(self.api.interface, hrn, creds, self.name))
44
45         # Validate that at least one of the credentials is good enough
46         found = False
47         for cred in creds:
48             try:
49                 self.api.auth.check(cred, 'createslice')
50                 origin_hrn = Credential(string=cred).get_gid_caller().get_hrn()
51                 found = True
52                 break
53             except:
54                 error = sys.exc_info()[:2]
55                 continue
56             
57         if not found:
58             raise InsufficientRights('CreateSliver: Access denied: %s -- %s' % (error[0],error[1]))
59              
60         
61         manager_base = 'sfa.managers'
62
63         if self.api.interface in ['geni_am']:
64             mgr_type = self.api.config.SFA_GENI_AGGREGATE_TYPE
65             manager_module = manager_base + ".geni_am_%s" % mgr_type
66             manager = __import__(manager_module, fromlist=[manager_base])
67             rspec = self.__run_sfatables(manager, SFATablesRules('INCOMING'),
68                                          hrn, origin_hrn, rspec)
69             return manager.CreateSliver(self.api, slice_xrn, creds, rspec)
70
71         return ''
72