initial checkin
[sfa.git] / sfa / methods / Allocate.py
1 from sfa.util.faults import SfaInvalidArgument, InvalidRSpec, SfatablesRejected
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 from sfa.trust.credential import Credential
6 from sfa.storage.parameter import Parameter, Mixed
7 from sfa.rspecs.rspec import RSpec
8
9 class Allocate(Method):
10     """
11     Allocate resources as described in a request RSpec argument 
12     to a slice with the named URN. On success, one or more slivers 
13     are allocated, containing resources satisfying the request, and 
14     assigned to the given slice. This method returns a listing and 
15     description of the resources reserved for the slice by this 
16     operation, in the form of a manifest RSpec. Allocated slivers 
17     are held for an aggregate-determined period. Clients must Renew 
18     or Provision slivers before the expiration time (given in the 
19     return struct), or the aggregate will automatically Delete them.
20
21     @param slice_urn (string) URN of slice to allocate to
22     @param credentials (dict) of credentials
23     @param rspec (string) rspec to allocate
24     
25     """
26     interfaces = ['aggregate', 'slicemgr']
27     accepts = [
28         Parameter(str, "Slice URN"),
29         Parameter(dict, "List of credentials")),
30         Parameter(str, "RSpec"),
31         Parameter(dict, "options"),
32         ]
33     returns = Parameter(str, "Allocated RSpec")
34
35     def call(self, xrn, creds, rspec, options):
36         xrn = Xrn(xrn, type='slice')
37         self.api.logger.info("interface: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, xrn.get_hrn(), self.name))
38
39         # Find the valid credentials
40         valid_creds = self.api.auth.checkCredentials(creds, 'createsliver', xrn.get_hrn())
41         
42         # make sure request is not empty
43         slivers = RSpec(rspec).version.get_nodes_with_slivers()
44         if not slivers:
45             raise InvalidRSpec("Missing <sliver_type> or <sliver> element. Request rspec must explicitly allocate slivers")    
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("Allocate: sfatables on chain %s"%chain_name)
53         origin_hrn = Credential(string=valid_creds[0]).get_gid_caller().get_hrn()
54         rspec = run_sfatables(chain_name, xrn.get_hrn(), origin_hrn, rspec)
55         slivers = RSpec(rspec).version.get_nodes_with_slivers()
56         if not slivers:
57             raise SfatablesRejected(slice_xrn)
58
59         result = self.api.manager.Allocate(self.api, xrn.get_urn(), creds, rspec, options)
60         return result