avoid as much as possible accessing logger through class instances, whenever that...
[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
12 class Allocate(Method):
13     """
14     Allocate resources as described in a request RSpec argument
15     to a slice with the named URN. On success, one or more slivers
16     are allocated, containing resources satisfying the request, and
17     assigned to the given slice. This method returns a listing and
18     description of the resources reserved for the slice by this
19     operation, in the form of a manifest RSpec. Allocated slivers
20     are held for an aggregate-determined period. Clients must Renew
21     or Provision slivers before the expiration time (given in the
22     return struct), or the aggregate will automatically Delete them.
23
24     @param slice_urn (string) URN of slice to allocate to
25     @param credentials (dict) of credentials
26     @param rspec (string) rspec to allocate
27     @param options (dict)
28
29     As of 3.1.16, the PL driver implements here an important option named
30     'pltags' that affects the management of slice tags.
31
32     This option can take 3 values
33       (*) options['pltags'] == 'ignore' (default)
34           This is the recommended mode; in this mode all slice tags passed
35           here are ignore, which correspond to the <planetlab:attribute> XML tags in
36           the <sliver_type> areas of incoming rspec to Allocate.
37           In other words you are guaranteed to leave slice tags alone.
38       (*) options['pltags'] == 'append'
39           All incoming slice tags are added to corresponding slivers,
40           unless an exact match can be found in the PLC db
41       (*) options['pltags'] == 'sync'
42           The historical mode, that attempts to leave the PLC db in a state
43           in sync with the ones specified in incoming rspec.
44
45     See also http://svn.planet-lab.org/wiki/SFASliceTags
46
47     """
48     interfaces = ['aggregate', 'slicemgr']
49     accepts = [
50         Parameter(str, "Slice URN"),
51         Parameter(type([dict]), "List of credentials"),
52         Parameter(str, "RSpec"),
53         Parameter(dict, "options"),
54     ]
55     returns = Parameter(str, "Allocated RSpec")
56
57     def call(self, xrn, creds, rspec, options):
58         xrn = Xrn(xrn, type='slice')
59
60         # Find the valid credentials
61         valid_creds = self.api.auth.checkCredentialsSpeaksFor(
62             creds, 'createsliver', xrn.get_hrn(), options=options)
63         the_credential = Credential(cred=valid_creds[0])
64
65         # use the expiration from the first valid credential to determine when
66         # the slivers should expire.
67         expiration = datetime_to_string(the_credential.expiration)
68
69         logger.debug(
70             "Allocate, received expiration from credential: %s" % expiration)
71
72 # turned off, as passing an empty rspec is indeed useful for cleaning up the slice
73 #        # make sure request is not empty
74 #        slivers = RSpec(rspec).version.get_nodes_with_slivers()
75 #        if not slivers:
76 #            raise InvalidRSpec("Missing <sliver_type> or <sliver> element. Request rspec must explicitly allocate slivers")
77
78         # flter rspec through sfatables
79         if self.api.interface in ['aggregate']:
80             chain_name = 'INCOMING'
81         elif self.api.interface in ['slicemgr']:
82             chain_name = 'FORWARD-INCOMING'
83         logger.debug("Allocate: sfatables on chain %s" % chain_name)
84         actual_caller_hrn = the_credential.actual_caller_hrn()
85         logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s" %
86                     (self.api.interface, actual_caller_hrn, xrn.get_hrn(), self.name))
87         rspec = run_sfatables(chain_name, xrn.get_hrn(),
88                               actual_caller_hrn, rspec)
89 # turned off, as passing an empty rspec is indeed useful for cleaning up the slice
90 #        slivers = RSpec(rspec).version.get_nodes_with_slivers()
91 #        if not slivers:
92 #            raise SfatablesRejected(slice_xrn)
93
94         # pass this to the driver code in case they need it
95         options['actual_caller_hrn'] = actual_caller_hrn
96         result = self.api.manager.Allocate(
97             self.api, xrn.get_urn(), creds, rspec, expiration, options)
98         return result