from PLC.Faults import *
from PLC.Parameter import Parameter
+from PLC.Filter import Filter
from PLC.Debug import profile
from PLC.Table import Row, Table
from PLC.SliceInstantiations import SliceInstantiations
'node_ids': Parameter([int], "List of nodes in this slice", ro = True),
'person_ids': Parameter([int], "List of accounts that can use this slice", ro = True),
'slice_attribute_ids': Parameter([int], "List of slice attributes", ro = True),
+ 'peer_id': Parameter(int, "Peer at which this slice was created", nullok = True),
}
+ # for Cache
+ class_key = 'name'
+ foreign_fields = ['instantiation', 'url', 'description', 'max_nodes', 'expires']
+ foreign_xrefs = [
+ {'field': 'node_ids' , 'class': 'Node', 'table': 'slice_node' },
+ {'field': 'person_ids', 'class': 'Person', 'table': 'slice_person'},
+ {'field': 'creator_person_id', 'class': 'Person', 'table': 'unused-on-direct-refs'},
+ {'field': 'site_id', 'class': 'Site', 'table': 'unused-on-direct-refs'},
+ ]
+ # forget about this one, it is read-only anyway
+ # handling it causes Cache to re-sync all over again
+ # 'created'
def validate_name(self, name):
# N.B.: Responsibility of the caller to ensure that login_base
raise PLCInvalidArgument, "Invalid slice name"
conflicts = Slices(self.api, [name])
- for slice_id, slice in conflicts.iteritems():
- if 'slice_id' not in self or self['slice_id'] != slice_id:
- raise PLCInvalidArgument, "Slice name already in use"
+ for slice in conflicts:
+ if 'slice_id' not in self or self['slice_id'] != slice['slice_id']:
+ raise PLCInvalidArgument, "Slice name already in use, %s"%name
return name
def validate_instantiation(self, instantiation):
- instantiations = SliceInstantiations(self.api)
+ instantiations = [row['instantiation'] for row in SliceInstantiations(self.api)]
if instantiation not in instantiations:
raise PLCInvalidArgument, "No such instantiation state"
return instantiation
+ # timestamps
+ def validate_created (self, timestamp):
+ return self.validate_timestamp (timestamp)
+
def validate_expires(self, expires):
# N.B.: Responsibility of the caller to ensure that expires is
# not too far into the future.
- if expires < time.time():
- raise PLCInvalidArgument, "Expiration date must be in the future"
-
- return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(expires))
-
- def validate_creator_person_id(self, person_id):
- persons = PLC.Persons.Persons(self.api, [person_id])
- if not persons:
- raise PLCInvalidArgument, "Invalid creator"
-
- return person_id
+ return self.validate_timestamp (expires,True)
def add_person(self, person, commit = True):
"""
self['node_ids'].remove(node_id)
node['slice_ids'].remove(slice_id)
+ ##########
def sync(self, commit = True):
"""
Add or update a slice.
# Before a new slice is added, delete expired slices
if 'slice_id' not in self:
- expired = Slices(self.api, expires = -int(time.time())).values()
+ expired = Slices(self.api, expires = -int(time.time()))
for slice in expired:
slice.delete(commit)
database.
"""
- def __init__(self, api, slice_id_or_name_list = None, expires = int(time.time())):
- self.api = api
+ def __init__(self, api, slice_filter = None, columns = None, expires = int(time.time())):
+ Table.__init__(self, api, Slice, columns)
sql = "SELECT %s FROM view_slices WHERE is_deleted IS False" % \
- ", ".join(Slice.fields)
+ ", ".join(self.columns)
if expires is not None:
if expires >= 0:
expires = -expires
sql += " AND expires < %(expires)d"
- if slice_id_or_name_list:
- # Separate the list into integers and strings
- slice_ids = filter(lambda slice_id: isinstance(slice_id, (int, long)),
- slice_id_or_name_list)
- names = filter(lambda name: isinstance(name, StringTypes),
- slice_id_or_name_list)
- sql += " AND (False"
- if slice_ids:
- sql += " OR slice_id IN (%s)" % ", ".join(map(str, slice_ids))
- if names:
- sql += " OR name IN (%s)" % ", ".join(api.db.quote(names))
- sql += ")"
-
- rows = self.api.db.selectall(sql, locals())
-
- for row in rows:
- self[row['slice_id']] = slice = Slice(api, row)
- for aggregate in 'node_ids', 'person_ids', 'slice_attribute_ids':
- if not slice.has_key(aggregate) or slice[aggregate] is None:
- slice[aggregate] = []
- else:
- slice[aggregate] = map(int, slice[aggregate].split(','))
+ if slice_filter is not None:
+ if isinstance(slice_filter, (list, tuple, set)):
+ # Separate the list into integers and strings
+ ints = filter(lambda x: isinstance(x, (int, long)), slice_filter)
+ strs = filter(lambda x: isinstance(x, StringTypes), slice_filter)
+ slice_filter = Filter(Slice.fields, {'slice_id': ints, 'name': strs})
+ sql += " AND (%s)" % slice_filter.sql(api, "OR")
+ elif isinstance(slice_filter, dict):
+ slice_filter = Filter(Slice.fields, slice_filter)
+ sql += " AND (%s)" % slice_filter.sql(api, "AND")
+
+ self.selectall(sql, locals())