2 # Functions for interacting with the leases table in the database
4 # Thierry Parmentelat -- INRIA
7 from datetime import datetime
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
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().
26 primary_key = 'lease_id'
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'],
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"),
47 def validate_time (self, timestamp, round_up):
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)
55 timestamp = (timestamp/granularity) * granularity
57 return Timestamp.sql_validate_utc(timestamp)
60 def validate_t_from(self,timestamp):
61 return self.validate_time (timestamp, round_up=True)
63 def validate_t_until (self, timestamp):
64 return self.validate_time (timestamp, round_up=False)
68 Representation of row(s) from the leases table in the
72 def __init__(self, api, lease_filter = None, columns = None):
73 Table.__init__(self, api, Lease, columns)
75 # the view that we're selecting upon: start with view_leases
77 sql = "SELECT %s FROM %s WHERE true" % (", ".join(self.columns.keys()),view)
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)
86 raise PLCInvalidArgument, "Wrong lease filter %r"%lease_filter
87 sql += " AND (%s) %s" % lease_filter.sql(api)