group db-related stuff in sfa/storage
[sfa.git] / sfa / methods / CreateSliver.py
1 from sfa.util.faults import SfaInvalidArgument
2 from sfa.util.xrn import urn_to_hrn
3 from sfa.util.method import Method
4 from sfa.util.sfatablesRuntime import run_sfatables
5
6 from sfa.trust.credential import Credential
7
8 from sfa.storage.parameter import Parameter, Mixed
9
10 from sfa.rspecs.rspec import RSpec
11
12 class CreateSliver(Method):
13     """
14     Allocate resources to a slice.  This operation is expected to
15     start the allocated resources asynchornously after the operation
16     has successfully completed.  Callers can check on the status of
17     the resources using SliverStatus.
18
19     @param slice_urn (string) URN of slice to allocate to
20     @param credentials ([string]) of credentials
21     @param rspec (string) rspec to allocate
22     
23     """
24     interfaces = ['aggregate', 'slicemgr']
25     accepts = [
26         Parameter(str, "Slice URN"),
27         Mixed(Parameter(str, "Credential string"),
28               Parameter(type([str]), "List of credentials")),
29         Parameter(str, "RSpec"),
30         Parameter(type([]), "List of user information"),
31         Parameter(dict, "options"),
32         ]
33     returns = Parameter(str, "Allocated RSpec")
34
35     def call(self, slice_xrn, creds, rspec, users, options={}):
36         hrn, type = urn_to_hrn(slice_xrn)
37
38         self.api.logger.info("interface: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, hrn, self.name))
39
40         # Find the valid credentials
41         valid_creds = self.api.auth.checkCredentials(creds, 'createsliver', hrn)
42         origin_hrn = Credential(string=valid_creds[0]).get_gid_caller().get_hrn()
43
44         # make sure users info is specified
45         if not users:
46             msg = "'users' musst be specified and cannot be null. You may need to update your client." 
47             raise SfaInvalidArgument(name='users', extra=msg)  
48
49         # flter rspec through sfatables
50         if self.api.interface in ['aggregate']:
51             chain_name = 'INCOMING'
52         elif self.api.interface in ['slicemgr']:
53             chain_name = 'FORWARD-INCOMING'
54         self.api.logger.debug("CreateSliver: sfatables on chain %s"%chain_name)
55         rspec = run_sfatables(chain_name, hrn, origin_hrn, rspec)
56         slivers = RSpec(rspec).version.get_nodes_with_slivers()
57         if slivers:
58             result = self.api.manager.CreateSliver(self.api, slice_xrn, creds, rspec, users, options)
59         else:
60             result = rspec     
61         return result