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 # turned off, as passing an empty rspec is indeed useful for cleaning up the slice
51 #        # make sure request is not empty
52 #        slivers = RSpec(rspec).version.get_nodes_with_slivers()
53 #        if not slivers:
54 #            raise InvalidRSpec("Missing <sliver_type> or <sliver> element. Request rspec must explicitly allocate slivers")    
55
56         # flter rspec through sfatables
57         if self.api.interface in ['aggregate']:
58             chain_name = 'INCOMING'
59         elif self.api.interface in ['slicemgr']:
60             chain_name = 'FORWARD-INCOMING'
61         self.api.logger.debug("Allocate: sfatables on chain %s"%chain_name)
62         actual_caller_hrn = the_credential.actual_caller_hrn()
63         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)) 
64         rspec = run_sfatables(chain_name, xrn.get_hrn(), actual_caller_hrn, rspec)
65 # turned off, as passing an empty rspec is indeed useful for cleaning up the slice
66 #        slivers = RSpec(rspec).version.get_nodes_with_slivers()
67 #        if not slivers:
68 #            raise SfatablesRejected(slice_xrn)
69
70         # pass this to the driver code in case they need it
71         options['actual_caller_hrn'] = actual_caller_hrn
72         result = self.api.manager.Allocate(self.api, xrn.get_urn(), creds, rspec, expiration, options)
73         return result