Merge Master in geni-v3 conflict resolution
[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
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         self.api.logger.info("interface: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, xrn.get_hrn(), self.name))
40
41         # Find the valid credentials
42         valid_creds = self.api.auth.checkCredentials(creds, 'createsliver', xrn.get_hrn())
43         # use the expiration from the first valid credential to determine when 
44         # the slivers should expire.
45         expiration = datetime_to_string(Credential(cred=valid_creds[0]).expiration)
46         
47         # make sure request is not empty
48         slivers = RSpec(rspec).version.get_nodes_with_slivers()
49         if not slivers:
50             raise InvalidRSpec("Missing <sliver_type> or <sliver> element. Request rspec must explicitly allocate slivers")    
51
52         # flter rspec through sfatables
53         if self.api.interface in ['aggregate']:
54             chain_name = 'INCOMING'
55         elif self.api.interface in ['slicemgr']:
56             chain_name = 'FORWARD-INCOMING'
57         self.api.logger.debug("Allocate: sfatables on chain %s"%chain_name)
58         origin_hrn = Credential(cred=valid_creds[0]).get_gid_caller().get_hrn()
59         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, origin_hrn, xrn, self.name)) 
60         rspec = run_sfatables(chain_name, xrn.get_hrn(), origin_hrn, rspec)
61         slivers = RSpec(rspec).version.get_nodes_with_slivers()
62         if not slivers:
63             raise SfatablesRejected(slice_xrn)
64
65         result = self.api.manager.Allocate(self.api, xrn.get_urn(), creds, rspec, expiration, options)
66         return result