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