5e99129b454187c3e72c3e6f4aed46d363106793
[plcapi.git] / PLC / Leases.py
1 #
2 # Functions for interacting with the leases table in the database
3 #
4 # Thierry Parmentelat -- INRIA
5 #
6
7 from datetime import datetime
8
9 from PLC.Faults import *
10 from PLC.Parameter import Parameter, Mixed
11 from PLC.Filter import Filter
12 from PLC.Table import Row, Table
13 from PLC.Nodes import Node, Nodes
14 from PLC.Slices import Slice, Slices
15 from PLC.LeaseFilter import LeaseFilter
16 from PLC.Timestamp import Timestamp
17
18 class Lease(Row):
19     """
20     Representation of a row in the leases table. To use, optionally
21     instantiate with a dict of values. Update as you would a
22     dict. Commit to the database with sync().
23     """
24
25     table_name = 'leases'
26     primary_key = 'lease_id'
27     join_tables = [ ]
28     fields = {
29         # native
30         'lease_id': Parameter(int, "Lease identifier"),
31         't_from': Timestamp.Parameter("timeslot start"),
32         't_until': Timestamp.Parameter("timeslot end"),
33         'node_id': Node.fields['node_id'],
34         'slice_id': Slice.fields['slice_id'],
35
36         # derived
37         'hostname': Node.fields['hostname'],
38         'node_type': Node.fields['node_type'],
39         'name': Slice.fields['name'],
40         'site_id': Slice.fields['site_id'],
41         'duration': Parameter(int, "duration in seconds"),
42         'expired' : Parameter(bool, "time slot is over"),
43         }
44
45     related_fields = { }
46
47     def validate_time (self, timestamp, round_up):
48         # convert to long
49         timestamp = Timestamp.cast_long(timestamp)
50         # retrieve configured granularity
51         granularity = self.api.config.PLC_RESERVATION_GRANULARITY
52         # the trick for rounding up rather than down
53         if round_up: timestamp += (granularity-1)
54         # round down
55         timestamp = (timestamp/granularity) * granularity
56         # return a SQL string
57         return Timestamp.sql_validate_utc(timestamp)
58
59     # round UP
60     def validate_t_from(self,timestamp):
61         return self.validate_time (timestamp, round_up=True)
62     # round DOWN
63     def validate_t_until (self, timestamp):
64         return self.validate_time (timestamp, round_up=False)
65
66 class Leases(Table):
67     """
68     Representation of row(s) from the leases table in the
69     database.
70     """
71
72     def __init__(self, api, lease_filter = None, columns = None):
73         Table.__init__(self, api, Lease, columns)
74
75         # the view that we're selecting upon: start with view_leases
76         view = "view_leases"
77         sql = "SELECT %s FROM %s WHERE true" % (", ".join(self.columns.keys()),view)
78
79
80         if lease_filter is not None:
81             if isinstance(lease_filter, (list, tuple, set, int, long)):
82                 lease_filter = Filter(Lease.fields, {'lease_id': lease_filter})
83             elif isinstance(lease_filter, dict):
84                 lease_filter = LeaseFilter(Lease.fields, lease_filter)
85             else:
86                 raise PLCInvalidArgument, "Wrong lease filter %r"%lease_filter
87             sql += " AND (%s) %s" % lease_filter.sql(api)
88
89         self.selectall(sql)