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