Merge branch 'geni-v3' of ssh://git.onelab.eu/git/sfa into geni-v3
[sfa.git] / sfa / methods / Allocate.py
1 from sfa.util.faults import SfaInvalidArgument, InvalidRSpec, SfatablesRejected
2 from sfa.util.sfatime import datetime_to_string 
3 from sfa.util.xrn import Xrn, urn_to_hrn
4 from sfa.util.method import Method
5 from sfa.util.sfatablesRuntime import run_sfatables
6 from sfa.trust.credential import Credential
7 from sfa.storage.parameter import Parameter, Mixed
8 from sfa.rspecs.rspec import RSpec
9 from sfa.util.sfalogging import logger
10
11 class Allocate(Method):
12     """
13     Allocate resources as described in a request RSpec argument 
14     to a slice with the named URN. On success, one or more slivers 
15     are allocated, containing resources satisfying the request, and 
16     assigned to the given slice. This method returns a listing and 
17     description of the resources reserved for the slice by this 
18     operation, in the form of a manifest RSpec. Allocated slivers 
19     are held for an aggregate-determined period. Clients must Renew 
20     or Provision slivers before the expiration time (given in the 
21     return struct), or the aggregate will automatically Delete them.
22
23     @param slice_urn (string) URN of slice to allocate to
24     @param credentials (dict) of credentials
25     @param rspec (string) rspec to allocate
26     
27     """
28     interfaces = ['aggregate', 'slicemgr']
29     accepts = [
30         Parameter(str, "Slice URN"),
31         Parameter(type([dict]), "List of credentials"),
32         Parameter(str, "RSpec"),
33         Parameter(dict, "options"),
34         ]
35     returns = Parameter(str, "Allocated RSpec")
36
37     def call(self, xrn, creds, rspec, options):
38         xrn = Xrn(xrn, type='slice')
39
40         # Find the valid credentials
41         valid_creds = self.api.auth.checkCredentialsSpeaksFor(creds, 'createsliver', xrn.get_hrn(), options=options)
42         the_credential = Credential(cred=valid_creds[0])
43
44         # use the expiration from the first valid credential to determine when 
45         # the slivers should expire.
46         expiration = datetime_to_string(the_credential.expiration)
47         
48         self.api.logger.debug("Allocate, received expiration from credential: %s"%expiration)
49
50         # make sure request is not empty
51         slivers = RSpec(rspec).version.get_nodes_with_slivers()
52         if not slivers:
53             raise InvalidRSpec("Missing <sliver_type> or <sliver> element. Request rspec must explicitly allocate slivers")    
54
55         # flter rspec through sfatables
56         if self.api.interface in ['aggregate']:
57             chain_name = 'INCOMING'
58         elif self.api.interface in ['slicemgr']:
59             chain_name = 'FORWARD-INCOMING'
60         self.api.logger.debug("Allocate: sfatables on chain %s"%chain_name)
61         actual_caller_hrn = the_credential.actual_caller_hrn()
62         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, actual_caller_hrn, xrn.get_hrn(), self.name)) 
63         rspec = run_sfatables(chain_name, xrn.get_hrn(), actual_caller_hrn, rspec)
64         slivers = RSpec(rspec).version.get_nodes_with_slivers()
65         if not slivers:
66             raise SfatablesRejected(slice_xrn)
67
68         # pass this to the driver code in case they need it
69         options['actual_caller_hrn'] = actual_caller_hrn
70         result = self.api.manager.Allocate(self.api, xrn.get_urn(), creds, rspec, expiration, options)
71         return result