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